From bb6ce9d1421b13b384e5b7efde4222b52095af49 Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Sat, 9 Jul 2016 05:28:03 -0700 Subject: [PATCH] minor create_manpage_completions.py improvements Don't truncate long lines with " [See Man Page]" suffix - use the reclaimed 15 characters for more-useful usage info. Improve the --verbose output with: - spacing fixes - diagnostics related to input print repr()/quoted as %r to be less confusing. - get rid of stupid name() and use type()/__class__.__name__, - Always use new-style (new as in post python 2.2) classes so this behaves the same whether we run in python 2 or 3. - Properly convert left-quotes and right-quotes to that character in deroff.py --- share/tools/create_manpage_completions.py | 68 ++++++++--------------- share/tools/deroff.py | 4 +- 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/share/tools/create_manpage_completions.py b/share/tools/create_manpage_completions.py index ea7a66085..ab6c274cb 100755 --- a/share/tools/create_manpage_completions.py +++ b/share/tools/create_manpage_completions.py @@ -155,12 +155,12 @@ def built_command(options, description): if not fish_options: return # Here's what we'll use to truncate if necessary - max_description_width = 63 + max_description_width = 78 if IS_PY3: - truncation_suffix = '… [See Man Page]' + truncation_suffix = '…' else: ELLIPSIS_CODE_POINT = 0x2026 - truncation_suffix = unichr(ELLIPSIS_CODE_POINT) + unicode(' [See Man Page]') + truncation_suffix = unichr(ELLIPSIS_CODE_POINT) # Try to include as many whole sentences as will fit # Clean up some probably bogus escapes in the process @@ -221,16 +221,13 @@ def remove_groff_formatting(data): data = data.replace("\f","") return data -class ManParser: +class ManParser(object): def is_my_type(self, manpage): return False def parse_man_page(self, manpage): return False - def name(self): - return "no-name" - class Type1ManParser(ManParser): def is_my_type(self, manpage): # print manpage @@ -250,7 +247,7 @@ class Type1ManParser(ManParser): options_parts_regex = re.compile("\.PP(.*?)\.RE", re.DOTALL) options_matched = re.search(options_parts_regex, options_section) # print options_matched - add_diagnostic('Command is ' + CMDNAME) + add_diagnostic("Command is %r" % CMDNAME) if options_matched == None: add_diagnostic('Unable to find options') @@ -272,7 +269,7 @@ class Type1ManParser(ManParser): optionName = data[0].strip() if ( optionName.find("-") == -1): - add_diagnostic(optionName + " doesn't contain - ") + add_diagnostic("%r doesn't contain '-' " % optionName) else: optionName = unquote_double_quotes(optionName) optionName = unquote_single_quotes(optionName) @@ -287,7 +284,7 @@ class Type1ManParser(ManParser): options_matched = re.search(options_parts_regex, options_section) def fallback(self, options_section): - add_diagnostic('Falling Back') + add_diagnostic('Trying fallback') options_parts_regex = re.compile("\.TP( \d+)?(.*?)\.TP", re.DOTALL) options_matched = re.search(options_parts_regex, options_section) if options_matched == None: @@ -301,7 +298,7 @@ class Type1ManParser(ManParser): if (len(data)>1 and len(data[1].strip())>0): # and len(data[1])<400): optionName = data[0].strip() if ( optionName.find("-") == -1): - add_diagnostic(optionName + "doesn't contains -") + add_diagnostic("%r doesn't contain '-'" % optionName) else: optionName = unquote_double_quotes(optionName) optionName = unquote_single_quotes(optionName) @@ -316,7 +313,7 @@ class Type1ManParser(ManParser): return True def fallback2(self, options_section): - add_diagnostic('Falling Back2') + add_diagnostic('Trying last chance fallback') ix_remover_regex = re.compile("\.IX.*") trailing_num_regex = re.compile('\\d+$') options_parts_regex = re.compile("\.IP (.*?)\.IP", re.DOTALL) @@ -324,7 +321,7 @@ class Type1ManParser(ManParser): options_section = re.sub(ix_remover_regex, "", options_section) options_matched = re.search(options_parts_regex, options_section) if options_matched == None: - add_diagnostic('Still not found2') + add_diagnostic('Still (still!) not found') return False while options_matched != None: data = options_matched.group(1) @@ -336,7 +333,7 @@ class Type1ManParser(ManParser): optionName = re.sub(trailing_num_regex, "", data[0].strip()) if ('-' not in optionName): - add_diagnostic(optionName + " doesn't contain -") + add_diagnostic("%r doesn't contain '-'" % optionName) else: optionName = optionName.strip() optionName = unquote_double_quotes(optionName) @@ -351,10 +348,6 @@ class Type1ManParser(ManParser): options_matched = re.search(options_parts_regex, options_section) return True - def name(self): - return "Type1" - - class Type2ManParser(ManParser): def is_my_type(self, manpage): options_section_matched = compile_and_search("\.SH OPTIONS(.*?)", manpage) @@ -372,10 +365,10 @@ class Type2ManParser(ManParser): options_parts_regex = re.compile("\.[I|T]P( \d+(\.\d)?i?)?(.*?)\.[I|T]P", re.DOTALL) options_matched = re.search(options_parts_regex, options_section) - add_diagnostic('Command is ' + CMDNAME) + add_diagnostic('Command is %r' % CMDNAME) if options_matched == None: - add_diagnostic(self.name() + ': Unable to find options') + add_diagnostic("%r: Unable to find options" % self) return False while (options_matched != None): @@ -389,7 +382,7 @@ class Type2ManParser(ManParser): if (len(data)>1 and len(data[1].strip())>0): # and len(data[1])<400): optionName = data[0].strip() if '-' not in optionName: - add_diagnostic(optionName + " doesn't contain -") + add_diagnostic("%r doesn't contain '-'" % optionName) else: optionName = unquote_double_quotes(optionName) optionName = unquote_single_quotes(optionName) @@ -401,9 +394,6 @@ class Type2ManParser(ManParser): options_section = options_section[options_matched.end()-3:] options_matched = re.search(options_parts_regex, options_section) - def name(self): - return "Type2" - class Type3ManParser(ManParser): def is_my_type(self, manpage): @@ -421,7 +411,7 @@ class Type3ManParser(ManParser): options_section = options_section_matched.group(1) options_parts_regex = re.compile("\.TP(.*?)\.TP", re.DOTALL) options_matched = re.search(options_parts_regex, options_section) - add_diagnostic('Command is ' + CMDNAME) + add_diagnostic("Command is %r" % CMDNAME) if options_matched == None: add_diagnostic('Unable to find options section') @@ -437,7 +427,7 @@ class Type3ManParser(ManParser): if (len(data)>1): # and len(data[1])<400): optionName = data[0].strip() if ( optionName.find("-") == -1): - add_diagnostic(optionName + "doesn't contain -") + add_diagnostic("%r doesn't contain '-'" % optionName) else: optionName = unquote_double_quotes(optionName) optionName = unquote_single_quotes(optionName) @@ -452,10 +442,6 @@ class Type3ManParser(ManParser): options_matched = re.search(options_parts_regex, options_section) - def name(self): - return "Type3" - - class Type4ManParser(ManParser): def is_my_type(self, manpage): options_section_matched = compile_and_search("\.SH FUNCTION LETTERS(.*?)", manpage) @@ -472,7 +458,7 @@ class Type4ManParser(ManParser): options_section = options_section_matched.group(1) options_parts_regex = re.compile("\.TP(.*?)\.TP", re.DOTALL) options_matched = re.search(options_parts_regex, options_section) - add_diagnostic('Command is ' + CMDNAME) + add_diagnostic("Command is %r" % CMDNAME) if options_matched == None: print >> sys.stderr, "Unable to find options section" @@ -488,7 +474,7 @@ class Type4ManParser(ManParser): if (len(data)>1): # and len(data[1])<400): optionName = data[0].strip() if ( optionName.find("-") == -1): - add_diagnostic(optionName + " doesn't contain - ") + add_diagnostic("%r doesn't contain '-' " % optionName) else: optionName = unquote_double_quotes(optionName) optionName = unquote_single_quotes(optionName) @@ -504,9 +490,6 @@ class Type4ManParser(ManParser): return True - def name(self): - return "Type4" - class TypeDarwinManParser(ManParser): def is_my_type(self, manpage): options_section_matched = compile_and_search("\.S[hH] DESCRIPTION", manpage) @@ -602,9 +585,6 @@ class TypeDarwinManParser(ManParser): return got_something - def name(self): - return "Darwin man parser" - class TypeDeroffManParser(ManParser): def is_my_type(self, manpage): @@ -657,9 +637,6 @@ class TypeDeroffManParser(ManParser): return got_something - def name(self): - return "Deroffing man parser" - # Return whether the file at the given path is overwritable # Raises IOError if it cannot be opened def file_is_overwritable(path): @@ -771,8 +748,7 @@ def parse_manpage_at_path(manpage_path, output_directory): add_diagnostic(manpage_path + ": Not supported") else: for parser in parsersToTry: - parser_name = parser.name() - add_diagnostic('Trying parser ' + parser_name) + add_diagnostic('Trying %s' % parser.__class__.__name__) diagnostic_indent += 1 success = parser.parse_man_page(manpage) diagnostic_indent -= 1 @@ -780,7 +756,7 @@ def parse_manpage_at_path(manpage_path, output_directory): if not built_command_output: success = False if success: - PARSER_INFO.setdefault(parser_name, []).append(CMDNAME) + PARSER_INFO.setdefault(parser.__class__.__name__, []).append(CMDNAME) break if success: @@ -798,7 +774,7 @@ def parse_manpage_at_path(manpage_path, output_directory): # Output the magic word Autogenerated so we can tell if we can overwrite this built_command_output.insert(1, "# Autogenerated from man page " + manpage_path) - built_command_output.insert(2, "# using " + parser_name) + built_command_output.insert(2, "# using " + parser.__class__.__name__) for line in built_command_output: output_file.write(line) output_file.write('\n') @@ -807,7 +783,7 @@ def parse_manpage_at_path(manpage_path, output_directory): if output_file != sys.stdout: output_file.close() else: - parser_names = ', '.join(p.name() for p in parsersToTry) + parser_names = ', '.join(p.__class__.__name__ for p in parsersToTry) #add_diagnostic('%s contains no options or is unparsable' % manpage_path, BRIEF_VERBOSE) add_diagnostic('%s contains no options or is unparsable (tried parser %s)' % (manpage_path, parser_names), BRIEF_VERBOSE) diff --git a/share/tools/deroff.py b/share/tools/deroff.py index 13737aa16..214f2815a 100755 --- a/share/tools/deroff.py +++ b/share/tools/deroff.py @@ -94,8 +94,8 @@ class Deroffer: 'en': '-', 'hy': '-', 'em': "--", - 'lq': "\"", # PCA: This used to be left and right smart quotes, but they look dumb - 'rq': "\"", # So just use ordinary double quotes + 'lq': '“', + 'rq': '”', 'Bq': ",,", 'oq': '`', 'cq': '\'',