No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import pkgutil
  2. import os
  3. from lodel.editorial_model.model import EditorialModel
  4. from lodel.leapi import lefactory
  5. LODEL_COMMANDS_PATH = os.path.join(__package__, "commands")
  6. def register_commands(manager):
  7. for _, modulename, _ in pkgutil.iter_modules([LODEL_COMMANDS_PATH]):
  8. command_module = __import__("%s.%s" % (LODEL_COMMANDS_PATH.replace("/","."),modulename), fromlist=['LodelCommand'])
  9. manager.add_command(modulename, getattr(command_module, 'LodelCommand')())
  10. ##
  11. # @todo move this method in the corresponding module and change its calls in the corresponding commands
  12. def generate_dyncode(model_file, translator):
  13. model = EditorialModel.load(translator, filename=model_file)
  14. dyncode = lefactory.dyncode_from_em(model)
  15. return dyncode
  16. def init_all_datasources(dyncode):
  17. from lodel.plugin.datasource_plugin import DatasourcePlugin
  18. from lodel import logger
  19. ds_cls = dict() # EmClass indexed by rw_datasource
  20. for cls in dyncode.dynclasses:
  21. ds = cls._datasource_name
  22. if ds not in ds_cls:
  23. ds_cls[ds] = [cls]
  24. else:
  25. ds_cls[ds].append(cls)
  26. for ds_name in ds_cls:
  27. mh = DatasourcePlugin.init_migration_handler(ds_name)
  28. #Retrieve plugin_name & ds_identifier for logging purpose
  29. plugin_name, ds_identifier = DatasourcePlugin.plugin_name(
  30. ds_name, False)
  31. try:
  32. mh.init_db(ds_cls[ds_name])
  33. except Exception as e:
  34. msg = "Migration failed for datasource %s(%s.%s) when running \
  35. init_db method: %s"
  36. msg %= (ds_name, plugin_name, ds_identifier, e)
  37. logger.info("Database initialisation done for %s(%s.%s)" % (
  38. ds_name, plugin_name, ds_identifier))
  39. def list_registered_hooks():
  40. from lodel.plugin.hooks import LodelHook
  41. from lodel.plugin.plugins import Plugin
  42. Plugin.load_all()
  43. hlist = LodelHook.hook_list()
  44. print("Registered hooks are : ")
  45. for name in sorted(hlist.keys()):
  46. print("\t- %s is registered by : " % name)
  47. for reg_hook in hlist[name]:
  48. hook, priority = reg_hook
  49. msg = "\t\t- {modname}.{funname} with priority : {priority}"
  50. msg = msg.format(
  51. modname = hook.__module__,
  52. funname = hook.__name__,
  53. priority = priority)
  54. print(msg)
  55. print("\n")
  56. ##@brief update plugin's discover cache
  57. def update_plugin_discover_cache(path_list = None):
  58. os.environ['LODEL2_NO_SETTINGS_LOAD'] = 'True'
  59. from lodel.plugin.plugins import Plugin
  60. res = Plugin.discover(path_list)
  61. print("Plugin discover result in %s :\n" % res['path_list'])
  62. for pname, pinfos in res['plugins'].items():
  63. print("\t- %s %s -> %s" % (
  64. pname, pinfos['version'], pinfos['path']))