ember/storage/appdata_storage.py

49 lines
1.8 KiB
Python

import appdirs
import os
from configparser import ConfigParser
import csv
from properties import config
class AppDataStorage(object):
SETTINGS_FILE = "config.ini"
RECENT_PROJECTS_FILE = "recent.cfg"
RECENT_PROJECTS_FIELDS = ["path", "name", "date", "pinned"]
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
def __parseRow(self, row):
row['date'] = int(row['date'])
row['pinned'] = row['pinned'].lower() in ['true', 'yes', '1', 'on']
return row
def readRecentProjects(self):
if (os.path.exists(self.__recentProjectsPath)):
with open(self.__recentProjectsPath, 'r') as recentProjectsFile:
csvreader = csv.DictReader(recentProjectsFile, fieldnames=AppDataStorage.RECENT_PROJECTS_FIELDS)
for row in csvreader:
yield self.__parseRow(row)
def writeRecentProjects(self, items):
self.__createPath(self.__recentProjectsPath)
with open(self.__recentProjectsPath, 'w') as recentProjectsFile:
csvwriter = csv.DictWriter(recentProjectsFile, AppDataStorage.RECENT_PROJECTS_FIELDS)
csvwriter.writerows(items)