~ubuntu-branches/ubuntu/precise/ubuntuone-client/precise

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/fsm/fsm_draw.py

  • Committer: Package Import Robot
  • Author(s): Rodney Dawes
  • Date: 2011-12-21 15:46:25 UTC
  • mfrom: (1.1.56)
  • Revision ID: package-import@ubuntu.com-20111221154625-ujvunri4frsecj2k
Tags: 2.99.0-0ubuntu1
* New upstream release.
  - Verify timestamp to avoid invalid auth failures (LP: #692597)
  - Files in new UDFs not uploaded due to filtering (LP: #869920)
* debian/patches:
  - Remove upstreamed patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import xdot
30
30
from ubuntuone.syncdaemon.fsm import fsm
31
31
 
 
32
 
32
33
def dict2label(d):
33
34
    'transform a dictionary into a label'
34
35
    t = '"%s"' % "\\n".join("%s=%s" % x for x in sorted(d.items()))
35
36
    return t
36
37
 
 
38
 
37
39
def main(filename, debug=False):
38
40
    'draw the state machine that is in $filename'
39
 
    
 
41
 
40
42
    print "Parsing file...   (%s)" % time.ctime()
41
43
    machine = fsm.StateMachine(filename)
42
44
 
43
45
    print "Building graph... (%s)" % time.ctime()
44
46
    despair = object()
45
 
    graph_base =  u'digraph G {\n%s [label="despair"]\n%%s\n}' % id(despair)
 
47
    graph_base = u'digraph G {\n%s [label="despair"]\n%%s\n}' % id(despair)
46
48
    graph_lines = []
47
49
    s2s = {}
48
50
    for state in machine.states.values():
49
51
        line = "%s [label=%s]" % (id(state), dict2label(state.values))
50
52
        graph_lines.append(line)
51
 
        
 
53
 
52
54
    for event in machine.events.values():
53
55
        for transition in event.draw_transitions:
54
 
            if all(map(lambda x: x=="*", transition.target.values())):
 
56
            if all(map(lambda x: x == "*", transition.target.values())):
55
57
                target = despair
56
58
            else:
57
59
                try:
58
60
                    target = machine.get_state(transition.target)
59
61
                except KeyError:
60
62
                    continue
61
 
                
 
63
 
62
64
            l = s2s.setdefault((fsm.hash_dict(transition.source),
63
65
                                fsm.hash_dict(transition.target)), [])
64
 
            l.append( transition )
65
 
                    
66
 
        
 
66
            l.append(transition)
 
67
 
67
68
    for (source, target), ts in s2s.items():
68
69
        try:
69
70
            source = machine.get_state(dict(source))
70
 
            if all(map(lambda x: x=="*", dict(target).values())):
 
71
            if all(map(lambda x: x == "*", dict(target).values())):
71
72
                target = despair
72
73
            else:
73
74
                target = machine.get_state(dict(target))
76
77
        cases = []
77
78
        for t in ts:
78
79
            ps = " ".join(["%s:%s" % (k, v) for k, v in t.parameters.items()])
79
 
            cases.append( "%s:%s" % (t.event, ps))
 
80
            cases.append("%s:%s" % (t.event, ps))
80
81
        line = '%s [label="%s", shape=box, fontsize=7]' % (
81
82
            id(ts), "\\n".join(cases))
82
83
        graph_lines.append(line)
83
 
        
84
 
        arrow = '%s -> %s' % (
85
 
            id(source), id(ts)
86
 
            )
87
 
        graph_lines.append(arrow)
88
 
        arrow = '%s -> %s' % (
89
 
            id(ts), id(target)
90
 
            )
91
 
        graph_lines.append(arrow)
92
 
        
 
84
 
 
85
        arrow = '%s -> %s' % (
 
86
            id(source), id(ts))
 
87
        graph_lines.append(arrow)
 
88
        arrow = '%s -> %s' % (
 
89
            id(ts), id(target))
 
90
        graph_lines.append(arrow)
 
91
 
93
92
    dotcode = graph_base % "\n".join(graph_lines)
94
93
    if debug:
95
94
        filename = tempfile.mkstemp(prefix='graph-', suffix='.debug')