Guest User

Untitled

a guest
Jul 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. import xml.etree.cElementTree as ET
  5. import pandas as pd
  6. import sys
  7. import xmltodict, json
  8. from collections import defaultdict
  9. from utils import json_writer
  10.  
  11. PRODUCT_TREE = ''
  12. WRITE_REF_PRODUCT_TREE_JSON = ''
  13.  
  14.  
  15. def check_name(root):
  16. if 'name' in root.attrib:
  17. return root.attrib['name']
  18.  
  19.  
  20. def check_tnr(root):
  21. if 'tnr' in root.attrib:
  22. return root.attrib['tnr']
  23.  
  24.  
  25. def check_pro(root):
  26. if 'PRO' in root.tag:
  27. return True
  28. else:
  29. return False
  30.  
  31.  
  32. def recursion_tree(root):
  33. global name_list, raw_data, pro_array, pro
  34.  
  35. name = check_name(root)
  36. tnr = check_tnr(root)
  37. pro = check_pro(root)
  38. pro_array.append(pro)
  39.  
  40. if pro_array[-2:] == [True, False]:
  41. name_list = name_list[0:-1]
  42.  
  43. if name is not None:
  44. name_list.append(name)
  45.  
  46. if tnr is not None:
  47. for elem in root.getchildren():
  48. recursion_tree(elem)
  49.  
  50.  
  51. if __name__ == '__main__':
  52. # Index 0 contains TREE
  53. tree = ET.parse(PRODUCT_TREE)
  54. root = tree.getroot()
  55. doc = root[0]
  56.  
  57. # Init Variables
  58. raw_data[tnr] = name_list
  59.  
  60. name_list = []
  61. raw_data = {}
  62. pro = False
  63. pro_array = [False]
  64.  
  65. recursion_tree(doc)
  66. json_writer(WRITE_REF_PRODUCT_TREE_JSON, raw_data)
Add Comment
Please, Sign In to add comment