ember/model/recent_project.py

60 lines
1.7 KiB
Python

import time
from typing import List
class RecentProject(object):
DICT_FIELDS = [ 'path', 'name', 'dateAccessed', 'pinned' ]
"""
Creates new instance of RecentProject.
Args:
name: name of the project
path: path to the project
dateAccessed: date (unix time) when project was last accessed
pinned: the project is pinned by the user
Remarks:
If dateAccessed = None, it will be set to the current date and time.
"""
def __init__(self, name: str, path: str, dateAccessed : int = None, pinned : bool = False):
self.name = name
self.path = path
self.dateAccessed = dateAccessed
self.pinned = pinned
if (self.dateAccessed is None):
self.dateAccessed = int(time.time())
"""
Creates new instance of RecentProject from a dictionary.
If the dateAccessed and pinned fields are strings, they are parsed.
Raises:
ValueError if date cannot be parsed.
"""
@staticmethod
def fromDictionary(data : dict) -> 'RecentProject':
name = data['name']
path = data['path']
dateAccessed = data['dateAccessed']
pinned = data['pinned']
if isinstance(dateAccessed, str):
dateAccessed = int(dateAccessed)
if isinstance(pinned, str):
pinned = pinned.lower() in [ 'yes', 'y', 'true', '1', 'on' ]
return RecentProject(name, path, dateAccessed, pinned)
"""
Serialize to dictionary.
"""
def toDictionary(self) -> dict:
return {
"name" : self.name,
"path" : self.path,
"dateAccessed" : self.dateAccessed,
"pinned" : self.pinned
}