9322eea0bde49953dbd1c8f72eebe937e51a4b8f
[wolnelektury.git] / src / catalogue / management / commands / importbooks.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 import os
5 import sys
6 from django.conf import settings
7 from django.core.management.base import BaseCommand
8 from django.core.management.color import color_style
9 from django.core.files import File
10 from django.db import transaction
11 from librarian.picture import ImageStore
12
13 from catalogue.models import Book
14 from picture.models import Picture
15
16
17 class Command(BaseCommand):
18     help = 'Imports books from the specified directories.'
19
20     def add_arguments(self, parser):
21         parser.add_argument(
22                 '-q', '--quiet', action='store_false', dest='verbose', default=True,
23                 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output')
24         parser.add_argument(
25                 '-f', '--force', action='store_true', dest='force',
26                 default=False, help='Overwrite works already in the catalogue')
27         parser.add_argument(
28                 '-D', '--dont-build', dest='dont_build', metavar="FORMAT,...",
29                 help="Skip building specified formats")
30         parser.add_argument(
31                 '-F', '--not-findable', action='store_false',
32                 dest='findable', default=True,
33                 help='Set book as not findable.')
34         parser.add_argument(
35                 '-p', '--picture', action='store_true', dest='import_picture',
36                 default=False, help='Import pictures')
37         parser.add_argument('directory', nargs='+')
38
39     def import_book(self, file_path, options):
40         verbose = options.get('verbose')
41         if options.get('dont_build'):
42             dont_build = options.get('dont_build').lower().split(',')
43         else:
44             dont_build = None
45         file_base, ext = os.path.splitext(file_path)
46         book = Book.from_xml_file(file_path, overwrite=options.get('force'),
47                                   dont_build=dont_build,
48                                   findable=options.get('findable'),
49                                   remote_gallery_url='file://' + os.path.dirname(os.path.abspath(file_base)) + '/img/'
50                                   )
51         for ebook_format in Book.ebook_formats:
52             if os.path.isfile(file_base + '.' + ebook_format):
53                 getattr(book, '%s_file' % ebook_format).save(
54                     '%s.%s' % (book.slug, ebook_format),
55                     File(file(file_base + '.' + ebook_format)),
56                     save=False
57                     )
58                 if verbose:
59                     print("Importing %s.%s" % (file_base, ebook_format))
60         book.save()
61
62     def import_picture(self, file_path, options, continue_on_error=True):
63         try:
64             image_store = ImageStore(os.path.dirname(file_path))
65             picture = Picture.from_xml_file(file_path, image_store=image_store, overwrite=options.get('force'))
66         except Exception as ex:
67             if continue_on_error:
68                 print("%s: %s" % (file_path, ex))
69                 return
70             else:
71                 raise ex
72         return picture
73
74     @transaction.atomic
75     def handle(self, **options):
76         self.style = color_style()
77
78         verbose = options.get('verbose')
79         import_picture = options.get('import_picture')
80
81         files_imported = 0
82         files_skipped = 0
83
84         for dir_name in options['directory']:
85             if not os.path.isdir(dir_name):
86                 print(self.style.ERROR("%s: Not a directory. Skipping." % dir_name))
87             else:
88                 # files queue
89                 files = sorted(os.listdir(dir_name))
90                 postponed = {}
91                 while files:
92                     file_name = files.pop(0)
93                     file_path = os.path.join(dir_name, file_name)
94                     file_base, ext = os.path.splitext(file_path)
95
96                     # Skip files that are not XML files
97                     if not ext == '.xml':
98                         continue
99
100                     if verbose > 0:
101                         print("Parsing '%s'" % file_path)
102                     else:
103                         sys.stdout.write('.')
104                         sys.stdout.flush()
105
106                     # Import book files
107                     try:
108                         if import_picture:
109                             self.import_picture(file_path, options)
110                         else:
111                             self.import_book(file_path, options)
112
113                         files_imported += 1
114
115                     except (Book.AlreadyExists, Picture.AlreadyExists):
116                         print(self.style.ERROR(
117                             '%s: Book or Picture already imported. Skipping. To overwrite use --force.' %
118                             file_path))
119                         files_skipped += 1
120
121                     except Book.DoesNotExist as e:
122                         if file_name not in postponed or postponed[file_name] < files_imported:
123                             # push it back into the queue, maybe the missing child will show up
124                             if verbose:
125                                 print(self.style.NOTICE('Waiting for missing children'))
126                             files.append(file_name)
127                             postponed[file_name] = files_imported
128                         else:
129                             # we're in a loop, nothing's being imported - some child is really missing
130                             raise e
131
132         # Print results
133         print()
134         print("Results: %d files imported, %d skipped, %d total." % (
135             files_imported, files_skipped, files_imported + files_skipped))
136         print()