-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
101 lines (88 loc) · 3.05 KB
/
cli.py
File metadata and controls
101 lines (88 loc) · 3.05 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
try:
import cairosvg
cairosvg_installed = True
except ImportError:
cairosvg_installed = False
import argparse
import sys
import logging
from src.builder import Builder
from src.parser import Parser, InvalidInputException
from src.colored_formatter import ColoredFormatter
handler = logging.StreamHandler()
handler.setFormatter(ColoredFormatter())
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def main():
parser = argparse.ArgumentParser(
prog="YAMscgen",
description="Generate flexible and customizable sequence diagrams using and extending the syntax of Mscgen: https://www.mcternan.me.uk/mscgen/.",
epilog="Written by Julien Van Roy under supervision of Prof. Bruno Quoitin (UMons). Code available at https://github.com/JulienVR/YAMscgen.",
)
parser.add_argument(
"-i",
"--input",
help="The input file to read from. If omitted, reads from the standard input.",
required=False,
)
parser.add_argument(
"-c",
"--css",
help="The css file used to style the output file.",
required=False,
)
parser.add_argument(
"-o",
"--output",
help="The output file to write to.",
required=True,
)
parser.add_argument(
"-t",
"--type",
help="" if cairosvg_installed else "YAMscgen can only generate SVG files without CairoSVG. Install CairoSVG for PNG and PDF output.",
choices=["svg", "png", "pdf"],
default="svg",
)
args = parser.parse_args()
if args.type in ('pdf', 'png') and not cairosvg_installed:
sys.exit("Unable to generate a PNG/PDF because cairosvg is not installed.")
if not args.input:
# read from stdin (until EOF: CTRL + D)
text_input = "".join(line for line in sys.stdin)
else:
# read from a file
with open(args.input) as f:
text_input = f.read()
try:
parser = Parser(text_input)
except InvalidInputException as e:
logger.error(e)
sys.exit()
css_content = False
if args.css:
with open(args.css) as f:
css_content = f.read()
try:
svgs = Builder(parser=parser, css_content=css_content).generate()
except InvalidInputException as e:
logger.error(e)
sys.exit()
filenames = []
for i, svg in enumerate(svgs):
if len(svgs) > 1:
filename_list = args.output.split('.')
filename = ''.join(filename_list[:-1]) + f"-{i+1}." + filename_list[-1]
filenames.append(filename)
else:
filename = args.output
if args.type == 'png':
cairosvg.svg2png(svg, write_to=filename)
elif args.type == 'pdf':
cairosvg.svg2pdf(svg, write_to=filename)
else:
with open(filename, "wb+") as f:
f.write(svg)
if len(svgs) > 1:
logger.info(f"The height of the diagram was larger than the 'max-height' set, hence it was divided into {len(svgs)} parts: {', '.join(filenames)}")