Skip to content

Commit

Permalink
Merge pull request #4 from gtrs/master
Browse files Browse the repository at this point in the history
Some bugfixes and new features.
  • Loading branch information
danielfett committed Jan 30, 2019
2 parents f4304cd + 8a384cc commit 59b5ab6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 22 deletions.
17 changes: 13 additions & 4 deletions annexlang/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def tikz_markers(self):
return ''

def contour(self, text):
c = getattr(self, 'draw_contour', True)
if not c:
return text
if not text:
return ''
return r"\contour{white}{%s}" % text
Expand Down Expand Up @@ -276,18 +279,22 @@ class Protocol(Serial):
yaml_tag = '!Protocol'
extra_steps = []
counter = 0
columns = []

def init(self, options):
self.options = options
# Set line numbers for each step
self.set_line(1 if self.has_groups else 0)

# Set column numbers for parties
col = 0
self.columns = []
for p in self.parties:
if p.column == None:
p.column = col
col += 1
p.column = len(self.columns)
if hasattr(p, 'extrawidth'):
self.columns.append({'num': len(self.columns), 'extrawidth': p.extrawidth})
else:
self.columns.append({'num': len(self.columns)})
for p in self.parties:
if isinstance(p.column, Party):
p.column = p.column.column
Expand All @@ -300,7 +307,9 @@ def init(self, options):
# determine start and end points of lifelines
last_starts = {}
for step in self.walk():
if getattr(step, 'startsparty', False):
if getattr(step, 'dummyparty', False):
continue
elif getattr(step, 'startsparty', False):
if step.party in last_starts:
raise Exception("Started party that was already started: " + repr(step.party))
last_starts[step.party] = step
Expand Down
80 changes: 70 additions & 10 deletions annexlang/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ def height(self):
return "2ex" + ("+2ex" * len(self.lines_below)), "north,yshift=1ex"
else:
return "1ex", "center"



class XHRRequest(HTTPRequest):
yaml_tag = '!xhr-request'
type = "xhr_request"


class HTTPResponse(HTTPRequest):
yaml_tag = '!http-response'
Expand All @@ -58,6 +63,11 @@ def _init(self, *args, **kwargs):
self.text_below = self.parameters


class XHRResponse(HTTPResponse):
yaml_tag = '!xhr-response'
type = "xhr_response"


class Websocket(HTTPRequest):
yaml_tag = '!websocket'
type = "websocket"
Expand All @@ -71,36 +81,56 @@ def _init(self, *args, **kwargs):
class HTTPRequestResponse(HTTPRequest):
yaml_tag = '!http-request-response'
type = "request_response"
type_above = 'http_request'
type_below = 'http_response'

def tikz_arrows(self):
src = self.get_pos(self.src.column, self.line)
dest = self.get_pos(self.dest.column, self.line)
return fr"""%% draw {self.type}
\draw[annex_http_request,transform canvas={{yshift=0.25ex}}{self.tikz_extra_style}] ({src}) to {self.tikz_above} ({dest});
\draw[annex_http_response,transform canvas={{yshift=-0.25ex}}{self.tikz_extra_style}] ({dest}) to {self.tikz_below} ({src});"""


\draw[annex_{self.type_above},transform canvas={{yshift=0.25ex}}{self.tikz_extra_style}] ({src}) to {self.tikz_above} ({dest});
\draw[annex_{self.type_below},transform canvas={{yshift=-0.25ex}}{self.tikz_extra_style}] ({dest}) to {self.tikz_below} ({src});"""


class XHRRequestResponse(HTTPRequestResponse):
yaml_tag = '!xhr-request-response'
type = "xhr_request_response"
type_above = 'xhr_request'
type_below = 'xhr_response'


class HTTPResponseRequest(HTTPRequest):
yaml_tag = '!http-response-request'
type = "response_request"
type_above = 'http_response'
type_below = 'http_request'

def tikz_arrows(self):
src = self.get_pos(self.src.column, self.line)
dest = self.get_pos(self.dest.column, self.line)
return fr"""%% draw {self.type}
\draw[annex_http_response,transform canvas={{yshift=0.25ex}}{self.tikz_extra_style}] ({dest}) to {self.tikz_above} ({src});
\draw[annex_http_request,transform canvas={{yshift=-0.25ex}}{self.tikz_extra_style}] ({src}) to {self.tikz_below} ({dest});"""
\draw[annex_{self.type_above},transform canvas={{yshift=0.25ex}}{self.tikz_extra_style}] ({dest}) to {self.tikz_above} ({src});
\draw[annex_{self.type_below},transform canvas={{yshift=-0.25ex}}{self.tikz_extra_style}] ({src}) to {self.tikz_below} ({dest});"""


class XHRResponseRequest(HTTPResponseRequest):
yaml_tag = '!xhr-request-response'
type = "xhr_request_response"
type_above = 'xhr_request'
type_below = 'xhr_response'


