Example #1
0
 def on_txt_fn(self):
     self.attributes('-topmost', False)
     fn = askopenfilename(
         title="Open Text File",
         initialdir=self.pref['Csv Folder'])
     if not fn:
         return
     detect = Factory1.Detect(fn)
     if not detect:
         messagebox.showerror("Data Format Error", "Unsupported input data / file format.")
         return
     self.import_file_name.set(fn)
     self.attributes('-topmost', True)
     fbase = os.path.splitext(fn)[0]
     nodes = os.path.split(fn)
     fname = nodes[-1]
     subject = os.path.splitext(fname)[0]
     self.order_class = OrderClass(
         class_name=subject,
         table_name=subject,
         db_name=fbase + OrderDef.DbType,
         file_name=fbase + OrderDef.CodeType)
     self.pref['Csv Folder'] = os.path.dirname(fn)
     self.order_class.home(self.pref)
     self.order_class.encoding = detect.encoding 
     self.order_class.sep      = detect.sep
     self._show_order()
Example #2
0
    def Extract(order_def, pref):
        from SqltDAO.Gui.DataPreferences import Preferences
        ''' Extract an OrderClass from an OrderDef. Raise an exception on error.'''
        if isinstance(order_def, OrderDef) is False:
            raise TypeError("Error: Instance of OrderClass is required.")
        for key in order_def._zdict:
            if order_def._zdict[key] == OrderDef.NONAME:
                raise TypeError("Error: Please define a '" + key + "' value.")

        order_def.fixup()

        table_name = None
        for key in order_def._zdict_tables:
            table_name = order_def._zdict_tables[key].get_table_name()
            break  # HIGHLANDER HACK
        if not table_name:
            raise TypeError("Error: No tables have been defined.")
        if isinstance(pref, Preferences) is False:
            raise TypeError("Error: DataPreferences.Preferences is required.")
        return OrderClass(class_name=table_name,
                          table_name=table_name,
                          db_name=pref['Csv Folder'] + "/" + table_name +
                          OrderDef.DbType,
                          file_name=pref['Code Folder'] + "/" + table_name +
                          OrderDef.CodeType)
Example #3
0
    def __init__(self, parent, pref, gendef=False, verbose=False):
        self.gen_ok = False
        self.gendef = gendef
        self.verbose = verbose
        if gendef:
            self.ztitle = "Data2Proj, Rev 0.2"
        else:
            self.ztitle = "Data2Code, Rev 0.2"
        self.order_class = None
        self.import_file_name = StringVar()
        self.import_file_name.set(" ")
        self.field_seps = OrderDef.DELIMITERS
        self.field_sel = IntVar()
        self.field_sel.set(0)

        if not pref:
            self.pref = DataPreferences.Load('.')
        else:
            self.pref = pref

        self.display_info = OrderedDict()
        dum = dict(OrderClass())
        for key in dum:
            self.display_info[key] = StringVar()
        super().__init__(parent=parent)
Example #4
0
    def Extract(order_def, pref):
        ''' Extract an OrderClass from an OrderDef. Raise an exception on error.'''
        if isinstance(order_def, OrderDef) is False:
            raise TypeError("Error: Instance of OrderClass is required.")
        if isinstance(pref, dict) is False:
            raise TypeError("Error: Dictionary of DataPrefrences is required.")
        for key in order_def.zdict:
            if order_def.zdict[key] == OrderDef.NONAME:
                raise TypeError("Error: Please define a '" + key + "' value.")

        try:
            for key in DataPrefrences.KEYS:
                if not pref[key]:
                    pass
        except:
            raise TypeError("Error: Key value '" + key + "' not found.")
        
        table_name = None
        for key in order_def.zdict_tables:
            table_name = order_def.zdict_tables[key].get_table_name()
            break # HIGHLANDER HACK
        if not table_name:
            raise TypeError("Error: No tables have been defined.")
        
        return OrderClass(
            class_name=table_name,
            table_name=table_name,
            db_name=pref['Csv Folder'] + "/" + table_name + ".sqlt3",
            file_name=pref['Projects'] + "/" + table_name + ".py")
Example #5
0
 def on_txt_fn(self):
     self.attributes('-topmost', False)
     fn = askopenfilename(title="Open Text File",
                          initialdir=self.pref['Csv Folder'])
     if not len(fn):
         return
     self.import_file_name.set(fn)
     self.attributes('-topmost', True)
     fbase = os.path.splitext(fn)[0]
     nodes = os.path.split(fn)
     fname = nodes[-1]
     subject = os.path.splitext(fname)[0]
     self.order_class = OrderClass(class_name=subject,
                                   table_name=subject,
                                   db_name=fbase + OrderDef.DbType,
                                   file_name=fbase + OrderDef.CodeType)
     self.pref['Csv Folder'] = os.path.dirname(fn)
     self.order_class.home(self.pref)
     self._show_order()
