Commits

Ginés Martínez Sánchez committed 2c74d38 Draft

developing wsgi server

Comments (0)

Files changed (7)

ginsfsm/compat.py

 except NameError: # pragma: no cover
     input_ = input
 
+try:  # pragma: no cover
+    from UserDict import DictMixin
+except ImportError:  # pragma: no cover
+    from collections.UserDict import DictMixin
 
 try:
     from StringIO import StringIO as NativeIO

ginsfsm/examples/wsgi/multiple_wsgi_server.ini

+[composite:main]
+use = call:ginsfsm.protocols.wsgi.common.urlmap:urlmap_factory
+use = call:ginsfsm.gaplic:gaplic_factory
+main = wsgi-server
+
+[app:wsgi-server]
+use = call:ginsfsm.examples.wsgi.multiple_wsgi_server:main
+wsgi-server.host = 0.0.0.0
+wsgi-server.port = 8000
+wsgi-server.application = composite:urlmap
+GSock.trace_dump = true
+GObj.trace_mach = true
+
+[composite:urlmap]
+use = call:ginsfsm.protocols.wsgi.common.urlmap:urlmap_factory
+/ = application1
+/app2 = application1
+/app3 = application1
+
+[app:application1]
+use = call:ginsfsm.examples.wsgi.multiple_wsgi_server:paste_app_factory1
+
+[app:application2]
+use = call:ginsfsm.examples.wsgi.multiple_wsgi_server:paste_app_factory2
+
+[app:application3]
+use = call:ginsfsm.examples.wsgi.multiple_wsgi_server:paste_app_factory3
+
+# Begin logging configuration
+
+[loggers]
+keys = root, ginsfsm
+
+[handlers]
+keys = console
+
+[formatters]
+keys = generic
+
+[logger_root]
+level = DEBUG
+handlers = console
+
+[logger_ginsfsm]
+level = DEBUG
+handlers =
+qualname = ginsfsm
+
+[handler_console]
+class = StreamHandler
+args = (sys.stderr,)
+level = NOTSET
+formatter = generic
+
+[formatter_generic]
+format = %(message)s
+
+# End logging configuration

ginsfsm/examples/wsgi/multiple_wsgi_server.py

+"""
+Simple WSGI server
+==================
+
+It uses :class:`ginsfsm.protocols.wsgi.server.c_wsgi_server.GWsgiServer`.
+
+.. autofunction:: main
+
+"""
+
+import logging
+logging.basicConfig(level=logging.DEBUG)
+
+from ginsfsm.gaplic import GAplic
+from ginsfsm.protocols.wsgi.server.c_wsgi_server import GWsgiServer
+from ginsfsm.protocols.wsgi.common.urlmap import URLMap
+
+
+#===============================================================
+#                   Server
+#===============================================================
+def application1(environ, start_response):
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    yield 'Hello World ONE!\n'
+
+
+def application2(environ, start_response):
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    yield 'Hello World TWO!\n'
+
+
+def application3(environ, start_response):
+    start_response('200 OK', [('Content-Type', 'text/plain')])
+    yield 'Hello World THREE!\n'
+
+
+def paste_app_factory1(global_config, **local_conf):
+    return application1
+
+
+def paste_app_factory2(global_config, **local_conf):
+    return application2
+
+
+def paste_app_factory3(global_config, **local_conf):
+    return application3
+
+
+#===============================================================
+#                   Main
+#===============================================================
+def main(global_config, **local_conf):
+    """ Entry point to run with gserve (PasteDeploy)
+    """
+    ga = GAplic('Wsgi-Example', **local_conf)
+    'wsgi-server.application'
+    ga.create_gobj(
+        'wsgi-server',
+        GWsgiServer,
+        None,
+        application=application1
+    )
+    return ga
+
+
+if __name__ == "__main__":
+    local_conf = {
+        'GObj.trace_mach': True,
+        'GObj.logger': logging,
+        'GSock.trace_dump': True,
+        'host': '0.0.0.0',
+        'port': 8002,
+    }
+    ga = main({}, **local_conf)
+
+    try:
+        ga.mt_process()
+    except (KeyboardInterrupt, SystemExit):
+        print('Program stopped')

ginsfsm/examples/wsgi/simple_wsgi_server.ini

 [composite:main]
 use = call:ginsfsm.gaplic:gaplic_factory
 main = wsgi-server
-others = wsgi-application
+wsgi = wsgi-application
 
 [app:wsgi-server]
 use = call:ginsfsm.examples.wsgi.simple_wsgi_server:main
-wsgi-server.host = 0.0.0.0
-wsgi-server.port = 8000
-wsgi-server.application = app:wsgi-application
+host = 0.0.0.0
+port = 8000
+application = wsgi-application
 GSock.trace_dump = true
 GObj.trace_mach = true
 

ginsfsm/examples/wsgi/simple_wsgi_server.py

 logging.basicConfig(level=logging.DEBUG)
 
 from ginsfsm.gaplic import GAplic
+from ginsfsm.globals import get_global_app
 from ginsfsm.protocols.wsgi.server.c_wsgi_server import GWsgiServer
 
 
 #===============================================================
 #                   Main
 #===============================================================
-def main(global_config, **settings):
+def main(global_config, **local_conf):
     """ Entry point to run with gserve (PasteDeploy)
     """
