2024-02-04 02:44:48 +01:00
|
|
|
import os
|
|
|
|
import shelve as shelf
|
|
|
|
import platform
|
|
|
|
from pathlib import Path
|
2024-02-10 18:49:38 +01:00
|
|
|
from utils import die, Project
|
|
|
|
|
2024-02-04 02:44:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Shelf:
|
|
|
|
def __init__(self, filename: str):
|
|
|
|
self.file: Path = None
|
|
|
|
self.db = None
|
|
|
|
|
|
|
|
os_type: str = platform.system()
|
|
|
|
|
|
|
|
if os_type == 'Linux':
|
|
|
|
self.file = os.environ.get('HOME') + '/.local/share/' + filename
|
|
|
|
elif os_type == 'Windows':
|
|
|
|
self.file = os.environ.get('APPDATA') + filename
|
|
|
|
else:
|
|
|
|
die('OS not supported', 2)
|
|
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
# Open the shelf and load data into self.db
|
|
|
|
self.db = shelf.open(self.file, writeback=True)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
# Save any updates to the shelf
|
|
|
|
if self.db is not None:
|
|
|
|
self.db.sync()
|
|
|
|
self.db.close()
|
|
|
|
|
|
|
|
|
2024-02-10 18:49:38 +01:00
|
|
|
def update(self, entries: list) -> None:
|
2024-02-04 02:44:48 +01:00
|
|
|
|
2024-02-10 18:49:38 +01:00
|
|
|
for entry in entries:
|
|
|
|
if self.db.get(entry.name, None) != entry:
|
|
|
|
self.db[entry.name] = entry
|
2024-02-04 02:44:48 +01:00
|
|
|
|
|
|
|
|
2024-02-10 18:49:38 +01:00
|
|
|
def list(self):
|
2024-02-04 02:44:48 +01:00
|
|
|
|
2024-02-10 18:49:38 +01:00
|
|
|
for _, proj in self.db.items():
|
|
|
|
project: Project = proj
|
|
|
|
print(str(project))
|