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 pathlib import Path
|
||||||
from database import Shelf
|
from database import Shelf
|
||||||
from utils import die
|
from utils import die, Project
|
||||||
|
|
||||||
|
from icecream import ic
|
||||||
|
|
||||||
|
|
||||||
app = typer.Typer()
|
app = typer.Typer()
|
||||||
project_subcommand = typer.Typer()
|
project_subcommand = typer.Typer()
|
||||||
app.add_typer(project_subcommand, name='project')
|
app.add_typer(project_subcommand, name='project')
|
||||||
|
|
||||||
entries: dict = {
|
entries: set[Project] = []
|
||||||
'dirs': [],
|
|
||||||
'projects': [],
|
|
||||||
'config': {}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@project_subcommand.command('add')
|
@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
|
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
|
die('Cannot specify name for a dir', 2) if name else None
|
||||||
|
|
||||||
for dir in os.listdir(path):
|
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:
|
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:
|
with Shelf('spyglass') as shelf:
|
||||||
shelf.update(entries)
|
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):
|
global entries
|
||||||
if not name:
|
#
|
||||||
name = os.path.dirname(path)
|
# 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:
|
@project_subcommand.command('list')
|
||||||
editor = None
|
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 shelve as shelf
|
||||||
import platform
|
import platform
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from utils import die
|
from utils import die, Project
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Shelf:
|
class Shelf:
|
||||||
|
@ -31,22 +32,16 @@ class Shelf:
|
||||||
self.db.sync()
|
self.db.sync()
|
||||||
self.db.close()
|
self.db.close()
|
||||||
|
|
||||||
def update(self, entries: dict):
|
|
||||||
db_values = self.db.values()
|
|
||||||
|
|
||||||
if db_values:
|
def update(self, entries: list) -> None:
|
||||||
dirs, projects, _ = db_values
|
|
||||||
new_dirs, new_projects, _ = entries.values()
|
|
||||||
|
|
||||||
for dir in new_dirs:
|
for entry in entries:
|
||||||
if dir not in dirs:
|
if self.db.get(entry.name, None) != entry:
|
||||||
self.db['dirs'].append(dir)
|
self.db[entry.name] = entry
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
def die(msg: str, code: str):
|
||||||
print(msg)
|
print(msg)
|
||||||
exit(code)
|
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]
|
typer[all]
|
||||||
attrs >= 23.2.0
|
|
||||||
shelve2 >= 1.0
|
shelve2 >= 1.0
|
||||||
pyYAML >= 6.0.1
|
icecream
|
||||||
|
|
Loading…
Reference in a new issue