state.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # coding: utf-8
  2. import typing
  3. from _elementtree import Element
  4. from lxml import etree
  5. from synergine2.config import Config
  6. from synergine2.log import get_logger
  7. from opencombat.exception import StateLoadError
  8. from opencombat.exception import NotFoundError
  9. from opencombat.simulation.base import TileStrategySimulation
  10. from opencombat.simulation.subject import TileSubject
  11. from opencombat.util import get_class_from_string_path
  12. from opencombat.util import pretty_xml
  13. from opencombat.util import get_text_xml_element
  14. class State(object):
  15. def __init__(
  16. self,
  17. config: Config,
  18. state_root: Element,
  19. simulation: TileStrategySimulation,
  20. ) -> None:
  21. self._config = config
  22. self._state_root = state_root
  23. self._subjects = None # type: typing.List[TileSubject]
  24. self._simulation = simulation
  25. @property
  26. def subjects(self) -> typing.List[TileSubject]:
  27. if self._subjects is None:
  28. self._subjects = self._get_subjects()
  29. return self._subjects
  30. def _get_subjects(self) -> typing.List[TileSubject]:
  31. subjects = []
  32. subject_elements = self._state_root.find('subjects').findall('subject')
  33. for subject_element in subject_elements:
  34. subject_class_path = subject_element.find('type').text
  35. subject_class = get_class_from_string_path(
  36. self._config,
  37. subject_class_path,
  38. )
  39. subject = subject_class(self._config, self._simulation)
  40. self._fill_subject(subject, subject_element)
  41. # TODO BS 2018-06-13: Fill subject with property
  42. subjects.append(subject)
  43. return subjects
  44. def _fill_subject(
  45. self,
  46. subject: TileSubject,
  47. subject_element: Element,
  48. ) -> None:
  49. subject_properties = {}
  50. subject.position = tuple(
  51. map(
  52. int,
  53. get_text_xml_element(subject_element, 'position').split(','),
  54. ),
  55. )
  56. subject.direction = float(
  57. get_text_xml_element(subject_element, 'direction'),
  58. )
  59. properties_element = subject_element.find('properties')
  60. decode_properties_map = self._get_decode_properties_map()
  61. for item_element in properties_element.findall('item'):
  62. key_text = item_element.find('key').text
  63. value_text = item_element.find('value').text
  64. try:
  65. decoded_value = decode_properties_map[key_text](value_text)
  66. except KeyError:
  67. raise NotFoundError(
  68. 'You try to load property "{}" but it is unknown'.format(
  69. key_text,
  70. )
  71. )
  72. subject_properties[key_text] = decoded_value
  73. subject.properties = subject_properties
  74. def _get_decode_properties_map(self) -> typing.Dict[str, typing.Callable[[str], typing.Any]]: # nopep8
  75. return {
  76. 'SELECTION_COLOR_RGB': lambda v: tuple(map(int, v.split(','))),
  77. 'FLAG': str,
  78. 'SIDE': str,
  79. }
  80. class StateDumper(object):
  81. def __init__(
  82. self,
  83. config: Config,
  84. simulation: TileStrategySimulation,
  85. ) -> None:
  86. self._logger = get_logger('StateDumper', config)
  87. self._config = config
  88. self._simulation = simulation
  89. state_template = self._config.resolve(
  90. 'global.state_template',
  91. 'opencombat/state_template.xml',
  92. )
  93. with open(state_template, 'r') as xml_file:
  94. template_str = xml_file.read()
  95. parser = etree.XMLParser(remove_blank_text=True)
  96. self._state_root = etree.fromstring(
  97. template_str.encode('utf-8'),
  98. parser,
  99. )
  100. self._state_root_filled = False
  101. def get_state_dump(self) -> str:
  102. if not self._state_root_filled:
  103. self._fill_state_root()
  104. return pretty_xml(
  105. etree.tostring(
  106. self._state_root,
  107. ).decode('utf-8'),
  108. )
  109. def _fill_state_root(self) -> None:
  110. subjects_element = self._state_root.find('subjects')
  111. for subject in self._simulation.subjects:
  112. subject_element = etree.SubElement(subjects_element, 'subject')
  113. position_element = etree.SubElement(subject_element, 'type')
  114. position_element.text = '.'.join([
  115. subject.__module__,
  116. subject.__class__.__name__,
  117. ])
  118. position_element = etree.SubElement(subject_element, 'position')
  119. position_element.text = ','.join(map(str, subject.position))
  120. direction_element = etree.SubElement(subject_element, 'direction')
  121. direction_element.text = str(subject.direction)
  122. properties_element = etree.SubElement(
  123. subject_element,
  124. 'properties',
  125. )
  126. encode_properties_map = self._get_encode_properties_map()
  127. for key, value in subject.properties.items():
  128. item_element = etree.SubElement(properties_element, 'item')
  129. key_element = etree.SubElement(item_element, 'key')
  130. value_element = etree.SubElement(item_element, 'value')
  131. key_element.text = str(key)
  132. value_element.text = encode_properties_map[key](value)
  133. self._state_root_filled = True
  134. def _get_encode_properties_map(self) -> typing.Dict[str, typing.Callable[[typing.Any], str]]: # nopep8:
  135. return {
  136. 'SELECTION_COLOR_RGB': lambda v: ','.join(map(str, v)),
  137. 'FLAG': str,
  138. 'SIDE': str,
  139. }
  140. class StateLoader(object):
  141. def __init__(
  142. self,
  143. config: Config,
  144. simulation: TileStrategySimulation,
  145. ) -> None:
  146. self._logger = get_logger('StateLoader', config)
  147. self._config = config
  148. self._simulation = simulation
  149. def get_state(
  150. self,
  151. state_file_path: str,
  152. ) -> State:
  153. return State(
  154. self._config,
  155. self._validate_and_return_state_element(state_file_path),
  156. self._simulation,
  157. )
  158. def _validate_and_return_state_element(
  159. self,
  160. state_file_path: str,
  161. ) -> Element:
  162. # open and read schema file
  163. schema_file_path = self._config.get(
  164. 'global.state_schema',
  165. 'opencombat/state.xsd',
  166. )
  167. with open(schema_file_path, 'r') as schema_file:
  168. schema_to_check = schema_file.read()
  169. # open and read xml file
  170. with open(state_file_path, 'r') as xml_file:
  171. xml_to_check = xml_file.read()
  172. xmlschema_doc = etree.fromstring(schema_to_check.encode('utf-8'))
  173. xmlschema = etree.XMLSchema(xmlschema_doc)
  174. try:
  175. doc = etree.fromstring(xml_to_check.encode('utf-8'))
  176. # check for file IO error
  177. except IOError as exc:
  178. self._logger.error(exc)
  179. raise StateLoadError('Invalid File "{}": {}'.format(
  180. state_file_path,
  181. str(exc),
  182. ))
  183. # check for XML syntax errors
  184. except etree.XMLSyntaxError as exc:
  185. self._logger.error(exc)
  186. raise StateLoadError('XML Syntax Error in "{}": {}'.format(
  187. state_file_path,
  188. str(exc.error_log),
  189. ))
  190. except Exception as exc:
  191. self._logger.error(exc)
  192. raise StateLoadError('Unknown error with "{}": {}'.format(
  193. state_file_path,
  194. str(exc),
  195. ))
  196. # validate against schema
  197. try:
  198. xmlschema.assertValid(doc)
  199. except etree.DocumentInvalid as exc:
  200. self._logger.error(exc)
  201. raise StateLoadError(
  202. 'Schema validation error with "{}": {}'.format(
  203. state_file_path,
  204. str(exc),
  205. )
  206. )
  207. except Exception as exc:
  208. self._logger.error(exc)
  209. raise StateLoadError(
  210. 'Unknown validation error with "{}": {}'.format(
  211. state_file_path,
  212. str(exc),
  213. )
  214. )
  215. return doc
  216. class StateConstructorBuilder(object):
  217. def __init__(
  218. self,
  219. config: Config,
  220. simulation: TileStrategySimulation,
  221. ) -> None:
  222. self._logger = get_logger('StateConstructorBuilder', config)
  223. self._config = config
  224. self._simulation = simulation
  225. def get_state_loader(
  226. self,
  227. ) -> StateLoader:
  228. class_address = self._config.resolve(
  229. 'global.state_loader',
  230. 'opencombat.state.StateLoader',
  231. )
  232. state_loader_class = get_class_from_string_path(
  233. self._config,
  234. class_address,
  235. )
  236. return state_loader_class(
  237. self._config,
  238. self._simulation,
  239. )
  240. def get_state_dumper(
  241. self,
  242. ) -> StateDumper:
  243. class_address = self._config.resolve(
  244. 'global.state_dumper',
  245. 'opencombat.state.StateDumper',
  246. )
  247. state_loader_class = get_class_from_string_path(
  248. self._config,
  249. class_address,
  250. )
  251. return state_loader_class(
  252. self._config,
  253. self._simulation,
  254. )