-    ga = GAplic('Wsgi-Example', **settings)
+    if 'application' in local_conf:
+        application = local_conf.pop('application')
+    else:
+        raise Exception('You must supply an wsgi application.')
+    application = get_global_app(application)
+    ga = GAplic('Wsgi-Example', **local_conf)
     ga.create_gobj(
         'wsgi-server',
         GWsgiServer,
         None,
-        application=application
+        application=application,
     )
     return ga
 
         'GSock.trace_dump': True,
         'host': '0.0.0.0',
         'port': 8002,
+        'application': 'wsgi-application',
     }
     ga = main({}, **local_conf)
 

ginsfsm/gaplic.py

 .. autoclass:: GAplicProcessWorker
     :members:
 
+.. autofunction:: gaplic_factory
+
 """
 import time
 import threading
 
 
 def gaplic_factory(loader, global_conf, **local_conf):
-    """ To use with PasteDeploy in composite, example::
+    """ To use with PasteDeploy in composite.
 
-        [composite:main]
-        use = call:ginsfsm.gaplic:gaplic_factory
-        main = wsgi-server
-        threads =
-        subprocesses =
-        others = wsgi-application
+        Items of *composite* section:
 
-        [app:wsgi-server]
-        use = call:ginsfsm.examples.example6-wsgi-server:main
-        host = 0.0.0.0
-        port = 8001
-        wsgi-server.application = app:wsgi-application
-        # TODO: wsgi-server.application = composite:urlmap
+        :main: name of a section that must return a :term:`gaplic`
+               instance. It will be the **principal** :term:`gaplic`.
 
-        [app:wsgi-application]
-        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+        :threads: name of sections that must return :term:`gaplic`
+               instances. They will run in threads.
 
-        # TODO: check
-        [composite:urlmap]
-        use = call:ginsfsm.protocols.wsgi.common.urlmap:urlmap_factory
-        / = home
-        /blog = blog
-        /wiki = wiki
+        :subprocesses: name of sections that must return :term:`gaplic`
+               instances. They will run in subprocesses.
 
-        [app:home]
-        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+        :wsgi: name of sections that must return a *app paste factory*.
+            Wsgi applications are saved as global apps (set_global_app()).
 
-        [app:blog]
-        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
 
-        [app:wiki]
-        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+        Example::
 
-    The prototype for ``others`` (paste app factory) is::
+            [composite:main]
+            use = call:ginsfsm.gaplic:gaplic_factory
+            main = wsgi-server
+            wsgi = wsgi-application
+
+            [app:wsgi-server]
+            use = call:ginsfsm.examples.wsgi.simple_wsgi_server:main
+            host = 0.0.0.0
+            port = 8000
+            application = wsgi-application
+            GSock.trace_dump = true
+            GObj.trace_mach = true
+
+            [app:wsgi-application]
+            use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+
+
+    The prototype for ``wsgi`` (paste app factory) is::
 
         def paste_app_factory(global_conf, **local_conf):
             return wsgi-application
 
-    The prototype for ``main``, ``threads`` and ``subprocesses`` is:
+    The prototype for ``main``, ``threads`` and ``subprocesses`` is::
 
-        def main(global_conf, **settings):
+        def main(global_conf, **local_config):
             return gaplic-instance
 
     """
     main = local_conf.get('main')
-    others = local_conf.get('others', '').split()
+    wsgis = local_conf.get('wsgi', '').split()
     threads = local_conf.get('threads', '').split()
     subprocesses = local_conf.get('subprocesses', '').split()
 
-    for other in others:
-        app = loader.get_app(other)
-        set_global_app(other, app)
+    for wsgi in wsgis:
+        app = loader.get_app(wsgi, global_conf=global_conf)
+        set_global_app(wsgi, app)
 
-    gaplic_main = loader.get_app(main)
+    gaplic_main = loader.get_app(main, global_conf=global_conf)
     set_global_main_gaplic(gaplic_main)
 
     for thread in threads:
-        gaplic = loader.get_app(thread)
+        gaplic = loader.get_app(thread, global_conf=global_conf)
         worker = GAplicThreadWorker(gaplic)
         worker.start()
 
     for subprocess in subprocesses:
-        gaplic = loader.get_app(subprocess)
+        gaplic = loader.get_app(subprocess, global_conf=global_conf)
         worker = GAplicProcessWorker(gaplic)
         worker.start()
 

ginsfsm/protocols/wsgi/common/urlmap.py

 Map URL prefixes to WSGI applications.  See ``URLMap``
 """
 
-try:
-    from UserDict import DictMixin
-except ImportError:  # pragma: no cover
-    # py3
-    from collections.UserDict import DictMixin
-
+from ginsfsm.compat import DictMixin
+from ginsfsm.compat import escape
 import re
 import os
-import cgi
 
 __all__ = ['URLMap', 'PathProxyURLMap']
 
 
 def urlmap_factory(loader, global_conf, **local_conf):
+    """
+        # TODO: check
+        [composite:urlmap]
+        use = call:ginsfsm.protocols.wsgi.common.urlmap:urlmap_factory
+        / = home
+        /blog = blog
+        /wiki = wiki
+
+        [app:home]
+        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+
+        [app:blog]
+        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+
+        [app:wiki]
+        use = call:ginsfsm.examples.wsgi.simple_wsgi_server:paste_app_factory
+    """
     if 'not_found_app' in local_conf:
         not_found_app = local_conf.pop('not_found_app')
     else:
         from paste import httpexceptions
         app = httpexceptions.HTTPNotFound(
             environ['PATH_INFO'],
-            comment=cgi.escape(extra)).wsgi_application
+            comment=escape(extra)).wsgi_application
         return app(environ, start_response)
 
     def normalize_url(self, url, trim=True):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.