Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

lodel_admin.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #-*- coding: utf-8 -*-
  2. import sys
  3. import os, os.path
  4. import argparse
  5. ##@brief Dirty hack to avoid problems with simlink to lodel2 lib folder
  6. #
  7. #In instance folder we got a loader.py (the one we want to import here when
  8. #writing "import loader". The problem is that lodel_admin.py is a simlink to
  9. #LODEL2LIB_FOLDER/install/lodel_admin.py . In this folder there is the
  10. #generic loader.py template. And when writing "import loader" its
  11. #LODEL2LIB_FOLDER/install/loader.py that gets imported.
  12. #
  13. #In order to solve this problem the _simlink_hack() function delete the
  14. #LODEL2LIB_FOLDER/install entry from sys.path
  15. #@note This problem will be solved once lodel lib dir will be in
  16. #/usr/lib/python3/lodel
  17. def _simlink_hack():
  18. sys.path[0] = os.getcwd()
  19. ## @brief Utility method to generate python code given an emfile and a
  20. # translator
  21. # @param model_file str : An em file
  22. # @param translator str : a translator name
  23. # @return python code as string
  24. def generate_dyncode(model_file, translator):
  25. from lodel.editorial_model.model import EditorialModel
  26. from lodel.leapi import lefactory
  27. model = EditorialModel.load(translator, filename = model_file)
  28. dyncode = lefactory.dyncode_from_em(model)
  29. return dyncode
  30. ## @brief Utility method to generate a python file representing leapi dyncode
  31. # given an em file and the associated translator name
  32. #
  33. # @param model_file str : An em file
  34. # @param translator str : a translator name
  35. # @param output_filename str : the output file
  36. def create_dyncode(model_file, translator, output_filename):
  37. from lodel import logger
  38. dyncode = generate_dyncode(model_file, translator)
  39. with open(output_filename, 'w+') as out_fd:
  40. out_fd.write(dyncode)
  41. out_fd.close()
  42. logger.info("Dynamic leapi code written in %s", output_filename)
  43. ## @brief Refresh dynamic leapi code from settings
  44. def refresh_dyncode():
  45. import loader
  46. from lodel.settings import Settings
  47. # EditorialModel update/refresh
  48. # TODO
  49. # Dyncode refresh
  50. create_dyncode( Settings.editorialmodel.emfile,
  51. Settings.editorialmodel.emtranslator,
  52. Settings.editorialmodel.dyncode)
  53. def init_all_dbs():
  54. import loader
  55. loader.start()
  56. import leapi_dyncode as dyncode
  57. from lodel.plugin.datasource_plugin import DatasourcePlugin
  58. from lodel.settings.utils import SettingsError
  59. from lodel.leapi.leobject import LeObject
  60. from lodel.plugin import Plugin
  61. from lodel import logger
  62. ds_cls = dict() # EmClass indexed by rw_datasource
  63. for cls in dyncode.dynclasses:
  64. ds = cls._datasource_name
  65. if ds not in ds_cls:
  66. ds_cls[ds] = [cls]
  67. else:
  68. ds_cls[ds].append(cls)
  69. for ds_name in ds_cls:
  70. mh = DatasourcePlugin.init_migration_handler(ds_name)
  71. #Retrieve plugin_name & ds_identifier for logging purpose
  72. plugin_name, ds_identifier = DatasourcePlugin.plugin_name(
  73. ds_name, False)
  74. try:
  75. mh.init_db(ds_cls[ds_name])
  76. except Exception as e:
  77. msg = "Migration failed for datasource %s(%s.%s) when running \
  78. init_db method: %s"
  79. msg %= (ds_name, plugin_name, ds_identifier, e)
  80. logger.info("Database initialisation done for %s(%s.%s)" % (
  81. ds_name, plugin_name, ds_identifier))
  82. def list_registered_hooks():
  83. import loader
  84. loader.start()
  85. from lodel.plugin.hooks import LodelHook
  86. hlist = LodelHook.hook_list()
  87. print("Registered hooks are : ")
  88. for name in sorted(hlist.keys()):
  89. print("\t- %s is registered by : " % name)
  90. for reg_hook in hlist[name]:
  91. hook, priority = reg_hook
  92. msg = "\t\t- {modname}.{funname} with priority : {priority}"
  93. msg = msg.format(
  94. modname = hook.__module__,
  95. funname = hook.__name__,
  96. priority = priority)
  97. print(msg)
  98. print("\n")
  99. ##@brief update plugin's discover cache
  100. #@note impossible to give arguments from a Makefile...
  101. #@todo write a __main__ to be able to run ./lodel_admin
  102. def update_plugin_discover_cache(path_list = None):
  103. os.environ['LODEL2_NO_SETTINGS_LOAD'] = 'True'
  104. import loader
  105. from lodel.plugin.plugins import Plugin
  106. res = Plugin.discover(path_list)
  107. print("Plugin discover result in %s :\n" % res['path_list'])
  108. for pname, pinfos in res['plugins'].items():
  109. print("\t- %s %s -> %s" % (
  110. pname, pinfos['version'], pinfos['path']))
  111. if __name__ == '__main__':
  112. _simlink_hack()
  113. import loader
  114. loader.start()
  115. from lodel.plugin.scripts import main_run
  116. main_run()