29 |
29 |
30 from cubicweb import CW_SOFTWARE_ROOT, set_log_methods |
30 from cubicweb import CW_SOFTWARE_ROOT, set_log_methods |
31 from cubicweb import RegistryNotFound, ObjectNotFound, NoSelectableObject |
31 from cubicweb import RegistryNotFound, ObjectNotFound, NoSelectableObject |
32 |
32 |
33 |
33 |
34 def _toload_info(path, _toload=None): |
34 def _toload_info(path, extrapath, _toload=None): |
35 """return a dictionary of <modname>: <modpath> and an ordered list of |
35 """return a dictionary of <modname>: <modpath> and an ordered list of |
36 (file, module name) to load |
36 (file, module name) to load |
37 """ |
37 """ |
38 from logilab.common.modutils import modpath_from_file |
38 from logilab.common.modutils import modpath_from_file |
39 if _toload is None: |
39 if _toload is None: |
|
40 assert isinstance(path, list) |
40 _toload = {}, [] |
41 _toload = {}, [] |
41 for fileordir in path: |
42 for fileordir in path: |
42 if isdir(fileordir) and exists(join(fileordir, '__init__.py')): |
43 if isdir(fileordir) and exists(join(fileordir, '__init__.py')): |
43 subfiles = [join(fileordir, fname) for fname in listdir(fileordir)] |
44 subfiles = [join(fileordir, fname) for fname in listdir(fileordir)] |
44 _toload_info(subfiles, _toload) |
45 _toload_info(subfiles, extrapath, _toload) |
45 elif fileordir[-3:] == '.py': |
46 elif fileordir[-3:] == '.py': |
46 modname = '.'.join(modpath_from_file(fileordir)) |
47 modname = '.'.join(modpath_from_file(fileordir, extrapath)) |
47 _toload[0][modname] = fileordir |
48 _toload[0][modname] = fileordir |
48 _toload[1].append((fileordir, modname)) |
49 _toload[1].append((fileordir, modname)) |
49 return _toload |
50 return _toload |
50 |
51 |
51 |
52 |
311 """return the most specific component according to the resultset""" |
312 """return the most specific component according to the resultset""" |
312 return self.select(self.registry_objects(registry, cid), *args, **kwargs) |
313 return self.select(self.registry_objects(registry, cid), *args, **kwargs) |
313 |
314 |
314 # intialization methods ################################################### |
315 # intialization methods ################################################### |
315 |
316 |
316 def init_registration(self, path): |
317 def init_registration(self, path, extrapath): |
317 # compute list of all modules that have to be loaded |
318 # compute list of all modules that have to be loaded |
318 self._toloadmods, filemods = _toload_info(path) |
319 self._toloadmods, filemods = _toload_info(path, extrapath) |
319 self._loadedmods = {} |
320 self._loadedmods = {} |
320 return filemods |
321 return filemods |
321 |
322 |
322 def register_objects(self, path, force_reload=None): |
323 def register_objects(self, path, force_reload=None, extrapath=None): |
323 if force_reload is None: |
324 if force_reload is None: |
324 force_reload = self.config.mode == 'dev' |
325 force_reload = self.config.mode == 'dev' |
325 elif not force_reload: |
326 elif not force_reload: |
326 # force_reload == False usually mean modules have been reloaded |
327 # force_reload == False usually mean modules have been reloaded |
327 # by another connection, so we want to update the registry |
328 # by another connection, so we want to update the registry |
337 if webdir in sys.path: |
338 if webdir in sys.path: |
338 sys.path.remove(webdir) |
339 sys.path.remove(webdir) |
339 if CW_SOFTWARE_ROOT in sys.path: |
340 if CW_SOFTWARE_ROOT in sys.path: |
340 sys.path.remove(CW_SOFTWARE_ROOT) |
341 sys.path.remove(CW_SOFTWARE_ROOT) |
341 # load views from each directory in the application's path |
342 # load views from each directory in the application's path |
342 filemods = self.init_registration(path) |
343 filemods = self.init_registration(path, extrapath) |
343 change = False |
344 change = False |
344 for filepath, modname in filemods: |
345 for filepath, modname in filemods: |
345 if self.load_file(filepath, modname, force_reload): |
346 if self.load_file(filepath, modname, force_reload): |
346 change = True |
347 change = True |
347 return change |
348 return change |