goa/goavreg.py
changeset 0 b97547f5f1fa
child 9 1901fcf55ed4
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """goa specific registry
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 from os import listdir
       
    10 from os.path import join, isdir
       
    11 
       
    12 from cubicweb import CW_SOFTWARE_ROOT
       
    13 from cubicweb.cwvreg import CubicWebRegistry
       
    14 
       
    15 
       
    16 def _pkg_name(cube, module):
       
    17     if cube is None:
       
    18         return module
       
    19     return '%s.%s' % (cube, module)
       
    20 
       
    21 class GAERegistry(CubicWebRegistry):
       
    22     
       
    23     def set_schema(self, schema):
       
    24         """disable reload hooks of cubicweb registry set_schema method"""
       
    25         self.schema = schema
       
    26 
       
    27     def load(self, applroot):
       
    28         from cubicweb.goa import db
       
    29         self.load_module(db) # AnyEntity class
       
    30         # explicit loading, we don't want to load __init__.py
       
    31         self.load_directory(join(CW_SOFTWARE_ROOT, 'entities'),
       
    32                             'cubicweb.entities', skip=('__init__.py',))
       
    33         self.load_directory(join(CW_SOFTWARE_ROOT, 'web', 'views'),
       
    34                             'cubicweb.web.views')
       
    35         self.load_directory(join(CW_SOFTWARE_ROOT, 'goa', 'appobjects'),
       
    36                             'cubicweb.goa.appobjects')
       
    37         for cube in reversed(self.config.cubes()):
       
    38             self.load_cube(cube)
       
    39         self.load_application(applroot)
       
    40         
       
    41     def load_directory(self, directory, cube, skip=()):
       
    42         for filename in listdir(directory):
       
    43             if filename[-3:] == '.py' and not filename in skip:
       
    44                 self._import('%s.%s' % (cube, filename[:-3]))
       
    45 
       
    46     def load_cube(self, cube):
       
    47         self._auto_load(self.config.cube_dir(cube),
       
    48                         cube in self.config['included-cubes'],
       
    49                         cube)
       
    50 
       
    51     def load_application(self, applroot):
       
    52         self._auto_load(applroot, self.config['schema-type'] == 'dbmodel')
       
    53 
       
    54     def _import(self, modname):
       
    55         obj = __import__(modname)
       
    56         for attr in modname.split('.')[1:]:
       
    57             obj = getattr(obj, attr)
       
    58         self.load_module(obj)
       
    59 
       
    60     def _auto_load(self, path, loadschema, cube=None):
       
    61         vobjpath = self.config.cube_vobject_path
       
    62         for filename in listdir(path):
       
    63             if filename[-3:] == '.py' and filename[:-3] in vobjpath:
       
    64                 self._import(_pkg_name(cube, filename[:-3]))
       
    65             else:
       
    66                 abspath = join(path, filename)
       
    67                 if isdir(abspath) and filename in vobjpath:
       
    68                     self.load_directory(abspath, _pkg_name(cube, filename))
       
    69         if loadschema:
       
    70             # when using db.Model defined schema, the defined class is used as
       
    71             # entity class as well and so have to be registered
       
    72             self._import(_pkg_name(cube, 'schema'))
       
    73 
       
    74 
       
    75