render.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import pathlib
  2. class render:
  3. '''
  4. This class is responsible for rendering files, such as HTML files to
  5. result. It could replace given formats with given values.
  6. '''
  7. def __init__(self, file: pathlib.Path):
  8. '''
  9. This is default render builder. It require handle to file which
  10. would be rendered.
  11. Parameters:
  12. file (Path): This is handle to file
  13. Returns:
  14. (render): New render object
  15. '''
  16. self.__list = dict()
  17. self.__cache = None
  18. self.__start_tag = ">>"
  19. self.__stop_tag = "<<"
  20. try:
  21. with file.open() as handle:
  22. self.__content = handle.read()
  23. except:
  24. Exception("Can not open file to render.")
  25. def add(self, name: str, content: str) -> None:
  26. '''
  27. This add new replace to the render. That mean all places, where
  28. special tags contains name, would be replaced with content.
  29. Parameters:
  30. name (str): Name of the parameter
  31. content (str): Content to replace when render
  32. Returns:
  33. (None)
  34. '''
  35. if name in self.__list:
  36. raise Exception("Parameter " + name + " had been already set.")
  37. self.__list[name] = content
  38. self.__clean_cache()
  39. def __clean_cache(self):
  40. ''' This clean render cache. '''
  41. self.__cache = None
  42. def validate(self, name: str) -> bool:
  43. '''
  44. This check that given name is valid, that mean not contain any
  45. blocked chars, and white chars.
  46. Parameters:
  47. name (str): Name to validate
  48. Returns:
  49. (bool): True if valid, or False when not
  50. '''
  51. blocked = ["'", "\\", "\""]
  52. for char in blocked:
  53. if char in name:
  54. return False
  55. for letter in name:
  56. if letter.isspace():
  57. return False
  58. return True
  59. @property
  60. def start_tag(self):
  61. ''' This return tag which start replace pattern. '''
  62. return self.__start_tag
  63. @property
  64. def stop_tag(self):
  65. ''' This return stop tag which end pattern. '''
  66. return self.__stop_tag
  67. @start_tag.setter
  68. def start_tag(self, tag: str) -> None:
  69. ''' This set new start tag. '''
  70. if not self.validate(tag):
  71. raise Exception("Start tag contain bad chars.")
  72. self.__start_tag = tag
  73. @stop_tag.setter
  74. def stop_tag(self, tag: str) -> None:
  75. ''' This set new stop tag. '''
  76. if not self.validate(tag):
  77. raise Exception("Stop tag can not contain blocked chars.")
  78. self.__stop_tag = tag
  79. def get(self, name: str) -> str:
  80. '''
  81. This retunr content set to given parameter.
  82. Parameters:
  83. name (str): Name of the parameter
  84. Returns:
  85. (str): Content of the parameter or blank string
  86. '''
  87. if name in self.__list:
  88. return self.__list[name]
  89. return str()
  90. def finalize(self) -> str:
  91. '''
  92. This function render given documents, using parameters set before,
  93. and return finalized.
  94. Returns:
  95. (str): Finalized rendered document
  96. '''
  97. if self.__cache is not None:
  98. return self.__cache
  99. start_length = len(self.start_tag)
  100. stop_length = len(self.stop_tag)
  101. result = self.__content
  102. last = 0
  103. while True:
  104. start = result.find(self.start_tag, last)
  105. stop = result.find(self.stop_tag, last)
  106. if start == -1:
  107. break
  108. if stop == -1:
  109. break
  110. name = result[start + start_length + 1 : stop]
  111. name = name.strip()
  112. last = start
  113. content = self.get(name)
  114. result \
  115. = result[: start] \
  116. + content \
  117. + result[stop + stop_length :]
  118. self.__cache = result
  119. return self.__cache
  120. def __str__(self) -> str:
  121. ''' This is string converter, and alias of finalize. '''
  122. return self.finalize()