PKnLAaFmanly.py""" manly ~~~~~ This script is used (through its' cli) to extract information from manual pages. More specifically, it tells the user, how the given flags modify a programs behaviour. """ __author__ = 'Carl Bordum Hansen' __version__ = '0.2.0' import sys import subprocess import re _ANSI_BOLD = '\033[1m{}\033[0m' HELP = """Usage: manly COMMAND FLAGS... explain commands Example: $ manly rm -r rm - remove files or directories ================================ -r, -R, --recursive remove directories and their contents recursively Arguments: -h, --help display this help and exit. -v, --version display version information and exit. Project resides at """ VERSION = ('manly %s\nCopyright (c) 2017 %s.\nMIT License: see LICENSE.\n\n' 'Written by %s and Mark Jameson.') % ( __version__, __author__, __author__) def parse_flags(raw_flags, single_dash=False): '''Split concatenated flags (eg. ls' -la) into individual flags (eg. '-la' -> '-l', '-a'). Args: raw_flags (list): The flags as they would be given normally. single_dash (bool): Indicate whether a manpage use long names prefixed with only one dash e.g. -nostdinc Returns: flags (list): The disassembled concatenations of flags, and regular verbose flags as given. ''' flags = [] for flag in raw_flags: if flag.startswith('--') or single_dash: flags.append(flag) elif flag.startswith('-'): for char in flag[1:]: flags.append('-' + char) return flags def parse_manpage(page, args): '''Scan the manpage for blocks of text, and check if the found blocks have sections that match the general manpage-flag descriptor style. Args: page (str): The utf-8 encoded manpage. args (iter): An iterable of flags passed to check against. Returns: output (list): The blocks of the manpage that match the given flags. ''' current_section = [] output = [] for line in page.splitlines(): line = line + '\n' if line != '\n': current_section.append(line) continue section = ''.join(current_section) section_top = section.strip().split('\n')[:2] first_line = section_top[0].split(',') for arg in args: try: if any(seg.strip().startswith(arg) for seg in first_line) \ or section_top[1].strip().startswith(arg): section = re.sub(r'(^|\s){}'.format(arg), _ANSI_BOLD.format(arg), section) output.append(section.rstrip()) break except IndexError: pass current_section = [] return output def main(): # HANDLE ALL INPUT try: command = sys.argv[1] except IndexError: print("manly: missing COMMAND\n" "Try 'manly --help' for more information.") sys.exit(0) if len(sys.argv) == 2: if sys.argv[1] in ('-h', '--help'): print(HELP) sys.exit(0) if sys.argv[1] in ('-v', '--version'): print(VERSION) sys.exit(0) print("manly: missing flags\n" "Try 'manly --help' for more information.") sys.exit(2) if sys.argv[1] == 'manly': print('There are no turtles.') sys.exit(0) try: manpage = subprocess.check_output(['man', command]).decode('utf-8') except subprocess.CalledProcessError: sys.exit(16) # because that's the exit status that `man` uses. # LOGIC uses_single_dash_names = any((re.match(r'\s+-\w{2,}', line) for line in manpage.splitlines())) flags = parse_flags(sys.argv[2:], uses_single_dash_names) output = parse_manpage(manpage, flags) title = _ANSI_BOLD.format( re.search( r'(?<=^NAME\n\s{5}).+', manpage, re.MULTILINE ).group(0).strip()) # OUTPUT if output: print('\n%s' % title) print('=' * (len(title) - 8), end='\n\n') for flag in output: print(flag, end='\n\n') else: print('No flags found.') if __name__ == '__main__': main() PK!H\"$&manly-0.2.0.dist-info/entry_points.txtN+I/N.,()M˩Vy\\PKnL2R33manly-0.2.0.dist-info/LICENSEMIT License Copyright (c) 2017 Carl Bordum Hansen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HxyUbmanly-0.2.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,Q034 /, (-JLR()*M ILR(4KM̫#DPK!H:0%8manly-0.2.0.dist-info/METADATATmk0_q}4m`k`IIa$}Edɓfޯv_st=Ͻd^‹;Z'N_(1RhհW9l^zoJ*!x}8H_)L"q*3Ԏɂ]վ06a\% 2a)J #|I}t 2UJ=x9&~xPr*%v*|'|xh,7zagmF Ch wՐ3#8]SNt̶젠Zrt%wH/Ԗ"}މ&İ1&s(DܕTT;ƧkAqLJqm\N˘v͆ BcRKl#GȥZ(<|U=S¢ WoE՘0Z `]mt؛ [Y ڻ֛s4a '#"ꨴyb2gA2ڶFTJ: 5pkc[gU\f51V;BDȿ4/vз[|AES@:aK5ZȔϿSr:WMW?PK!H0Dg'manly-0.2.0.dist-info/RECORDur0@ѽh@^.@HkQ_0jED3u{*;wuz^`Z(c 59e&EV12PP*jUNwgE%jU w#nseÜѶ]xfe0C%zo u^ͶҤ@}uq8]jgDOWWBAQ7—iRk4槖Es>#s5&9= G&C|Ɓ6IyZ*q7І PKnLAaFmanly.pyPK!H\"$&manly-0.2.0.dist-info/entry_points.txtPKnL2R33manly-0.2.0.dist-info/LICENSEPK!HxyUbmanly-0.2.0.dist-info/WHEELPK!H:0%8manly-0.2.0.dist-info/METADATAPK!H0Dg'#manly-0.2.0.dist-info/RECORDPK