-
Notifications
You must be signed in to change notification settings - Fork 0
/
treevis.py
49 lines (44 loc) · 1.75 KB
/
treevis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python3
'''
treevis.py
Traslate your tree description file into SVG.
'''
# import sys # sys.argv
import os # os.path.basename
import argparse
from treevis.treevisualizer import TreeVis
##################################################
# Command line interface & file input processing
def main():
# Parse the command line
cmd_parser = argparse.ArgumentParser(description="turns a tree described in a text file into an SVG visible")
cmd_parser.add_argument("tree_description_file", metavar="<tree dedcription file>", \
help="The text file describing the tree. See README.md for syntax of such file")
cmd_parser.add_argument("-o", "--output-file-name", metavar="output_SVG_file_name", \
help="Specify the output file name. If you don\'t give this option, it will be inferred from your input file name")
args = cmd_parser.parse_args() # will exit the program if anything wrong
# Build the tree
basename = os.path.basename(args.tree_description_file)
_name = basename.split(sep=".")
outputname = _name[0]
try:
t = TreeVis(args.tree_description_file)
except FileNotFoundError:
print("No such file \"" + args.tree_description_file + "\"")
return
except IndexError:
print("Error processing, invalid input file syntax?")
return
except:
raise
# Write to file
if args.output_file_name is None:
outputname += ".svg"
else: # len(sys.argv) == 3:
outputname = args.output_file_name
t.writesvg(outputname)
# Prompt the user upon finishing
print("Tree visualization saved as \"" + outputname + "\"")
##################################################
if __name__ == "__main__":
main()