12 import twisted
.internet
.reactor
13 import twisted
.web
.resource
14 import twisted
.web
.server
16 import entangled_network
18 class ObserverResource(twisted
.web
.resource
.Resource
):
23 def node_pic(self
, node
, tree
):
25 name
= self
.names
[node
]
29 if node
in tree
['child_left']: left_node
= tree
['child_left'][node
]
30 if node
in tree
['child_right']: right_node
= tree
['child_right'][node
]
31 left_pic
= self
.node_pic(left_node
, tree
)
32 right_pic
= self
.node_pic(right_node
, tree
)
33 lefts
= left_pic
.split('\n')
34 retu
+= '-' + lefts
[0] + '\n'
35 for line
in lefts
[1:]:
36 retu
+= ' |' + line
+ '\n'
38 rights
= right_pic
.split('\n')
39 retu
+= ' -' + rights
[0] + '\n'
40 for line
in rights
[1:]:
41 retu
+= ' ' + line
+ '\n'
46 def tree_pic(self
, tree
):
48 return self
.node_pic(root
, tree
)
50 def add_name(self
, node
):
51 if node
and not (node
in self
.names
):
53 self
.names
[node
] = '%02d' % self
.last_name
55 def render_GET(self
, request
):
56 queries
= request
.args
57 print 'queries = %s' % repr(queries
)
59 if 'action' in queries
: action
= queries
['action'][0]
60 if action
== 'new_root':
61 feed_url
= queries
['rss'][0]
62 root
= eval(queries
['root'][0])
64 if feed_url
in self
.trees
:
67 self
.trees
[feed_url
] = {'root': None, 'child_left': {}, 'child_right': {}}
68 self
.trees
[feed_url
]['root'] = root
70 elif action
== 'adopt_child':
71 parent
= eval(queries
['parent'][0]);
72 child
= eval(queries
['child'][0]);
73 feed_url
= queries
['rss'][0]
74 loc
= queries
['loc'][0]
78 self
.trees
[feed_url
]['child_left'][parent
] = child
81 self
.trees
[feed_url
]['child_right'][parent
] = child
86 if 'rss' in queries
: rss
= queries
['rss'][0]
88 if rss
in self
.trees
: return self
.tree_pic(self
.trees
[rss
])
89 else: return 'tree for %s not found' % rss
94 rsrc
= ObserverResource()
95 site
= twisted
.web
.server
.Site(rsrc
)
96 twisted
.internet
.reactor
.listenTCP(9501, site
)
97 twisted
.internet
.reactor
.run()