aboutsummaryrefslogtreecommitdiff
path: root/src/main.py
blob: 1f07785b33897bc21dceaef6766336f0121ac561 (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
# Manage book library.
#
# Required dependencies:
#
#   - python >= 3.11
#   - requests
#   - pillow
#   - ebook-convert CLI (from calibre)

import sys
import os

import library.command
import view.command
import new.command

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 browser-cmd

Environment variables:

    BOOK_LIBRARY: path to book library.""")

def get_book_library():
    path = os.getenv('BOOK_LIBRARY')
    if path is None or not os.path.isdir(path):
        print_help(title='BOOK_LIBRARY environment variable is required.')
        exit(1)
    else:
        return path

def main():
    match sys.argv:
        case [ _, 'new' ]:
            book_library = get_book_library()
            new.command.run(book_library)
        case [ _, 'new', book_source ]:
            if os.path.isfile(book_source):
                book_library = get_book_library()
                new.command.run(book_library, book_source)
            else:
                print_help(title=f'File not found: {book_source}.')
                exit(1)
        case [ _, 'library' ]:
            book_library = get_book_library()
            library.command.run(book_library)
        case [ _, 'view', browser_cmd ]:
            book_library = get_book_library()
            view.command.run(book_library, browser_cmd)
        case [ _, '--help' ]:
            print_help()
        case [ _, '-h' ]:
            print_help()
        case _:
            print_help('Command not found.')
            exit(1)

if __name__ == "__main__":
        main()