import appdirs import os from configparser import ConfigParser import csv from typing import Iterable from properties import config from model import RecentProject class AppDataStorage(object): SETTINGS_FILE = "config.ini" RECENT_PROJECTS_FILE = "recent.cfg" def __init__(self): self.__appDataPath = appdirs.user_data_dir(config.APP_NAME, config.APP_AUTHOR_SHORT, config.APP_VERSION) self.__settingsPath = os.path.join(self.__appDataPath, AppDataStorage.SETTINGS_FILE) self.__recentProjectsPath = os.path.join(self.__appDataPath, AppDataStorage.RECENT_PROJECTS_FILE) # make missing dirs in path def __createPath(self, path): dir = os.path.dirname(path) os.makedirs(dir, exist_ok=True) def readSettings(self): if (os.path.exists(self.__settingsPath)): parser = ConfigParser() parser.read(self.__settingsPath) # todo: finish this def writeSettings(self, settings): pass # todo: finish this """ Reads recent projects list. Returns: list of recent projects """ def readRecentProjects(self) -> Iterable[RecentProject]: if (os.path.exists(self.__recentProjectsPath)): with open(self.__recentProjectsPath, 'r') as recentProjectsFile: csvreader = csv.DictReader(recentProjectsFile, fieldnames=RecentProject.DICT_FIELDS) for row in csvreader: try: yield RecentProject.fromDictionary(row) except ValueError: print("Recent projects parse error - invalid date.", row) except KeyError: print("Recent projects parse error - fields not valid.", row) """ Writes a list of recent projects. Args: items: list of RecentProjects """ def writeRecentProjects(self, items : Iterable[RecentProject]) -> None: self.__createPath(self.__recentProjectsPath) with open(self.__recentProjectsPath, 'w') as recentProjectsFile: csvwriter = csv.DictWriter(recentProjectsFile, RecentProject.DICT_FIELDS) for item in items: csvwriter.writerow(item.toDictionary())