goa/goavreg.py
changeset 6366 1806148d6ce8
parent 6333 e3994fcc21c3
parent 6365 a15cc5e16178
child 6367 d4c485ec1ca1
equal deleted inserted replaced
6333:e3994fcc21c3 6366:1806148d6ce8
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """goa specific registry
       
    19 
       
    20 """
       
    21 __docformat__ = "restructuredtext en"
       
    22 
       
    23 from os import listdir
       
    24 from os.path import join, isdir
       
    25 
       
    26 from cubicweb import CW_SOFTWARE_ROOT
       
    27 from cubicweb.cwvreg import CubicWebVRegistry
       
    28 
       
    29 
       
    30 def _pkg_name(cube, module):
       
    31     if cube is None:
       
    32         return module
       
    33     return 'cubes.%s.%s' % (cube, module)
       
    34 
       
    35 class GAEVRegistry(CubicWebVRegistry):
       
    36 
       
    37     def set_schema(self, schema):
       
    38         """disable reload hooks of cubicweb registry set_schema method"""
       
    39         self.schema = schema
       
    40 
       
    41     def load(self, applroot):
       
    42         from cubicweb.goa import db
       
    43         self.load_module(db) # AnyEntity class
       
    44         # explicit loading, we don't want to load __init__.py
       
    45         self.load_directory(join(CW_SOFTWARE_ROOT, 'entities'),
       
    46                             'cubicweb.entities', skip=('__init__.py',))
       
    47         self.load_directory(join(CW_SOFTWARE_ROOT, 'web', 'views'),
       
    48                             'cubicweb.web.views')
       
    49         self.load_directory(join(CW_SOFTWARE_ROOT, 'goa', 'appobjects'),
       
    50                             'cubicweb.goa.appobjects')
       
    51         for cube in reversed(self.config.cubes()):
       
    52             self.load_cube(cube)
       
    53         self.load_instance(applroot)
       
    54 
       
    55     def load_directory(self, directory, cube, skip=()):
       
    56         for filename in listdir(directory):
       
    57             if filename[-3:] == '.py' and not filename in skip:
       
    58                 self._import('%s.%s' % (cube, filename[:-3]))
       
    59 
       
    60     def load_cube(self, cube):
       
    61         self._auto_load(self.config.cube_dir(cube),
       
    62                         cube in self.config['included-cubes'],
       
    63                         cube)
       
    64 
       
    65     def load_instance(self, applroot):
       
    66         self._auto_load(applroot, self.config['schema-type'] == 'dbmodel')
       
    67 
       
    68     def _import(self, modname):
       
    69         obj = __import__(modname)
       
    70         for attr in modname.split('.')[1:]:
       
    71             obj = getattr(obj, attr)
       
    72         self.load_module(obj)
       
    73 
       
    74     def _auto_load(self, path, loadschema, cube=None):
       
    75         vobjpath = self.config.cube_appobject_path
       
    76         for filename in listdir(path):
       
    77             if filename[-3:] == '.py' and filename[:-3] in vobjpath:
       
    78                 self._import(_pkg_name(cube, filename[:-3]))
       
    79             else:
       
    80                 abspath = join(path, filename)
       
    81                 if isdir(abspath) and filename in vobjpath:
       
    82                     self.load_directory(abspath, _pkg_name(cube, filename))
       
    83         if loadschema:
       
    84             # when using db.Model defined schema, the defined class is used as
       
    85             # entity class as well and so have to be registered
       
    86             self._import(_pkg_name(cube, 'schema'))