def do_tree(self, arg, opts=None): """Draw an ASCII chart of the hierarchy under the given wiseguy. """ wiseguy = Wiseguy.find_one(id=opts.id) if not wiseguy: print "No Wiseguy found!" return print draw_tree(wiseguy.tree())
def print_cfgelem_ast(translation_unit): # for debugging import asciitree # must be v0.2, newer doesn't work! for cursor in tu.cursor.get_children(): if (cursor.kind == CursorKind.VAR_DECL and cursor.type.spelling.startswith("const struct cfgelem")): asciitree.draw_tree(cursor, lambda n: n.get_children(), lambda n: "{} ({}) at {}".format(n.spelling or n.displayname, str(n.kind).split(".")[1], n.extent))
def test_negamax(): root_node = _make_tree(TREE) #root_node = _make_tree(TREE2) print asciitree.draw_tree(root_node) print "\nHit enter to run algo" raw_input() #print ("NEGAMAX", minimax.negamax(root_node)) print ("NEGAMAX", minimax.dumb_negamax(root_node, 99)) print asciitree.draw_tree(root_node) assert 1 == 2
def lxmlTree(lxmls, returning=False, printing=True, simplify=True, namedAttrs=None): if namedAttrs is None: namedAttrs = ['class', 'id'] outps = [] max_lens = 0 if not isinstance(lxmls, list): lxmls = [lxmls] for num, lxml in enumerate(lxmls): z = lxml_traverser(lxml, [], simplify, namedAttrs) #outp = pinpoint(lxmls, num) outp = asciitree.draw_tree(Node(lxml_get_name(lxml, namedAttrs), z)) max_lens = max(max_lens, max([len(x) for x in outp.split('\n')])) num += 1 if num * (max_lens + 10) > 270: print('can only fit', num - 1, 'out of', len(lxmls)) break outps.append(outp.split('\n')) newoutps = [] max_lines = max([len(x) for x in outps]) for i in range(max_lines): tmp = "" for x in outps: try: tmp += '{:<{}}'.format(x[i], max_lens + 10) except IndexError: tmp += '{:<{}}'.format('', max_lens + 10) newoutps.append(tmp) if printing: print('\n', "\n".join(newoutps)) if returning: return("\n".join(newoutps))
def drawtree(): root = find_root(db) if root is None: print("No root for the tree, cannot draw it.") else: tree = asciitree.draw_tree(root) print(tree)
def soupTree(soups, returning=False, printing=True, pruning=True, namedAttrs=None): if namedAttrs is None: namedAttrs = ['class', 'id'] outps = [] max_lens = 0 if not isinstance(soups, list): soups = [soups] for num, soup in enumerate(soups): parent = soup.children z = traverser(parent, [], pruning, namedAttrs) outp = asciitree.draw_tree(Node(get_name(soup, namedAttrs), z)) max_lens = max(max_lens, max([len(x) for x in outp.split('\n')])) num += 1 if num * (max_lens + 10) > 230: print('can only fit', num - 1, 'out of', len(soups)) break outps.append(outp.split('\n')) newoutps = [] max_lines = max([len(x) for x in outps]) for i in range(max_lines): tmp = "" for x in outps: try: tmp += '{:<{}}'.format(x[i], max_lens + 10) except: tmp += '{:<{}}'.format('', max_lens + 10) newoutps.append(tmp) if printing: print('\n', "\n".join(newoutps)) if returning: return("\n".join(newoutps))
def __str__(self): def node_children(node): return self._children[node] def print_node(node): return '[' + str(node) + '] ' + self._nodes[node].__str__() return asciitree.draw_tree(0, node_children, print_node)
def process(self, verbose = False): """ Process the translation unit Generates an ast.TranslationUnit instance with all the translation unit data (AST, macro instances, etc). Also sets up the needed data for reflection code generation (See runJinja() method below) """ if verbose: self.logger.info('Parsing file: {} {} ...'.format( os.path.basename(self.filePath), ' '.join(self.compileArgs))) self.clang_tu = clang.cindex.Index.create().parse(self.filePath, args = self.compileArgs) if verbose: for d in self.clang_tu.diagnostics: GlobalLogger.error().step('Line {} (severity {}): {}'.format(d.location.line, d.severity, d.spelling)) if verbose: self.logger.info('Processing AST...') self.translation_unit = TranslationUnit(self.clang_tu.cursor, self.filePath) # This is the root of the AST. For easy visitation, check TranslationUnit.nodes() method, # it gives an iterable on the translation unit AST self.root = self.translation_unit.root # Print the processed AST and the full AST given by libclang if self.print_ast: if verbose: self.logger.info('Dumping AST to {}...'.format(self.ast_file_path)) with open(self.ast_file_path, 'w') as ast_file: import asciitree ast_file.write(asciitree.draw_tree(self.root, lambda n: list(n.get_children()), lambda n: n.print_ast_node() )) ast_file.write('\n\n\nFULL AST BELLOW:\n\n\n') ast_file.write(asciitree.draw_tree(self.clang_tu.cursor, lambda c: list(c.get_children()), lambda c: 'File {}, line {}: \'{}\', {}'.format(c.location.file, c.location.line, c.displayname or c.spelling, c.kind) ))
def scan_results(self, inputFile): """ """ logging.info("reading blast output") organismsGi = {} with open(inputFile) as blastReader: for line in blastReader: info = line.split() if info[4] in organismsGi: organismsGi[info[4]] = organismsGi[info[4]] + 1 else: organismsGi[info[4]] = 1 logging.info("convert gi to tax") gi2taxId = Gi2taxIdConverter(Configuration.instance.getGlobalOption("giToTax")) gi2taxIdDict = gi2taxId.convert(organismsGi.keys()) organismsTax = {} for gi in organismsGi.keys(): if gi in gi2taxIdDict: organismsTax[gi2taxIdDict[gi]] = organismsGi[gi] del organismsGi[gi] del organismsGi logging.info("creating tree") self.tree = {} self.tree["1"] = TaxonomyNode("1", 0) self.fullTax = self.parseTax() for taxId in organismsTax.keys(): self.appendToTree(taxId, organismsTax[taxId]) self.tree["1"].updateName(self.parseNames()) with open(os.path.splitext(inputFile)[0] + "TreeUnpruned.txt", "w") as writer: writer.write(asciitree.draw_tree(self.tree["1"])) logging.info("pruning tree") self.tree["1"].pruneCounts() self.tree["1"].pruneParents() logging.info("drawing tree") with open(os.path.splitext(inputFile)[0] + "Tree.txt", "w") as writer: writer.write(asciitree.draw_tree(self.tree["1"]))
def process(self): """ Process the translation unit Generates an ast.TranslationUnit instance with all the translation unit data (AST, macro instances, etc). Also sets up the needed data for reflection code generation (See runJinja() method below) """ self.logger.info('Parsing file...') ast_options = clang.cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD self.clang_tu = self.index.parse(self.filePath, args = self.compileArgs, options = ast_options) for d in self.clang_tu.diagnostics: GlobalLogger.error().step('Line {} (severity {}): {}'.format(d.location.line, d.severity, d.spelling)) self.logger.info('Processing AST...') self.classes = [] self.namespace = [] self.translation_unit = TranslationUnit(self.clang_tu.cursor, self.filePath) print '\r' + ' '*100 + '\r', # Clean progress # This is the root of the AST. For easy visitation, check TranslationUnit.nodes() method, # it gives an iterable on the translation unit AST self.root = self.translation_unit.root # Print the processed AST and the full AST given by libclang. if self.print_ast: self.logger.info('Dumping AST to {}...'.format(self.ast_file_path)) with open(self.ast_file_path, 'w') as ast_file: import asciitree ast_file.write(asciitree.draw_tree(self.root, lambda n: list(n.get_children()), lambda n: n.print_ast_node() )) ast_file.write('\n\n\nFULL AST BELLOW:\n\n\n') ast_file.write(asciitree.draw_tree(self.clang_tu.cursor, lambda c: list(c.get_children()), lambda c: 'File {}, line {}: \'{}\', {}'.format(c.location.file, c.location.line, c.displayname or c.spelling, c.kind) ))
def format_asciitree(self): """Return a string representing this tree using asciitree (requires the 'asciitree' package).""" import asciitree def child_iter(tree): return tree.subtrees() def text_str(tree): return ' %s%s %s' % (tree.label, tree.label_suffix, tree.token or '') return asciitree.draw_tree(self, child_iter=child_iter, text_str=text_str)
def main(): global comdblist try: # Parse args parser = argparse.ArgumentParser(description='pyclang argparser') parser.add_argument('infiles', nargs='+', help="Source file(s)") parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout) parser.add_argument('compdb', nargs='?', type=argparse.FileType('r'), help="Path to compile_commands.json file") toolargs = parser.parse_args() outfile = toolargs.outfile sys.stdout = outfile if toolargs.compdb: with open(toolargs.compdb) as data_file: comdblist = json.load(data_file) init() index = clang.cindex.Index(clang.cindex.conf.lib.clang_createIndex(False, True)) for infile in toolargs.infiles: args = ['-x'] if infile.endswith(".c"): args.append('c') elif infile.endswith(".cpp") or infile.endswith(".cc"): args.append('c++') if toolargs.compdb: args.append(argumentAdjuster(findCmdForFile(infile))) try: # index.parse does not pass through errors from libclang. Errors are # emitted to stderr translation_unit = index.parse(infile, args) except Exception, TranslationUnitLoadError: print traceback.format_exc() return print asciitree.draw_tree(translation_unit.cursor, lambda n: list(n.get_children()), lambda n: "%s (%s)" % (n.spelling or n.displayname, str(n.kind).split(".")[1])) except Exception, err: print traceback.format_exc() return
def dump(cursor): """ Display the AST represented by the cursor """ def node_children(node): return list(node.get_children()) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind).split('.')[1] return '{} {}'.format(kind, text) return draw_tree(cursor, node_children, print_node)
def __repr__(self): """ """ def node_children(node): return filter(self.function, node.get_children()) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind)[str(node.kind).index('.')+1:] access = node.access_specifier return '{} {}'.format(kind, text) return "\n".join(asciitree.draw_tree(translation_unit.cursor, node_children, print_node) for translation_unit in self._translation_units)
def _tree(f, root_name='/', ret=False, show_chunks=False): _names = [] def get_names(name, obj): if isinstance(obj, h5py.Dataset): dtype = str(obj.dtype) shape = str(obj.shape) if show_chunks: chunks = str(obj.chunks) _names.append("%s [%s, %s, %s]" % (name[1:], dtype, shape, chunks)) else: _names.append("%s [%s, %s]" % (name[1:], dtype, shape)) else: _names.append(name[1:]) # f._visititems(get_names) _visititems(f, get_names) class Node(object): def __init__(self, name, children): self.name = name self.children = children def __str__(self): return self.name root = Node(root_name, dict()) def add_to_node(node, ns): if len(ns) == 0: return if ns[0] not in node.children: node.children[ns[0]] = Node(ns[0], dict()) add_to_node(node.children[ns[0]], ns[1:]) _names = sorted(_names) for n in _names: ns = n.split('/') add_to_node(root, ns) def child_iter(node): keys = list(node.children.keys()) indices = np.argsort(keys) indices = np.asarray(indices) vals = list(node.children.values()) return list(np.asarray(vals)[indices]) msg = asciitree.draw_tree(root, child_iter) if ret: return msg print(msg)
def as_asciitree(self, str_func=None): """Represent this Sentence as an ASCII tree string. Requires the asciitree package. A default token stringifier is provided but for custom formatting, specify a str_func which should take a single Token and return a string.""" import asciitree from collections import defaultdict children = defaultdict(list) # since erased nodes may be missing, multiple tokens may have same # index (CCprocessed), etc. token_to_index = {} roots = [] for token in self: children[token.head].append(token) token_to_index[token] = token.index if token.head == 0: roots.append(token) assert roots, "Couldn't find root Token(s)" if len(roots) > 1: # multiple roots so we make a fake one to be their parent root = Token(0, 'ROOT', 'ROOT-LEMMA', 'ROOT-CPOS', 'ROOT-POS', None, None, 'ROOT-DEPREL', None, None) token_to_index[root] = 0 children[0] = roots else: root = roots[0] def child_func(token): index = token_to_index[token] return children[index] if not str_func: def str_func(token): return ' %s [%s]' % (token.form, token.deprel) return asciitree.draw_tree(root, child_func, str_func)
def compile(velis_code): parsed = parser.parse(velis_code) # Print "AST" followed by the AST indented by two tabs logger.debug("AST\n\t\t" + asciitree.draw_tree(parsed).replace('\n', '\n\t\t')) return output_spreadsheet(parsed).as_json()
function volsaccont(t): guard(t<10) = if(primarysex == "M", 10,20) guard() = if(age < 65, 1, 0) function withdrawal(t) = if(age >= 65, 0-100,0) function primarysex(t) = "M" function awesomeness(t) = if(primarysex=="M",2,3) function predtest(t) = 3 == 5 function ffitest(t): guard(t==0) = 0 guard() = sum( sal(t-=[0..1]), awesomeness(t-=[1..0]) ) """ ] testing2 = [ """ dimension t = [0..5] function refertwo(t) = sum(3) """ ] for test in testing: print "-=| %s |=-" % test parsed = parser.parse(test) print asciitree.draw_tree(parsed) print "Finished outputting tree." print output_excel(parsed)
#!/usr/bin/python # vim: set fileencoding=utf-8 import clang.cindex import asciitree import sys def node_children(node): return (c for c in node.get_children() if c.location.file.name == sys.argv[1]) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind)[str(node.kind).index('.')+1:] return '{} {}'.format(kind, text) if len(sys.argv) != 2: print("Usage: dump_ast.py [header file name]") sys.exit() clang.cindex.Config.set_library_file('/usr/lib/llvm-3.6/lib/libclang.so') index = clang.cindex.Index.create() translation_unit = index.parse(sys.argv[1], ['-x', 'c++', '-std=c++11', '-D__CODE_GENERATOR__']) print(asciitree.draw_tree(translation_unit.cursor, node_children, print_node))
def dump_cursor(self, cursor): print asciitree.draw_tree(cursor, self.node_children, self.print_node)
#!/usr/bin/python # vim: set fileencoding=utf-8 import clang.cindex import asciitree import sys def node_children(node): return (c for c in node.get_children() if c.location.file.name == sys.argv[1]) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind)[str(node.kind).index('.') + 1:] return '{} {}'.format(kind, text) if len(sys.argv) != 2: print("Usage: dump_ast.py [header file name]") sys.exit() clang.cindex.Config.set_library_file('/usr/lib/llvm-3.6/lib/libclang.so') index = clang.cindex.Index.create() translation_unit = index.parse( sys.argv[1], ['-x', 'c++', '-std=c++11', '-D__CODE_GENERATOR__']) print(asciitree.draw_tree(translation_unit.cursor, node_children, print_node))
def printAST(xx): print asciitree.draw_tree( xx, lambda n: [x for x in n.get_children()], lambda n: "%s (%s - %s)" % (n.spelling or n.displayname, str(n.kind).split(".")[1], n.type. spelling))
def node_children(node): return [c for c in node.get_children()] # return (c for c in node.get_children() if c.location.file.name == sys.argv[1]) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind) #[str(node.kind).index('.')+1:] return '{} {}'.format(kind, text) def find_typerefs(node, typename): """ Find all references to the type named 'typename' """ if node.kind.is_reference(): ref_node = clang.cindex.Cursor_ref(node) if ref_node.spelling == typename: print 'Found %s [line=%s, col=%s]' % (typename, node.location.line, node.location.column) # Recurse for children of this node for c in node.get_children(): find_typerefs(c, typename) clang.cindex.Config.set_library_file('/usr/lib/x86_64-linux-gnu/libclang.so') index = clang.cindex.Index.create() tu = index.parse(sys.argv[1]) print 'Translation unit:', tu.spelling print asciitree.draw_tree(tu.cursor, node_children, print_node) find_typerefs(tu.cursor, sys.argv[2])
return i return None data = [] for line in sys.stdin: input = filter(None,line.split(' ')) #filter the '' data.append(input[0:-1])# strip the terminating \n root = Node('root', []) iterator = root j = 1 for i in range(0,len(data)): if(len(data[i]) == 1):#exception for root continue while(data[i][j] in iterator.listChildren()):#loop through the dirs while they are already in the dict iterator = iterator.find(data[i][j]) j += 1 for k in range(j,len(data[i])):#dirs no longer found so start adding them to dict iterator.children.append(Node(data[i][k],[])) iterator = iterator.children[-1] iterator.name += " (" +str(data[i][0]) + ")" #append the request count to the leaf j = 1; #reset iterator variables iterator = root print asciitree.draw_tree(root)#print the tree
def __str__(self): return self.name def is_prime(n): """Trial division method: Tests primality by testing if n is divisible by a prime number from 2 to ceil(sqrt(n))""" # past ceil(sqrt(n)), the factors are the same for i in range(2,int(math.ceil(math.sqrt(n)))+1): if not n % i: return False return True def get_factors(n): while not is_prime(n): for i in range(2,n): if not n % i: n = n/i yield i, n break yield n, None if __name__ == "__main__": sys.stdin = StringIO.StringIO("1767150") n = int(sys.stdin.readline().strip()) root = Node(str(n),[]) current_node = root for x,y in get_factors(n): if y is not None: current_node.children = [Node(str(y),[]), Node(str(x),[])] current_node = current_node.children[0] print(asciitree.draw_tree(root))
parsers = (MsgParser(), NullOutputParser(), StreamParser(), TitleParser(), ClassParser(), TCountParser()) if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option("-d", "--debug", dest="debug", default=False, action="store_true") (options, args) = parser.parse_args() if options.debug: log.setLevel(logging.DEBUG) data = open("info-planes.txt", "r") l = data.readline() while l: prefix, array = (None, None) try: (prefix, array) = l.split(":", 1) except: log.warning("Could not split apart %s", l) handled = False for p in parsers: if p.can_handle(prefix): p.do_handle(split_arguments(array)) handled = True break if not handled: log.debug("Did not handle prefix %s", prefix) l = data.readline() print asciitree.draw_tree(g_sourceInfo, lambda n: n.children.values())
import clang.cindex, asciitree, sys, os kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS') index = clang.cindex.Index(clang.cindex.conf.lib.clang_createIndex(False, True)) translation_unit = index.parse(os.path.join(kInputsDir, 'hello.cpp'), ['-x', 'c++']) print(asciitree.draw_tree(translation_unit.cursor, lambda n: list(n.get_children()), lambda n: "%s (%s)" % (n.spelling or n.displayname, str(n.kind).split(".")[1])))
import clang.cindex import asciitree def node_children(node): return [c for c in node.get_children()] # return (c for c in node.get_children() if c.location.file.name == sys.argv[1]) def print_node(node): text = node.spelling or node.displayname kind = str(node.kind) #[str(node.kind).index('.')+1:] return '{} {}'.format(kind, text) def find_typerefs(node, typename): """ Find all references to the type named 'typename' """ if node.kind.is_reference(): ref_node = clang.cindex.Cursor_ref(node) if ref_node.spelling == typename: print 'Found %s [line=%s, col=%s]' % ( typename, node.location.line, node.location.column) # Recurse for children of this node for c in node.get_children(): find_typerefs(c, typename) clang.cindex.Config.set_library_file('/usr/lib/x86_64-linux-gnu/libclang.so') index = clang.cindex.Index.create() tu = index.parse(sys.argv[1]) print 'Translation unit:', tu.spelling print asciitree.draw_tree(tu.cursor, node_children, print_node) find_typerefs(tu.cursor, sys.argv[2])
self.fragment = fragment def children(self): children = [] for operator in db.operators.find({'query_id': self.fragment['query_id'], 'fragment_id': self.fragment['id'], 'parent_id': None}): children.append(Operator(operator)) return children def __str__(self): return "F%s" % self.fragment['id'] class Operator(object): def __init__(self, operator): self.operator = operator def children(self): children = [] for operator in db.operators.find({'query_id': self.operator['query_id'], 'fragment_id': self.operator['fragment_id'], 'parent_id': self.operator['id']}): children.append(Operator(operator)) for fragment in db.fragments.find({'query_id': self.operator['query_id'], 'exchange_id': self.operator['id']}): children.append(Fragment(fragment)) return children def __str__(self): return "%s %s" % (self.operator['id'], self.operator['name']) db = pymongo.MongoClient().impala queryId = sys.argv[1] root = Fragment(db.fragments.find_one({'query_id': ObjectId(queryId), 'exchange_id': None})) print draw_tree(root, lambda n: n.children())
def print_tree(node): print(asciitree.draw_tree(node, node_children, print_node))
def dump_str(self, bone, level=0): """Dump graph as a string.""" print('\n', asciitree.draw_tree(bone, lambda x: x.child, str))
def getLaTeXReport(self): txt = "\\section{Tree of found homolog sequences}\n" txt = txt + "\\begin{verbatim}\n" txt = txt + asciitree.draw_tree(self.tree["1"]) txt = txt + "\\end{verbatim}\n" return txt
#!/usr/bin/env python import clang.cindex, asciitree, sys index = clang.cindex.Index.create() translation_unit = index.parse(sys.argv[1], ['-x', 'c++']) print asciitree.draw_tree( translation_unit.cursor, lambda n: n.get_children(), lambda n: "%s (%s)" % (n.spelling or n.displayname, str(n.kind).split(".")[1]))