diff options
Diffstat (limited to 'cli/main.py')
-rw-r--r-- | cli/main.py | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/cli/main.py b/cli/main.py new file mode 100644 index 0000000..01434fa --- /dev/null +++ b/cli/main.py @@ -0,0 +1,80 @@ +# Manage book library. +# +# Required dependencies: +# +# - python >= 3.11 +# - requests +# - pillow +# - ebook-convert CLI (from calibre) + +import cli.library.command +import cli.new.command +import cli.view.command +import os +import sys + +def main(bin_dir): + match sys.argv: + case [ _, 'new' ]: + books_library = get_books_library() + cli.new.command.run(books_library) + case [ _, 'new', book_source ]: + if os.path.isfile(book_source): + books_library = get_books_library() + cli.new.command.run(books_library, book_source) + else: + print_help(title=f'File not found: {book_source}.') + exit(1) + case [ _, 'library' ]: + books_library = get_books_library() + cli.library.command.run(books_library) + case [ _, 'view' ]: + books_library = get_books_library() + books_browser = get_env_var('BOOKS_BROWSER') + cli.view.command.run(books_library, books_browser, bin_dir) + case [ _, '--help' ]: + print_help() + case [ _, '-h' ]: + print_help() + case _: + print_help('Command not found.') + exit(1) + +def get_books_library(): + books_library = get_env_var('BOOKS_LIBRARY') + if os.path.isdir(books_library): + return books_library + else: + print_help(title=f'BOOKS_LIBRARY {books_library} not found.') + exit(1) + +def get_env_var(key): + value = os.getenv(key) + if value: + return value + else: + print_help(title=f'{key} environment variable is required.') + exit(1) + +def print_help(title='Manage book library'): + print(f"""{title} + +- Insert book entry with optional ebook file: + + $ python {sys.argv[0]} new [path-to-book] + +- Print library metadata as json: + + $ python {sys.argv[0]} library + +- View books in web page: + + $ python {sys.argv[0]} view + +Environment variables: + + BOOKS_LIBRARY: path to book library, + BOOKS_BROWSER: browser command executed to view the library.""") + +if __name__ == "__main__": + main() |