def searchStudent(rollEntry, nameEntry, dataLabel): studentRoll = rollEntry.get() studentName = nameEntry.get() nameEntry.delete(0, END) rollEntry.delete(0, END) if (studentRoll == ""): logger.warn(messages.INVALID_ENTRY) messagebox.showerror(title="INVALID_ENTRY", message=messages.INVALID_ENTRY) return studentRecord = utils.getStudentRecord(dbconn, currtable, studentRoll, studentName) schema = utils.getSchemaFromTable(dbconn, currtable) if studentRecord == None: logger.warning(messages.STUDENT_RECORD_EMPTY) return if not schema: logger.warning(messages.SCHEMA_EMPTY) return updateStudentDisplay(dataLabel, schema, studentRecord)
def preparevalues(datalist, irrelevantcolindices, dbfieldlist, currtable, rowstoload): dobindex = dbfieldlist.index("DOB") rollindex = dbfieldlist.index("Roll_Number") nameindex = dbfieldlist.index("Name") poppeditems = 0 for eachindex in irrelevantcolindices: del datalist[eachindex - poppeditems] poppeditems = poppeditems + 1 if datalist[rollindex] == None: logger.warning(messages.ROLL_NUM_EMPTY.format(rowstoload)) return False if datalist[nameindex] == None: logger.warning(messages.NAME_EMPTY.format(rowstoload)) return False if not datalist[dobindex] is None: datalist[dobindex] = processdate(datalist[dobindex]) datarow = tuple(datalist) rowtoinsert = datarow + (getage(currtable, datarow[dobindex]), ) return rowtoinsert
def createDB(dataLabel, dbname): # create a new db and pop an info/error message with the Path global currtable global dbconn global currdb if dbconn != None: logger.info(messages.DB_DETAILS_CLEAN.format(currdb, currtable)) dbconn.close() currdb = None currtable = None if (dbname != "" and dbname != None): serverdir = PROJECT_ROOT + os.path.sep + SERVER if not (dbname.endswith(".db")): dbname = dbname + ".db" logger.info(messages.USER_GIVEN_DBNAME.format(dbname)) dbpath = Path(serverdir + os.path.sep + dbname) dbconn = utils.connectwithDB(dbpath) if dbconn: currdb = dbname logger.info(messages.CURRDB_VALUE.format(currdb)) dataLabel.configure(text="Successfully connected to " + currdb + " database") else: dataLabel.configure(text="Currently not connected to any Database") logger.warning(messages.INVALID_DB_NAME_ENTERED.format(dbname)) messagebox.showinfo(title='INVALID DB NAME', message=messages.INVALID_DB_NAME)
def processtabledetails(klass, term, year, dataLabel): if (dbconn == None or currdb == None): logger.warning(messages.SELECT_DB_FIRST) messagebox.showinfo(title="SELECT A DATABASE FIRST", message=messages.SELECT_DB_FIRST) return global currtable if currtable != None: logger.info(messages.TABLE_DETAILS_CLEAN.format(currtable)) currtable = None tablename = "class_" + klass + "_term_" + term + "_year_" + year schemaFile = Path(PROJECT_ROOT + os.path.sep + CONFIG + os.path.sep + SCHEMA_FILE) result = utils.createtable(dbconn, tablename, schemaFile) if (result == True): currtable = tablename logger.info(messages.CURRTABLE_VALUE.format(currtable)) dataLabel.configure(text="Connected to the Table " + currtable + " \npresent in the Database " + currdb) else: messagebox.showerror(title="TABLE_SELECTION_ERROR", message=messages.TABLE_SELECTION_ERROR) currtable = None
def loadData(filedisplay): global loadfilename if (loadfilename == None or loadfilename == ""): messagebox.showinfo(title="NO_EXCEL_FILE_SELECTED", message=messages.NO_EXCEL_FILE_SELECTED) logger.warning(messages.NO_EXCEL_FILE_SELECTED) return fieldmapfile = Path(PROJECT_ROOT + os.path.sep + CONFIG + os.path.sep + FIELDMAP_FILE) insertedrows, rowstoload = utils.exceltosql(dbconn, currtable, loadfilename, fieldmapfile) result = (insertedrows == rowstoload) if (result == True): logger.info( messages.DATA_LOAD_PASS.format(insertedrows, currtable, loadfilename)) filedisplay.configure( text=str(insertedrows) + " records have been successfully uploaded to \n" + currtable) else: logger.info( messages.DATA_LOAD_FAIL.format(insertedrows, currtable, loadfilename)) messagebox.showerror(title="DATA_UPLOAD_ERROR", message=messages.DATA_UPLOAD_ERROR.format( insertedrows, rowstoload))
def change_permissions(path, permission="0755", recursive=True): '''change_permissions will use subprocess to change permissions of a file or directory. Recursive is default True :param path: path to change permissions for :param permission: the permission level (default is 0755) :param recursive: do recursively (default is True) ''' if not isinstance(permission, str): logger.warning( "Please provide permission as a string, not number! Skipping.") else: permission = str(permission) cmd = ["chmod", permission, "-R", path] if recursive == False: cmd = ["chmod", permission, path] logger.info("Changing permission of %s to %s with command %s", path, permission, " ".join(cmd)) return run_command(cmd)
def get_fullpath(file_path, required=True): '''get_fullpath checks if a file exists, and returns the full path to it if it does. If required is true, an error is triggered. :param file_path: the path to check :param required: is the file required? If True, will exit with error ''' file_path = os.path.abspath(file_path) if os.path.exists(file_path): return file_path # If file is required, we exit if required == True: logger.error("Cannot find file %s, exiting.", file_path) sys.exit(1) # If file isn't required and doesn't exist, return None logger.warning("Cannot find file %s", file_path) return None
def get_fullpath(file_path,required=True): '''get_fullpath checks if a file exists, and returns the full path to it if it does. If required is true, an error is triggered. :param file_path: the path to check :param required: is the file required? If True, will exit with error ''' file_path = os.path.abspath(file_path) if os.path.exists(file_path): return file_path # If file is required, we exit if required == True: logger.error("Cannot find file %s, exiting.",file_path) sys.exit(1) # If file isn't required and doesn't exist, return None logger.warning("Cannot find file %s",file_path) return None
def getStudentRecord(dbconn, currtable, studentRoll, studentName): searchQuery = formSearchQuery(currtable, studentRoll, studentName) logger.info(messages.STUDENT_SEARCH_QUERY.format(searchQuery)) try: searchcursor = dbconn.cursor() searchcursor.execute(searchQuery) studentRecords = searchcursor.fetchall() if (len(studentRecords) == 0): logger.warning(messages.RECORD_NOT_FOUND) messagebox.showerror(title="RECORD_NOT_FOUND", message=messages.RECORD_NOT_FOUND) return None if (len(studentRecords) > 1): messagebox.showerror(title="DUPLICATE_RECORDS", message=messages.DUPLICATE_RECORDS) return None studentRecord = studentRecords[0] logger.info(messages.OBTAINED_RECORD.format(str(studentRecord))) return studentRecord except sqlite3.Error as e: messagebox.showerror(title="SEARCH_FAILED", message=messages.SEARCH_FAILED) logger.critical(messages.SEARCH_FAILED.format(searchQuery)) logger.critical(e, exc_info=True) return None except: messagebox.showerror(title="UNEXPECTED_ERROR", message=messages.UNEXPECTED_ERROR) logger.error(messages.UNEXPECTED_ERROR) logger.error("StackTrace : ", exc_info=True) return None
def chooseFile(filedisplay): global loadfilename if loadfilename != None: logger.info(messages.LOADFILE_CLEAR.format(loadfilename)) loadfilename = None chosenfile = filedialog.askopenfilename(initialdir=Path(PROJECT_ROOT), title="Select an Excel file", filetypes=(("Excel workbook", "*.xlsx"), )) if chosenfile == None or chosenfile == "": logger.warning(messages.NO_EXCEL_FILE_SELECTED) messagebox.showinfo(title="NO_EXCEL_FILE_SELECTED", message=messages.NO_EXCEL_FILE_SELECTED) loadfilename = None else: loadfilename = chosenfile logger.info(messages.LOADFILE_VALUE.format(loadfilename)) filedisplay.configure( text="The Student data will be imported from \n " + loadfilename)
def selectDB(dataLabel): # open the DB. and open an connection. Store it as a global variable. Show error message global currtable global dbconn global currdb if dbconn != None: logger.info(messages.DB_DETAILS_CLEAN.format(currdb, currtable)) dbconn.close() currdb = None currtable = None serverdir = PROJECT_ROOT + os.path.sep + SERVER filepattern = serverdir + os.path.sep + "*.db" if not (glob.glob(filepattern)): logger.warning(messages.NO_DB_EXISTS) messagebox.showerror(title="No DATABASE PRESENT", message=messages.NO_DB_EXISTS) return else: dbpath = filedialog.askopenfilename(initialdir=Path(SERVER), title="Select an Database file", filetypes=(("SQLite3 Databases", "*.db"), )) if dbpath == '': logger.warn(messages.INVALID_DB_SELECTION) messagebox.showinfo(title="INVALID_DB_SELECTION", message=messages.INVALID_DB_SELECTION) dataLabel.configure(text="Currently not connected to any Database") return else: dbname = os.path.basename(Path(dbpath)) dbconn = utils.connectwithDB(dbpath) if dbconn: currdb = dbname logger.info(messages.CURRDB_VALUE.format(currdb)) dataLabel.configure(text="Successfully connected to " + currdb + " database")