Browse Source

Add the possibility to indicate an alias when exposing a list of objects

Changed the exposure specs to allow giving an alias for an object when importing using the from x import y,z form
Yann Weber 8 years ago
parent
commit
1257e49de6
1 changed files with 10 additions and 2 deletions
  1. 10
    2
      lodel/context.py

+ 10
- 2
lodel/context.py View File

@@ -149,10 +149,14 @@ length == 2 but got : %s" % spec)
149 149
         errors = []
150 150
         module = importlib.import_module(fullname)
151 151
         for o_name in objects:
152
+            if isinstance(o_name, str):
153
+                alias = o_name
154
+            else:
155
+                o_name, alias = o_name
152 156
             if not hasattr(module, o_name):
153 157
                 errors.append(o_name)
154 158
             else:
155
-                cls.safe_exposure(globs, getattr(module, o_name), o_name)
159
+                cls.safe_exposure(globs, getattr(module, o_name), alias)
156 160
         if len(errors) > 0:
157 161
             msg = "Module %s does not have any of [%s] as attribute" % (
158 162
                 fullname, ','.join(errors))
@@ -204,8 +208,12 @@ submodule : '%s'" % module_fullname)
204 208
     #In this case items of specs is a string representing the alias name
205 209
     #for the module we are exposing
206 210
     #@par from x import i,j,k equivalent
207
-    #In this case items are lists of object name to expose as it in globals
211
+    #In this case items are lists of object name to expose as it in globals.
212
+    #You can specify an alias by giving a tuple instead of a string as 
213
+    #list element. In this case the first element of the tuple is the object
214
+    #name and the second it's alias in the globals
208 215
     #
216
+    #@todo make the specs format more consitant
209 217
     #@param cls : bultin params
210 218
     #@param globs dict : the globals dict of the caller module
211 219
     #@param specs dict : specs of exposure (see comments of this method)

Loading…
Cancel
Save