state.py 9.2KB

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