WIP GRAPHVIZ

This commit is contained in:
Romain Dorgueil
2017-05-25 16:41:01 +02:00
parent 171fa3415b
commit be844c3ed7
10 changed files with 165 additions and 14 deletions

View File

@ -1,7 +1,7 @@
# This file has been auto-generated. # This file has been auto-generated.
# All changes will be lost, see Projectfile. # All changes will be lost, see Projectfile.
# #
# Updated at 2017-05-03 18:02:59.359160 # Updated at 2017-05-08 11:34:30.472553
PACKAGE ?= bonobo PACKAGE ?= bonobo
PYTHON ?= $(shell which python) PYTHON ?= $(shell which python)

View File

@ -60,6 +60,7 @@ entry_points = {
], ],
'bonobo.commands': [ 'bonobo.commands': [
'init = bonobo.commands.init:register', 'init = bonobo.commands.init:register',
'graph = bonobo.commands.graph:register',
'run = bonobo.commands.run:register', 'run = bonobo.commands.run:register',
'version = bonobo.commands.version:register', 'version = bonobo.commands.version:register',
], ],

112
bin/imgcat Executable file
View File

@ -0,0 +1,112 @@
#!/bin/bash
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
# only accepts ESC backslash for ST.
function print_osc() {
if [[ $TERM == screen* ]] ; then
printf "\033Ptmux;\033\033]"
else
printf "\033]"
fi
}
# More of the tmux workaround described above.
function print_st() {
if [[ $TERM == screen* ]] ; then
printf "\a\033\\"
else
printf "\a"
fi
}
# print_image filename inline base64contents print_filename
# filename: Filename to convey to client
# inline: 0 or 1
# base64contents: Base64-encoded contents
# print_filename: If non-empty, print the filename
# before outputting the image
function print_image() {
print_osc
printf '1337;File='
if [[ -n "$1" ]]; then
printf 'name='`printf "%s" "$1" | base64`";"
fi
VERSION=$(base64 --version 2>&1)
if [[ "$VERSION" =~ fourmilab ]]; then
BASE64ARG=-d
elif [[ "$VERSION" =~ GNU ]]; then
BASE64ARG=-di
else
BASE64ARG=-D
fi
printf "%s" "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}'
printf ";inline=$2"
printf ":"
printf "%s" "$3"
print_st
printf '\n'
if [[ -n "$4" ]]; then
echo $1
fi
}
function error() {
echo "ERROR: $*" 1>&2
}
function show_help() {
echo "Usage: imgcat [-p] filename ..." 1>& 2
echo " or: cat filename | imgcat" 1>& 2
}
## Main
if [ -t 0 ]; then
has_stdin=f
else
has_stdin=t
fi
# Show help if no arguments and no stdin.
if [ $has_stdin = f -a $# -eq 0 ]; then
show_help
exit
fi
# Look for command line flags.
while [ $# -gt 0 ]; do
case "$1" in
-h|--h|--help)
show_help
exit
;;
-p|--p|--print)
print_filename=1
;;
-*)
error "Unknown option flag: $1"
show_help
exit 1
;;
*)
if [ -r "$1" ] ; then
has_stdin=f
print_image "$1" 1 "$(base64 < "$1")" "$print_filename"
else
error "imgcat: $1: No such file or directory"
exit 2
fi
;;
esac
shift
done
# Read and print stdin
if [ $has_stdin = t ]; then
print_image "" 1 "$(cat | base64)" ""
fi
exit 0

1
bin/test_graph Normal file
View File

@ -0,0 +1 @@
bonobo graph bonobo/examples/tutorials/tut02_03_writeasmap.py | dot -otest.png -Tpng && bin/imgcat test.png

22
bonobo/commands/graph.py Normal file
View File

@ -0,0 +1,22 @@
import json
from bonobo.util.objects import get_name
from bonobo.commands.run import read_file
from bonobo.constants import BEGIN
def execute(file):
graph, plugins, services = read_file(file)
print('digraph {')
print(' rankdir = LR;')
print(' "BEGIN" [shape="point"];')
for i in graph.outputs_of(BEGIN):
print(' "BEGIN" -> ' + json.dumps(get_name(graph.nodes[i])) + ';')
print('}')
def register(parser):
import argparse
parser.add_argument('file', type=argparse.FileType())
return execute

View File

@ -1,11 +1,7 @@
import argparse
import os import os
import bonobo import bonobo
from bonobo.constants import DEFAULT_SERVICES_ATTR, DEFAULT_SERVICES_FILENAME
DEFAULT_SERVICES_FILENAME = '_services.py'
DEFAULT_SERVICES_ATTR = 'get_services'
def get_default_services(filename, services=None): def get_default_services(filename, services=None):
@ -29,7 +25,7 @@ def get_default_services(filename, services=None):
return services or {} return services or {}
def execute(file, quiet=False): def read_file(file):
with file: with file:
code = compile(file.read(), file.name, 'exec') code = compile(file.read(), file.name, 'exec')
@ -56,19 +52,24 @@ def execute(file, quiet=False):
).format(len(graphs)) ).format(len(graphs))
graph = list(graphs.values())[0] graph = list(graphs.values())[0]
plugins = []
services = get_default_services(
file.name, context.get(DEFAULT_SERVICES_ATTR)() if DEFAULT_SERVICES_ATTR in context else None
)
return graph, plugins, services
def execute(file, quiet=False):
graph, plugins, services = read_file(file)
# todo if console and not quiet, then add the console plugin # todo if console and not quiet, then add the console plugin
# todo when better console plugin, add it if console and just disable display # todo when better console plugin, add it if console and just disable display
return bonobo.run( return bonobo.run(graph, plugins=plugins, services=services)
graph,
plugins=[],
services=get_default_services(
file.name, context.get(DEFAULT_SERVICES_ATTR)() if DEFAULT_SERVICES_ATTR in context else None
)
)
def register(parser): def register(parser):
import argparse
parser.add_argument('file', type=argparse.FileType()) parser.add_argument('file', type=argparse.FileType())
parser.add_argument('--quiet', action='store_true') parser.add_argument('--quiet', action='store_true')
return execute return execute

View File

@ -4,3 +4,5 @@ BEGIN = Token('Begin')
END = Token('End') END = Token('End')
INHERIT_INPUT = Token('InheritInput') INHERIT_INPUT = Token('InheritInput')
NOT_MODIFIED = Token('NotModified') NOT_MODIFIED = Token('NotModified')
DEFAULT_SERVICES_FILENAME = '_services.py'
DEFAULT_SERVICES_ATTR = 'get_services'

View File

@ -29,3 +29,5 @@ class Graph:
def __len__(self): def __len__(self):
return len(self.nodes) return len(self.nodes)

9
bonobo/util/graphviz.py Normal file
View File

@ -0,0 +1,9 @@
def render_as_dot(graph):
"""
:param bonobo.Graph graph:
:return: str
"""
pass

View File

@ -65,6 +65,7 @@ setup(
entry_points={ entry_points={
'bonobo.commands': [ 'bonobo.commands': [
'init = bonobo.commands.init:register', 'init = bonobo.commands.init:register',
'graph = bonobo.commands.graph:register',
'run = bonobo.commands.run:register', 'run = bonobo.commands.run:register',
'version = bonobo.commands.version:register' 'version = bonobo.commands.version:register'
], ],