Example #6
0
 def on_txt_fn(self):
     self.attributes('-topmost', False)
     fn = askopenfilename()
     if len(fn):
         self.file_name.set(fn)
     self.attributes('-topmost', True)
     fbase = os.path.splitext(fn)[0]
     nodes = os.path.split(fn)
     fname = nodes[-1]
     subject = os.path.splitext(fname)[0]
     self.order_info = OrderClass(class_name=subject,
                                  table_name=subject,
                                  db_name=fbase + ".sqlt3",
                                  file_name=fbase + ".py")
     self._show_order()
Example #7
0
    def __init__(self, parent, gendef=False, verbose=False):
        self.gen_ok = False
        self.gendef = gendef
        self.verbose = verbose
        self.ztitle = "Text Table Tool, Rev 0.1"
        self.order_info = None
        self.file_name = StringVar()
        self.file_name.set(" ")
        self.field_sel = StringVar()
        self.field_seps = ((0, 'CSV', '","'), (1, 'TAB', '\t'),
                           (2, 'PIPE', '|'), (3, 'COMMA', ','))
        self.field_sel = IntVar()
        self.field_sel.set(0)

        self.display_info = OrderedDict()
        dum = dict(OrderClass())
        for key in dum:
            self.display_info[key] = StringVar()
        super().__init__(parent=parent)
Example #8
0
    def __init__(self, parent, pref, verbose=False):
        self.gen_ok = False
        self.verbose = verbose
        self.ztitle = "DataAI, Rev 0.1"
        self.order_class = None
        self.import_file_name = StringVar()
        self.import_file_name.set(" ")
        self.field_seps = "Gen Proj", "Gen Code", "Gen Both"
        self.field_sel = StringVar()
        self.field_sel.set(0)

        if not pref:
            self.pref = DataPreferences.Load('.')
        else:
            self.pref = pref

        self.display_info = OrderedDict()
        dum = dict(OrderClass())
        for key in dum:
            self.display_info[key] = StringVar()
        super().__init__(parent=parent)
Example #9
0
from SqltDAO.CodeGen01.OrderClass import OrderClass
from SqltDAO.CodeGen01.CodeGen import DaoGen

if __name__ == "__main__":
    data_file = "../DaoTest01/nasdaqlisted.txt"
    order = OrderClass(class_name='NasdaqDAO',
                       table_name='TOK_NASDAQ',
                       db_name='./StockInfo.sqlt3',
                       file_name='./NasdaqDAO.py')

    gen = DaoGen()
    print(gen.write_code(order, data_file, sep="|"))
Example #10
0
import os
import sys
sys.path.insert(1, os.path.join(sys.path[0], '../..'))

from SqltDAO.CodeGen01.OrderClass import OrderClass
from SqltDAO.CodeGen01.CodeGen import DaoGen
from SqltDAO.Gui.DataPreferences import Dp1 as DataPreferences

test = DaoGen()

order = OrderClass()
order.class_name = "TC001"
order.file_name = "./foo.py"
order.sep = 'CSV'
print("Order Delimiter:", order.sep)

try:
    data_file = "./tc001_data.txt"
    print(test.write_code(DataPreferences.Load('.'), order, data_file))
except:
    data_file = "./DaoTest01/tc001_data.txt"
    order.file_name = "./DaoTest01/foo.py"
    print(test.write_code(DataPreferences.Load('.'), order, data_file))
Example #11
0
        # 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))

        for ss, table in enumerate(zorder2, 1):
            print(ss, table)

        odd = Factory1.Extract(zorder2, pref)
        print(odd)

        os.unlink(zorder.project_name)

        # OrderClass Inter-Operation
        from SqltDAO.CodeGen01.TextDataDetector import TextData
        detect = TextData()
        detect.fields = list()
        detect.fields.append(("MyAccount", "Integer"))
        detect.fields.append(("MyField", "TEXT"))
        detect.encoding = "utf-8"
        detect.sep = 'CSV'
        order_class = OrderClass()
        order_def = Factory1.Create(order_class, detect)
Example #12
0
# ######

output_file = "./CrudMeister"
try:
    import os
    os.remove(output_file + '.py')
    # os.remove(output_file + '.sqlt3')
except:
    pass

# ######
# STEP 3: Enjoy!
# ######

from SqltDAO.CodeGen01.OrderClass import OrderClass
from SqltDAO.CodeGen01.SqlSyntax import SqliteCrud    
for line in tables:
    zname = line["ObjectName"]
    print("Table:", zname)
    order = OrderClass(
        db_name=output_file + ".sqlt3",
        class_name=zname,
        table_name=zname,
        file_name="./" + zname + ".py")
    zfields = line["Fields"]
    sql = SqliteCrud(order, zfields)
    data_file = str(zname + '.csv')
    result = sql.code_class_template(data_file, sep='","')
    with open(output_file + ".py", 'a') as fh:
        print(result, file=fh)
