This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
/
__init__.py
151 lines (125 loc) · 5.09 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
""" cortex.services.web
"""
import os
import copy
import urllib
from nevow import appserver
from twisted.web import static, server
from twisted.web.static import File
from twisted.web.client import getPage
import cortex
from cortex.core.util import report
from cortex.services import FecundService
from cortex.core.agent import Agent
from cortex.mixins import LocalQueue
from cortex.util.decorators import constraint
from cortex.mixins.flavors import ThreadedIterator
from cortex.services.web.resource import ObjResource, EFrame
from cortex.services.web.resource.conf import ConfResource
from cortex.services.web.resource.root import Root
from cortex.services.web.resource.tree import TreeResource
from cortex.services.web.resource.plotter import Plotter, Multiplotter
from cortex.services.web.resource.redirect import Redirect
from cortex.services.web.resource.data_source import DataSource
from cortex.services.web.util import draw_ugraph
from .eventhub import EventHub
from .pchoose import PortChooser
class WebRoot(Agent, PortChooser):
""" abstraction for / """
def iterate(self):
""" WebRoot is a trivial Agent with no true concurrency
flavor. nothing to do here, but this iterate method will
run exactly once. don't use it for setup. use setup()
for setup.
"""
def stop(self):
""" TODO: stop doesn't stop anything except the
eventhub it to turn off the webs also
http://mumak.net/stuff/twisted-disconnect.html
"""
self.listener.stopListening()
super(WebRoot, self).stop()
@property
def static_dir(self):
return os.path.join(
os.path.dirname(__file__),
'static')
def make_data_stream(self, endpoint, fxn):
""" attaches a datastream at the url `endpoint`.
the datastream itself will be generated by
consecutive calls to `fxn`, and `fxn` should
only return values that can be used as JSON.
`endpoint` need not start with '/'
"""
stream = DataSource(fxn)
self.putChild(endpoint, stream)
return stream
def make_redirect(self, _from, b):
""" """
rsrc = Redirect(b)
self.putChild(_from, rsrc)
url = 'http://{0}:{1}/{2}'.\
format(self.universe.host,
self.universe.port_for(self),
_from)
return rsrc, url
def setup(self):
""" setup for several things that can be
easily handled outside of the main loop.
"""
favicon = os.path.join(self.static_dir, 'favicon.ico')
self.root = Root(favicon=favicon, static=self.static_dir)
self.putChild = self.root.putChild
self.parent.putChild = self.putChild
self.populate_chldren()
site = server.Site(self.root)
self.listener = self.universe.listenTCP(self.port, site)
def populate_chldren(self):
""" NB: no relationship to Agent.children() """
self.root.parent = self # what for?
# url that generates plots.
# e.g. to see a plot for the datastream @ "/datastream"
# load "/plot?endpoint=/datastream&title=some_title"
self.putChild('plot', Plotter())
self.putChild('_multiplot', Multiplotter(wrapped=False))
self.putChild('multiplot', Multiplotter(wrapped=True))
# serialized version of the universe topology.
# this is used to generate the graph @ '/'
self.putChild('tree.json', TreeResource(self.universe))
# shows the current cortex configuration file
self.putChild('conf', ConfResource(self.universe))
# conveniences; this makes tabs for some special objects
# really just an example of how to use ObjResource
self.putChild('web', ObjResource(self))
self.putChild('universe', ObjResource(self.universe))
# the event frame
eframe = EFrame()
self.putChild('eframe', eframe)
eframe.universe=self.universe#.host,self.ehub.port,chan),)
eframe.ehub = self.parent.filter_by_type(EventHub)[0]
# self-host the source code that's running everything.
# really just an example of how to use static.File.
src_dir = os.path.dirname(cortex.__file__)
self.putChild("_code", static.File(src_dir))
class Web(FecundService):
""" Web Service:
start: start main webserver, and secondary event-hub
stop: brief description of shutdown here
"""
class Meta:
abstract = False
children = [EventHub, WebRoot]
def new_multiplot(self):
""" """
from cortex.services.web.util import Multiplot
return Multiplot(self)
def __getattr__(self,name):
""" proxy everything I can't answer to WebRoot child """
if not self.started:
raise AttributeError('wont proxy getattr for "{0}" '
'until after universe is started')
else:
return getattr(self.filter_by_type(WebRoot)[0], name)
@constraint(boot_first='postoffice')
def start(self):
super(Web, self).start()