mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-12 22:14:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -7,9 +7,13 @@ additional features:
|
|||
create a complete usage summary
|
||||
* options set attributes of a passed-in object
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys, string, re
|
||||
import getopt
|
||||
import re
|
||||
import string
|
||||
import sys
|
||||
|
||||
from distutils.errors import *
|
||||
|
||||
# Much like command_re in distutils.core, this is close to but not quite
|
||||
|
|
@ -20,12 +24,13 @@ longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
|
|||
longopt_re = re.compile(r'^%s$' % longopt_pat)
|
||||
|
||||
# For recognizing "negative alias" options, eg. "quiet=!verbose"
|
||||
neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
|
||||
neg_alias_re = re.compile('^({})=!({})$'.format(longopt_pat, longopt_pat))
|
||||
|
||||
# This is used to translate long options to legitimate Python identifiers
|
||||
# (for use as attributes of some object).
|
||||
longopt_xlate = str.maketrans('-', '_')
|
||||
|
||||
|
||||
class FancyGetopt:
|
||||
"""Wrapper around the standard 'getopt()' module that provides some
|
||||
handy extra functionality:
|
||||
|
|
@ -90,7 +95,8 @@ class FancyGetopt:
|
|||
def add_option(self, long_option, short_option=None, help_string=None):
|
||||
if long_option in self.option_index:
|
||||
raise DistutilsGetoptError(
|
||||
"option conflict: already an option '%s'" % long_option)
|
||||
"option conflict: already an option '%s'" % long_option,
|
||||
)
|
||||
else:
|
||||
option = (long_option, short_option, help_string)
|
||||
self.option_table.append(option)
|
||||
|
|
@ -111,15 +117,19 @@ class FancyGetopt:
|
|||
assert isinstance(aliases, dict)
|
||||
for (alias, opt) in aliases.items():
|
||||
if alias not in self.option_index:
|
||||
raise DistutilsGetoptError(("invalid %s '%s': "
|
||||
"option '%s' not defined") % (what, alias, alias))
|
||||
raise DistutilsGetoptError((
|
||||
"invalid %s '%s': "
|
||||
"option '%s' not defined"
|
||||
) % (what, alias, alias))
|
||||
if opt not in self.option_index:
|
||||
raise DistutilsGetoptError(("invalid %s '%s': "
|
||||
"aliased option '%s' not defined") % (what, alias, opt))
|
||||
raise DistutilsGetoptError((
|
||||
"invalid %s '%s': "
|
||||
"aliased option '%s' not defined"
|
||||
) % (what, alias, opt))
|
||||
|
||||
def set_aliases(self, alias):
|
||||
"""Set the aliases for this option parser."""
|
||||
self._check_alias_dict(alias, "alias")
|
||||
self._check_alias_dict(alias, 'alias')
|
||||
self.alias = alias
|
||||
|
||||
def set_negative_aliases(self, negative_alias):
|
||||
|
|
@ -127,7 +137,7 @@ class FancyGetopt:
|
|||
'negative_alias' should be a dictionary mapping option names to
|
||||
option names, both the key and value must already be defined
|
||||
in the option table."""
|
||||
self._check_alias_dict(negative_alias, "negative alias")
|
||||
self._check_alias_dict(negative_alias, 'negative alias')
|
||||
self.negative_alias = negative_alias
|
||||
|
||||
def _grok_option_table(self):
|
||||
|
|
@ -149,23 +159,32 @@ class FancyGetopt:
|
|||
else:
|
||||
# the option table is part of the code, so simply
|
||||
# assert that it is correct
|
||||
raise ValueError("invalid option tuple: %r" % (option,))
|
||||
raise ValueError('invalid option tuple: {!r}'.format(option))
|
||||
|
||||
# Type- and value-check the option names
|
||||
if not isinstance(long, str) or len(long) < 2:
|
||||
raise DistutilsGetoptError(("invalid long option '%s': "
|
||||
"must be a string of length >= 2") % long)
|
||||
raise DistutilsGetoptError(
|
||||
(
|
||||
"invalid long option '%s': "
|
||||
'must be a string of length >= 2'
|
||||
) % long,
|
||||
)
|
||||
|
||||
if (not ((short is None) or
|
||||
(isinstance(short, str) and len(short) == 1))):
|
||||
raise DistutilsGetoptError("invalid short option '%s': "
|
||||
"must a single character or None" % short)
|
||||
if (
|
||||
not ((short is None) or
|
||||
(isinstance(short, str) and len(short) == 1))
|
||||
):
|
||||
raise DistutilsGetoptError(
|
||||
"invalid short option '%s': "
|
||||
'must a single character or None' % short,
|
||||
)
|
||||
|
||||
self.repeat[long] = repeat
|
||||
self.long_opts.append(long)
|
||||
|
||||
if long[-1] == '=': # option takes an argument?
|
||||
if short: short = short + ':'
|
||||
if short:
|
||||
short = short + ':'
|
||||
long = long[0:-1]
|
||||
self.takes_arg[long] = 1
|
||||
else:
|
||||
|
|
@ -175,11 +194,12 @@ class FancyGetopt:
|
|||
if alias_to is not None:
|
||||
if self.takes_arg[alias_to]:
|
||||
raise DistutilsGetoptError(
|
||||
"invalid negative alias '%s': "
|
||||
"aliased option '%s' takes a value"
|
||||
% (long, alias_to))
|
||||
"invalid negative alias '%s': "
|
||||
"aliased option '%s' takes a value"
|
||||
% (long, alias_to),
|
||||
)
|
||||
|
||||
self.long_opts[-1] = long # XXX redundant?!
|
||||
self.long_opts[-1] = long # XXX redundant?!
|
||||
self.takes_arg[long] = 0
|
||||
|
||||
# If this is an alias option, make sure its "takes arg" flag is
|
||||
|
|
@ -188,10 +208,11 @@ class FancyGetopt:
|
|||
if alias_to is not None:
|
||||
if self.takes_arg[long] != self.takes_arg[alias_to]:
|
||||
raise DistutilsGetoptError(
|
||||
"invalid alias '%s': inconsistent with "
|
||||
"aliased option '%s' (one of them takes a value, "
|
||||
"the other doesn't"
|
||||
% (long, alias_to))
|
||||
"invalid alias '%s': inconsistent with "
|
||||
"aliased option '%s' (one of them takes a value, "
|
||||
"the other doesn't"
|
||||
% (long, alias_to),
|
||||
)
|
||||
|
||||
# Now enforce some bondage on the long option name, so we can
|
||||
# later translate it to an attribute name on some object. Have
|
||||
|
|
@ -199,8 +220,9 @@ class FancyGetopt:
|
|||
# '='.
|
||||
if not longopt_re.match(long):
|
||||
raise DistutilsGetoptError(
|
||||
"invalid long option name '%s' "
|
||||
"(must be letters, numbers, hyphens only" % long)
|
||||
"invalid long option name '%s' "
|
||||
'(must be letters, numbers, hyphens only' % long,
|
||||
)
|
||||
|
||||
self.attr_name[long] = self.get_attr_name(long)
|
||||
if short:
|
||||
|
|
@ -235,7 +257,7 @@ class FancyGetopt:
|
|||
raise DistutilsArgError(msg)
|
||||
|
||||
for opt, val in opts:
|
||||
if len(opt) == 2 and opt[0] == '-': # it's a short option
|
||||
if len(opt) == 2 and opt[0] == '-': # it's a short option
|
||||
opt = self.short2long[opt[1]]
|
||||
else:
|
||||
assert len(opt) > 2 and opt[:2] == '--'
|
||||
|
|
@ -339,19 +361,21 @@ class FancyGetopt:
|
|||
# Case 1: no short option at all (makes life easy)
|
||||
if short is None:
|
||||
if text:
|
||||
lines.append(" --%-*s %s" % (max_opt, long, text[0]))
|
||||
lines.append(' --%-*s %s' % (max_opt, long, text[0]))
|
||||
else:
|
||||
lines.append(" --%-*s " % (max_opt, long))
|
||||
lines.append(' --%-*s ' % (max_opt, long))
|
||||
|
||||
# Case 2: we have a short option, so we have to include it
|
||||
# just after the long option
|
||||
else:
|
||||
opt_names = "%s (-%s)" % (long, short)
|
||||
opt_names = '{} (-{})'.format(long, short)
|
||||
if text:
|
||||
lines.append(" --%-*s %s" %
|
||||
(max_opt, opt_names, text[0]))
|
||||
lines.append(
|
||||
' --%-*s %s' %
|
||||
(max_opt, opt_names, text[0]),
|
||||
)
|
||||
else:
|
||||
lines.append(" --%-*s" % opt_names)
|
||||
lines.append(' --%-*s' % opt_names)
|
||||
|
||||
for l in text[1:]:
|
||||
lines.append(big_indent + l)
|
||||
|
|
@ -361,7 +385,7 @@ class FancyGetopt:
|
|||
if file is None:
|
||||
file = sys.stdout
|
||||
for line in self.generate_help(header):
|
||||
file.write(line + "\n")
|
||||
file.write(line + '\n')
|
||||
|
||||
|
||||
def fancy_getopt(options, negative_opt, object, args):
|
||||
|
|
@ -370,7 +394,8 @@ def fancy_getopt(options, negative_opt, object, args):
|
|||
return parser.getopt(args, object)
|
||||
|
||||
|
||||
WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}
|
||||
WS_TRANS = {ord(_wschar): ' ' for _wschar in string.whitespace}
|
||||
|
||||
|
||||
def wrap_text(text, width):
|
||||
"""wrap_text(text : string, width : int) -> [string]
|
||||
|
|
@ -386,7 +411,7 @@ def wrap_text(text, width):
|
|||
text = text.expandtabs()
|
||||
text = text.translate(WS_TRANS)
|
||||
chunks = re.split(r'( +|-+)', text)
|
||||
chunks = [ch for ch in chunks if ch] # ' - ' results in empty strings
|
||||
chunks = [ch for ch in chunks if ch] # ' - ' results in empty strings
|
||||
lines = []
|
||||
|
||||
while chunks:
|
||||
|
|
@ -444,7 +469,7 @@ class OptionDummy:
|
|||
setattr(self, opt, None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
text = """\
|
||||
Tra-la-la, supercalifragilisticexpialidocious.
|
||||
How *do* you spell that odd word, anyways?
|
||||
|
|
@ -452,6 +477,6 @@ How *do* you spell that odd word, anyways?
|
|||
say, "How should I know?"].)"""
|
||||
|
||||
for w in (10, 20, 30, 40):
|
||||
print("width: %d" % w)
|
||||
print("\n".join(wrap_text(text, w)))
|
||||
print('width: %d' % w)
|
||||
print('\n'.join(wrap_text(text, w)))
|
||||
print()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue