def LoadFile(fq_file): ''' Will always return an Instance, else False ''' if not os.path.exists(fq_file): return False try: with open(fq_file, 'r') as fh: data = fh.readline() data = data.strip() zdict = eval(data) order_def = OrderDef() for key in zdict: if key == OrderDef.IOKEY: pw_def = TableDef(name=OrderDef.NONAME) zrows = eval(zdict[key]) for row in zrows: if pw_def.get_table_name() != row[0]: if pw_def.get_table_name() != OrderDef.NONAME: order_def.add_table(pw_def) pw_def = TableDef(name=row[0]) pw_def.add_field(row[1], row[2]) order_def.add_table(pw_def) else: order_def._zdict[key] = zdict[key] order_def.fixup() return order_def except Exception as ex: print(ex.__traceback__) return False
def assign(self, detect, table_name): ''' Populate ourselves based upon a detected set of fields. Effects are cumulative. Returns True upon success, else False. ''' from SqltDAO.CodeGen01.TextDataDetector import TextData assert (isinstance(detect, TextData)) self.sep = detect.sep self.encoding = detect.encoding ztable = TableDef(name=table_name) for field in detect.fields: if ztable.add_field(field[0], field[1]) is False: return False return self.add_table(ztable)
def Create(order_class, fields): ''' Create an OrderDef from an OrderClass + Fields. Raise an exception on error. ''' if isinstance(order_class, OrderClass) is False: raise TypeError("Error: Instance of OrderClass is required.") if not fields: raise TypeError("Error: No fields detected.") result = OrderDef() data = order_class.__dict__() for key in data: result.zdict[key] = data[key] ztable = TableDef(name=order_class.table_name) for field in fields: if ztable.add_field(field[0], field[1]) is False: raise GenOrderError("Error: Invalid Data Definition.") result.add_table(ztable) return result
detect = Factory1.Detect(file) print(detect.__dict__) else: from SqltDAO.Gui.DataPreferences import Dp1 as DataPreferences pref = DataPreferences.Load('.') zorder = OrderDef(name=OrderDef.DEFAULT_SCHEMA) zname = zorder.project_name print("zname", zname) if os.path.exists(zname): print("unlinking", zname) os.unlink(zname) for ss in range(4): table = "zTable " + str(ss) ztable = TableDef(name=table) for ztype in TableDef.SupportedTypes: ztable.add_field(table + ' ' + ztype, ztype) zorder.add_table(ztable) print("zorder:\n", zorder, '\n') zname = zorder.project_name # print("zname", zname) if os.path.exists(zname): print("unlinking", zname) os.unlink(zname) assert (Factory1.SaveFile(pref, zorder)) zorder2 = Factory1.LoadFile(zname) # print("zorder2:\n", zorder2, '\n') assert (zorder2 is not False) assert (str(zorder) == str(zorder2))