github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/help/process.py (about) 1 """Tool to preprocess some of the help files into an appropriate format.""" 2 3 import json 4 import re 5 import sys 6 7 8 DOCSTRING_RE = re.compile(r'^( *[^ ]+) (\([^\)]+\)):', flags=re.MULTILINE) 9 10 11 def main(filename): 12 with open(filename) as f: 13 data = json.load(f) 14 online_help = lambda f: '${RESET}Online help is available at ${BLUE}https://please.build/lexicon.html#%s${RESET}.\n' % f 15 m = lambda k, v: '${BOLD_YELLOW}%s${RESET}(%s)\n\n%s\n\n%s' % (k, ', '.join( 16 '${GREEN}%s${RESET}' % a['name'] for a in v['args']), colourise(v.get('docstring', ''), v['args'], data['functions']), online_help(k)) 17 18 json.dump({ 19 'topics': {k: m(k, v) for k, v in data['functions'].items()}, 20 'preamble': '${BOLD_BLUE}%s${RESET} is a built-in build rule in Please. Instructions for use & its arguments:', 21 }, sys.stdout, sort_keys=True) 22 23 24 def colourise(docstring, args, functions): 25 def replace(m): 26 if any(arg for arg in args if arg['name'] == m.group(1).strip() and arg.get('deprecated')): 27 return '${GREY}' + m.group(0) 28 return '${YELLOW}%s${RESET} ${GREEN}%s${RESET}:' % (m.group(1), m.group(2)) 29 30 # Must order by longest first in case of overlapping options (cgo_library vs. go_library etc) 31 for function in sorted(functions, key=lambda f: -len(f)): 32 docstring = docstring.replace(function, '${BLUE}%s${RESET}' % function) 33 docstring = docstring.replace('Args:\n', '${BOLD_YELLOW}Args:${RESET}\n') 34 return DOCSTRING_RE.sub(replace, docstring) 35 36 37 if __name__ == '__main__': 38 main(sys.argv[1])