hazer_hazer

Untitled

Nov 17th, 2019
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. class Converter():
  2.     def __init__(self, obj):
  3.         self.obj = obj
  4.         self.indentCount = 0
  5.         self.currentVarNum = 0
  6.         self.result = ''
  7.  
  8.     def indent(self, delta = 0):
  9.         tabs = ''
  10.         for i in range(self.indentCount + delta):
  11.             tabs += '\t'
  12.         return tabs
  13.  
  14.     def convert(self):
  15.         for o in self.obj:
  16.             self.convertObj(o)
  17.         return self.result
  18.  
  19.     def convertObj(self, o, parent = None):
  20.         if o['type'] == 'obj':
  21.             if parent:
  22.                 parent['val'].append({
  23.                     'key': o['key'],
  24.                     'type': 'val',
  25.                     'dataType': o['key'].upper()
  26.                 })
  27.             self.currentVarNum = 0
  28.             self.result += self.indent() +'message '+ o['key'] +' {\n'
  29.             self.indentCount += 1
  30.             for pair in o['val']:
  31.                 self.convertObj(pair, o)
  32.             self.indentCount -= 1
  33.             self.result += self.indent() + '}\n'
  34.         elif o['type'] == 'val' and o['dataType'] != 'null':
  35.             self.currentVarNum += 1
  36.             self.result += self.indent() + 'required '+ o['dataType'] +' '+ o['key'] +' = '+ str(self.currentVarNum) +'\n'
  37.  
  38.     def error(message):
  39.         raise Exception('[CONVERTER][ERROR] ' + message);
Add Comment
Please, Sign In to add comment