|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Display a family tree""" |
| 3 | + |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import re |
| 7 | +from dire import die |
| 8 | +from graphviz import Digraph |
| 9 | + |
| 10 | + |
| 11 | +# -------------------------------------------------- |
| 12 | +def get_args(): |
| 13 | + """Get command-line arguments""" |
| 14 | + |
| 15 | + parser = argparse.ArgumentParser( |
| 16 | + description='Display a family tree', |
| 17 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 18 | + |
| 19 | + parser.add_argument('file', |
| 20 | + metavar='FILE', |
| 21 | + type=argparse.FileType('r'), |
| 22 | + help='File input') |
| 23 | + |
| 24 | + parser.add_argument('-o', |
| 25 | + '--outfile', |
| 26 | + help='Output filename', |
| 27 | + metavar='str', |
| 28 | + type=str, |
| 29 | + default='') |
| 30 | + |
| 31 | + parser.add_argument('-v', '--view', help='View image', action='store_true') |
| 32 | + |
| 33 | + return parser.parse_args() |
| 34 | + |
| 35 | + |
| 36 | +# -------------------------------------------------- |
| 37 | +def main(): |
| 38 | + """Make a jazz noise here""" |
| 39 | + |
| 40 | + args = get_args() |
| 41 | + fh = args.file |
| 42 | + out_file = args.outfile or os.path.basename(fh.name) + '.gv' |
| 43 | + |
| 44 | + nodes, edges = parse_tree(fh) |
| 45 | + |
| 46 | + if not nodes and not edges: |
| 47 | + die('No nodes or edges in "{}".'.format(fh.name)) |
| 48 | + |
| 49 | + dot = Digraph(comment='Tree') |
| 50 | + |
| 51 | + # keys are initials which we don't need |
| 52 | + for _, name in nodes.items(): |
| 53 | + dot.node(name) |
| 54 | + |
| 55 | + for n1, n2 in edges: |
| 56 | + # see if node has alias in nodes, else use node itself |
| 57 | + n1 = nodes.get(n1, n1) |
| 58 | + n2 = nodes.get(n2, n2) |
| 59 | + dot.edge(n1, n2) |
| 60 | + |
| 61 | + dot.render(out_file, view=args.view) |
| 62 | + |
| 63 | + print('Done, see output in "{}".'.format(out_file)) |
| 64 | + |
| 65 | + |
| 66 | +# -------------------------------------------------- |
| 67 | +def parse_tree(fh): |
| 68 | + """parse input file""" |
| 69 | + |
| 70 | + name_patt = r'(.+)\s*=\s*(.+)' |
| 71 | + married_patt = r'(.+)\s+married\s+(.+)' |
| 72 | + begat_patt = r'(.+)\s+and\s+(.+)\s+begat\s+(.+)' |
| 73 | + |
| 74 | + edges = set() |
| 75 | + nodes = {} |
| 76 | + |
| 77 | + for line in fh: |
| 78 | + name_match = re.match(name_patt, line) |
| 79 | + begat_match = re.match(begat_patt, line) |
| 80 | + married_match = re.match(married_patt, line) |
| 81 | + |
| 82 | + if name_match: |
| 83 | + initials, name = name_match.groups() |
| 84 | + nodes[initials.strip()] = name.strip() |
| 85 | + elif married_match: |
| 86 | + p1, p2 = married_match.groups() |
| 87 | + edges.add((p1.strip(), p2.strip())) |
| 88 | + elif begat_match: |
| 89 | + p1, p2, begat = begat_match.groups() |
| 90 | + children = re.split(r'\s*,\s*', begat) |
| 91 | + for parent in p1, p2: |
| 92 | + for child in children: |
| 93 | + edges.add((parent.strip(), child.strip())) |
| 94 | + |
| 95 | + return nodes, edges |
| 96 | + |
| 97 | + |
| 98 | +# -------------------------------------------------- |
| 99 | +if __name__ == '__main__': |
| 100 | + main() |
0 commit comments