<div class="socmaildefaultfont" dir="ltr" style="font-family:Arial;font-size:10.5pt" ><div dir="ltr" >Yes Cyril, your suggestion is fair, we should keep it consistent across all files in a project. There is always debating between tabs and whitespaces. Even though personally I prefer white spaces (that's why my editor automatically convert tab to whitespace for me), I agree that for this project, I should use tabs. Will make an update soon.
<div> </div>
<div class="socmaildefaultfont" dir="ltr" style="font-family:Arial;font-size:10.5pt" ><div class="socmaildefaultfont" dir="ltr" style="font-family:Arial;font-size:10.5pt" ><div class="socmaildefaultfont" dir="ltr" style="font-family:Arial;font-size:10.5pt" ><div dir="ltr" >GOU, Peng Fei (苟鹏飞), Ph.D.</div>
<div dir="ltr" >OpenPOWER Enablement.</div>
<div dir="ltr" >+86-21-609-28631</div></div></div></div>
<div> </div>
<div> </div>
<blockquote data-history-content-modified="1" style="border-left:solid #aaaaaa 2px; margin-left:5px; padding-left:5px; direction:ltr; margin-right:0px" >----- Original message -----<br>From: Cyril Bur <cyrilbur@gmail.com><br>To: Peng Fei BG Gou/China/IBM@IBMCN<br>Cc: "openbmc" <openbmc@lists.ozlabs.org>, "OpenBMC Patches" <openbmc-patches@stwcx.xyz><br>Subject: Re: 回复:回复:[PATCH phosphor-rest-server] The streaming support for obmc-rest.<br>Date: Fri, Jan 29, 2016 10:09 AM<br>
<div><font face="Default Monospace,Courier New,Courier,monospace" size="2" >On Fri, 29 Jan 2016 00:49:39 +0000<br>"Peng Fei BG Gou" <shgoupf@cn.ibm.com> wrote:<br><br>> And the white space you saw in the email thread and coding is probably caused by the mixing use of tab and white space. The original code uses tab for indent, while I prefer to use 4 white spaces for indent. I will discuss with Brad regarding the indent convention for our code. Will fix it up if we strictly need tab in our project.<br><br>In a world where we all have to read each others code, we should make an effort<br>to not make it hard for everyone. What you have done here is mix different<br>indenting styles which only makes it harder to read for everyone to read the<br>code. Imagine a world where I preferred 8 space indenting and I went through<br>and my additions to the file were 8 space indented, this would be impossible to<br>read.<br><br>Typically when a file has been written in one way, it is left that way unless<br>that particular way is absolutely abhorrent (5 space indenting...). Tabs are a<br>perfectly valid way of indenting, I don't see the issue here.<br><br>Futhurmore, I know this isn't really for python, if in doubt we should probably<br>be falling back to the openbmc/docs/contributing file and I quote:<br><br>Components of the OpenBMC sources should have consistent style.<br><br>For C code, we typically use the Linux coding style, which is<br>documented at:<br><br><a href="http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/CodingStyle" target="_blank" >http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/CodingStyle</a><br><br>(unless you want to write a completely separate Python document, and change all<br>the python in this project, I suggest we follow this one where appropriate)<br><br>Indent with tabs instead of spaces, set at 8 columns<br><br>tl;dr<br><br>Don't mix indenting styles, way too hard to read the code.<br><br>> <br>> <br>> <br>> 在 2016年1月29日,上午8:38:15,"Peng Fei BG Gou" <shgoupf@cn.ibm.com> 写道:<br>> <br>> Python is indent based languang, so the function will fail if we have<br>> incorrect indenting. I have tested this in real bmc machine so I believe the<br>> indenting should be fine for now. Please let Brad review this change since he<br>> is familiar with Python.<br>> 在 2016年1月29日,上午8:24:58,"Cyril Bur" <cyrilbur@gmail.com> 写道:<br>> <br>> On Thu, 28 Jan 2016 02:00:37 -0600<br>> OpenBMC Patches wrote:<br>> > From: shgoupf<br>> > <br>> Hi Peng,<br>> So I'm don't really know python all that well but I do believe this<br>> language is white space sensitive... I'll let a pythoner respond about the<br>> rest...<br>> > Changes:<br>> > 1) The main idea of this change is to have a streaming path as below:<br>> > dbus signal -> obmc-rest capture the dbus signal -> obmc-rest<br>> > notify the client of the signal receiving. 2) Replace rocket with<br>> > gevent WSGI server to support multiple async accesses. 3) Use gevent<br>> > queue to notify the dbus signal receiving. 4) The uri to the streaming<br>> > should be in the form as below: <a href="https:////stream/" target="_blank" >https:////stream/</a><br>> > ---<br>> > obmc-rest | 115<br>> > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file<br>> > changed, 104 insertions(+), 11 deletions(-) mode change 100644 =><br>> > 100755 obmc-rest<br>> ><br>> > diff --git a/obmc-rest b/obmc-rest<br>> > old mode 100644<br>> > new mode 100755<br>> > index c6d2949..481dafa<br>> > --- a/obmc-rest<br>> > +++ b/obmc-rest<br>> > @@ -3,7 +3,9 @@<br>> > import os<br>> > import sys<br>> > import dbus<br>> > +import gobject<br>> > import dbus.exceptions<br>> > +import dbus.mainloop.glib<br>> > import json<br>> > import logging<br>> > from xml.etree import ElementTree<br>> > @@ -14,6 +16,10 @@ from OpenBMCMapper import Mapper, PathTree,<br>> > IntrospectionNodeParser, ListMatch import spwd<br>> > import grp<br>> > import crypt<br>> > +import threading<br>> > +import gevent<br>> > +from gevent.pywsgi import WSGIServer<br>> > +from gevent.queue import Queue<br>> > <br>> > DBUS_UNKNOWN_INTERFACE = 'org.freedesktop.UnknownInterface'<br>> > DBUS_UNKNOWN_METHOD = 'org.freedesktop.DBus.Error.UnknownMethod'<br>> > @@ -59,12 +65,13 @@ def makelist(data):<br>> > <br>> > class RouteHandler(object):<br>> > _require_auth = makelist(valid_user)<br>> > - def __init__(self, app, bus, verbs, rules):<br>> > + def __init__(self, app, bus, verbs, rules, skips = []):<br>> > self.app = app<br>> > self.bus = bus<br>> > self.mapper = Mapper(bus)<br>> > self._verbs = makelist(verbs)<br>> > self._rules = rules<br>> > + self._skips = skips<br>> > <br>> > def _setup(self, **kw):<br>> > request.route_data = {}<br>> > @@ -79,7 +86,7 @@ class RouteHandler(object):<br>> > return getattr(self, 'do_' + request.method.lower())(**kw)<br>> > <br>> > def install(self):<br>> > - self.app.route(self._rules, callback = self,<br>> > + self.app.route(self._rules, callback = self, skip = self._skips,<br>> > method = ['GET', 'PUT', 'PATCH', 'POST', 'DELETE'])<br>> > <br>> > @staticmethod<br>> > @@ -108,6 +115,58 @@ class RouteHandler(object):<br>> > return None<br>> > raise<br>> > <br>> > +class SignalHandler(RouteHandler):<br>> > + verbs = ['GET']<br>> > + rules = '/stream/'<br>> > +<br>> > + def __init__(self, app, bus):<br>> > + super(SignalHandler, self).__init__(<br>> > + app, bus, self.verbs, self.rules)<br>> > +<br>> > + def find(self, path, signal):<br>> > + busses = self.try_mapper_call(self.mapper.get_object,<br>> > + path = path)<br>> > + for items in busses.iteritems():<br>> > + s = self.find_signal_on_bus(path, signal, *items)<br>> > + if s:<br>> > + return s<br>> > +<br>> > + abort(404, _4034_msg %('signal', 'found', signal))<br>> > +<br>> > + def setup(self, path, signal):<br>> > + request.route_data['map'] = self.find(path, signal)<br>> > +<br>> > + def do_get(self, path, signal):<br>> > + body = Queue()<br>> > + dsignal = DbusSignal(bus, request.route_data['map'][0],<br>> > + request.route_data['map'][1],<br>> > path)<br>> > + dsignal.onData(body.put)<br>> > + dsignal.onFinish(lambda: body.put(StopIteration))<br>> > + dsignal.signalSnooping()<br>> > + return body<br>> > +<br>> > + @staticmethod<br>> > + def find_signal(signal, signals):<br>> > + if signals is None:<br>> > + return None<br>> > +<br>> > + signal = find_case_insensitive(signal, signals.keys())<br>> > + if signal is not None:<br>> > + return signal<br>> > +<br>> > + def find_signal_on_bus(self, path, signal, bus, interfaces):<br>> > + obj = self.bus.get_object(bus, path, introspect = False)<br>> > + iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE)<br>> > + data = iface.Introspect()<br>> > + parser = IntrospectionNodeParser(<br>> > + ElementTree.fromstring(data),<br>> > + intf_match = ListMatch(interfaces))<br>> > + for x,y in parser.get_interfaces().iteritems():<br>> > + s = self.find_signal(signal,<br>> > + y.get('signal'))<br>> > + if s:<br>> > + return (x,s)<br>> > +<br>> > class DirectoryHandler(RouteHandler):<br>> > verbs = 'GET'<br>> > rules = '/'<br>> > @@ -715,7 +774,8 @@ class RestApp(Bottle):<br>> > self.install(JSONPlugin(**json_kw))<br>> > self.install(JsonApiErrorsPlugin(**json_kw))<br>> > self.install(AuthorizationPlugin())<br>> > - self.install(JsonApiResponsePlugin())<br>> > + self.json_response_plugin = JsonApiResponsePlugin() <br>> Indenting?<br>> > + self.install(self.json_response_plugin)<br>> > self.install(JsonApiRequestPlugin())<br>> > self.install(JsonApiRequestTypePlugin())<br>> > <br>> > @@ -726,6 +786,7 @@ class RestApp(Bottle):<br>> > <br>> > def create_handlers(self):<br>> > # create route handlers<br>> > + self.signal_handler = SignalHandler(self, self.bus)<br>> > self.session_handler = SessionHandler(self, self.bus)<br>> > self.directory_handler = DirectoryHandler(self, self.bus)<br>> > self.list_names_handler = ListNamesHandler(self, self.bus)<br>> > @@ -736,6 +797,11 @@ class RestApp(Bottle):<br>> > self.instance_handler = InstanceHandler(self, self.bus)<br>> > <br>> > def install_handlers(self):<br>> > + # Skip json response for signal handler because it<br>> > requires to<br>> > + # return a gevent iterable which cannot be handled by<br>> > json<br>> > + # response plugin<br>> > + self.signal_handler._skips =<br>> > [self.json_response_plugin] <br>> Indenting?<br>> > + self.signal_handler.install()<br>> > self.session_handler.install()<br>> > self.directory_handler.install()<br>> > self.list_names_handler.install()<br>> > @@ -766,21 +832,48 @@ class RestApp(Bottle):<br>> > parts = filter(bool, path.split('/'))<br>> > request.environ['PATH_INFO'] = '/' + '/'.join(parts) + trailing<br>> > <br>> > +class DbusSignal():<br>> > + def __init__(self, bus, dbus_interface, signal_name, path):<br>> > + # Register the dbus recieve handler<br>> > + bus.add_signal_receiver(self.signalReciever,<br>> > + dbus_interface = dbus_interface,<br>> > + signal_name = signal_name,<br>> > + path = path)<br>> > +<br>> > + self.snooping = True<br>> > +<br>> > + def signalReciever(self, msg):<br>> > + self.send("Recieved message: %s" % msg)<br>> > + self.snooping = False<br>> > +<br>> > + def onData(self, send):<br>> > + self.send = send<br>> > +<br>> > + def onFinish(self, f):<br>> > + self.finish = f<br>> > +<br>> > + def signalSnooping(self):<br>> > + while self.snooping:<br>> > + mainloop = gobject.MainLoop()<br>> > + gevent.sleep(1)<br>> > + gobject.timeout_add(1, mainloop.quit)<br>> > + mainloop.run()<br>> > +<br>> > + self.finish()<br>> > +<br>> > if __name__ == '__main__':<br>> > log = logging.getLogger('Rocket.Errors')<br>> > log.setLevel(logging.INFO)<br>> > log.addHandler(logging.StreamHandler(sys.stdout))<br>> > <br>> > + dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) <br>> Indenting?<br>> > bus = dbus.SystemBus()<br>> > app = RestApp(bus)<br>> > + <br>> ?<br>> > default_cert = os.path.join(sys.prefix, 'share',<br>> > os.path.basename(__file__), 'cert.pem')<br>> > <br>> > - server = Rocket(('0.0.0.0',<br>> > - 443,<br>> > - default_cert,<br>> > - default_cert),<br>> > - 'wsgi', {'wsgi_app': app},<br>> > - min_threads = 1,<br>> > - max_threads = 1)<br>> > - server.start()<br>> > + server = WSGIServer(("0.0.0.0", 443), app, keyfile =<br>> > default_cert,<br>> > + certfile = default_cert)<br>> > +<br>> > + server.serve_forever() <br>> Indenting?</font><br> </div></blockquote>
<div> </div></div></div><BR>