Converted project to C++.
This commit is contained in:
@ -1 +0,0 @@
|
||||
from .appdata_storage import AppDataStorage
|
@ -1,63 +0,0 @@
|
||||
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())
|
61
storage/appdatastorage.cpp
Normal file
61
storage/appdatastorage.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <QStandardPaths>
|
||||
#include <pugixml.hpp>
|
||||
|
||||
#include <properties/config.h>
|
||||
#include "appdatastorage.h"
|
||||
|
||||
namespace Ember
|
||||
{
|
||||
|
||||
AppDataStorage::AppDataStorage()
|
||||
{
|
||||
QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
m_appData = appData.toStdString();
|
||||
m_recentProjects = m_appData;
|
||||
m_recentProjects += RECENT_PROJECTS_FILENAME;
|
||||
}
|
||||
|
||||
void AppDataStorage::readRecentProjects(std::vector<RecentProject> &projects)
|
||||
{
|
||||
if (boost::filesystem::exists(m_recentProjects))
|
||||
{
|
||||
pugi::xml_document doc;
|
||||
doc.load_file(m_recentProjects.c_str());
|
||||
|
||||
for (auto& node : doc.document_element())
|
||||
{
|
||||
if (strcmp(node.name(), "recentProject") == 0)
|
||||
{
|
||||
RecentProject recent;
|
||||
recent.name = node.attribute("name").as_string();
|
||||
recent.path = node.attribute("path").as_string();
|
||||
recent.access = static_cast<time_t>(node.attribute("access").as_llong());
|
||||
recent.pinned = node.attribute("pinned").as_bool();
|
||||
projects.push_back(recent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AppDataStorage::storeRecentProjects(const std::vector<RecentProject> &projects)
|
||||
{
|
||||
pugi::xml_document doc;
|
||||
doc.append_child(pugi::node_declaration);
|
||||
|
||||
auto root = doc.append_child("recentProjects");
|
||||
for (RecentProject recent : projects)
|
||||
{
|
||||
auto node = root.append_child("recentProject");
|
||||
node.append_attribute("name").set_value(recent.name.c_str());
|
||||
node.append_attribute("path").set_value(recent.path.c_str());
|
||||
node.append_attribute("access").set_value(static_cast<long long>(recent.access));
|
||||
node.append_attribute("pinned").set_value(recent.pinned);
|
||||
}
|
||||
|
||||
// Save file, ensure directory exists
|
||||
boost::filesystem::create_directories(m_appData);
|
||||
doc.save_file(m_recentProjects.string().c_str());
|
||||
}
|
||||
|
||||
}
|
35
storage/appdatastorage.h
Normal file
35
storage/appdatastorage.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef APPDATASTORAGE_H
|
||||
#define APPDATASTORAGE_H
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <vector>
|
||||
|
||||
#include <model/project.h>
|
||||
|
||||
namespace Ember
|
||||
{
|
||||
|
||||
class AppDataStorage
|
||||
{
|
||||
public:
|
||||
AppDataStorage();
|
||||
|
||||
/**
|
||||
* @brief Reads recent projects
|
||||
* @param projects List will be saved to given vector.
|
||||
*/
|
||||
void readRecentProjects(std::vector<RecentProject>& projects);
|
||||
|
||||
/**
|
||||
* @brief Stores recent projects
|
||||
* @param projects List of projects.
|
||||
*/
|
||||
void storeRecentProjects(const std::vector<RecentProject>& projects);
|
||||
|
||||
private:
|
||||
boost::filesystem::path m_appData;
|
||||
boost::filesystem::path m_recentProjects;
|
||||
};
|
||||
|
||||
}
|
||||
#endif // APPDATASTORAGE_H
|
@ -1,59 +0,0 @@
|
||||
import os
|
||||
import json
|
||||
from typing import Iterable
|
||||
from model import Project, ProjectItem, ItemType
|
||||
from properties import config
|
||||
|
||||
def loadProject(self, projFilePath) -> Project:
|
||||
p = Project(projFilePath, self)
|
||||
p.rootDir = os.path.dirname(projFilePath)
|
||||
|
||||
|
||||
def saveProject(self, project : Project):
|
||||
pass
|
||||
|
||||
"""
|
||||
Determines the type of the project item.
|
||||
"""
|
||||
def getItemType(self, projectItem : ProjectItem) -> ItemType:
|
||||
path = projectItem.absolutePath()
|
||||
|
||||
if os.path.isdir(path):
|
||||
return ItemType.DIRECTORY
|
||||
|
||||
elif os.path.isfile(path):
|
||||
_, ext = os.path.splitext(path)
|
||||
ext = ext.lower().lstrip('.') # remove leading .
|
||||
|
||||
if ext == config.PROJECT_EXTENSION:
|
||||
return ItemType.PROJECT
|
||||
|
||||
elif ext == config.SEQUENCE_EXTENSION:
|
||||
return ItemType.SEQUENCE
|
||||
|
||||
elif ext == config.COMPOSITION_EXTENSION:
|
||||
return ItemType.COMPOSITION
|
||||
|
||||
elif ext in config.DEBUG_SUPPORTED_AUDIO:
|
||||
return ItemType.AUDIO
|
||||
|
||||
elif ext in config.DEBUG_SUPPORTED_VIDEO:
|
||||
return ItemType.VIDEO
|
||||
|
||||
elif ext in config.DEBUG_SUPPORTED_IMAGE:
|
||||
return ItemType.IMAGE
|
||||
|
||||
elif ext in config.DEBUG_SUPPORTED_SUB:
|
||||
return ItemType.SUBTITLES
|
||||
|
||||
return ItemType.UNKNOWN
|
||||
|
||||
|
||||
def getItemChildren(self, projectItem : ProjectItem) -> Iterable[ProjectItem]:
|
||||
if projectItem.itemType() == ItemType.DIRECTORY:
|
||||
for item in os.listdir(projectItem.absolutePath()):
|
||||
yield ProjectItem(item, projectItem.project, self, projectItem)
|
||||
|
||||
def getProjectItems(self, project : Project) -> Iterable[ProjectItem]:
|
||||
for item in os.listdir(project.rootDir):
|
||||
yield ProjectItem(item, project, self, None)
|
Reference in New Issue
Block a user