29 from zope.interface import implements |
29 from zope.interface import implements |
30 from nevow import appserver, inevow, tags, loaders, athena, url, rend |
30 from nevow import appserver, inevow, tags, loaders, athena, url, rend |
31 from nevow.page import renderer |
31 from nevow.page import renderer |
32 from formless import annotate |
32 from formless import annotate |
33 from formless import webform |
33 from formless import webform |
|
34 from formless import configurable |
34 |
35 |
35 from twisted.internet import reactor |
36 from twisted.internet import reactor |
36 import util.paths as paths |
37 import util.paths as paths |
37 |
38 |
38 PAGE_TITLE = 'Beremiz Runtime Web Interface' |
39 PAGE_TITLE = 'Beremiz Runtime Web Interface' |
137 def defaultVal(category): |
138 def defaultVal(category): |
138 def _defaultVal(ctx,argument): |
139 def _defaultVal(ctx,argument): |
139 return lastKnownConfig[category].get(argument.name, None) |
140 return lastKnownConfig[category].get(argument.name, None) |
140 return _defaultVal |
141 return _defaultVal |
141 |
142 |
|
143 |
|
144 class ConfigurableBindings(configurable.Configurable): |
|
145 |
|
146 def __init__(self): |
|
147 configurable.Configurable.__init__(self, None) |
|
148 self.bindingsNames = [] |
|
149 |
|
150 def getBindingNames(self, ctx): |
|
151 return self.bindingsNames |
|
152 |
|
153 def addExtension(self, name, desc, fields, callback): |
|
154 print(name, fields, callback) |
|
155 def _bind(ctx): |
|
156 return annotate.MethodBinding( |
|
157 'action_'+name, |
|
158 annotate.Method(arguments=[ |
|
159 annotate.Argument(name, fieldtype) |
|
160 for fieldname,fieldtype in fields], |
|
161 label = desc), |
|
162 action = _("Set")) |
|
163 setattr(self, 'bind_'+name, _bind) |
|
164 |
|
165 def _action(**kw): |
|
166 callback(**kw) |
|
167 |
|
168 setattr(self, 'action_'+name, _action) |
|
169 |
|
170 self.bindingsNames.append(name) |
|
171 |
|
172 ConfigurableSettings = ConfigurableBindings() |
142 |
173 |
143 class ISettings(annotate.TypedInterface): |
174 class ISettings(annotate.TypedInterface): |
144 def networkConfig( |
175 def networkConfig( |
145 ctx = annotate.Context(), |
176 ctx = annotate.Context(), |
146 mode = annotate.Choice(["DHCP", "Static"], |
177 mode = annotate.Choice(["DHCP", "Static"], |
152 default=defaultVal('net')), |
183 default=defaultVal('net')), |
153 mask = annotate.String(label=_("Network mask"),default=defaultVal('net')), |
184 mask = annotate.String(label=_("Network mask"),default=defaultVal('net')), |
154 DNS = annotate.String(label=_("DNS address"),default=defaultVal('net'))): |
185 DNS = annotate.String(label=_("DNS address"),default=defaultVal('net'))): |
155 pass |
186 pass |
156 |
187 |
157 networkConfig = annotate.autocallable(networkConfig, label=_("Network settings"), action="Set", ) |
188 networkConfig = annotate.autocallable(networkConfig, label=_("Network settings"), action=_("Set")) |
158 |
189 |
159 |
190 |
160 class SettingsPage(rend.Page): |
191 class SettingsPage(rend.Page): |
161 # We deserve a slash |
192 # We deserve a slash |
162 addSlash = True |
193 addSlash = True |
164 # This makes webform_css url answer some default CSS |
195 # This makes webform_css url answer some default CSS |
165 child_webform_css = webform.defaultCSS |
196 child_webform_css = webform.defaultCSS |
166 |
197 |
167 implements(ISettings) |
198 implements(ISettings) |
168 |
199 |
169 def networkConfig(*args, **kwargs): |
|
170 # TODO do the settings |
|
171 print(kwargs) |
|
172 lastKnownConfig['net'] = kwargs |
|
173 |
200 |
174 docFactory = loaders.stan([tags.html[ |
201 docFactory = loaders.stan([tags.html[ |
175 tags.head[ |
202 tags.head[ |
176 tags.title[_("Beremiz Runtime Settings")], |
203 tags.title[_("Beremiz Runtime Settings")], |
177 tags.link(rel='stylesheet', |
204 tags.link(rel='stylesheet', |
178 type='text/css', |
205 type='text/css', |
179 href=url.here.child("webform_css")) |
206 href=url.here.child("webform_css")) |
180 ], |
207 ], |
181 tags.body[ |
208 tags.body[ |
182 webform.renderForms() |
209 tags.h1["Runtime settings:"], |
|
210 webform.renderForms('staticSettings'), |
|
211 tags.h2["Extensions settings:"], |
|
212 webform.renderForms('dynamicSettings'), |
183 ]]]) |
213 ]]]) |
184 |
214 |
|
215 def __init__(self): |
|
216 rend.Page.__init__(self) |
|
217 |
|
218 def configurable_staticSettings(self, ctx): |
|
219 return configurable.TypedInterfaceConfigurable(self) |
|
220 |
|
221 def configurable_dynamicSettings(self, ctx): |
|
222 return ConfigurableSettings |
|
223 |
|
224 def networkConfig(self, *args, **kwargs): |
|
225 # TODO do the settings |
|
226 print(kwargs) |
|
227 lastKnownConfig['net'] = kwargs |
|
228 ConfigurableSettings.addExtension( |
|
229 "wamp", |
|
230 "wamp DEscription", |
|
231 [("Host",annotate.String(label=_("IP address")))], |
|
232 lambda**k:print(k)) |
185 |
233 |
186 |
234 |
187 class WebInterface(athena.LivePage): |
235 class WebInterface(athena.LivePage): |
188 |
236 |
189 docFactory = loaders.stan([tags.raw(xhtml_header), |
237 docFactory = loaders.stan([tags.raw(xhtml_header), |