Created
March 15, 2012 07:04
-
-
Save killwing/2042662 to your computer and use it in GitHub Desktop.
[seqgen] generate ascii sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Alice|Bob|Carol | |
1|1->2|INVITE(hello) | |
2|2->3|INVITE | |
-|3|waiting... | |
3|2<-3|200OK | |
4|1<-2|200OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from sys import stdout | |
class Config: | |
cfgFile = 'seqgen.cfg' | |
nodes = [] | |
seqs = [] | |
intervals = [] | |
def __init__(self): | |
f = open(self.cfgFile, 'r') | |
first = True | |
for line in f: | |
if first: | |
self.nodes = line.rstrip('\n').split('|') | |
first = False | |
else: | |
line = line.strip(' \n') | |
if len(line) != 0: | |
self.seqs.append(line.split('|')) | |
f.close() | |
self.computeInterval() | |
self.tuneNodes() | |
def computeInterval(self): | |
for i in range(len(self.nodes)+1): | |
maxlen = 15 # at least 15 | |
for seq in self.seqs: | |
if seq[1] == str(i) + '->' + str(i+1) or seq[1] == str(i) + '<-' + str(i+1): | |
if len(seq[2]) > maxlen: | |
maxlen = len(seq[2]) | |
self.intervals.append(maxlen+5) | |
def countIntervals(self, i, j): | |
if (j <= i): | |
return 0 | |
sum = -1 | |
for n in range(i, j): | |
sum += self.intervals[n] + 1 | |
return sum | |
def tuneNodes(self): | |
for i in range(len(self.nodes)): | |
self.nodes[i] = ' '+self.nodes[i]+' ' | |
if len(self.nodes[i]) % 2 == 0: | |
self.nodes[i] += ' ' | |
class Drawer: | |
char = '-' | |
def __init__(self, config): | |
self.cfg = config | |
def drawSeqs(self): | |
for seq in self.cfg.seqs: | |
if seq[0].isdigit(): | |
# draw msg | |
bar = '|' | |
i = 0 | |
while i <= len(self.cfg.nodes): | |
if i == len(self.cfg.nodes): | |
bar = '' | |
found = False | |
k = 0 | |
for j in range(i+1, len(self.cfg.nodes)+2): | |
msg = ' ' + seq[0] + '.' + seq[2] | |
pad = ' ' * (self.cfg.countIntervals(i, j) - len(msg)) | |
if seq[1] == str(i) + '->' + str(j): | |
found = True | |
stdout.write(pad + msg + bar) | |
elif seq[1] == str(i) + '<-' + str(j): | |
found = True | |
stdout.write(msg + pad + bar) | |
if found: | |
k = j - i - 1 | |
break | |
if not found: | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
i += 1 + k | |
stdout.write('\n') | |
#draw lines | |
bar = '|' | |
i = 0 | |
while i <= len(self.cfg.nodes): | |
if i == len(self.cfg.nodes): | |
bar = '' | |
found = False | |
k = 0 | |
for j in range(i+1, len(self.cfg.nodes)+2): | |
pad = self.char * (self.cfg.countIntervals(i, j) - 1) | |
if seq[1] == str(i) + '->' + str(j): | |
found = True | |
stdout.write(pad + '>' + bar) | |
elif seq[1] == str(i) + '<-' + str(j): | |
found = True | |
stdout.write('<' + pad + bar) | |
if found: | |
k = j - i - 1 | |
break | |
if not found: | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
i += 1 + k | |
stdout.write('\n') | |
self.drawBlankLine(); | |
else: | |
bar = '|' | |
upcorner = '.' | |
for i in range(len(self.cfg.nodes)): | |
if seq[1] == str(i+1): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2]))/2 - 1) + upcorner + '-'*len(seq[2]) + upcorner) | |
elif seq[1] == str(i): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2])-1)/2 - 1) + bar) | |
else: | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
stdout.write('\n') | |
for i in range(len(self.cfg.nodes)): | |
if seq[1] == str(i+1): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2]))/2 - 1) + bar + seq[2] + bar) | |
elif seq[1] == str(i): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2])-1)/2 - 1) + bar) | |
else: | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
stdout.write('\n') | |
btcorner = '\'' | |
for i in range(len(self.cfg.nodes)): | |
if seq[1] == str(i+1): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2]))/2 - 1) + btcorner + '-'*len(seq[2]) + btcorner) | |
elif seq[1] == str(i): | |
stdout.write(' ' * (self.cfg.intervals[i] - (len(seq[2])-1)/2 - 1) + bar) | |
else: | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
stdout.write('\n') | |
self.drawBlankLine(); | |
def drawBlankLine(self): | |
'''draw a blank line''' | |
bar = '|' | |
for i in range(len(self.cfg.nodes)+1): | |
if i == len(self.cfg.nodes): | |
bar = '' | |
stdout.write(' ' * self.cfg.intervals[i] + bar) | |
stdout.write('\n') | |
def drawNodes(self): | |
corner = '+' | |
for i in range(len(self.cfg.nodes)): | |
l = 0 | |
if i > 0: | |
l = len(self.cfg.nodes[i-1]) | |
r = len(self.cfg.nodes[i]) | |
stdout.write(' ' * (self.cfg.intervals[i] - (l+r)/2 - 1) + corner + '-' * len(self.cfg.nodes[i]) + corner) | |
stdout.write('\n') | |
bar = '|' | |
for i in range(len(self.cfg.nodes)): | |
l = 0 | |
if i > 0: | |
l = len(self.cfg.nodes[i-1]) | |
r = len(self.cfg.nodes[i]) | |
stdout.write(' ' * (self.cfg.intervals[i] - (l+r)/2 - 1) + bar + self.cfg.nodes[i] + bar) | |
stdout.write('\n') | |
for i in range(len(self.cfg.nodes)): | |
l = 0 | |
if i > 0: | |
l = len(self.cfg.nodes[i-1]) | |
r = len(self.cfg.nodes[i]) | |
stdout.write(' ' * (self.cfg.intervals[i] - (l+r)/2 - 1) + corner + '-' * len(self.cfg.nodes[i]) + corner) | |
stdout.write('\n') | |
self.drawBlankLine(); | |
config = Config() | |
drawer = Drawer(config) | |
drawer.drawNodes() | |
drawer.drawSeqs() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment