BaseContainer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from Base.Arxml import Arxml
  2. import logging
  3. import copy
  4. class BaseContainer:
  5. def __init__(self, element):
  6. self.__element = element
  7. # self.__container_tag = ''
  8. # self.__container_attribute = {}
  9. # self.__container_text = ''
  10. self.set_container_attr()
  11. # print(self.get_container_attr())
  12. def set_container_attr(self):
  13. if self.__element is not None:
  14. self.__container_tag = self.__element.tag
  15. self.__container_attribute = self.__element.attrib
  16. self.__container_text = self.__element.text
  17. else:
  18. self.__container_tag = ''
  19. self.__container_attribute = {}
  20. self.__container_text = ''
  21. def __str__(self):
  22. container = 'Container Tag:{}, Attribute:{}, Text:{}'.format(self.__container_tag,
  23. self.__container_attribute,
  24. self.__container_text)
  25. return container
  26. def update_container_by_attr(self, attr_dict):
  27. pass
  28. def get_container_attr(self):
  29. attr_dict = {}
  30. attr_dict['TAG'] = self.__container_tag.split('}')[-1]
  31. attr_dict['ATTRIB'] = self.__container_attribute.get('DEST', '')
  32. attr_dict['TEXT'] = self.__container_text
  33. return attr_dict
  34. class EcucContainerValue:
  35. '''
  36. short_name
  37. container_definition_ref: (attrib_type, definition_text)
  38. parameter_values:
  39. {
  40. split(parameter_name):(tag_type, parameter_attrib_type, parameter_name, parameter_value),
  41. ...
  42. }
  43. reference_values:
  44. {
  45. split(reference_name):(tag_type, reference_name_attrib_type, reference_name, reference_value_attrib_type, reference_value),
  46. ...
  47. }
  48. eg:
  49. {
  50. 'short_name': 'CCP_SigGW_050ms_PDU00_PT_8c62d7ea_Tx',
  51. 'container_definition_ref': ['ECUC-PARAM-CONF-CONTAINER-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu'],
  52. 'parameter_values':
  53. {
  54. 'IpduMContainedPduHeaderId': ['ECUC-NUMERICAL-PARAM-VALUE', 'ECUC-INTEGER-PARAM-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedPduHeaderId', '1114112'],
  55. 'IpduMContainedTxPduCollectionSemantics': ['ECUC-TEXTUAL-PARAM-VALUE', 'ECUC-ENUMERATION-PARAM-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxPduCollectionSemantics', 'IPDUM_COLLECT_LAST_IS_BEST'],
  56. 'IpduMContainedTxPduTrigger': ['ECUC-TEXTUAL-PARAM-VALUE', 'ECUC-ENUMERATION-PARAM-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxPduTrigger', 'IPDUM_TRIGGER_ALWAYS'],
  57. 'IpduMContainedTxPduConfirmation': ['ECUC-NUMERICAL-PARAM-VALUE', 'ECUC-BOOLEAN-PARAM-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxPduConfirmation', 'true'],
  58. 'IpduMContainedTxPduHandleId': ['ECUC-NUMERICAL-PARAM-VALUE', 'ECUC-INTEGER-PARAM-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxPduHandleId', '43']
  59. },
  60. 'reference_values':
  61. {
  62. 'IpduMContainedTxInContainerPduRef': ['ECUC-REFERENCE-VALUE', 'ECUC-REFERENCE-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxInContainerPduRef', 'ECUC-CONTAINER-VALUE', '/ActiveEcuC/IpduM/IpduMConfig/CCP_PTCANFD_050ms_Container03_52bb19c8_Tx'],
  63. 'IpduMContainedTxPduRef': ['ECUC-REFERENCE-VALUE', 'ECUC-REFERENCE-DEF', '/MICROSAR/IpduM/IpduMConfig/IpduMContainedTxPdu/IpduMContainedTxPduRef', 'ECUC-CONTAINER-VALUE', '/ActiveEcuC/EcuC/EcucPduCollection/CCP_SigGW_050ms_PDU00_PT_8c62d7ea_Tx']
  64. }
  65. }
  66. '''
  67. def __init__(self, ecuc_container):
  68. self.__ecuc_container = ecuc_container
  69. self.__get_ecuc_container_attr()
  70. def __get_ecuc_container_attr(self):
  71. '''
  72. 获取一个ECUC-CONTAINER-VALUE container中的所有属性,使用成员变量,存储属性字典,和子container
  73. :return:
  74. '''
  75. # SHORT-NAME container
  76. short_name_container = Arxml.get_first_match_element(self.__ecuc_container,
  77. './SHORT-NAME')
  78. short_name = Arxml.get_container_text(short_name_container)
  79. # DEFINITION-REF container
  80. definition_container = Arxml.get_first_match_element(self.__ecuc_container, './DEFINITION-REF')
  81. definition_container_attr = BaseContainer(definition_container).get_container_attr()
  82. # print(definition_container_attr) # DEST attribute and container text
  83. container_definition_ref = [definition_container_attr['ATTRIB'], definition_container_attr['TEXT']]
  84. '''
  85. PARAMETER-VALUES
  86. [ECUC-NUMERICAL-PARAM-VALUE]-0
  87. DEFINITION-REF [/MICROSAR/CanIf/CanIfInitCfg/CanIfBufferCfg/CanIfBufferSize]-2
  88. DEST [ECUC-INTEGER-PARAM-DEF]-1
  89. VALUE [0]-3
  90. '''
  91. # PARAMETER-VALUES container
  92. parameter_containers = Arxml.get_first_match_element(self.__ecuc_container, './PARAMETER-VALUES')
  93. if parameter_containers is None: # fix,when get parameter_containers is None, for iterator will get ERROR
  94. parameter_containers = []
  95. parameter_values = {}
  96. parameter_containers_dict = {}
  97. for ele in parameter_containers:
  98. # parameter container tag is parameter type
  99. tag_type = BaseContainer(ele).get_container_attr()['TAG']
  100. # parameter DEFINITION-REF container , include attribute, text(parameter name), tag(DEFINITION-REF)
  101. def_container = Arxml.get_first_match_element(ele, './DEFINITION-REF')
  102. def_attr = BaseContainer(def_container).get_container_attr()
  103. parameter_attrib_type = def_attr['ATTRIB']
  104. parameter_name = def_attr['TEXT']
  105. parameter_name_short = parameter_name.split('/')[-1]
  106. # parameter VALUE container , include attribute, text(actual parameter value), tag(VALUE)
  107. value_container = Arxml.get_first_match_element(ele, './VALUE')
  108. value_attr = BaseContainer(value_container).get_container_attr() ## get text info
  109. parameter_value = value_attr['TEXT']
  110. parameter_values[parameter_name_short] = [tag_type, parameter_attrib_type, parameter_name, parameter_value]
  111. parameter_containers_dict[parameter_name_short] = ele
  112. #reference container
  113. reference_containers = Arxml.get_first_match_element(self.__ecuc_container, './REFERENCE-VALUES')
  114. if reference_containers is None: # fix,when get reference_containers is None, for iterator will get ERROR
  115. reference_containers = []
  116. '''
  117. REFERENCE-VALUES
  118. [ECUC-REFERENCE-VALUE]-0
  119. DEFINITION-REF [/MICROSAR/CanIf/CanIfInitCfg/CanIfBufferCfg/CanIfBufferHthRef]-2
  120. DEST [ECUC-REFERENCE-DEF]-1
  121. VALUE-REF [/ActiveEcuC/CanIf/CanIfInitCfg/CanIfInitHohCfg/CanIfHthCfg_CCP_ADCANFD_010ms_Container36_574ce325_Tx]-4
  122. DEST [ECUC-CONTAINER-VALUE]-3
  123. '''
  124. reference_values = {}
  125. reference_containers_dict = {}
  126. for ele in reference_containers:
  127. tag_type = BaseContainer(ele).get_container_attr()['TAG']
  128. def_container = Arxml.get_first_match_element(ele, './DEFINITION-REF')
  129. def_attr = BaseContainer(def_container).get_container_attr()
  130. reference_name_attrib_type = def_attr['ATTRIB']
  131. reference_name = def_attr['TEXT']
  132. reference_name_short = reference_name.split('/')[-1]
  133. value_container = Arxml.get_first_match_element(ele, './VALUE-REF') # difference with parameter container
  134. value_attr = BaseContainer(value_container).get_container_attr()
  135. reference_value = value_attr['TEXT']
  136. reference_value_attrib_type = value_attr['ATTRIB']
  137. reference_values[reference_name_short] = [tag_type, reference_name_attrib_type, reference_name, reference_value_attrib_type, reference_value]
  138. reference_containers_dict[reference_name_short] = ele
  139. self.__container_attr = {
  140. 'short_name':short_name,
  141. 'container_definition_ref':container_definition_ref,
  142. 'parameter_values':parameter_values,
  143. 'reference_values':reference_values
  144. }
  145. self.__container = {
  146. 'short_name': short_name_container,
  147. 'container_definition_ref': definition_container,
  148. 'parameter_values': parameter_containers_dict,
  149. 'reference_values': reference_containers_dict
  150. }
  151. @property
  152. def container_attr(self):
  153. return copy.deepcopy(self.__container_attr)
  154. @container_attr.setter
  155. def container_attr(self, new_conatiner_attr):
  156. self.__container_attr = new_conatiner_attr
  157. def __update_ecuc_base_attr(self, new_attr):
  158. pass
  159. def __update_ecuc_base_container_attr(self, new_attr, container_type):
  160. value_tag_name = 'VALUE' if 'PARAM' == container_type else 'VALUE-REF'
  161. attr_key = 'parameter_values' if 'PARAM' == container_type else 'reference_values'
  162. if len(new_attr[attr_key]) >= len(self.__container_attr[attr_key]) and \
  163. new_attr[attr_key] != self.__container_attr[attr_key]:
  164. for key, value in new_attr[attr_key].items():
  165. logging.info(key)
  166. logging.info(value)
  167. if key in self.__container_attr[attr_key]:
  168. ## param or reference key in attr dict? yes
  169. # find param VALUE or reference VALUE-REF element
  170. value_container = Arxml.get_first_match_element(self.__container[attr_key][key], './' + value_tag_name)
  171. # print(value_container)
  172. if value_container is not None:
  173. # VALUE or reference VALUE-REF element is exit, so update value.
  174. value_container.text = value[-1] # set text info
  175. # elif value[-1] != '':
  176. else:
  177. # has key but without VALUE or reference VALUE-REF element.
  178. Arxml.creat_child_new_element(self.__container[attr_key][key], # new add element in container, can not find by xpath, through it has append to container
  179. value_tag_name, text_str=value[-1]) # TO DO creat VALUE-REF child with attribute_dict
  180. pass
  181. else: # new attr ,need to be created
  182. param_type_tag = value[0]
  183. # if <PARAMETER-VALUES/> or <REFERENCE-VALUES/> is empty, when add element to it, will cause error.
  184. first_container_key = list(self.__container[attr_key].keys())[0] # find first element in <PARAMETER-VALUES/> or <REFERENCE-VALUES/>
  185. # find first brother container
  186. brother_container = self.__container[attr_key][first_container_key]
  187. # creat new brother element <ECUC-REFERENCE-VALUE/>
  188. new_container = Arxml.creat_brother_new_element(brother_container,
  189. param_type_tag)
  190. # creat child <DEFINITION-REF/> element
  191. Arxml.creat_child_new_element(new_container,
  192. 'DEFINITION-REF', text_str=value[2], attribute_dict={'DEST':value[1]})
  193. if 'PARAM' == container_type:
  194. # creat child <VALUE/> element
  195. Arxml.creat_child_new_element(new_container, value_tag_name,
  196. text_str=value[-1])
  197. else:
  198. # creat child <VALUE-REF/> element
  199. Arxml.creat_child_new_element(new_container, value_tag_name, text_str=value[-1], attribute_dict={'DEST':value[-2]})
  200. # self.__container_attr = new_attr
  201. def __update_ecuc_parameter_attr(self, new_attr):
  202. self.__update_ecuc_base_container_attr(new_attr, 'PARAM')
  203. def __update_ecuc_reference_attr(self, new_attr):
  204. self.__update_ecuc_base_container_attr(new_attr, 'REF')
  205. def update_ecuc_container(self, new_attr):
  206. '''
  207. update info or add new sub-container
  208. :param new_attr:
  209. :return:
  210. '''
  211. self.__update_ecuc_base_attr(new_attr)
  212. self.__update_ecuc_parameter_attr(new_attr)
  213. self.__update_ecuc_reference_attr(new_attr)
  214. self.__container_attr = new_attr
  215. if __name__ == '__main__':
  216. arxml = Arxml('EH32_GW04_IpduM_ecuc.arxml')
  217. element = Arxml.get_first_match_element(arxml.root, './/SHORT-NAME', 'IpduMConfig', 'TEXT_EQ')
  218. par_ele = Arxml.get_parent_element(element)
  219. # ecuc = Arxml.get_first_match_element(par_ele, './SUB-CONTAINERS/ECUC-CONTAINER-VALUE')
  220. #
  221. # ecuc_container = EcucContainerValue(ecuc)
  222. # attr = ecuc_container.container_attr
  223. # attr['parameter_values']['IpduMContainedPduHeaderId'][-1] ='1122'
  224. # attr['parameter_values']['aaaa'] = ['test_tag', 'test_attribute', 'test_param_name', 'test_param_value']
  225. # attr['reference_values']['bbbb'] = ['test_ref_tag', 'test_ref_attribute', 'test_ref_name', 'test_ref_value_attrib', 'test_ref_value']
  226. # ecuc_container.update_ecuc_container(attr)
  227. ecuc_containers = Arxml.get_all_match_element(par_ele, './SUB-CONTAINERS/ECUC-CONTAINER-VALUE')
  228. for ecuc in ecuc_containers:
  229. ecuc_container = EcucContainerValue(ecuc)
  230. print(ecuc_container.container_attr)
  231. arxml.xml_write_to_file(arxml.root, 'test.arxml')