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.

lodel_admin.py 4.6KB

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