Aestate
nodes.py
Go to the documentation of this file.
1 # -*- utf-8 -*-
2 import importlib
3 import re
4 from abc import ABC
5 
6 from aestate.exception import NotFindTemplateError, TagAttributeError, TagHandlerError
7 from aestate.util.Log import ALog
9 from aestate.work.xmlhandler.base import AestateNode
10 
11 
12 class AbstractNode(ABC):
13  """抽象节点,所有节点的父类"""
14 
15  # 节点自身的操作
16 
17  def __init__(self, target_obj, params, aestate_xml_cls, root, value, XML_KEY, XML_IGNORE_NODES):
18  self.target_obj = target_obj
19  self.params = params
20  self.aestate_xml_cls = aestate_xml_cls
21  self.root = root
22  self.node = value
23  self.XML_KEY = XML_KEY
24  self.XML_IGNORE_NODES = XML_IGNORE_NODES
25 
26  # 得到节点的值
27  def apply(self, *args, **kwargs):
28  ...
29 
30  def parseNode(self, texts: AestateNode, node):
31  for root_index, root_value in enumerate(node.childNodes):
32  if root_value.nodeName in self.XML_KEY.keys():
33  obj = self.XML_KEY[root_value.nodeName](self.target_obj, self.params, self.aestate_xml_cls, self.root,
34  root_value, self.XML_KEY, self.XML_IGNORE_NODES)
35  texts = obj.apply(texts=texts)
36  elif root_value.nodeName in self.XML_IGNORE_NODES:
37  try:
38  texts.add(node=root_value, index=root_index)
39  except Exception as e:
40  ALog.log_error(
41  msg=''.join(e.args),
42  obj=e, LogObject=self.target_obj.log_obj, raise_exception=True)
43  try:
44  texts.extend(AestateNode(self.root, root_value))
45  except Exception as e:
46  ALog.log_error(
47  msg=''.join(e.args),
48  obj=e, LogObject=self.target_obj.log_obj, raise_exception=True)
49  return texts
50 
51 
53 
54  def apply(self, *args, **kwargs):
55  # 取得已有的文本
56  texts = kwargs['texts']
57  axc_node = self.aestate_xml_cls(self.root, self.node, self.params)
58  # 返回值类型
59  resultType = axc_node.attrs['resultType']
60  texts.mark['resultType'] = resultType.text
61  return self.parseNode(texts, self.node)
62 
63 
65  class TempTextNode:
66  def __init__(self, text):
67  self.text = text
68 
69  def apply(self, *args, **kwargs):
70  # 取得已有的文本
71  texts = kwargs['texts']
72  axc_node = self.aestate_xml_cls(self.root, self.node, self.params)
73  # 返回值类型
74  has_last_id = axc_node.attrs['last'] if 'last' in axc_node.attrs.keys() else self.TempTextNode('True')
75  texts.mark['has_last_id'] = has_last_id.text == 'True'
76  return self.parseNode(texts, self.node)
77 
78 
80 
81  def apply(self, *args, **kwargs):
82  texts = kwargs['texts']
83  axc_node = self.aestate_xml_cls(self.root, self.node, self.params)
84  if 'test' not in axc_node.attrs.keys():
85  ALog.log_error(
86  msg=f'The attribute`test` in the if tag is missing a required structure',
87  obj=TagAttributeError, LogObject=self.target_obj.log_obj, raise_exception=True)
88  return
89  test_syntax = axc_node.attrs['test']
90  syntax_re_text = re.findall('(.*?)([>=|<=|==|<|>]+)(.*)', test_syntax.text)
91  if len(syntax_re_text) == 0:
92  # 缺少必要的test标签语法
93  ALog.log_error(
94  msg=f'The attribute`test` in the if tag is missing a required structure',
95  obj=TagAttributeError, LogObject=self.target_obj.log_obj, raise_exception=True)
96  # 移除空集
97  syntax_using = [x for x in syntax_re_text[0] if x != '']
98  if len(syntax_using) == 2 or len(syntax_using) > 3:
99  ALog.log_error(
100  msg=f'The node rule parsing failed and did not conform to the grammatical structure.{syntax_using}',
101  obj=TagHandlerError, LogObject=self.target_obj.log_obj, raise_exception=True)
102 
103  initial_field = syntax_using[0]
104  symbol = syntax_using[1]
105  value = syntax_using[2]
106 
107  rfield = re.findall('#{(.*?)}', initial_field)
108 
109  field = rfield[0] if len(rfield) > 0 and rfield[0] in self.params.keys() else initial_field
110  # 让事件器来执行
111  ih = IfHandler(initial_field=initial_field, field=field, params=self.params, value=value, symbol=symbol)
112 
113  success = ih.handleNode(self.target_obj)
114 
115  if success:
116  texts = self.parseNode(texts, node=self.node)
117 
118  if ih.checking_mark(self.node):
119  # 设置为反的
120  texts.mark['if_next'] = not success
121 
122  return texts
123 
124 
126 
127  def apply(self, *args, **kwargs):
128  texts = kwargs['texts']
129  if 'if_next' not in texts.mark.keys():
130  ALog.log_error(
131  msg='Cannot find the if tag in front of the else tag',
132  obj=TagHandlerError, LogObject=self.target_obj.log_obj, raise_exception=True)
133  else:
134  if_next = texts.mark['if_next']
135  if not if_next:
136  texts.mark.pop('if_next')
137  return texts
138  else:
139  texts.mark.pop('if_next')
140  return self.parseNode(texts, self.node)
141 
142 
144 
145  def apply(self, *args, **kwargs):
146  texts = kwargs['texts']
147  axc_node = self.aestate_xml_cls(self.root, self.node, self.params)
148  field_name = axc_node.attrs['field'].text
149  try:
150  value = self.params[field_name]
151  except KeyError as ke:
152  ALog.log_error(
153  msg=f'The parameter named `{field_name}` does not exist in the called method',
154  obj=TagHandlerError, LogObject=self.target_obj.log_obj, raise_exception=True)
155  case_nodes = axc_node.children['case']
156  check_node = None
157  for cn in case_nodes:
158  if cn.attrs['value'].text == value:
159  check_node = cn.node
160  break
161  if check_node is None:
162  check_node = axc_node.children['default'][0].node
163  texts = self.parseNode(texts, check_node)
164 
165  return texts
166 
167 
169 
170  def apply(self, *args, **kwargs):
171  texts = kwargs['texts']
172  axc_node = self.aestate_xml_cls(self.root, self.node, self.params)
173  from_node_name = axc_node.attrs['from'].text
174  templates = self.target_obj.xNode.children['template']
175  target_template = None
176  for t in templates:
177  if t.attrs['id'].text == from_node_name:
178  target_template = t
179  if target_template is None:
180  ALog.log_error(
181  msg=f'The template named `{from_node_name}` could not be found from the node',
182  obj=NotFindTemplateError, LogObject=self.target_obj.log_obj, raise_exception=True)
183  texts = self.parseNode(texts, target_template.node)
184  return texts
185 
186 
187 class ResultABC(ABC):
188  @staticmethod
189  def get_type(structure: dict):
190  t = structure['_type'].split('.')
191  cls_name = t[len(t) - 1]
192  package_name = '.'.join(t[:len(t) - 1])
193  package = importlib.import_module(package_name)
194  _type = getattr(package, cls_name)
195  return _type
196 
197  @staticmethod
198  def generate(data: list, structure: dict):
199  ret = []
200  if not isinstance(data, list):
201  data = [data]
202  for _data_item in data:
203  obj = ResultABC.get_type(structure)()
204  for field, properties in structure.items():
205  if field != '_type':
206  if isinstance(properties, dict):
207  setattr(obj, field, ResultABC.generate(_data_item, properties))
208  else:
209  setattr(obj, properties, _data_item[field])
210  ret.append(obj)
211 
212  return ret
213 
214 
215 class ResultMapNode(object):
216  def __init__(self, target_obj, node, data):
217  self.target_obj = target_obj
218  self.node = node
219  self.data = data
220 
221  def apply(self):
222  resultMapTags = self.target_obj.xNode.children['resultMap']
223  resultNode = None
224  for i in resultMapTags:
225  if i.attrs['id'].text == self.node.mark['resultType']:
226  # 这里不需要break,因为可以重复,取最后一位
227  resultNode = i
228 
229  if resultNode is None:
230  ALog.log_error(
231  msg="Can't find resultMap template",
232  obj=NotFindTemplateError, LogObject=self.target_obj.log_obj, raise_exception=True)
233  structure = ForeignNode.apply(resultNode)
234  return ResultABC.generate(self.data, structure)
235 
236 
238  @staticmethod
239  def apply(resultNode):
240  structure = {'_type': resultNode.attrs['type'].text}
241  if 'result' in resultNode.children.keys():
242  for i in resultNode.children['result']:
243  structure[i.attrs['field'].text] = i.attrs['properties'].text
244 
245  if 'foreign' in resultNode.children.keys():
246  for i in resultNode.children['foreign']:
247  structure[i.attrs['name'].text] = ForeignNode.apply(i)
248  return structure
aestate.work.xmlhandler.nodes.UpdateNode.TempTextNode.__init__
def __init__(self, text)
Definition: nodes.py:66
aestate.work.xmlhandler.nodes.IfNode
Definition: nodes.py:79
aestate.work.xmlhandler.XMLScriptBuilder
Definition: XMLScriptBuilder.py:1
aestate.work.xmlhandler.nodes.ResultMapNode.target_obj
target_obj
Definition: nodes.py:217
aestate.work.xmlhandler.nodes.ResultMapNode.apply
def apply(self)
Definition: nodes.py:221
aestate.work.xmlhandler.nodes.SwitchNode
Definition: nodes.py:143
aestate.work.xmlhandler.nodes.ForeignNode.apply
def apply(resultNode)
Definition: nodes.py:239
aestate.work.xmlhandler.nodes.ResultABC.generate
def generate(list data, dict structure)
Definition: nodes.py:198
aestate.work.xmlhandler.nodes.AbstractNode.parseNode
def parseNode(self, AestateNode texts, node)
Definition: nodes.py:30
aestate.work.xmlhandler.base
Definition: base.py:1
aestate.work.xmlhandler.nodes.ResultMapNode.node
node
Definition: nodes.py:218
aestate.work.xmlhandler.nodes.SelectNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:54
aestate.work.xmlhandler.nodes.ForeignNode
Definition: nodes.py:237
aestate.work.xmlhandler.base.AestateNode
Definition: base.py:28
aestate.work.xmlhandler.nodes.AbstractNode.target_obj
target_obj
Definition: nodes.py:18
aestate.work.xmlhandler.nodes.ResultMapNode.__init__
def __init__(self, target_obj, node, data)
Definition: nodes.py:216
aestate.work.xmlhandler.XMLScriptBuilder.IfHandler
Definition: XMLScriptBuilder.py:20
aestate.work.xmlhandler.nodes.AbstractNode.params
params
Definition: nodes.py:19
aestate.work.xmlhandler.nodes.UpdateNode.TempTextNode
Definition: nodes.py:65
aestate.work.xmlhandler.nodes.UpdateNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:69
aestate.work.xmlhandler.nodes.AbstractNode.XML_IGNORE_NODES
XML_IGNORE_NODES
Definition: nodes.py:24
aestate.work.xmlhandler.nodes.AbstractNode.root
root
Definition: nodes.py:21
aestate.work.xmlhandler.nodes.SelectNode
Definition: nodes.py:52
aestate.util.Log
Definition: Log.py:1
aestate.exception
Definition: __init__.py:1
aestate.work.xmlhandler.nodes.ResultABC
Definition: nodes.py:187
aestate.work.xmlhandler.nodes.UpdateNode.TempTextNode.text
text
Definition: nodes.py:67
aestate.work.xmlhandler.nodes.IncludeNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:170
aestate.work.xmlhandler.nodes.AbstractNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:27
aestate.work.xmlhandler.nodes.AbstractNode.aestate_xml_cls
aestate_xml_cls
Definition: nodes.py:20
aestate.work.xmlhandler.nodes.ResultABC.get_type
def get_type(dict structure)
Definition: nodes.py:189
aestate.work.xmlhandler.nodes.AbstractNode.node
node
Definition: nodes.py:22
aestate.work.xmlhandler.nodes.IncludeNode
Definition: nodes.py:168
aestate.work.xmlhandler.nodes.ElseNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:127
aestate.work.xmlhandler.nodes.ElseNode
Definition: nodes.py:125
aestate.work.xmlhandler.nodes.ResultMapNode
Definition: nodes.py:215
aestate.work.xmlhandler.nodes.AbstractNode.XML_KEY
XML_KEY
Definition: nodes.py:23
aestate.work.xmlhandler.nodes.IfNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:81
aestate.work.xmlhandler.nodes.AbstractNode.__init__
def __init__(self, target_obj, params, aestate_xml_cls, root, value, XML_KEY, XML_IGNORE_NODES)
Definition: nodes.py:17
aestate.work.xmlhandler.nodes.UpdateNode
Definition: nodes.py:64
aestate.work.xmlhandler.nodes.AbstractNode
Definition: nodes.py:12
aestate.work.xmlhandler.nodes.SwitchNode.apply
def apply(self, *args, **kwargs)
Definition: nodes.py:145
aestate.work.xmlhandler.nodes.ResultMapNode.data
data
Definition: nodes.py:219