state.py 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. map_element = self._state_root.find('map')
  111. map_name_element = etree.SubElement(map_element, 'name')
  112. map_name_element.text = self._config.resolve('_runtime.map_dir_path')
  113. for subject in self._simulation.subjects:
  114. subject_element = etree.SubElement(subjects_element, 'subject')
  115. position_element = etree.SubElement(subject_element, 'type')
  116. position_element.text = '.'.join([
  117. subject.__module__,
  118. subject.__class__.__name__,
  119. ])
  120. position_element = etree.SubElement(subject_element, 'position')
  121. position_element.text = ','.join(map(str, subject.position))
  122. direction_element = etree.SubElement(subject_element, 'direction')
  123. direction_element.text = str(subject.direction)
  124. properties_element = etree.SubElement(
  125. subject_element,
  126. 'properties',
  127. )
  128. encode_properties_map = self._get_encode_properties_map()
  129. for key, value in subject.properties.items():
  130. item_element = etree.SubElement(properties_element, 'item')
  131. key_element = etree.SubElement(item_element, 'key')
  132. value_element = etree.SubElement(item_element, 'value')
  133. key_element.text = str(key)
  134. value_element.text = encode_properties_map[key](value)
  135. self._state_root_filled = True
  136. def _get_encode_properties_map(self) -> typing.Dict[str, typing.Callable[[typing.Any], str]]: # nopep8:
  137. return {
  138. 'SELECTION_COLOR_RGB': lambda v: ','.join(map(str, v)),
  139. 'FLAG': str,
  140. 'SIDE': str,
  141. }
  142. class StateLoader(object):
  143. def __init__(
  144. self,
  145. config: Config,
  146. simulation: TileStrategySimulation,
  147. ) -> None:
  148. self._logger = get_logger('StateLoader', config)
  149. self._config = config
  150. self._simulation = simulation
  151. def get_state(
  152. self,
  153. state_file_path: str,
  154. ) -> State:
  155. return State(
  156. self._config,
  157. self._validate_and_return_state_element(state_file_path),
  158. self._simulation,
  159. )
  160. def _validate_and_return_state_element(
  161. self,
  162. state_file_path: str,
  163. ) -> Element:
  164. # open and read schema file
  165. schema_file_path = self._config.get(
  166. 'global.state_schema',
  167. 'opencombat/state.xsd',
  168. )
  169. with open(schema_file_path, 'r') as schema_file:
  170. schema_to_check = schema_file.read()
  171. # open and read xml file
  172. with open(state_file_path, 'r') as xml_file:
  173. xml_to_check = xml_file.read()
  174. xmlschema_doc = etree.fromstring(schema_to_check.encode('utf-8'))
  175. xmlschema = etree.XMLSchema(xmlschema_doc)
  176. try:
  177. doc = etree.fromstring(xml_to_check.encode('utf-8'))
  178. # check for file IO error
  179. except IOError as exc:
  180. self._logger.error(exc)
  181. raise StateLoadError('Invalid File "{}": {}'.format(
  182. state_file_path,
  183. str(exc),
  184. ))
  185. # check for XML syntax errors
  186. except etree.XMLSyntaxError as exc:
  187. self._logger.error(exc)
  188. raise StateLoadError('XML Syntax Error in "{}": {}'.format(
  189. state_file_path,
  190. str(exc.error_log),
  191. ))
  192. except Exception as exc:
  193. self._logger.error(exc)
  194. raise StateLoadError('Unknown error with "{}": {}'.format(
  195. state_file_path,
  196. str(exc),
  197. ))
  198. # validate against schema
  199. try:
  200. xmlschema.assertValid(doc)
  201. except etree.DocumentInvalid as exc:
  202. self._logger.error(exc)
  203. raise StateLoadError(
  204. 'Schema validation error with "{}": {}'.format(
  205. state_file_path,
  206. str(exc),
  207. )
  208. )
  209. except Exception as exc:
  210. self._logger.error(exc)
  211. raise StateLoadError(
  212. 'Unknown validation error with "{}": {}'.format(
  213. state_file_path,
  214. str(exc),
  215. )
  216. )
  217. return doc
  218. class StateConstructorBuilder(object):
  219. def __init__(
  220. self,
  221. config: Config,
  222. simulation: TileStrategySimulation,
  223. ) -> None:
  224. self._logger = get_logger('StateConstructorBuilder', config)
  225. self._config = config
  226. self._simulation = simulation
  227. def get_state_loader(
  228. self,
  229. ) -> StateLoader:
  230. class_address = self._config.resolve(
  231. 'global.state_loader',
  232. 'opencombat.state.StateLoader',
  233. )
  234. state_loader_class = get_class_from_string_path(
  235. self._config,
  236. class_address,
  237. )
  238. return state_loader_class(
  239. self._config,
  240. self._simulation,
  241. )
  242. def get_state_dumper(
  243. self,
  244. ) -> StateDumper:
  245. class_address = self._config.resolve(
  246. 'global.state_dumper',
  247. 'opencombat.state.StateDumper',
  248. )
  249. state_loader_class = get_class_from_string_path(
  250. self._config,
  251. class_address,
  252. )
  253. return state_loader_class(
  254. self._config,
  255. self._simulation,
  256. )