-
Notifications
You must be signed in to change notification settings - Fork 205
/
generate.py
executable file
·75 lines (58 loc) · 1.92 KB
/
generate.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
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
#!/usr/bin/python
# coding=utf-8
# vim ts=4
import urllib
import re
import os.path
from os import walk
def getSequenceDiagram(wsdfile, outputFile, style='default'):
request = {}
assert os.path.isfile(wsdfile)
with open(wsdfile, "r") as f:
message = f.read()
request["message"] = message
request["style"] = style
request["apiVersion"] = "1"
url = urllib.urlencode(request)
line = None
try:
f = urllib.urlopen("http://www.websequencediagrams.com/", url)
line = f.readline()
f.close()
except IOError as e:
print(e)
return False
expr = re.compile("(\?(img|pdf|png|svg)=[a-zA-Z0-9]+)")
m = expr.search(line)
if m == None:
print "Invalid response from server."
return False
urllib.urlretrieve("http://www.websequencediagrams.com/" + m.group(0),
outputFile)
return True
def getSources(root="./src"):
sources = []
for (root, _, files) in walk("src"):
for f in files:
if f.endswith(".wsd"):
sources.append(os.path.join(root, f))
return sources
def generate(src="src", target="output", format="png", style="modern-blue"):
for f in getSources(src):
src_path = os.path.dirname(f)
src_file = os.path.basename(f)
target_path = target + src_path[src_path.index('/'):]
output_file = src_file[:src_file.rindex('.') + 1] + format
if not os.path.isdir(target_path):
os.makedirs(target_path)
output = target_path + "/" + output_file
print("generating %s ..." % output)
if getSequenceDiagram(f, output, style):
print("Succeed to generate %s!" % output)
else:
print("Failed to generate %s!" % output)
def main():
generate("src", "output", "png",
"modern-blue") # png only, svg & pdf must register and pay 15$.
if __name__ == "__main__":
main()