def parse(self, data="", name="BaseClass"): model = DjangoModel() model.setClassName(name) if data == "": self.jsonFile = open(self.filename, 'r') self.parse_rec(json.load(self.jsonFile), model) else: self.parse_rec(data, model) try: from pprint import pprint for e in self.parsedModel: print(e) self.convert() except Exception as e: print "ICI =" + str(e)
class ParsePyModel: parsedAPI = None pyFile = None inClass = False inMeta = False className = "" parsedModel = [] tmpModel = DjangoModel() def __init__(self, fileName="out.py"): self.pyFile = open(fileName, 'r') self.parsedModel = [] self.inClass = False self.inMeta = False self.ClassName = "" self.tmpModel = DjangoModel() self.varType = None self.varName = None def parse(self): for line in self.pyFile.readlines(): if line[0:5] == "class": if self.inClass: self.parsedModel.append(self.tmpModel) self.inMeta = False; self.tmpModel = DjangoModel() else: self.inClass = True self.inMeta = False; self.className = line[6:].split("(")[0] self.tmpModel.setClassName(self.className) if "Meta" in line: self.inMeta = True; if self.inClass and not self.inMeta and "=" in line: self.varName = line.split("=")[0].replace(" ", "") if "ForeignKey" in line: self.varType = line.split("=")[1].split(",")[0].replace(" ", "").replace("\n", "") else: self.varType = line.split("=")[1].split("(")[0].replace(" ", "").replace("\n", "") self.tmpModel.addFunc(self.varName, self.varType) return self.parsedModel
def parse_rec(self, it, model): if (type(it) is list): if len(it) < 1: return self.parse_rec(it[0], model) if type(it) is dict: if model is None: model = DjangoModel() model.setClassName("BaseClass" + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(1))) for key, value in it.items(): varName = key if type(value) is list: newModel = DjangoModel() newModel.setClassName(varName) self.parse_rec(value[0], newModel) if (key not in model.var): model.addFunc(varName, "list(%s)" % varName.capitalize()) #if newModel.nameClass not in self.genered: # self.parsedModel.append(newModel) # self.genered.append(newModel.nameClass) elif type(value) is dict: newModel = DjangoModel() newModel.setClassName(varName) self.parse_rec(value, newModel) if (key not in model.var): model.addFunc(varName, varName) # print model.nameClass # print key + ", " + key else: # print model.nameClass # print key + ", " + str(type(value)) if (key not in model.var): model.addFunc(varName, str(type(value))) if model.nameClass not in (o.nameClass for o in self.parsedModel): self.parsedModel.append(model)