Started work on Ember
This commit is contained in:
		
							
								
								
									
										44
									
								
								storage/appdata_storage.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								storage/appdata_storage.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
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 readRecentProjects(self):
 | 
			
		||||
        if (os.path.exists(self.__recentProjectsPath)):
 | 
			
		||||
            with open(self.__recentProjectsPath, 'rb') as recentProjectsFile:
 | 
			
		||||
                csvreader = csv.DictReader(recentProjectsFile, fieldnames=AppDataStorage.RECENT_PROJECTS_FIELDS)
 | 
			
		||||
                for row in csvreader:
 | 
			
		||||
                    yield row
 | 
			
		||||
 | 
			
		||||
    def writeRecentProjects(self, items):
 | 
			
		||||
        self.__createPath(self.__recentProjectsPath)
 | 
			
		||||
        with open(self.__recentProjectsPath, 'wb') as recentProjectsFile:
 | 
			
		||||
            csvwriter = csv.DictWriter(recentProjectsFile, AppDataStorage.RECENT_PROJECTS_FIELDS)
 | 
			
		||||
            csvwriter.writerows(items)
 | 
			
		||||
		Reference in New Issue
	
	Block a user