Some performance improvements
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 0ed40c8 · Browse files · 3 files·+48 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+48 added−28 removed
- other:+48 added−28 removed
Provenance
- Commit
e5dc29d- Tree
5281656- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0ed40c8
3 files changed+48 added−28 removed
modified:click/_optparse.py+28 added-12 removed
1-import optparse1+import sys2 3+# optparse never fully supported gettext but for some reason it still4+# imports it which slows things down quite a bit. In case we are the5+# first importer of optparse and gettext we can disable gettext support6+# through this hackery.7+#8+# In case someone does depend on gettext being loaded into optparse we9+# might have a problem but I don't think anyone will actually notice.10+_gettext = sys.modules.get('gettext')11+sys.modules['gettext'] = None12+from optparse import IndentedHelpFormatter, OptionParser, Option13+if _gettext is not None:14+ sys.modules['gettext'] = _gettext15+16 from .helpers import get_terminal_size17 from .formatting import TextWrapper18 from .exceptions import UsageError19 20 8-class _SimplifiedFormatter(optparse.IndentedHelpFormatter):21+class _SimplifiedFormatter(IndentedHelpFormatter):22 23 def __init__(self):11- optparse.IndentedHelpFormatter.__init__(self,24+ IndentedHelpFormatter.__init__(self,25 width=min(get_terminal_size()[0], 80) - 2)26 self.default_tag = ''27 28 def format_usage(self, usage):22 unmodified lines51 self.dedent()52 return rv53 54 def format_option_strings(self, option):42- rv = optparse.IndentedHelpFormatter.format_option_strings(self, option)55+ rv = IndentedHelpFormatter.format_option_strings(self, option)56 if hasattr(option, '_negative_version'):44- rv += ' / ' + optparse.IndentedHelpFormatter.format_option_strings(57+ rv += ' / ' + IndentedHelpFormatter.format_option_strings(58 self, option._negative_version)59 return rv60 61 49-class _SimplifiedOptionParser(optparse.OptionParser):62+class _SimplifiedOptionParser(OptionParser):63 64 def __init__(self, ctx, **extra):65 usage = ctx.command_path + ' ' + ctx.command.options_metavar53- optparse.OptionParser.__init__(self, prog=ctx.command_path,54- usage=usage,55- add_help_option=False,56- formatter=_SimplifiedFormatter(),57- **extra)66+ OptionParser.__init__(self, prog=ctx.command_path,67+ usage=usage,68+ add_help_option=False,69+ formatter=_SimplifiedFormatter(),70+ **extra)71 self.__ctx = ctx72 73 def expand_prog_name(self, s):74 return s75 76 def format_option_help(self, formatter):64- rv = optparse.OptionParser.format_option_help(self, formatter)77+ rv = OptionParser.format_option_help(self, formatter)78 extra_help = self.__ctx.command._format_extra_help(self.__ctx)79 if extra_help:80 rv = '%s\n\n%s' % (extra_help, rv)81 return rv82 83 def error(self, msg):84 raise UsageError(msg, self.__ctx)85+86+87+make_option = Optionmodified:click/core.py+8 added-9 removed
1 import os2 import sys3-import optparse3 from itertools import chain4 5 from .types import convert_type, BOOL6 from .utils import echo7 from .exceptions import UsageError, Abort8 from .helpers import prompt, confirm10-from ._optparse import _SimplifiedOptionParser9 10+from . import _optparse11 12+13 class Context(object):14 """The context is a special internal object that holds state relevant15 for the script execution at every single level. It's normally invisible16 to commands unless they opt-in to getting access to it.209 unmodified lines226 if not param.is_eager:227 yield param228 229 def _make_parser(self, ctx):230- parser = _SimplifiedOptionParser(230+ parser = _optparse._SimplifiedOptionParser(231 ctx, description=self.help, epilog=self.epilog)232 for param in self.params:233 param._add_to_parser(parser, ctx)234 return parser480 unmodified lines715 716 if self.is_flag:717 kwargs.pop('nargs', None)718 if self.is_bool_flag and self.secondary_opts:719- pos_opt = optparse.Option(*self.opts, action=action + '_true',720- **kwargs)719+ pos_opt = _optparse.make_option(720+ *self.opts, action=action + '_true', **kwargs)721 kwargs.pop('default', None)722 kwargs.pop('help', None)723- neg_opt = optparse.Option(*self.secondary_opts,724- action=action + '_false',725- help=optparse.SUPPRESS_HELP,726- **kwargs)723+ neg_opt = _optparse.make_option(724+ *self.secondary_opts, action=action + '_false',725+ help=_optparse.SUPPRESS_HELP, **kwargs)726 pos_opt._negative_version = neg_opt727 parser.add_option(pos_opt)728 parser.add_option(neg_opt)729 else:120 unmodified linesmodified:click/helpers.py+12 added-7 removed
1 import os2-import shutil2+import sys3 import struct4-import getpass4 5 from ._compat import raw_input6 from .utils import echo7 from .exceptions import Abort, UsageError1 unmodified line9 10 11 # The prompt functions to use. The doc tools currently override these12 # functions to customize how they work.14-hidden_prompt_func = getpass.getpass13 visible_prompt_func = raw_input14 15 16+def hidden_prompt_func(prompt):17+ import getpass18+ return getpass.getpass(prompt)19+20+21 def prompt(text, default=None, hide_input=False,22 confirmation_prompt=False, type=None,23 value_proc=None):24 """Prompts a user for input. This is a convenience function that can92 unmodified lines117 """Returns the current size of the terminal as tuple in the form118 ``(width, height)`` in columns and rows.119 """120 # If shutil has get_terminal_size() (Python 3.3 and later) use that121+ if sys.version_info >= (3, 3):122+ import shutil118- shutil_get_terminal_size = getattr(shutil, 'get_terminal_size', None)119- if shutil_get_terminal_size:120- sz = shutil_get_terminal_size()121- return sz.columns, sz.lines123+ shutil_get_terminal_size = getattr(shutil, 'get_terminal_size', None)124+ if shutil_get_terminal_size:125+ sz = shutil_get_terminal_size()126+ return sz.columns, sz.lines127 128 def ioctl_gwinsz(fd):129 try:130 import fcntl21 unmodified lines