Guest User

Untitled

a guest
Dec 10th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.62 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2.  
  3. class StackDict:
  4.     def __init__(self,default_value=None):
  5.         self.path = []
  6.         self.default = default_value or {}
  7.         self.data = self.make_new_node()
  8.         self.current_point = self.data
  9.     def push(self,path):
  10.         self.path.append(path)
  11.         self.current_point = self.data
  12.         for node in self.path:
  13.             new_point = self.current_point['subchildren'].get(node)
  14.             if new_point is None:
  15.                 new_point = self.make_new_node()
  16.                 self.current_point['subchildren'][node] = new_point
  17.             self.current_point = new_point
  18.     def make_new_node(self):
  19.         new_node = {}
  20.         new_node['subchildren'] = {}
  21.         for name,val in self.default.items():
  22.             new_node[name] = val() if callable(val) else val
  23.         return new_node
  24.     def pop(self):
  25.         # previous path is implicitly gauranteed to exist.
  26.         self.path.pop()
  27.         self.current_point = self.data
  28.         for node in self.path:
  29.             self.current_point = self.current_point['subchildren'][node]
  30.     def top(self):
  31.         self.path = []
  32.         self.current_point = self.data
  33.  
  34. def print_schema(schema,prefix=' '):
  35.     for node in schema.current_point['subchildren'].keys():
  36.         schema.push(node)
  37.         print(prefix[1:],node,':',sep='')
  38.         print(prefix[1:],schema.current_point['data'].__name__)
  39.         if len(schema.current_point['attributes'])>0:
  40.             print(prefix[1:],"attributes->")
  41.             for attr, kind in schema.current_point['attributes'].items():
  42.                 print(prefix+' ',attr,':',kind.__name__)
  43.         if len(schema.current_point['subchildren']) > 0:
  44.             print(prefix[1:],"children->")
  45.             print_schema(schema,prefix + prefix[0] * 2)
  46.         schema.pop()
  47.  
  48. class NoData:pass
  49.  
  50. def analyze(f):
  51.     parser = ET.XMLPullParser(['start','end'])
  52.     block_size = 1024
  53.     schema = StackDict({
  54.         'attributes':dict,
  55.         'data':None
  56.     })
  57.     def identify_data_type(txt):
  58.         try:
  59.             txt = int(txt)
  60.             return int
  61.         except:
  62.             return NoData if txt is None or (isinstance(txt,str) and len(txt)<1) else str
  63.         return NoData
  64.     with f:
  65.         while True:
  66.             data = f.read(block_size)
  67.             parser.feed(data)
  68.             for event, el in parser.read_events():
  69.                 if event=='start':
  70.                     schema.push(el.tag)
  71.                     if schema.current_point['data'] is None or schema.current_point['data'] is NoData:
  72.                         schema.current_point['data'] = identify_data_type(el.text if len(el)<1 else None)
  73.                     for attr_name,attr_val in el.items():
  74.                         if not attr_name in schema.current_point['attributes']:
  75.                             schema.current_point['attributes'][attr_name] = identify_data_type(attr_val)
  76.                 elif event=='end':
  77.                     schema.pop()
  78.             if len(data) < block_size:
  79.                 break
  80.     print("Calculated schema:")
  81.     schema.top()
  82.     return schema
  83.  
  84. if __name__=="__main__":
  85.     import json
  86.  
  87.     class TypeEncoder(json.JSONEncoder):
  88.         def default(self,obj):
  89.             if isinstance(obj,type):
  90.                 return {
  91.                     int:'int',
  92.                     float:'double',
  93.                     NoData:None,
  94.                     str:'string'
  95.                 }[obj]
  96.             return super().default(obj)
  97.  
  98.     schema = analyze(open("data.xml"))
  99.     with open("structure.json",'w') as f:
  100.         json.dump(schema.data,f,cls=TypeEncoder,indent=2)
Advertisement
Add Comment
Please, Sign In to add comment