Example #13
0
from SqltDAO.CodeGen01.OrderClass import OrderClass
from SqltDAO.CodeGen01.CodeGen import DaoGen

test = DaoGen()

data_file = "./tc001_data.txt"
order = OrderClass()
order.class_name = "TC001"
order.file_name = "./foo.py"

print(test.write_code(order, data_file))
Example #14
0
class DataAI(simpledialog.Dialog):

    def __init__(self, parent, pref, verbose=False):
        self.gen_ok = False
        self.verbose = verbose
        self.ztitle = "DataAI, Rev 0.1"
        self.order_class = None
        self.import_file_name = StringVar()
        self.import_file_name.set(" ")
        self.field_seps = "Gen Proj", "Gen Code", "Gen Both"
        self.field_sel = StringVar()
        self.field_sel.set(0)

        if not pref:
            self.pref = DataPreferences.Load('.')
        else:
            self.pref = pref

        self.display_info = OrderedDict()
        dum = dict(OrderClass())
        for key in dum:
            self.display_info[key] = StringVar()
        super().__init__(parent=parent)

    def _show_order(self):
        zdict = dict(self.order_class)
        for key in zdict:
            self.display_info[key].set(zdict[key])

    def on_txt_fn(self):
        self.attributes('-topmost', False)
        fn = askopenfilename(
            title="Open Text File",
            initialdir=self.pref['Csv Folder'])
        if not fn:
            return
        detect = Factory1.Detect(fn)
        if not detect:
            messagebox.showerror("Data Format Error", "Unsupported input data / file format.")
            return
        self.import_file_name.set(fn)
        self.attributes('-topmost', True)
        fbase = os.path.splitext(fn)[0]
        nodes = os.path.split(fn)
        fname = nodes[-1]
        subject = os.path.splitext(fname)[0]
        self.order_class = OrderClass(
            class_name=subject,
            table_name=subject,
            db_name=fbase + OrderDef.DbType,
            file_name=fbase + OrderDef.CodeType)
        self.pref['Csv Folder'] = os.path.dirname(fn)
        self.order_class.home(self.pref)
        self.order_class.encoding = detect.encoding 
        self.order_class.sep      = detect.sep
        self._show_order()

    def can_create(self, gen):
        return True # TODO: Check for input & output file existance

    def _create_project(self):
        gen = DaoGen()
        if self.can_create(gen):
            self.gen_ok = gen.write_project(
                self.pref,
                self.order_class,
                self.import_file_name.get())
            return self.gen_ok
        return False

    def _create_code(self):
        gen = DaoGen()
        if self.can_create(gen):
            self.gen_ok = gen.write_code(
                self.pref,
                self.order_class,
                self.import_file_name.get())
            return self.gen_ok
        return False

    def apply(self):
        zsel = self.field_seps[int(self.field_sel.get())]
        try:
            if zsel == "Gen Proj":
                if self.verbose:
                    print("Verbose: Creating Project")
                    self.gen_ok = self._create_project()
            elif zsel == "Gen Code":
                    print("Verbose: Creating Code")
                    self.gen_ok = self._create_code()
            else:
                    print("Verbose: Creating Both")
                    self.gen_ok = self._create_project()
                    if self.gen_ok:
                        self.gen_ok = self._create_code()
        except Exception as ex:
            self.gen_ok = False
            if self.verbose:
                print(ex)
        finally:
            if self.verbose:
                if self.gen_ok:
                    messagebox.showinfo("Generation Success", "File has been generated.")
                else:
                    messagebox.showerror("Data Format Error", "Review input file & definitions to try again.")

    def body(self, zframe):
        self.title(self.ztitle)
        self.resizable(width=False, height=False)
        self.attributes('-topmost',True)
        
        # File Selection
        zfa = LabelFrame(zframe, text=" Table ")
        
        Button(zfa,
               text=" ... ",
               command=self.on_txt_fn
               ).grid(column=0, row=0)
        Label(zfa, text="File: ").place(relx=0.1, rely=0.2)
        efn = Entry(zfa, width=50, textvariable=self.import_file_name)
        efn.place(relx=0.2, rely=0.2)

        # Radio Group
        fradio = LabelFrame(zframe, text = " Field Sep")

        for ss, key in enumerate(self.field_seps):
            zrb = Radiobutton(
                fradio, text=" " + key,
                variable=self.field_sel,
                value=ss)
            zrb.grid(column=ss, row=0)

        # Order Metadata
        zfb = LabelFrame(zframe, text=" Detection ")
        for ss, key in enumerate(self.display_info):
            zShow = self.display_info[key]
            print(key, zShow)
            Label(zfb, text=key + ": ").grid(column=0, row=ss)
            Entry(zfb, width=50, state='readonly',
                  textvariable=zShow).grid(column=1, row=ss)

        zfa.pack(fill=BOTH)
        fradio.pack(fill=BOTH)
        zfb.pack(fill=BOTH)
        zframe.pack(fill=BOTH)
        return self