Update mechanism and listing
This commit is contained in:
parent
374d921626
commit
bd0c32c5e8
70
app/app.py
70
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
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
|
15
app/utils.py
15
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}"
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
typer[all]
|
||||
attrs >= 23.2.0
|
||||
shelve2 >= 1.0
|
||||
pyYAML >= 6.0.1
|
||||
icecream
|
||||
|
|
Loading…
Reference in a new issue