def info(input, verbose, pyformat, **kwargs): """ Provides info about the input. Requires valid input. """ if not input: input = '-' with click.open_file(input, mode='rb') as f: # root = xml_utils.load(f) tree = ET.parse(f) root = tree.getroot() d = {} d.update({'xml': xml_utils.element_info(root)}) rf_metrics = { 'suites': xml_utils.count_elements(tree, xpath='//suite'), 'tests': xml_utils.count_elements(tree, xpath='//test'), 'messages': xml_utils.count_elements(tree, xpath='//msg') } d.update({'robot': rf_metrics}) if verbose: d['_object'] = { 'type': type(root), # 'repr': repr(root), # 'vars': sorted(vars(root)), # 'dir': sorted(dir(root)), 'members': sorted(varsdict(root).keys()) } # click.echo(d) # click.echo(sorted(d.items())) if pyformat: s = pformat(d) else: s = json.dumps(d, indent=2, sort_keys=True) click.echo(s)
def elements(input, verbose, pretty, **kwargs): """ Prints information about each element (i.e. tag) in the input. Requires valid input. Examples: \b $ echo '<a><b><c/></b><b><d><e/></d><d/></b></a>' | python -mclifunzone.xmltool elements | tail -n 1 {"path":"/a/b[2]/d","content":{"tag":"d"}} \b $ echo '<a><b><c/></b><b><d><e/></d><d/></b></a>' | python -mclifunzone.xmltool elements | head -n 1 {"path":"/a","content":{"tag":"a"},"metrics":{"children":{"count":2,"tags":["b"]},\\ "descendants":{"count":6,"tags":["b","c","d","e"]}}} """ if not input: input = '-' with click.open_file(input, mode='rb') as f: tree = ET.parse(f) root = tree.getroot() tag = None # 'div' or whatever items = root.iter(tag=tag) if tag else root.iter() # items = [i for i in items] items = [xml_utils.element_info(i, tree=tree) for i in items] if not verbose: # remove 'noise' from the data output to make it more understandable/readable/concise # items = [dict_utils.map_values(i, lambda v: '' if v == '\n' else v) for i in items] items = [dict_utils.remove_if(i, lambda k, v: v == '\n', output=True) for i in items] items = [dict_utils.remove_if(i, lambda k, v: v == '', output=True) for i in items] items = [dict_utils.remove_if(i, lambda k, v: v is None, output=True) for i in items] items = [dict_utils.remove_if(i, lambda k, v: v == [], output=True) for i in items] items = [dict_utils.remove_if(i, lambda k, v: v == {}, output=True) for i in items] # items = [dict_utils.filter_none_values(i) for i in items] # items = [dict_utils.filter_empty_values(i) for i in items] if pretty: output = json.dumps(items, indent=4) click.echo(output) else: lines = [json.dumps(i, indent=None, separators=(',', ':')) for i in items] for line in lines: click.echo(line)