Explorar el Código

Attempt to expose dyncode in contexts + bugfixes

Yann Weber hace 7 años
padre
commit
9ceb726a4a

+ 25
- 4
lodel/context.py Ver fichero

@@ -103,7 +103,7 @@ class LodelContext(object):
103 103
     
104 104
     ##@brief Create a new context
105 105
     #@see LodelContext.new()
106
-    def __init__(self, site_id):
106
+    def __init__(self, site_id, instance_path = None):
107 107
         print("Registering new context for '%s'" % site_id)
108 108
         if site_id is None:
109 109
             #Monosite instanciation
@@ -131,6 +131,15 @@ site_id when we are in MONOSITE beahvior")
131 131
             self.__pkg_name = '%s.%s' % (CTX_PKG, site_id)
132 132
             if self.__id == LOAD_CTX:
133 133
                 self.__pkg_name = 'lodel'
134
+            elif instance_path is None:
135
+                """
136
+                raise ContextError("Cannot create a context without an \
137
+instance path")
138
+                """
139
+                warnings.warn("It can be a really BAD idea to create a \
140
+a context without a path......")
141
+            else:
142
+                self.__instance_path = os.path.realpath(instance_path)
134 143
             #Importing the site package to trigger its creation
135 144
             self.__package = importlib.import_module(self.__pkg_name)
136 145
             self.__class__._contexts[site_id] = self
@@ -149,6 +158,12 @@ length == 2 but got : %s" % spec)
149 158
         else:
150 159
             self._expose_objects(globs, module_fullname, exposure_spec)
151 160
     
161
+    ##@brief Implements LodelContext::expose_dyncode()
162
+    def _expose_dyncode(self, globs, alias = 'leapi_dyncode'):
163
+        sys.path.append(self.__instance_path)
164
+        dyncode = importlib.import_module('leapi_dyncode')
165
+        self.safe_exposure(globs, dyncode, alias)
166
+
152 167
     ##@brief Utility method to expose a module with an alias name in globals
153 168
     #@param globs globals() : concerned globals dict
154 169
     #@param fullname str : module fullname
@@ -217,8 +232,8 @@ submodule : '%s'" % module_fullname)
217 232
     #@param site_id str : context name
218 233
     #@return the context instance
219 234
     @classmethod
220
-    def new(cls, site_id):
221
-        return cls(site_id)
235
+    def new(cls, site_id, instance_path = None):
236
+        return cls(site_id, instance_path)
222 237
 
223 238
     ##@brief Helper function that import and expose specified modules
224 239
     #
@@ -245,6 +260,11 @@ submodule : '%s'" % module_fullname)
245 260
         for spec in specs.items():
246 261
             ctx.expose(globs, spec)
247 262
     
263
+    ##@brief Expose leapi_dyncode module
264
+    @classmethod
265
+    def expose_dyncode(cls, globs, alias = 'leapi_dyncode'):
266
+        cls.get()._expose_dyncode(globs, alias)
267
+
248 268
     ##@brief Initialize the context manager
249 269
     #
250 270
     #@note Add the LodelMetaPathFinder class to sys.metapath if type is
@@ -319,10 +339,11 @@ key '%s'" % alias)
319 339
             raise ContextError("Cannot create a context from a path in \
320 340
 MONOSITE mode")
321 341
         site_id = os.path.basename(path.strip('/'))
342
+        path = os.path.realpath(path)
322 343
         if not cls.validate_identifier(site_id):
323 344
             raise ContextError(
324 345
                 "Unable to create a context named '%s'" % site_id)
325
-        cls.new(site_id)
346
+        cls.new(site_id, path)
326 347
         return site_id
327 348
 
328 349
     ##@brief Delete a site's context

+ 1
- 1
lodel/plugins/webui/interface/template/loader.py Ver fichero

@@ -4,9 +4,9 @@ import os
4 4
 
5 5
 from lodel.context import LodelContext
6 6
 LodelContext.expose_modules(globals(), {'lodel.settings': ['Settings']})
7
+LodelContext.expose_dyncode(globals())
7 8
 
8 9
 from ...client import WebUiClient as WebUiClient
9
-import leapi_dyncode
10 10
 
11 11
 from .api import api_lodel_templates
12 12
 from .exceptions.not_allowed_custom_api_key_error import NotAllowedCustomAPIKeyError

+ 4
- 1
lodel/plugins/webui/run.py Ver fichero

@@ -61,7 +61,10 @@ def empty_cookie(response):
61 61
     response.set_cookie(COOKIE_SESSION_ID, '')
62 62
 
63 63
 #Starting instance
64
-loader.start()
64
+try:
65
+    loader.start() #Works only in MONOSITE mode
66
+except NameError:
67
+    pass
65 68
 #providing access to dyncode
66 69
 
67 70
 ##@todo Dirty & quick dyncode access providing. Replace it by a clean access

Loading…
Cancelar
Guardar