Better handling of multipart DAISY.
[librarian.git] / src / librarian / command_line.py
1 import argparse
2 import os.path
3 from .builders import builders
4 from .document import WLDocument
5
6
7 def main(*args, **kwargs):
8     parser = argparse.ArgumentParser(description="PARSER DESCRIPTION")
9
10     parser.add_argument(
11         'builder',
12         choices=builders.keys(),
13         help="Builder"
14     )
15     parser.add_argument('input_file')
16     parser.add_argument(
17         '-o', '--output-file', metavar='FILE',
18         help='specifies the output file'
19     )
20     parser.add_argument(
21         '-O', '--output-dir', metavar='DIR',
22         help='specifies the directory for output'
23     )
24
25     parser.add_argument(
26         '--mp3',
27         metavar="FILE",
28         nargs="*",
29         help='specifies an MP3 file, if needed'
30     )
31     
32     args = parser.parse_args()
33     builder = builders[args.builder]
34
35     if args.output_file:
36         output_file_path = args.output_file
37     else:
38         output_file_path = '.'.join((
39             os.path.splitext(args.input_file)[0],
40             builder.file_extension
41         ))
42         if args.output_dir:
43             output_file_path = '/'.join((
44                 args.output_dir,
45                 output_file_path.rsplit('/', 1)[-1]
46             ))
47
48     document = WLDocument(filename=args.input_file)
49
50     builder = builders[args.builder]
51     kwargs = {
52         "mp3": args.mp3,
53     }
54
55     output = document.build(builder, **kwargs)
56     with open(output_file_path, 'wb') as f:
57         f.write(output.get_bytes())