Browse Source

Commenting and reorganising code of lodel/context.py

Yann Weber 8 years ago
parent
commit
8d4b764472
1 changed files with 80 additions and 65 deletions
  1. 80
    65
      lodel/context.py

+ 80
- 65
lodel/context.py View File

115
                 ctx.expose_modules(globals(), {'lodel.logger': 'logger'})
115
                 ctx.expose_modules(globals(), {'lodel.logger': 'logger'})
116
                 logger.info("New context instanciation named '%s'" % site_id)
116
                 logger.info("New context instanciation named '%s'" % site_id)
117
         if site_id is None:
117
         if site_id is None:
118
-            self.__id = None
118
+            self.__id = "MONOSITE"
119
             #Monosite instanciation
119
             #Monosite instanciation
120
             if self.multisite():
120
             if self.multisite():
121
                 raise ContextError("Cannot instanciate a context with \
121
                 raise ContextError("Cannot instanciate a context with \
169
         else:
169
         else:
170
             self._expose_objects(globs, module_fullname, exposure_spec)
170
             self._expose_objects(globs, module_fullname, exposure_spec)
171
     
171
     
172
-    ##@brief Implements LodelContext::expose_dyncode()
173
-    def _expose_dyncode(self, globs, alias = 'leapi_dyncode'):
174
-        sys.path.append(self.__instance_path)
175
-        dyncode = importlib.import_module('leapi_dyncode')
176
-        self.safe_exposure(globs, dyncode, alias)
172
+    ##@brief Delete a site's context
173
+    #@param site_id str : the site's name to remove the context
174
+    def remove(cls, site_id):
175
+        if site_id is None:
176
+            if cls._type == cls.MULTISITE:
177
+                raise ContextError("Cannot have a context with \
178
+site_id set to None when we are in MULTISITE beahavior")
179
+            del cls._contexts
180
+        else:
181
+            if cls._type == cls.MULTISITE:
182
+                if site_id in cls._contexts:
183
+                    del cls._contexts[site_id]
184
+                else:
185
+                    raise ContextError("No site %s exist" % site_id)
186
+            else:
187
+                raise ContextError("Cannot have a context with \
188
+    site_id set when we are in MONOSITE beahavior")
177
     
189
     
190
+    ##@return True if the class is in MULTISITE mode
178
     @classmethod
191
     @classmethod
179
     def multisite(cls):
192
     def multisite(cls):
180
         return cls._type == cls.MULTISITE
193
         return cls._type == cls.MULTISITE
181
-
194
+    
195
+    ##@brief helper class to use LodeContext with with statement
196
+    #@note alias to get method
197
+    #@note maybe useless
198
+    #@todo delete me
182
     @classmethod
199
     @classmethod
183
     def with_context(cls, target_ctx_id):
200
     def with_context(cls, target_ctx_id):
184
         return cls.get(target_ctx_id)
201
         return cls.get(target_ctx_id)
185
 
202
 
186
-    ##@brief Utility method to expose a module with an alias name in globals
187
-    #@param globs globals() : concerned globals dict
188
-    #@param fullname str : module fullname
189
-    #@param alias str : alias name
190
-    @classmethod
191
-    def _expose_module(cls, globs, fullname, alias):
192
-        module = importlib.import_module(fullname)
193
-        cls.safe_exposure(globs, module, alias)
194
-    
195
-    ##@brief Utility mehod to expose objects like in a from x import y,z
196
-    #form
197
-    #@param globs globals() : dict of globals
198
-    #@param fullename str : module fullname
199
-    #@param objects list : list of object names to expose
200
-    @classmethod
201
-    def _expose_objects(cls, globs, fullname, objects):
202
-        errors = []
203
-        module = importlib.import_module(fullname)
204
-        for o_name in objects:
205
-            if isinstance(o_name, str):
206
-                alias = o_name
207
-            else:
208
-                o_name, alias = o_name
209
-            if not hasattr(module, o_name):
210
-                errors.append(o_name)
211
-            else:
212
-                cls.safe_exposure(globs, getattr(module, o_name), alias)
213
-        if len(errors) > 0:
214
-            msg = "Module %s does not have any of [%s] as attribute" % (
215
-                fullname, ','.join(errors))
216
-            raise ImportError(msg)
217
-    
218
-    ##@brief Translate a module fullname to the context equivalent
219
-    #@param module_fullname str : a module fullname
220
-    #@return The module name in the current context
221
-    def _translate(self, module_fullname):
222
-        if not module_fullname.startswith('lodel'):
223
-            raise ContextModuleError("Given module is not lodel or any \
224
-submodule : '%s'" % module_fullname)
225
-        return module_fullname.replace('lodel', self.__pkg_name)
226
-
227
     ##@brief Set a context as active
203
     ##@brief Set a context as active
228
     #@param site_id str : site identifier (identify a context)
204
     #@param site_id str : site identifier (identify a context)
229
     @classmethod
205
     @classmethod
327
             #Add a single context with no site_id
303
             #Add a single context with no site_id
328
             cls._contexts = cls._current = cls(None)
304
             cls._contexts = cls._current = cls(None)
329
         cls.__initialized = True
305
         cls.__initialized = True
330
-
306
+    
307
+    ##@return True if the class is initialized
331
     @classmethod
308
     @classmethod
332
     def is_initialized(cls):
309
     def is_initialized(cls):
333
         return cls.__initialized
310
         return cls.__initialized
382
                 "Unable to create a context named '%s'" % site_id)
359
                 "Unable to create a context named '%s'" % site_id)
383
         cls.new(site_id, path)
360
         cls.new(site_id, path)
384
         return site_id
361
         return site_id
362
+
363
+
364
+
365
+    ##@brief Utility method to expose a module with an alias name in globals
366
+    #@param globs globals() : concerned globals dict
367
+    #@param fullname str : module fullname
368
+    #@param alias str : alias name
369
+    @classmethod
370
+    def _expose_module(cls, globs, fullname, alias):
371
+        module = importlib.import_module(fullname)
372
+        cls.safe_exposure(globs, module, alias)
385
     
373
     
386
-    ##@brief Delete a site's context
387
-    #@param site_id str : the site's name to remove the context
388
-    def remove(cls, site_id):
389
-        if site_id is None:
390
-            if cls._type == cls.MULTISITE:
391
-                raise ContextError("Cannot have a context with \
392
-site_id set to None when we are in MULTISITE beahavior")
393
-            del cls._contexts
394
-        else:
395
-            if cls._type == cls.MULTISITE:
396
-                if site_id in cls._contexts:
397
-                    del cls._contexts[site_id]
398
-                else:
399
-                    raise ContextError("No site %s exist" % site_id)
374
+    ##@brief Utility mehod to expose objects like in a from x import y,z
375
+    #form
376
+    #@param globs globals() : dict of globals
377
+    #@param fullename str : module fullname
378
+    #@param objects list : list of object names to expose
379
+    @classmethod
380
+    def _expose_objects(cls, globs, fullname, objects):
381
+        errors = []
382
+        module = importlib.import_module(fullname)
383
+        for o_name in objects:
384
+            if isinstance(o_name, str):
385
+                alias = o_name
400
             else:
386
             else:
401
-                raise ContextError("Cannot have a context with \
402
-    site_id set when we are in MONOSITE beahavior")
387
+                o_name, alias = o_name
388
+            if not hasattr(module, o_name):
389
+                errors.append(o_name)
390
+            else:
391
+                cls.safe_exposure(globs, getattr(module, o_name), alias)
392
+        if len(errors) > 0:
393
+            msg = "Module %s does not have any of [%s] as attribute" % (
394
+                fullname, ','.join(errors))
395
+            raise ImportError(msg)
403
     
396
     
397
+    ##@brief Implements LodelContext::expose_dyncode()
398
+    def _expose_dyncode(self, globs, alias = 'leapi_dyncode'):
399
+        sys.path.append(self.__instance_path)
400
+        dyncode = importlib.import_module('leapi_dyncode')
401
+        self.safe_exposure(globs, dyncode, alias)
402
+    
403
+    ##@brief Translate a module fullname to the context equivalent
404
+    #@param module_fullname str : a module fullname
405
+    #@return The module name in the current context
406
+    def _translate(self, module_fullname):
407
+        if not module_fullname.startswith('lodel'):
408
+            raise ContextModuleError("Given module is not lodel or any \
409
+submodule : '%s'" % module_fullname)
410
+        return module_fullname.replace('lodel', self.__pkg_name)
411
+
404
     ##@brief Implements the with statement behavior
412
     ##@brief Implements the with statement behavior
413
+    #@see https://www.python.org/dev/peps/pep-0343/
414
+    #@see https://wiki.python.org/moin/WithStatement
405
     def __enter__(self):
415
     def __enter__(self):
416
+        if not self.multisite:
417
+            warnings.warn("Using LodelContext with with statement in \
418
+MONOSITE mode")
406
         if self.__previous_ctx is not None:
419
         if self.__previous_ctx is not None:
407
             raise ContextError("__enter__ called but a previous context \
420
             raise ContextError("__enter__ called but a previous context \
408
 is allready registered !!! Bailout")
421
 is allready registered !!! Bailout")
414
         return self
427
         return self
415
 
428
 
416
     ##@brief Implements the with statement behavior
429
     ##@brief Implements the with statement behavior
430
+    #@see https://www.python.org/dev/peps/pep-0343/
431
+    #@see https://wiki.python.org/moin/WithStatement
417
     def __exit__(self, exc_type, exc_val, exc_tb):
432
     def __exit__(self, exc_type, exc_val, exc_tb):
418
         prev = self.__previous_ctx
433
         prev = self.__previous_ctx
419
         self.__previous_ctx = None
434
         self.__previous_ctx = None

Loading…
Cancel
Save