Skip to content

Commit

Permalink
Feature: rstrip argument for render and renders functions as well as …
Browse files Browse the repository at this point in the history
…the --no-rstrip flag for the command line
  • Loading branch information
pylover committed Aug 6, 2021
1 parent aaa95e2 commit d6ff86a
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 72 deletions.
8 changes: 4 additions & 4 deletions adia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
__version__ = '0.1.1'


def renders(source):
def renders(source, rstrip=True):
"""High level API to generate ASCII diagram.
Equivalent to:
Expand All @@ -43,10 +43,10 @@ def renders(source):
:return: ASCII diagram.
:rtype: str
"""
return Diagram(source).renders()
return Diagram(source).renders(rstrip)


def render(source, out):
def render(source, out, rstrip=True):
"""High level API to write ASCII diagram into file.
Equivalent to:
Expand All @@ -72,4 +72,4 @@ def render(source, out):
:param source: The ADia source code.
:type source: str or file-like
"""
Diagram(source).render(out)
Diagram(source).render(out, rstrip)
2 changes: 1 addition & 1 deletion adia/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def extendtop(self, addrows):
self.rows += addrows

def __str__(self):
return '\n'.join(str(l) for l in self._backend)
return '\n'.join(str(l) for l in self._backend) + '\n'

def set_char(self, col, row, char):
if col >= self.cols:
Expand Down
8 changes: 4 additions & 4 deletions adia/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def parse(self, string):
with StringIO(string) as f:
self.parsefile(f)

def render(self, filelike):
Renderer(self).dump(filelike)
def render(self, filelike, rstrip=True):
Renderer(self).dump(filelike, rstrip)

def renders(self):
return Renderer(self).dumps()
def renders(self, rstrip=True):
return Renderer(self).dumps(rstrip)

def _set_title(self, attr, value):
self.title = value.strip()
Expand Down
30 changes: 16 additions & 14 deletions adia/renderer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import itertools

from .sequence import SequenceDiagram, Call, Condition, Loop, Note
Expand All @@ -23,8 +24,6 @@ def twiniter(l):


class Renderer:
dirty = True

def __init__(self, diagram, canvas=None):
self._repeats = set()
self.diagram = diagram
Expand Down Expand Up @@ -71,21 +70,24 @@ def render(self):
if isinstance(unit, SequenceDiagram):
SequenceRenderer(unit, self.canvas).render()

# TODO: Remove
self.dirty = False
def _dumplines(self, rstrip):
for l in self.canvas:
line = str(l)
if rstrip:
line = line.rstrip()

yield f'{line}\n'

def dump(self, filelike):
if self.dirty:
self.render()
def dump(self, filelike, rstrip=True):
self.render()

for l in self.canvas:
filelike.write(str(l))
filelike.write('\n')
for line in self._dumplines(rstrip):
filelike.write(line)

def dumps(self):
if self.dirty:
self.render()
return str(self.canvas)
def dumps(self, rstrip=True):
out = io.StringIO()
self.dump(out, rstrip)
return out.getvalue()


class SequenceRenderer(Renderer):
Expand Down
7 changes: 6 additions & 1 deletion adiacli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ADia(Root):
__help__ = 'ASCII diagram language interpreter'
__arguments__ = [
Argument('-V', '--version', action='store_true'),
Argument('--no-rstrip', action='store_true'),
Argument(
'-C', '--change-directory',
default='.',
Expand All @@ -36,7 +37,11 @@ def __call__(self, args):
outfile = sys.stdout

def render(infile):
adia.render(infile, outfile)
adia.render(
infile,
outfile,
rstrip=False if args.no_rstrip is True else False
)

if args.change_directory != '.':
os.chdir(args.change_directory)
Expand Down
8 changes: 6 additions & 2 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import contextlib

from adia.diagram import Diagram


def eqbigstr(a, b, offset=8):
if hasattr(a, 'dumps'):
Expand Down Expand Up @@ -57,18 +59,20 @@ def print_columns(size):
print(f' {first}')
print(f' {second}')

if isinstance(a, Diagram):
a = a.renders(rstrip=False)

bb = []
for l in b.strip().splitlines():
bb.append(l[offset+2:-2])

bb.pop(0)
bb.pop()
# bb[-1] += '\n'
bb[-1] += '\n'
b = '\n'.join(bb)
maxlen = maxwidth(a, b)
maxanlen = maxlen + 4

# a += '\n'
if a != b:
print('\nGiven:')
print_columns(maxlen)
Expand Down
61 changes: 55 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_render_string():
foo -> bar
''')

assert eqdia(d.renders(), '''
assert eqdia(d, '''
...................
. DIAGRAM: My dia .
. .
Expand Down Expand Up @@ -41,7 +41,7 @@ def test_render_file():
''')

f = StringIO()
d.render(f)
d.render(f, rstrip=False)
assert eqdia(f.getvalue(), '''
...................
. DIAGRAM: My dia .
Expand All @@ -57,7 +57,6 @@ def test_render_file():
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
..
...................
''')

Expand All @@ -68,7 +67,7 @@ def test_renders_function():
sequence:
foo -> bar
''')
''', rstrip=False)

assert eqdia(out, '''
...................
Expand All @@ -88,6 +87,31 @@ def test_renders_function():
...................
''')

out = renders('''
diagram: My dia
sequence:
foo -> bar
''', rstrip=True)

assert eqdia(out, '''
...................
. DIAGRAM: My dia .
. .
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
. | | .
. |~~~~~~>| .
. | | .
. |<------| .
. | | .
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
...................
''')


def test_render_function():
out = StringIO()
Expand All @@ -96,7 +120,7 @@ def test_render_function():
sequence:
foo -> bar
''', out)
''', out, rstrip=False)

