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.

loader.py 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. import jinja2
  3. import os
  4. from lodel.settings import Settings
  5. from ...client import WebUiClient as WebUiClient
  6. import leapi_dyncode
  7. from .api import api_lodel_templates
  8. from .exceptions.not_allowed_custom_api_key_error import NotAllowedCustomAPIKeyError
  9. from ...main import root_url as root_url
  10. from ...main import PLUGIN_PATH
  11. TEMPLATE_PATH = os.path.realpath(os.path.join(PLUGIN_PATH, 'templates/'))
  12. class TemplateLoader(object):
  13. _reserved_template_keys = ['lodel']
  14. ## @brief Initializes a template loader
  15. #
  16. # @param search_path str : the base path from which the templates are searched. To use absolute paths, you can set
  17. # it to the root "/". By default, it will be the root of the project, defined in the settings of the application.
  18. # @param follow_links bool : indicates whether or not to follow the symbolic links (default: True)
  19. # @param is_cache_active bool : indicates whether or not the cache should be activated or not (default: True)
  20. # @todo connect this to the new settings system
  21. def __init__(self, search_path=TEMPLATE_PATH, follow_links=True, is_cache_active=True):
  22. self.search_path = search_path
  23. self.follow_links = follow_links
  24. self.is_cache_active = is_cache_active
  25. ## @brief Renders a HTML content of a template
  26. #
  27. # @see template.loader.TemplateLoader.render_to_response
  28. #
  29. # @return str. String containing the HTML output of the processed templated
  30. def render_to_html(self, template_file, template_vars={}, template_extra=None):
  31. loader = jinja2.FileSystemLoader(searchpath=self.search_path)
  32. environment = jinja2.Environment(loader=loader) if self.is_cache_active else jinja2.Environment(loader=loader,
  33. cache_size=0)
  34. template = environment.get_template(template_file)
  35. # lodel2 default api is loaded
  36. # TODO change this if needed
  37. template.globals['lodel'] = api_lodel_templates
  38. template.globals['leapi'] = leapi_dyncode
  39. template.globals['settings'] = Settings
  40. template.globals['client'] = WebUiClient
  41. template.globals['root_url'] = root_url()
  42. template.globals['url'] = lambda sufix='': root_url()\
  43. + ('' if sufix.startswith('/') else '/')\
  44. + sufix
  45. # Extra modules are loaded
  46. if template_extra is not None:
  47. for extra in template_extra:
  48. if not self._is_allowed_template_key(extra[0]):
  49. raise NotAllowedCustomAPIKeyError("The name '%s' is a reserved one for the loaded APIs in "
  50. "templates" % extra[0])
  51. template.globals[extra[0]] = extra[1]
  52. return template.render(template_vars)
  53. ## @brief Renders a template into an encoded form ready to be sent to a wsgi response
  54. #
  55. # @param template_file str : path to the template file (starting from the base path used to instanciate the
  56. # TemplateLoader)
  57. # @param template_vars dict : parameters to be used in the template
  58. # @param template_extra list : list of tuples indicating the custom modules to import in the template
  59. # (default: None).
  60. #
  61. # The modules are given as tuples with the format : ('name_to_use_in_the_template', module)
  62. #
  63. # @return str
  64. def render_to_response(self, template_file, template_vars={}, template_extra=None):
  65. return self.render_to_html(template_file=template_file, template_vars=template_vars,
  66. template_extra=template_extra).encode()
  67. ## @brief Checks if the key used for the template is allowed
  68. #
  69. # @param key str
  70. # @return bool
  71. def _is_allowed_template_key(self, key):
  72. return False if key in self.__class__.__reserved_template_keys else True