def test_app_cleans_up_plugins(self): testdir = os.path.dirname(__file__) diagpath = os.path.join(testdir, 'diagrams', 'plugin_autoclass.diag') loaded_plugins = [] def cleanup(): from blockdiag import plugins loaded_plugins.extend(plugins.loaded_plugins) try: tmpdir = TemporaryDirectory() fd, tmpfile = tmpdir.mkstemp() os.close(fd) args = ['-T', 'SVG', '-o', tmpfile, diagpath] app = BlockdiagApp() app.register_cleanup_handler(cleanup) # to get internal state app.run(args) from blockdiag import plugins self.assertTrue(loaded_plugins) # check plugins were loaded self.assertFalse(plugins.loaded_plugins) # and unloaded finally finally: tmpdir.clean()
def test_app_cleans_up_images(self): testdir = os.path.dirname(__file__) diagpath = os.path.join(testdir, 'diagrams', 'background_url_image.diag') urlopen_cache = {} def cleanup(): from blockdiag.utils import images urlopen_cache.update(images.urlopen_cache) try: tmpdir = TemporaryDirectory() fd, tmpfile = tmpdir.mkstemp() os.close(fd) args = ['-T', 'SVG', '-o', tmpfile, diagpath] app = BlockdiagApp() app.register_cleanup_handler(cleanup) # to get internal state app.run(args) self.assertTrue(urlopen_cache) # check images were cached for path in urlopen_cache.values(): self.assertFalse(os.path.exists(path)) # and removed finally finally: tmpdir.clean()
def _generate(self, name, diagram_type=None, options=None): if options is None: options = MultiDict() with open('test/fixtures/' + name + '_source.txt', 'r') as file: source = file.read() with open('test/fixtures/' + name + '_expected.svg', 'r') as file: lines = file.readlines() expected = '\n'.join([item.strip() for item in lines]) if diagram_type is None: diagram_type = name if diagram_type == 'nwdiag': app, name = NwdiagApp(), 'network' elif diagram_type == 'blockdiag': app, name = BlockdiagApp(), 'block' elif diagram_type == 'seqdiag': app, name = SeqdiagApp(), 'sequence' elif diagram_type == 'actdiag': app, name = ActdiagApp(), 'activity' elif diagram_type == 'packetdiag': app, name = PacketdiagApp(), 'packet' elif diagram_type == 'rackdiag': app, name = RackdiagApp(), 'rack' else: raise InvalidUsage('Unknown diagram type: ' + diagram_type) result = generate_diag(app, name, 'svg', source, options) actual = '\n'.join([item.strip() for item in result.split('\n')]) return actual, expected
def main(): tree = parse(open(sys.argv[1]).read()) tmpfile = NamedTemporaryFile(dir='.', delete=False) tmpfile.write("{\n") tmpfile.write(" orientation = portrait\n") tmpfile.write(" edge_layout = flowchart\n") tmpfile.write(" node_width = 256\n") write_stmt(tmpfile, tree.body) tmpfile.write("}\n") tmpfile.close() args = [tmpfile.name, '--no-transparency'] BlockdiagApp().run(args)
def finalize_blockdiag(self): #TODO: Make this into an object pattern img_dir = "{0}{1}img".format(self.blockdiag_dir, os.sep) self.blockdiag_out += "\n}" try: if not os.path.exists(img_dir): os.makedirs(img_dir) from blockdiag.command import BlockdiagApp with open("{0}{1}{2}.diag".format(self.blockdiag_dir, os.sep, self.name),'w') as f: f.write(self.blockdiag_out) BlockdiagApp().run(["{0}{1}{2}.diag".format(self.blockdiag_dir, os.sep, self.name), "-Tsvg", "-o", "{0}{1}img{1}{2}.svg".format(self.blockdiag_dir, os.sep, self.name)]) except Exception as err: print("Unable to write blockdiag: {err}".format(err=traceback.format_exc()))
def blockdiag(output_format, source=None): return _generate_diagram(BlockdiagApp(), 'block', output_format, source or request.get_data(as_text=True))