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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. from loader import *
  3. import warnings
  4. def refreshdyn():
  5. import sys
  6. from EditorialModel.model import Model
  7. from leapi.lefactory import LeFactory
  8. from EditorialModel.backend.json_backend import EmBackendJson
  9. from DataSource.MySQL.leapidatasource import LeDataSourceSQL
  10. OUTPUT = Settings.dynamic_code_file
  11. EMJSON = Settings.em_file
  12. # Load editorial model
  13. em = Model(EmBackendJson(EMJSON))
  14. # Generate dynamic code
  15. fact = LeFactory(OUTPUT)
  16. # Create the python file
  17. fact.create_pyfile(em, LeDataSourceSQL, {})
  18. def db_init():
  19. from EditorialModel.backend.json_backend import EmBackendJson
  20. from EditorialModel.model import Model
  21. mh = getattr(migrationhandler,Settings.mh_classname)()
  22. em = Model(EmBackendJson(Settings.em_file))
  23. em.migrate_handler(mh)
  24. def em_graph(output_file = None, image_format = None):
  25. from EditorialModel.model import Model
  26. from EditorialModel.backend.json_backend import EmBackendJson
  27. from EditorialModel.backend.graphviz import EmBackendGraphviz
  28. import subprocess
  29. if image_format is None:
  30. if hasattr(Settings, 'em_graph_format'):
  31. image_format = Settings.em_graph_format
  32. else:
  33. image_format = 'png'
  34. if output_file is None:
  35. if hasattr(Settings, 'em_graph_output'):
  36. output_file = Settings.em_graph_output
  37. else:
  38. output_file = '/tmp/em_%s_graph.dot'
  39. image_format = image_format.lower()
  40. try:
  41. output_file = output_file%Settings.sitename
  42. except TypeError:
  43. warnings.warn("Bad filename for em_graph output. The filename should be in the form '/foo/bar/file_%s_name.png")
  44. pass
  45. dot_file = output_file+".dot"
  46. graphviz_bckend = EmBackendGraphviz(dot_file)
  47. edmod = Model(EmBackendJson(Settings.em_file))
  48. graphviz_bckend.save(edmod)
  49. dot_cmd = [
  50. "dot",
  51. "-T%s"%image_format,
  52. dot_file
  53. ]
  54. with open(output_file, "w+") as outfp:
  55. subprocess.check_call(dot_cmd, stdout=outfp)
  56. os.unlink(dot_file)
  57. print("Output image written in file : '%s'" % output_file)
  58. def dir_init():
  59. import os
  60. # Templates
  61. print("Creating Base Templates ...")
  62. templates = {
  63. 'base': {'file': 'base.html', 'content': '{% extends "templates/base.html" %}'},
  64. 'base_backend': {'file': 'base_backend.html', 'content': '{% extends "templates/base_backend.html" %}'}
  65. }
  66. current_directory = os.path.dirname(os.path.abspath(__file__))
  67. templates_directory = os.path.join(current_directory,'templates')
  68. if not os.path.exists(templates_directory):
  69. os.makedirs(templates_directory)
  70. for _, template in templates.items():
  71. my_file_path = os.path.join(templates_directory, template['file'])
  72. if os.path.exists(my_file_path):
  73. os.unlink(my_file_path)
  74. with open(my_file_path, 'w') as my_file:
  75. my_file.write(template['content'])
  76. print("Created %s" % my_file_path)