Skip to content
Myr

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

3 files+48 added−28 removed

modified:click/_optparse.py+28 added-12 removed
1import optparse1import sys23# 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')11sys.modules['gettext'] = None12from optparse import IndentedHelpFormatter, OptionParser, Option13if _gettext is not None:14 sys.modules['gettext'] = _gettext1516from .helpers import get_terminal_size17from .formatting import TextWrapper18from .exceptions import UsageError19208class _SimplifiedFormatter(optparse.IndentedHelpFormatter):21class _SimplifiedFormatter(IndentedHelpFormatter):2223 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 = ''2728 def format_usage(self, usage):
22 unmodified lines
51 self.dedent()52 return rv5354 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 rv606149class _SimplifiedOptionParser(optparse.OptionParser):62class _SimplifiedOptionParser(OptionParser):6364 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 = ctx7273 def expand_prog_name(self, s):74 return s7576 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 rv8283 def error(self, msg):84 raise UsageError(msg, self.__ctx)858687make_option = Option
modified:click/core.py+8 added-9 removed
1import os2import sys3import optparse3from itertools import chain45from .types import convert_type, BOOL6from .utils import echo7from .exceptions import UsageError, Abort8from .helpers import prompt, confirm10from ._optparse import _SimplifiedOptionParser910from . import _optparse111213class 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 lines
226 if not param.is_eager:227 yield param228229 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 parser
480 unmodified lines
715716 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 lines
modified:click/helpers.py+12 added-7 removed
1import os2import shutil2import sys3import struct4import getpass45from ._compat import raw_input6from .utils import echo7from .exceptions import Abort, UsageError
1 unmodified line
91011# The prompt functions to use. The doc tools currently override these12# functions to customize how they work.14hidden_prompt_func = getpass.getpass13visible_prompt_func = raw_input141516def hidden_prompt_func(prompt):17 import getpass18 return getpass.getpass(prompt)192021def 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 can
92 unmodified lines
117 """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.lines127128 def ioctl_gwinsz(fd):129 try:130 import fcntl
21 unmodified lines