def main(): app = QtGui.QApplication(sys.argv) logging.info("Absolute app root: '%s'", AppSettings.APP_ABS_ROOT) # set application icon app.setWindowIcon(QtGui.QIcon(AppSettings.APP_ICON_PATH)) # create neccessary paths if missing ifNotExCreate(AppSettings.TMP_PATH) ifNotExCreate(AppSettings.BACKUP_PATH) ifNotExCreate(AppSettings.DATA_PATH) ifNotExCreate(AppSettings.DB_PATH) ifNotExCreate(AppSettings.ICONS_PATH) # preapare languages AppSettings.writeLanguage("sk") AppSettings.LANG = AppSettings.readLanguage() TransController.loadTranslation("sk") TransController.loadTranslation("en") # DB controller instance db_con = DbController() # login dialog instance login_dialog = LoginDialog(db_con) db_path = AppSettings.readDbFilePath() logging.info("DB path: '%s'", db_path) if (not os.path.exists(AppSettings.decodePath(db_path))): # if default DB file doesnt exists, run create DB dialog login_dialog.enLogIn(False) else: # leave only last 10 backups backups = sorted(os.listdir(AppSettings.BACKUP_PATH), reverse=True) if len(backups) >= 10: for i in range(9, len(backups)): os.remove(AppSettings.BACKUP_PATH + backups[i]) # first backup database backup_file = AppSettings.BACKUP_PATH + time.strftime("%Y-%m-%dT%H:%M:%S_", time.localtime()) + os.path.basename(db_path) logging.info("backup file: '%s'", backup_file) shutil.copyfile(AppSettings.decodePath(db_path), AppSettings.decodePath(backup_file)) login_dialog.show() w = MainWindow(db_con) # when succesfully logged load main window login_dialog.signalSuccessfullyLogged.connect(w.setUserReloadShow) sys.exit(app.exec_())
def ifNotExCreate(directory): """ If does not exists dir, create it. @param directory: directory to check and create """ if (not os.path.exists(AppSettings.decodePath(directory))): # missing data dir logging.info("creating dir: '%s'", directory) os.makedirs(AppSettings.decodePath(directory))
def readFile(self, file_path): """ Read file binary. Return read data. @param file_path: path to file @return: on succes binary data, else None """ data = None f = None try: logging.info("reading file: %s", file_path) f = open(AppSettings.decodePath(file_path), "rb") data = f.read() logging.info("file size: %i", len(data)) except IOError as e: logging.exception(e) raise e except: # all other exceptions logging.exception("exception, file: %s", file_path) raise "exception, file: " + file_path finally: if (f): f.close() return data
def readImage(self, path): """ Reads image and prepare it for SQLite db. @param path: path to image @return: BLOB type """ img = None icon = None # open and read icon try: logging.debug("reading icon image from path: %s", path) img = open(AppSettings.decodePath(path), "rb") icon = img.read() icon = sqlite3.Binary(icon) except IOError as e: logging.exception(e) raise e finally: if img: img.close() return icon
def loadTranslation(lang): """ Load translation from file. """ f = None try: f = open(AppSettings.decodePath(AppSettings.TRANS_PATH + lang + AppSettings.TRANS_SUFFIX)) lines = f.readlines() tmp = {} for i in range(0, len(lines) - 1): if (i % 2 == 0): logging.info(lines[i]) # create key and value, remove last \n character and decode to utf8 key = str(lines[i][:len(lines[i]) - 1]).decode('utf-8') value = str(lines[i + 1][:len(lines[i + 1]) - 1]).decode('utf-8') logging.debug("key: '%s', value: '%s'", key, value) tmp[key] = value TRANSLATION[lang] = tmp except IOError as e: logging.exception(e) raise e finally: if (f): f.close
def closeEvent(self, event): """ Do matters on close event. In example delete clipboard. """ logging.debug("deleting clipboard") QtGui.QApplication.clipboard().clear() try: logging.info("removing tmp dir: '%s'", AppSettings.TMP_PATH) # remove tmp files shutil.rmtree(AppSettings.decodePath(AppSettings.TMP_PATH)) except Exception as e: logging.exception(e) InfoMsgBoxes.showErrorMsg(e)
def writeFile(self, file_path): """ Write file to disk. @param file_path: file to write """ f = None try: f = open(AppSettings.decodePath(file_path), "wb") f.write(self._attachment_data) except IOError as e: logging.exception(e) raise e except: logging.exception("exception writting file: %s", file_path) raise e finally: if (f): f.close()