def addBackgroundImage(self): lastDirectory = self.mw.welcome.getLastAccessedDirectory() """File dialog that request an existing file. For opening an image.""" filename = QFileDialog.getOpenFileName( self, self.tr("Open Image"), lastDirectory, self.tr("Image files (*.jpg; *.jpeg; *.png)"))[0] if filename: try: px = QPixmap() valid = px.load(filename) del px if valid: shutil.copy(filename, writablePath("resources/backgrounds")) return os.path.basename(filename) else: QMessageBox.warning( self, self.tr("Error"), self.tr("Unable to load selected file")) except Exception as e: QMessageBox.warning( self, self.tr("Error"), self.tr("Unable to add selected image:\n{}").format( str(e))) return None
def newTheme(self): path = writablePath("resources/themes") name = self.tr("newtheme") if os.path.exists(os.path.join(path, "{}.theme".format(name))): i = 1 while os.path.exists(os.path.join(path, "{}_{}.theme".format(name, i))): i += 1 name = os.path.join(path, "{}_{}.theme".format(name, i)) else: name = os.path.join(path, "{}.theme".format(name)) settings = QSettings(name, QSettings.IniFormat) settings.setValue("Name", self.tr("New theme")) settings.sync() self.populatesThemesList()
def newTheme(self): path = writablePath("resources/themes") name = self.tr("newtheme") if os.path.exists(os.path.join(path, "{}.theme".format(name))): i = 1 while os.path.exists(os.path.join(path, "{}_{}.theme".format(name, i))): i += 1 name = os.path.join(path, "{}_{}.theme".format(name, i)) else: name = os.path.join(path, "{}.theme".format(name)) settings = QSettings(name, QSettings.IniFormat) settings.setValue("Name", self.tr("New theme")) settings.sync() self.populatesThemesList()
def addBackgroundImage(self): lastDirectory = self.mw.welcome.getLastAccessedDirectory() """File dialog that request an existing file. For opening an image.""" filename = QFileDialog.getOpenFileName(self, self.tr("Open Image"), lastDirectory, self.tr("Image files (*.jpg; *.jpeg; *.png)"))[0] if filename: try: px = QPixmap() valid = px.load(filename) del px if valid: shutil.copy(filename, writablePath("resources/backgrounds")) return os.path.basename(filename) else: QMessageBox.warning(self, self.tr("Error"), self.tr("Unable to load selected file")) except Exception as e: QMessageBox.warning(self, self.tr("Error"), self.tr("Unable to add selected image:\n{}").format(str(e))) return None
def getDefaultLogFile(): """Returns a filename to log to inside {datadir}/logs/. It also prunes old logs so that we do not hog disk space excessively over time. """ # Ensure logs directory exists. logsPath = os.path.join(writablePath(), "logs") os.makedirs(logsPath, exist_ok=True) # Prune irrelevant log files. They are only kept for 35 days. try: # Guard against os.scandir() in the name of paranoia. now = time.time() with os.scandir(logsPath) as it: for f in it: try: # Avoid triggering outer try-except inside loop. if f.is_dir(): continue # If a subdirectory exists for whatever reason, don't touch it. if (now - f.stat().st_ctime) // (24 * 3600) >= 35: os.remove(f) except OSError: continue # Fail silently, but make sure we check other files. except OSError: pass # Fail silently. Don't explode and prevent Manuskript from starting. return os.path.join(logsPath, "%Y-%m-%d_%H-%M-%S_manuskript#%#.log")
def getResourcesPath(cls): path = os.path.join(writablePath(), "resources", "dictionaries", cls.getLibraryName()) if not os.path.exists(path): os.makedirs(path) return path
def getSettingsPath(self): return os.path.join(writablePath(), "exporter.ini")