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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. import warnings
  3. import sys
  4. # Startup
  5. #
  6. # Loading instance config to fetch lodel2 lib path
  7. # @TODO migrate this portion of code in a bootstrap.py file
  8. import instance_settings
  9. sys.path.append(instance_settings.lodel2_lib_path)
  10. # Load Lodel2 settings module
  11. from Lodel.settings import Settings
  12. # Settings initialisation
  13. Settings.load_module(instance_settings)
  14. globals()['Settings'] = Settings
  15. ## @brief fetch full module name of dynamic code
  16. # @warning hardcoded names are used in loader.py and in Makefile
  17. # @param name str : can be 'leapi' or 'aclapi'
  18. def dyn_module_fullname(name):
  19. if name == 'leapi':
  20. return 'dyncode.internal_api'
  21. else:
  22. return 'dyncode.acl_api'
  23. ## @brief fetch dynamic codefile path
  24. # @warning hardcoded names are used in loader.py and in Makefile
  25. # @param name str : can be 'leapi' or 'aclapi'
  26. def dyn_code_filename(name):
  27. if name == 'leapi':
  28. return 'dyncode/internal_api.py'
  29. else:
  30. return 'dyncode/acl_api.py'
  31. ## @brief Refresh dynamic code
  32. #
  33. # Generate dynamic leapi code and ACL wrapper for dynamic leapi classes
  34. def refreshdyn():
  35. import sys
  36. import os, os.path
  37. from EditorialModel.model import Model
  38. from leapi.lefactory import LeFactory
  39. from EditorialModel.backend.json_backend import EmBackendJson
  40. from DataSource.MySQL.leapidatasource import LeDataSourceSQL
  41. from acl.factory import AclFactory
  42. EMJSON = Settings.em_file
  43. # Load editorial model
  44. em = Model(EmBackendJson(EMJSON))
  45. # Generate dynamic code
  46. fact = LeFactory(dyn_code_filename('leapi'))
  47. # Create the python file
  48. fact.create_pyfile(em, LeDataSourceSQL, {})
  49. # Generate wrapped API
  50. fact = AclFactory(dyn_code_filename('aclapi'))
  51. fact.create_pyfile(em, dyn_module_fullname('leapi'))
  52. def db_init():
  53. import loader
  54. from loader import migrationhandler
  55. from EditorialModel.backend.json_backend import EmBackendJson
  56. from EditorialModel.model import Model
  57. mh = getattr(migrationhandler,Settings.mh_classname)()
  58. em = Model(EmBackendJson(Settings.em_file))
  59. em.migrate_handler(mh)
  60. ## @brief Generate a graphviz representation of instance's editorial model
  61. # @param output_file str : output filename
  62. # @param image_format str : takes value in png, jpg, svg (or any accepted graphviz output format)
  63. def em_graph(output_file = None, image_format = None):
  64. import loader
  65. from EditorialModel.model import Model
  66. from EditorialModel.backend.json_backend import EmBackendJson
  67. from EditorialModel.backend.graphviz import EmBackendGraphviz
  68. import subprocess
  69. if image_format is None:
  70. if hasattr(Settings, 'em_graph_format'):
  71. image_format = Settings.em_graph_format
  72. else:
  73. image_format = 'png'
  74. if output_file is None:
  75. if hasattr(Settings, 'em_graph_output'):
  76. output_file = Settings.em_graph_output
  77. else:
  78. output_file = '/tmp/em_%s_graph.dot'
  79. image_format = image_format.lower()
  80. try:
  81. output_file = output_file%Settings.sitename
  82. except TypeError:
  83. warnings.warn("Bad filename for em_graph output. The filename should be in the form '/foo/bar/file_%s_name.png")
  84. pass
  85. dot_file = output_file+".dot"
  86. graphviz_bckend = EmBackendGraphviz(dot_file)
  87. edmod = Model(EmBackendJson(Settings.em_file))
  88. graphviz_bckend.save(edmod)
  89. dot_cmd = [
  90. "dot",
  91. "-T%s"%image_format,
  92. dot_file
  93. ]
  94. with open(output_file, "w+") as outfp:
  95. subprocess.check_call(dot_cmd, stdout=outfp)
  96. os.unlink(dot_file)
  97. print("Output image written in file : '%s'" % output_file)
  98. def dir_init():
  99. import os
  100. # Templates
  101. print("Creating Base Templates ...")
  102. templates = {
  103. 'base': {'file': 'base.html', 'content': '{% extends "templates/base.html" %}'},
  104. 'base_backend': {'file': 'base_backend.html', 'content': '{% extends "templates/base_backend.html" %}'}
  105. }
  106. current_directory = os.path.dirname(os.path.abspath(__file__))
  107. templates_directory = os.path.join(current_directory,'templates')
  108. if not os.path.exists(templates_directory):
  109. os.makedirs(templates_directory)
  110. for _, template in templates.items():
  111. my_file_path = os.path.join(templates_directory, template['file'])
  112. if os.path.exists(my_file_path):
  113. os.unlink(my_file_path)
  114. with open(my_file_path, 'w') as my_file:
  115. my_file.write(template['content'])
  116. print("Created %s" % my_file_path)