aboutsummaryrefslogtreecommitdiff
path: root/cli/main.py
blob: 01434fa168d82d0a28210696d0caffbf3bc7abf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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()