"""distutils.fancy_getoptWrapper around the standard getopt module that provides the followingadditional features:* short and long options are tied together* options have help strings, so fancy_getopt could potentiallycreate a complete usage summary* options set attributes of a passed-in object"""import sys, string, reimport getoptfrom distutils.errors import *# Much like command_re in distutils.core, this is close to but not quite# the same as a Python NAME -- except, in the spirit of most GNU# utilities, we use '-' in place of '_'. (The spirit of LISP lives on!)# The similarities to NAME are again not a coincidence...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))# 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 somehandy extra functionality:* short and long options are tied together* options have help strings, and help text can be assembledfrom them* options set attributes of a passed-in object* boolean options can have "negative aliases" -- eg. if--quiet is the "negative alias" of --verbose, then "--quiet"on the command line sets 'verbose' to false"""def __init__(self, option_table=None):# The option table is (currently) a list of tuples. The# tuples may have 3 or four values:# (long_option, short_option, help_string [, repeatable])# if an option takes an argument, its long_option should have '='# appended; short_option should just be a single character, no ':'# in any case. If a long_option doesn't have a corresponding# short_option, short_option should be None. All option tuples# must have long options.self.option_table = option_table# 'option_index' maps long option names to entries in the option# table (ie. those 3-tuples).self.option_index = {}if self.option_table:self._build_index()# 'alias' records (duh) alias options; {'foo': 'bar'} means# --foo is an alias for --barself.alias = {}# 'negative_alias' keeps track of options that are the boolean# opposite of some other optionself.negative_alias = {}# These keep track of the information in the option table. We# don't actually populate these structures until we're ready to# parse the command-line, since the 'option_table' passed in here# isn't necessarily the final word.self.short_opts = []self.long_opts = []self.short2long = {}self.attr_name = {}self.takes_arg = {}# And 'option_order' is filled up in 'getopt()'; it records the# original order of options (and their values) on the command-line,# but expands short options, converts aliases, etc.self.option_order = []def _build_index(self):self.option_index.clear()for option in self.option_table:self.option_index[option[0]] = optiondef set_option_table(self, option_table):self.option_table = option_tableself._build_index()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)else:option = (long_option, short_option, help_string)self.option_table.append(option)self.option_index[long_option] = optiondef has_option(self, long_option):"""Return true if the option table for this parser has anoption with long name 'long_option'."""return long_option in self.option_indexdef get_attr_name(self, long_option):"""Translate long option name 'long_option' to the form ithas as an attribute of some object: ie., translate hyphensto underscores."""return long_option.translate(longopt_xlate)def _check_alias_dict(self, aliases, what):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))if opt not in self.option_index: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.alias = aliasdef set_negative_aliases(self, negative_alias):"""Set the negative aliases for this option parser.'negative_alias' should be a dictionary mapping option names tooption names, both the key and value must already be definedin the option table."""self._check_alias_dict(negative_alias, "negative alias")self.negative_alias = negative_aliasdef _grok_option_table(self):"""Populate the various data structures that keep tabs on theoption table. Called by 'getopt()' before it can do anythingworthwhile."""self.long_opts = []self.short_opts = []self.short2long.clear()self.repeat = {}for option in self.option_table:if len(option) == 3:long, short, help = optionrepeat = 0elif len(option) == 4:long, short, help, repeat = optionelse:# the option table is part of the code, so simply# assert that it is correctraise ValueError("invalid option tuple: %r" % (option,))# Type- and value-check the option namesif not isinstance(long, str) or len(long) < 2: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)self.repeat[long] = repeatself.long_opts.append(long)if long[-1] == '=': # option takes an argument?if short: short = short + ':'long = long[0:-1]self.takes_arg[long] = 1else:# Is option is a "negative alias" for some other option (eg.# "quiet" == "!verbose")?alias_to = self.negative_alias.get(long)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))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# the same as the option it's aliased to.alias_to = self.alias.get(long)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))# Now enforce some bondage on the long option name, so we can# later translate it to an attribute name on some object. Have# to do this a bit late to make sure we've removed any trailing# '='.if not longopt_re.match(long):raise DistutilsGetoptError("invalid long option name '%s' ""(must be letters, numbers, hyphens only" % long)self.attr_name[long] = self.get_attr_name(long)if short:self.short_opts.append(short)self.short2long[short[0]] = longdef getopt(self, args=None, object=None):"""Parse command-line options in args. Store as attributes on object.If 'args' is None or not supplied, uses 'sys.argv[1:]'. If'object' is None or not supplied, creates a new OptionDummyobject, stores option values there, and returns a tuple (args,object). If 'object' is supplied, it is modified in place and'getopt()' just returns 'args'; in both cases, the returned'args' is a modified copy of the passed-in 'args' list, whichis left untouched."""if args is None:args = sys.argv[1:]if object is None:object = OptionDummy()created_object = Trueelse:created_object = Falseself._grok_option_table()short_opts = ' '.join(self.short_opts)try:opts, args = getopt.getopt(args, short_opts, self.long_opts)except getopt.error as msg:raise DistutilsArgError(msg)for opt, val in opts:if len(opt) == 2 and opt[0] == '-': # it's a short optionopt = self.short2long[opt[1]]else:assert len(opt) > 2 and opt[:2] == '--'opt = opt[2:]alias = self.alias.get(opt)if alias:opt = aliasif not self.takes_arg[opt]: # boolean option?assert val == '', "boolean option can't have value"alias = self.negative_alias.get(opt)if alias:opt = aliasval = 0else:val = 1attr = self.attr_name[opt]# The only repeating option at the moment is 'verbose'.# It has a negative option -q quiet, which should set verbose = 0.if val and self.repeat.get(attr) is not None:val = getattr(object, attr, 0) + 1setattr(object, attr, val)self.option_order.append((opt, val))# for optsif created_object:return args, objectelse:return argsdef get_option_order(self):"""Returns the list of (option, value) tuples processed by theprevious run of 'getopt()'. Raises RuntimeError if'getopt()' hasn't been called yet."""if self.option_order is None:raise RuntimeError("'getopt()' hasn't been called yet")else:return self.option_orderdef generate_help(self, header=None):"""Generate help text (a list of strings, one per suggested line ofoutput) from the option table for this FancyGetopt object."""# Blithely assume the option table is good: probably wouldn't call# 'generate_help()' unless you've already called 'getopt()'.# First pass: determine maximum length of long option namesmax_opt = 0for option in self.option_table:long = option[0]short = option[1]l = len(long)if long[-1] == '=':l = l - 1if short is not None:l = l + 5 # " (-x)" where short == 'x'if l > max_opt:max_opt = lopt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter# Typical help block looks like this:# --foo controls foonabulation# Help block for longest option looks like this:# --flimflam set the flim-flam level# and with wrapped text:# --flimflam set the flim-flam level (must be between# 0 and 100, except on Tuesdays)# Options with short names will have the short name shown (but# it doesn't contribute to max_opt):# --foo (-f) controls foonabulation# If adding the short option would make the left column too wide,# we push the explanation off to the next line# --flimflam (-l)# set the flim-flam level# Important parameters:# - 2 spaces before option block start lines# - 2 dashes for each long option name# - min. 2 spaces between option and explanation (gutter)# - 5 characters (incl. space) for short option name# Now generate lines of help text. (If 80 columns were good enough# for Jesus, then 78 columns are good enough for me!)line_width = 78text_width = line_width - opt_widthbig_indent = ' ' * opt_widthif header:lines = [header]else:lines = ['Option summary:']for option in self.option_table:long, short, help = option[:3]text = wrap_text(help, text_width)if long[-1] == '=':long = long[0:-1]# 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]))else:lines.append(" --%-*s " % (max_opt, long))# Case 2: we have a short option, so we have to include it# just after the long optionelse:opt_names = "%s (-%s)" % (long, short)if text:lines.append(" --%-*s %s" %(max_opt, opt_names, text[0]))else:lines.append(" --%-*s" % opt_names)for l in text[1:]:lines.append(big_indent + l)return linesdef print_help(self, header=None, file=None):if file is None:file = sys.stdoutfor line in self.generate_help(header):file.write(line + "\n")def fancy_getopt(options, negative_opt, object, args):parser = FancyGetopt(options)parser.set_negative_aliases(negative_opt)return parser.getopt(args, object)WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}def wrap_text(text, width):"""wrap_text(text : string, width : int) -> [string]Split 'text' into multiple lines of no more than 'width' characterseach, and return the list of strings that results."""if text is None:return []if len(text) <= width:return [text]text = text.expandtabs()text = text.translate(WS_TRANS)chunks = re.split(r'( +|-+)', text)chunks = [ch for ch in chunks if ch] # ' - ' results in empty stringslines = []while chunks:cur_line = [] # list of chunks (to-be-joined)cur_len = 0 # length of current linewhile chunks:l = len(chunks[0])if cur_len + l <= width: # can squeeze (at least) this chunk incur_line.append(chunks[0])del chunks[0]cur_len = cur_len + lelse: # this line is full# drop last chunk if all spaceif cur_line and cur_line[-1][0] == ' ':del cur_line[-1]breakif chunks: # any chunks left to process?# if the current line is still empty, then we had a single# chunk that's too big too fit on a line -- so we break# down and break it up at the line widthif cur_len == 0:cur_line.append(chunks[0][0:width])chunks[0] = chunks[0][width:]# all-whitespace chunks at the end of a line can be discarded# (and we know from the re.split above that if a chunk has# *any* whitespace, it is *all* whitespace)if chunks[0][0] == ' ':del chunks[0]# and store this line in the list-of-all-lines -- as a single# string, of course!lines.append(''.join(cur_line))return linesdef translate_longopt(opt):"""Convert a long option name to a valid Python identifier bychanging "-" to "_"."""return opt.translate(longopt_xlate)class OptionDummy:"""Dummy class just used as a place to hold command-line optionvalues as instance attributes."""def __init__(self, options=[]):"""Create a new OptionDummy instance. The attributes listed in'options' will be initialized to None."""for opt in options:setattr(self, opt, None)if __name__ == "__main__":text = """\Tra-la-la, supercalifragilisticexpialidocious.How *do* you spell that odd word, anyways?(Someone ask Mary -- she'll know [or she'llsay, "How should I know?"].)"""for w in (10, 20, 30, 40):print("width: %d" % w)print("\n".join(wrap_text(text, w)))print()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。