commit 4abd7435e2426a8e9f29c2c1c94d6e1b20867d85 Author: Јован Ђокић-Шумарац Date: Sat Jan 13 20:06:21 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c554a7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv/ +.venv diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..73b66b0 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a9e38a --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# spyglass \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0e56538 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +typer[all] +pyYAML diff --git a/spyglass.py b/spyglass.py new file mode 100755 index 0000000..93ccabe --- /dev/null +++ b/spyglass.py @@ -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()