class PostMessage(ProtocolStep):
yaml_tag = '!postmessage'
body = ""
comment = ""
id_above = True
text_style = "annex_postmessage_text"

def _init(self, *args, **kwargs):
super()._init(*args, **kwargs)
self.text_above = self.body
self.text_below = self.comment
self._affecting_nodes = [
self.get_pos(self.src.column, self.line),
self.get_pos(self.dest.column, self.line)
Expand All @@ -115,12 +145,21 @@ def tikz_arrows(self):
src = self.get_pos(self.src.column, self.line)
dest = self.get_pos(self.dest.column, self.line)
return fr"""%% draw postmessage
\draw[annex_postmessage{self.tikz_extra_style}] ({src}) to {self.tikz_above} ({dest});"""
\draw[annex_postmessage{self.tikz_extra_style}] ({src}) to {self.tikz_above} {self.tikz_below} ({dest});"""

# def height(self):
# if self.tikz_above:
# return "4ex", "south,yshift=-1ex"
# else:
# return "1ex", "center"
@property
def height(self):
if self.tikz_above:
if self.tikz_above and self.tikz_below:
return "4ex" + ("+2ex" * len(self.lines_below)), "north,yshift=3ex"
elif self.tikz_above:
return "4ex", "south,yshift=-1ex"
elif self.tikz_below:
return "2ex" + ("+2ex" * len(self.lines_below)), "north,yshift=1ex"
else:
return "1ex", "center"

Expand All @@ -140,7 +179,8 @@ def tikz(self):

@property
def height(self):
return "3ex", "center"
h = 1 + 2 * len(self.label.split("\\\\"))
return f"{h}ex", "center"


class ScriptAction(Action):
Expand Down Expand Up @@ -223,6 +263,26 @@ def tikz_arrows(self):
return out


class DummyParty(ProtocolStep):
yaml_tag = '!dummy-party'
skip_number = True
dummyparty = True

def _init(self, *args, **kwargs):
super()._init(*args, **kwargs)
self.node_name = self.create_affecting_node_name()

# def tikz(self):
# pos = self.get_pos(self.party.column, self.line)
# text = self.party.name
# out = fr"""\node[name={self.node_name},annex_{self.type}_box,{self.party.style}] at ({pos}) {{{text}}};"""
# return out
#
# @property
# def height(self):
# return "5ex", "center"


class OpenWindowStartParty(StartParty):
yaml_tag = '!open-window-start-party'
id_above = True
Expand Down
2 changes: 2 additions & 0 deletions annexlang/styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class StyleDefault(yaml.YAMLObject):
% individual steps
annex_http_request/.style={-Latex,line,draw=purple},
annex_http_response/.style={-Latex[open],line,draw=purple},
annex_xhr_request/.style={-Latex,line,draw=blue},
annex_xhr_response/.style={-Latex[open],line,draw=blue},
annex_websocket/.style={-Latex,draw=red},
annex_postmessage/.style={->,line,dashed,draw=red},
annex_action/.style={fill=white,inner sep=0ex,minimum height=1.5em},
Expand Down
19 changes: 11 additions & 8 deletions annexlang/tikzpicture.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def dump_matrix(self, f):
matrix_dummy_heights = [[] for i in range(lines + line_offset)]

for step in self.protocol.walk():
if hasattr(step, 'height'):
if hasattr(step, 'height_overwrite'):
matrix_dummy_heights[step.line].append(step.height_overwrite)
elif hasattr(step, 'height'):
matrix_dummy_heights[step.line].append(step.height)

f.write(r"""
Expand All @@ -47,16 +49,17 @@ def dump_matrix(self, f):

# Draw the matrix (no real node contents yet)
for line in range(len(matrix_dummy_heights)):
for col in range(len(self.protocol.parties)):
position = self.protocol.get_pos(col, line)
for i in range(len(self.protocol.columns)): # we need to be able to refer to the following column, hence, we use this kind of iteration
col = self.protocol.columns[i]
position = self.protocol.get_pos(i, line)
f.write(r"""\node[annex_matrix_node,inner sep=0,outer sep=0](%s){};""" % (position,))

extrawidths = []
if hasattr(self.protocol.parties[col], 'extrawidth'):
extrawidths.append(self.protocol.parties[col].extrawidth + "/2")
if col < (len(self.protocol.parties) - 1) and hasattr(self.protocol.parties[col+1], 'extrawidth'):
extrawidths.append(self.protocol.parties[col+1].extrawidth + "/2")
if col < len(self.protocol.parties) - 1:
if 'extrawidth' in col:
extrawidths.append(col['extrawidth'] + "/2")
if i < (len(self.protocol.columns) - 1) and hasattr(self.protocol.columns[i+1], 'extrawidth'):
extrawidths.append(self.protocol.columns[i+1]['extrawidth'] + "/2")
if i < (len(self.protocol.columns) - 1):
f.write(r""" &""" )
if extrawidths:
f.write(f"[{'+'.join(extrawidths)}]")
Expand Down

0 comments on commit 59b5ab6

Please sign in to comment.