assert eqdia(out.getvalue(), '''
...................
Expand All @@ -113,6 +137,31 @@ def test_render_function():
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
..
...................
''')

out = StringIO()
render('''
diagram: My dia
sequence:
foo -> bar
''', out, rstrip=True)

assert eqdia(out.getvalue(), '''
...................
. DIAGRAM: My dia .
. .
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
. | | .
. |~~~~~~>| .
. | | .
. |<------| .
. | | .
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
...................
''')
7 changes: 1 addition & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_standardinput(app):
foo -> bar: Hello World!
'''

with app(stdin=source):
with app('--no-rstrip', stdin=source):
assert eqdia(stdout, '''
...............................
. DIAGRAM: Foo .
Expand All @@ -56,7 +56,6 @@ def test_standardinput(app):
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
..
...............................
''', offset=8)

Expand Down Expand Up @@ -88,7 +87,6 @@ def test_inputfile(app, tempstruct):
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
..
...............................
''', offset=8)

Expand Down Expand Up @@ -141,7 +139,6 @@ def test_multiple_inputfiles(app, tempstruct):
. +-----+ +-----+ .
. | baz | | bar | .
. +-----+ +-----+ .
..
...............................
''', offset=8)

Expand Down Expand Up @@ -187,7 +184,6 @@ def test_multiple_inputfiles_error(app, tempstruct):
. | foo | | bar | .
. +-----+ +-----+ .
..
..
...............................
''', offset=8)

Expand Down Expand Up @@ -219,6 +215,5 @@ def test_changedirectory(app, tempstruct):
. +-----+ +-----+ .
. | foo | | bar | .
. +-----+ +-----+ .
..
...............................
''', offset=8)
2 changes: 1 addition & 1 deletion tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_diagram_break_issue9():
sequence: Foo#2
foo -> bar
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
...................
. DIAGRAM: Foo .
. version: 1.0 .
Expand Down
10 changes: 5 additions & 5 deletions tests/test_diagram_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ def test_diagram_header():
diagram: Foo
version: 1.0
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
................
. DIAGRAM: Foo .
. version: 1.0 .
................
''')

d = Diagram()
assert eqdia(d.renders(), '''
assert eqdia(d, '''
.............................
. DIAGRAM: Untitled Diagram .
.............................
Expand All @@ -25,7 +25,7 @@ def test_diagram_header():
d = Diagram('''
diagram: Foo
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
................
. DIAGRAM: Foo .
................
Expand All @@ -35,7 +35,7 @@ def test_diagram_header():
diagram: Foo
author: alice
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
.................
. DIAGRAM: Foo .
. author: alice .
Expand All @@ -47,7 +47,7 @@ def test_diagram_header():
author: alice
version: 1.0
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
.................
. DIAGRAM: Foo .
. author: alice .
Expand Down
4 changes: 2 additions & 2 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_readme_quickstart():
foo -> bar: Hello World!
''')
assert eqdia(d.renders(), '''
assert eqdia(d, '''
...............................
. DIAGRAM: Foo .
. .
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_readme_complte_examaple():
bob -> db: delete(token)
''')

assert eqdia(d.renders(), '''
assert eqdia(d, '''
...............................................................................
. DIAGRAM: Authentication .
. author: pylover .
Expand Down
Loading

0 comments on commit d6ff86a

Please sign in to comment.