class FileLogSchemaModel:

    query = Query()

    def __init__(self, db_path):
        dbpath = os.path.join(db_path, 'filelogdb.json')
        self._db = TinyDB(dbpath)

    def req_fields_json(self, dict_result):
        req_items = ['flid', 'filename', 'filesize', 'filetype', 'bizdomain', 'uploadtm', 'ingesttm', 'filedesc', 'filedelim', 'fileencode', 'tags', 'state', "nodename", "bizdomain"]
        return {key: value for key, value in dict_result.items()
                if key in req_items}

    def search_op(self, req_dict):
        ####exists = self.search_res(req_dict['db']) && self.search_res(req_dict['schema']) && self.search_res(req_dict['table']) && self.search_res(req_dict['fieldname'])
        exists = self._db.search((FileLogSchemaModel.query.filename == req_dict['filename']) & (FileLogSchemaModel.query.filetype == req_dict['filetype']))

        return True if exists else False

    def get_allrecs(self):
        return self._db.search(FileLogSchemaModel.query.flid > 0)

    
    def search_res(self, filename):
        return self._db.search(FileLogSchemaModel.query.filename == filename)

    
    def search_file(self, filename):
        ctentry = Query();
        #res_info = self._db.search(ctentry.fieldname == fieldname)
        res_info = self._db.search(ctentry.filename.search(filename,flags=re.IGNORECASE))
        return res_info

    def search_file_byid(self, fileid):
        ctentry = Query()
        res_info = self._db.search(ctentry.flid.search(fileid))
        return res_info

    def search_file_bynode(self, node):
        ctentry = Query()
        res_info = self._db.search(ctentry.nodename.search(node))
        return res_info
    
    def get_total_recs(self):
        ctqry = Query();
        dlen = self._db.count(ctqry.flid > 0)
        return dlen
            
    def insert_op(self, req_dict):
        if not self.search_op(req_dict):
            rec_id = self._db.__len__()
            req_dict['flid'] = rec_id+1;
            self._db.insert(req_dict)
            return self._db.all()
        return "None_Insert"

    def delete_op(self, req_dict):
        if self.search_op(req_dict):
            self._db.remove(where('fieldname') == req_dict['fieldname'])
            return self._db.all()
        return "None_Delete"

    def update_op(self, old_fieldname, req_dict):
        if not self.search_res(old_fieldname):
            return False
        self._db.update({'db': req_dict['db'],
                         'schema': req_dict['schema'],
                         'table': req_dict['table'],
                         'fieldname': req_dict['fieldname'],
                         'fielddtype': req_dict['fielddtype']
                         },
                        SchemaModel.query.fieldname == old_fieldname)

        return self._db.all()
     
    def update_state_bynode(self, nodename, state):
        if not self.search_file_bynode(nodename):
            return False
        self._db.update({'state': state
                         },
                        FileLogSchemaModel.query.nodename == nodename)

        return True
     


    
    
    def stop_db(self):
        self._db.close()
Exemple #2
0
### Aufgabe 2 - Kontaktbuch - CSV mit Dicts ####

import csv
from tinydb import TinyDB, Query

db = TinyDB( 'db.json' )

kontaktListe = Query()

db_len = db.__len__()

print("--- Kontaktbuch mit DB usw was los???? OK . ---")

def menuAnzeigen():

    print("\n Menü: \n - 1 Alle Kontakte anzeigen \n - 2 Kontakt hinzufügen \n - 3 Einzelne Kontakte suchen \n - 4 Einzelne Kontakte ändern \n - 5 Kontakt löschen \n - 6 Export in eine CSV Datei (Speichern) \n - 7 Import aus einer CSV Datei (Laden) \n - 8 Programm beenden")
    print("Bitte geben Sie das Menü an: ")


def navigation():

    try:
        navigation_eingabe = int(input())

        if navigation_eingabe == 1:
            kontakteAnzeigen()

        elif navigation_eingabe == 2:
            kontaktHinzufugen()

        elif navigation_eingabe == 3: