Initial commit
This commit is contained in:
commit
4abd7435e2
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.venv/
|
||||||
|
.venv
|
28
LICENSE
Normal file
28
LICENSE
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
BSD 3-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2023, Јован Ђокић-Шумарац
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
typer[all]
|
||||||
|
pyYAML
|
100
spyglass.py
Executable file
100
spyglass.py
Executable file
|
@ -0,0 +1,100 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
import os
|
||||||
|
import yaml
|
||||||
|
import platform
|
||||||
|
from pathlib import Path
|
||||||
|
# from rich import print
|
||||||
|
|
||||||
|
import typer
|
||||||
|
|
||||||
|
|
||||||
|
app = typer.Typer()
|
||||||
|
project_subcommand = typer.Typer()
|
||||||
|
app.add_typer(project_subcommand, name='project')
|
||||||
|
|
||||||
|
entries: list = []
|
||||||
|
|
||||||
|
|
||||||
|
@project_subcommand.command('add')
|
||||||
|
def project_add(path: Path, name: str = None, runner: str = None, editor: str = 'code', multi: bool = False):
|
||||||
|
|
||||||
|
global entries
|
||||||
|
|
||||||
|
if multi:
|
||||||
|
if name:
|
||||||
|
print('\n[bold red]Cannot name multiple projects the same.\n')
|
||||||
|
exit(69)
|
||||||
|
|
||||||
|
for dir in os.listdir(path):
|
||||||
|
print(dir)
|
||||||
|
entries.append(assamble_entry(path/dir, name, runner, editor))
|
||||||
|
else:
|
||||||
|
entries.append(assamble_entry(path, name, runner, editor))
|
||||||
|
|
||||||
|
for project in entries:
|
||||||
|
store_project(project)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_database_path():
|
||||||
|
database: str = None
|
||||||
|
os_type: str = platform.system()
|
||||||
|
|
||||||
|
if os_type == 'Linux':
|
||||||
|
database = os.environ.get('HOME') + '/.local/share/spyglass-db.yaml'
|
||||||
|
elif os_type == 'Windows':
|
||||||
|
database = os.environ.get('APPDATA') + 'spyglass-db.yaml'
|
||||||
|
else:
|
||||||
|
print('OS not supported')
|
||||||
|
exit(69)
|
||||||
|
|
||||||
|
return database
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def store_project(project: dict):
|
||||||
|
database: str = get_database_path()
|
||||||
|
|
||||||
|
if not os.path.exists(database):
|
||||||
|
with open(database, 'w') as f:
|
||||||
|
f.write('projects:')
|
||||||
|
data = None
|
||||||
|
with open(database, 'r') as f:
|
||||||
|
data = yaml.safe_load(f)
|
||||||
|
|
||||||
|
project_list: list = [proj for proj in data['projects'] if os.path.isdir(proj)]
|
||||||
|
|
||||||
|
print(project_list)
|
||||||
|
|
||||||
|
if project not in project_list:
|
||||||
|
project_list.append(project)
|
||||||
|
else:
|
||||||
|
print('Project already exists')
|
||||||
|
|
||||||
|
with open(database, 'w') as f:
|
||||||
|
yaml.safe_dump(data, f)
|
||||||
|
|
||||||
|
|
||||||
|
def assamble_entry(path: Path, name: str, runner: str, editor: str):
|
||||||
|
if not name:
|
||||||
|
name = os.path.dirname(path)
|
||||||
|
|
||||||
|
if not runner:
|
||||||
|
runner = 'None'
|
||||||
|
|
||||||
|
if not editor:
|
||||||
|
editor = 'code'
|
||||||
|
|
||||||
|
return {name: {'path': str(path), 'runner': str(path/runner), 'editor': editor}}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
app()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in a new issue