def format_providermodule(module): moddoc = sane_fill(module.__doc__, initial_indent='', subsequent_indent='') if moddoc is None: moddoc = '' functions = get_providers(module) lines = [] name = t.bold_underline(module.__name__) for val, function in functions.items(): #Get the docs doc = function.__doc__ if doc is None: doc = '' if hasattr(function, 'highlight'): highlight = '(%s)' % function.highlight color_highlight = get_highlight_color(function.highlight)(highlight) else: color_highlight = highlight = '' color_header = "%s%s: "%(t.bold(val), color_highlight) white_header = "%s%s:"%(val, highlight) lines.append(wrap_lines_with_color_header(color_header, white_header, doc)) lines = '\n\n'.join(lines) s = ("{name}\n{moddoc}\n" "The following providers are defined in this module:\n\n" "{lines}".format(name=name, moddoc = moddoc, lines=lines)) return s
def print_providertree(providertree): it = iter(providertree) result = StringIO() seen = set() res_providers = {} unknown_lines = OrderedDict() config_lines = OrderedDict() function = next(it) result.write(t.bold_underline(function.__name__)) result.write('\n\n') result.write("Defined in: %s\n\n" % t.blue_underline(function.__module__)) if hasattr(function, 'highlight'): hl = function.highlight result.write("Generates: %s\n\n" % get_highlight_color(hl)(hl)) result.write(print_signature(function)) result.write('\n\n') doc = function.__doc__ if doc is not None: result.write(sane_fill(sane_dedent(doc), initial_indent='', subsequent_indent='')) result.write('\n\n') def walk(it, provider=''): for spec in it: tp, name, value = spec #If we require it for the current provider '' drop the past one. if name not in res_providers or res_providers[name] != '': res_providers[name] = provider if name in seen: continue seen.add(name) if tp == 'config': config_lines[name] = format_config_line(name, value[0], sig_index=0) walk(value[1:], name) elif tp == 'provider': #We only care about the first level of nested providers. if provider=='': walk(value[1:], name) else: walk(value[1:], provider) elif tp == 'unknown': val_tp = get_annotation_string(value.annotation) default = ' = {}'.format(value.default if value.default is not value.empty else '') line = " {}{}{}".format(t.bold(name), val_tp, default) unknown_lines[name] = line else: raise ValueError("Unknown walk spec") walk(it) for reosurce,provider in res_providers.items(): if provider: s = t.blue('[Used by %s' % t.underline(provider)) + t.blue(']') if reosurce in config_lines: config_lines[reosurce] += '\n ' + s if reosurce in unknown_lines: unknown_lines[reosurce] += ' ' + s if config_lines: result.write("The following resources are read from the configuration:\n\n") #Sort so that the direct dependencies come first. Because the sort is #stable, the total ordering looks good this way. result.write('\n\n'.join(config_lines[k] for k in sorted(config_lines, key=lambda x:res_providers[x]!=''))) if unknown_lines: result.write( "\n\nThe following additionl arguments can " "be used to control the\nbehaviour. " "They are set by default to sensible values:\n\n""" ) result.write('\n'.join(unknown_lines[k] for k in sorted(unknown_lines, key=lambda x:res_providers[x]!=''))) return result.getvalue()