From bd0c32c5e8399ed3e7cfedeb5cda67628d1d2f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=88=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=82=D0=BE=D0=BA=D0=B8?= =?UTF-8?q?=D1=9B-=D0=A8=D1=83=D0=BC=D0=B0=D1=80=D0=B0=D1=86?= Date: Sat, 10 Feb 2024 18:49:38 +0100 Subject: [PATCH] Update mechanism and listing --- app/app.py | 70 +++++++++++++++++++++++++++++++++++------------- app/database.py | 27 ++++++++----------- app/utils.py | 15 +++++++++++ requirements.txt | 3 +-- 4 files changed, 79 insertions(+), 36 deletions(-) diff --git a/app/app.py b/app/app.py index 31b029b..9db2b31 100755 --- a/app/app.py +++ b/app/app.py @@ -3,24 +3,26 @@ import typer from pathlib import Path from database import Shelf -from utils import die +from utils import die, Project +from icecream import ic app = typer.Typer() project_subcommand = typer.Typer() app.add_typer(project_subcommand, name='project') -entries: dict = { - 'dirs': [], - 'projects': [], - 'config': {} -} - +entries: set[Project] = [] @project_subcommand.command('add') -def project_add(path: Path, name: str = None, runner: str = None, editor: str = 'code', multi: bool = False): +def project_add( + path: Path, + name: str = None, + runner: str = None, + editor: str = 'nvim', + multi: bool = False +): global entries @@ -28,27 +30,59 @@ def project_add(path: Path, name: str = None, runner: str = None, editor: str = die('Cannot specify name for a dir', 2) if name else None for dir in os.listdir(path): - entries['dirs'].append(create_entry(path/dir, name, runner, editor)) + + entry = create_entry(path/dir, dir, runner, editor, True) + + ic(path/dir) + entries.append(entry) else: - entries['projects'].append(create_entry(path, name, runner, editor)) + entry = create_entry(path.resolve(), name, runner, editor, False) + ic(entry) + entries.append(entry) with Shelf('spyglass') as shelf: shelf.update(entries) +@project_subcommand.command('remove') +def project_remove(path: Path, name: str = None, multi: bool = False): -def create_entry(path: Path, name: str, runner: str, editor: str): - if not name: - name = os.path.dirname(path) + global entries + # + # if multi: + # die('Cannot specify name for a dir', 2) if name else None + # + # for dir in os.listdir(path): + # entries['dirs'].append(create_entry(path/dir, name, runner, editor)) + # + # else: + # entries['projects'].append(create_entry(path, name, runner, editor)) + # + # with Shelf('spyglass') as shelf: + # shelf.update(entries) + # - if not runner: - runner = None - if not editor: - editor = None +@project_subcommand.command('list') +def project_list(): - return {name: {'path': str(path.resolve()), 'runner': str(path/runner) if runner else None, 'editor': editor}} + with Shelf('spyglass') as shelf: + shelf.list() + + + +def create_entry(path: Path, name: str, runner: str, editor: str, multi: bool) -> Project: + + proj: Project = Project( + path, + name if name else os.path.dirname(path), + str(path/runner) if runner else None, + editor if editor else 'nvim', + multi + ) + + return proj diff --git a/app/database.py b/app/database.py index 47dc28a..9f4a2b2 100755 --- a/app/database.py +++ b/app/database.py @@ -2,7 +2,8 @@ import os import shelve as shelf import platform from pathlib import Path -from utils import die +from utils import die, Project + class Shelf: @@ -31,22 +32,16 @@ class Shelf: self.db.sync() self.db.close() - def update(self, entries: dict): - db_values = self.db.values() - if db_values: - dirs, projects, _ = db_values - new_dirs, new_projects, _ = entries.values() + def update(self, entries: list) -> None: - for dir in new_dirs: - if dir not in dirs: - self.db['dirs'].append(dir) - - for project in new_projects: - if project not in projects: - self.db['projects'].append(project) - else: - for k, v in entries.items(): - self.db[k] = v + for entry in entries: + if self.db.get(entry.name, None) != entry: + self.db[entry.name] = entry + def list(self): + + for _, proj in self.db.items(): + project: Project = proj + print(str(project)) diff --git a/app/utils.py b/app/utils.py index e3ac1ed..0ac5214 100644 --- a/app/utils.py +++ b/app/utils.py @@ -1,5 +1,20 @@ +from pathlib import Path + def die(msg: str, code: str): print(msg) exit(code) + +class Project: + def __init__(self, dir: Path, name: str = 'proj', editor: str = 'nvim', runner: str = None, multi: bool = False): + self.name: str = name + self.dir: Path = dir + self.editor: str = editor + self.runner: str = runner + self.multi: bool = multi + + def __str__(self): + return f"{self.name} ->\n\tpath -> {self.dir}\n\teditor -> {self.editor}\n\trunner -> {self.runner}" + + diff --git a/requirements.txt b/requirements.txt index 466178a..906373b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ typer[all] -attrs >= 23.2.0 shelve2 >= 1.0 -pyYAML >= 6.0.1 +icecream