Skip to content

Instantly share code, notes, and snippets.

@NiKoTron
Last active September 12, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NiKoTron/86d3df0a936322eea8bb49793a7e081c to your computer and use it in GitHub Desktop.
Save NiKoTron/86d3df0a936322eea8bb49793a7e081c to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import re
from sys import argv
p = re.compile('(\s+@apiSuccess\s)({+.+}+)(\s(\w+|[.]+)+)(.*)')
objType = 'Object'
objArr = 'Object[]'
clses = {}
def parse(input_string):
li = p.findall(input_string)
clses['__root__'] = []
for item in li:
type = item[1].replace('{','').replace('}','')
full_name = item[2].strip()
name = item[3].strip()
comment = item[4].strip()
if type == objType:
clses[full_name] = [] # это новый класс с пустым списком методов
type = full_name
elif type == objArr:
clses[full_name] = []
type = full_name+'[]'
parts = full_name.split('.')
if len(parts) == 1:
prnt = '__root__'
else:
prnt = '.'.join(full_name.split('.')[0:-1]) # это имя класса
clses[prnt].append((type,name,comment))
print_class(clses['__root__'],'root',0)
def print_class(cls, name, tabs_count):
tabs = ''
for t in range(0,tabs_count):
tabs+='\t'
name = name.split('.')[-1].replace('[]','');
print(tabs+'public static class '+name.capitalize()+' {')
for mthd in cls:
type = mthd[0].split('.')[-1].capitalize()
if '[]' in type:
type = 'List<'+type.replace('[]','')+'>'
print(tabs+'\tpublic '+type+' '+mthd[1]+'; //'+mthd[2])
if mthd[0].replace('[]','') in clses:
print_class(clses[mthd[0].replace('[]','')], mthd[0],(tabs_count+1))
print(tabs+'}')
if __name__ == "__main__":
if len(argv) <= 1:
print('use $parse.py %file_to_parse%')
else:
f = open(argv[1], 'r')
parse(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment