async def render(request): post_data = await request.post() try: processor = PROCESSORS[post_data['processor']] except KeyError: return web.Response(text='Unsupported processor: %s' % post_data['processor'], status=501) try: style_facts = fact_parser.parse(relviz.load_default_style()) except Exception as e: return web.Response(text=str(e), status=500) try: facts = fact_parser.parse(post_data['facts'] + '\n') style_facts.extend(facts) g = relviz.to_graphviz(facts, style_facts) dot = await asyncio.create_subprocess_exec( processor, '-Tsvg', stdin=PIPE, stdout=PIPE, stderr=PIPE) output, errors = await dot.communicate(g.to_string().encode('utf-8')) if dot.returncode == 0: return web.Response(text=output.decode('utf-8'), content_type='image/svg+xml') return web.Response(text=errors.decode('utf-8'), status=500) except (fact_parser.ParseException, relviz.GraphError) as e: return web.Response(text=str(e), status=400) except Exception as e: return web.Response(text=str(e), status=500)
def main(): args = parse_args() logging.basicConfig(stream=sys.stderr, format='%(message)s') logging.getLogger(__name__).setLevel(logging.DEBUG if args.verbose else logging.INFO) if args.default_style: style_data = load_default_style() style_facts = fact_parser.parse(style_data) if args.style: style_data = args.style.read() + '\n' style_facts.extend(fact_parser.parse(style_data)) fact_data = args.facts.read() + '\n' facts = fact_parser.parse(fact_data) g = to_graphviz(facts, style_facts) g.write(args.output)