def test_render_cache(tmpdir, monkeypatch, sources): cache_dir = str(tmpdir.mkdir('cache')) print('Cache directory at: {}'.format(cache_dir)) assert not listdir(cache_dir) for src in sources: print('Rendering without cache {} ...'.format(src)) outfile = render_file(src, cacheopts={ 'use_cache': True, 'cache_dir': cache_dir }) # Make sure output is at cwd outfile = join(getcwd(), outfile) assert isfile(outfile) print('Output file at {}'.format(outfile)) assert listdir(cache_dir) # Re-render using cache from plantweb import render as rendermod def plantuml(server, format, content): raise Exception('You shouldn\'t have got here.') monkeypatch.setattr(rendermod, 'plantuml', plantuml) for src in sources: print('Rendering {} ...'.format(src)) # Render content directly with open(src, 'rb') as fd: output, format, engine, sha = render(fd.read().decode('utf-8'), cacheopts={ 'use_cache': True, 'cache_dir': cache_dir }) # Assert cache exists assert isfile(join(cache_dir, '{}.{}'.format(sha, format))) # Test server recall if not use_cache for src in sources: with open(src, 'rb') as fd: with raises(Exception) as e: render(fd.read().decode('utf-8'), cacheopts={'use_cache': False}) assert str(e.value) == 'You shouldn\'t have got here.'
def generate_uml(self): content = self.manager_.export() if content == self.current_diagram_ or content == '@startuml\n\n\n@enduml': return False self.current_diagram_ = content try: output = render(content, engine='plantuml', format='svg', cacheopts={'use_cache': False})[0] except RequestException: timestamp = datetime.now().strftime( '%Y%m%d%H%M%S') # TODO(KNR): replace by mktemp temp_file_name = path.join(self.temp_dir_, timestamp) + '.svg' cmd = "echo '{}' | plantuml -pipe > {} -tsvg".format( content, temp_file_name) subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate() output = open(temp_file_name, 'rb').read() subprocess.call( ['rm', temp_file_name] ) # TODO(KNR): replace by pythoning file remove (if still necessary for mktemp) return output
def test_render_forced_and_defaults(monkeypatch): # Monkey path providers to use just known defaults monkeypatch.setattr( defaults, 'DEFAULTS_PROVIDERS', [defaults.DEFAULTS_PROVIDERS[0]] ) # Force reload of defaults defaults.read_defaults(cached=False) # Test forced render src = """\ digraph G { a1 -> a2; a2 -> a3; } """ output, frmt, engine, sha = render(src, engine='graphviz') assert b'<title>a1</title>' in output assert b'<title>a2</title>' in output assert b'<title>a3</title>' in output assert b'<title>a4</title>' not in output assert b'Syntax Error' not in output # Test default engine src = """\ Bob -> Alice : hello """ output, frmt, engine, sha = render(src) assert b'Bob</text>' in output assert b'Alice</text>' in output assert b'hello</text>' in output # Test mismatched forced engine src = """\ @startuml Bob -> Alice : hello @enduml """ output, frmt, engine, sha = render(src, engine='ditaa') assert b'Bob</text>' in output assert b'Alice</text>' in output assert b'hello</text>' in output
def test_render_forced_and_defaults(monkeypatch): # Monkey path providers to use just known defaults monkeypatch.setattr(defaults, 'DEFAULTS_PROVIDERS', [defaults.DEFAULTS_PROVIDERS[0]]) # Force reload of defaults defaults.read_defaults(cached=False) # Test forced render src = """\ digraph G { a1 -> a2; a2 -> a3; } """ output, frmt, engine, sha = render(src, engine='graphviz') assert b'<title>a1</title>' in output assert b'<title>a2</title>' in output assert b'<title>a3</title>' in output assert b'<title>a4</title>' not in output assert b'Syntax Error' not in output # Test default engine src = """\ Bob -> Alice : hello """ output, frmt, engine, sha = render(src) assert b'Bob</text>' in output assert b'Alice</text>' in output assert b'hello</text>' in output # Test mismatched forced engine src = """\ @startuml Bob -> Alice : hello @enduml """ output, frmt, engine, sha = render(src, engine='ditaa') assert b'Bob</text>' in output assert b'Alice</text>' in output assert b'hello</text>' in output
def render_plantuml(match): plantuml_text = PLANTUML_STYLE.read_text() + match.group("code") (output, format, engine, sha) = render(plantuml_text, engine="plantuml", format="svg", server=SERVER_URL) fname = sha + ".svg" (IMG_DIR / fname).write_bytes(output) return f'<img src="{IMG_PATH + fname}" class="svg">'
def test_render(tmpdir, sources): cache_dir = str(tmpdir.mkdir('cache')) print('Cache directory at: {}'.format(cache_dir)) for src in sources: print('Rendering {} ...'.format(src)) # Render content directly with open(src, 'rb') as fd: output, format, engine, sha = render(fd.read().decode('utf-8'), cacheopts={ 'use_cache': True, 'cache_dir': cache_dir }) # Assert cache exists assert isfile(join(cache_dir, '{}.{}'.format(sha, format)))
def test_render(tmpdir, sources): cache_dir = str(tmpdir.mkdir('cache')) print('Cache directory at: {}'.format(cache_dir)) for src in sources: print('Rendering {} ...'.format(src)) # Render content directly with open(src, 'rb') as fd: output, format, engine, sha = render( fd.read().decode('utf-8'), cacheopts={ 'use_cache': True, 'cache_dir': cache_dir } ) # Assert cache exists assert isfile(join(cache_dir, '{}.{}'.format(sha, format)))
from __future__ import unicode_literals, absolute_import from __future__ import print_function, division from plantweb.render import render CONTENT = """ actor Foo1 boundary Foo2 control Foo3 entity Foo4 database Foo5 Foo1 -> Foo2 : To boundary Foo1 -> Foo3 : To control Foo1 -> Foo4 : To entity Foo1 -> Foo5 : To database """ if __name__ == '__main__': print('==> INPUT:') print(CONTENT) output = render(CONTENT, engine='plantuml', format='svg', cacheopts={'use_cache': False}) print('==> OUTPUT:') print(output)
def test_render_cache(tmpdir, monkeypatch, sources): cache_dir = str(tmpdir.mkdir('cache')) print('Cache directory at: {}'.format(cache_dir)) assert not listdir(cache_dir) for src in sources: print('Rendering without cache {} ...'.format(src)) outfile = render_file( src, cacheopts={ 'use_cache': True, 'cache_dir': cache_dir } ) # Make sure output is at cwd outfile = join(getcwd(), outfile) assert isfile(outfile) print('Output file at {}'.format(outfile)) assert listdir(cache_dir) # Re-render using cache from plantweb import render as rendermod def plantuml(server, format, content): raise Exception('You shouldn\'t have got here.') monkeypatch.setattr(rendermod, 'plantuml', plantuml) for src in sources: print('Rendering {} ...'.format(src)) # Render content directly with open(src, 'rb') as fd: output, format, engine, sha = render( fd.read().decode('utf-8'), cacheopts={ 'use_cache': True, 'cache_dir': cache_dir } ) # Assert cache exists assert isfile(join(cache_dir, '{}.{}'.format(sha, format))) # Test server recall if not use_cache for src in sources: with open(src, 'rb') as fd: with raises(Exception) as e: render( fd.read().decode('utf-8'), cacheopts={ 'use_cache': False } ) assert str(e.value) == 'You shouldn\'t have got here.'
transitions = state_node.transitions for t in transitions: t_string = f"{t.source.id} --> {t.target[0].id} : {t.event}\n" result += t_string children = state_node.states.values() for c in children: child_string = state_node_to_viz(c) result += child_string if not state_node.parent: result += "@enduml\n" return result if __name__ == '__main__': output = render( state_node_to_viz(simple_machine.root), engine='plantuml', format='svg', cacheopts={ 'use_cache': False } )[0] file1 = open('test.svg', 'w') file1.write(output.decode("utf-8")) file1.close()
CONTENT = """ actor Foo1 boundary Foo2 control Foo3 entity Foo4 database Foo5 Foo1 -> Foo2 : To boundary Foo1 -> Foo3 : To control Foo1 -> Foo4 : To entity Foo1 -> Foo5 : To database """ if __name__ == '__main__': print('==> INPUT:') print(CONTENT) output = render( CONTENT, engine='plantuml', format='svg', cacheopts={ 'use_cache': False } ) print('==> OUTPUT:') print(output)