239da77d4bcb7b83681c1f026d30e038b84bc593
[wolnelektury.git] / apps / catalogue / management / commands / importbooks.py
1 import os
2 import sys
3 from optparse import make_option
4
5 from django.core.management.base import BaseCommand
6 from django.core.management.color import color_style
7 from django.core.files import File
8
9 from catalogue.models import Book
10
11
12 class Command(BaseCommand):
13     option_list = BaseCommand.option_list + (
14         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
15             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
16         make_option('-f', '--force', action='store_true', dest='force', default=False,
17             help='Print status messages to stdout')
18     )
19     help = 'Imports books from the specified directories.'
20     args = 'directory [directory ...]'
21
22     def handle(self, *directories, **options):
23         from django.db import transaction
24
25         self.style = color_style()
26
27         verbose = options.get('verbose')
28         force = options.get('force')
29         show_traceback = options.get('traceback', False)
30
31         # Start transaction management.
32         transaction.commit_unless_managed()
33         transaction.enter_transaction_management()
34         transaction.managed(True)
35
36         files_imported = 0
37         files_skipped = 0
38         
39         for dir_name in directories:
40             if not os.path.isdir(dir_name):
41                 print self.style.ERROR("%s: Not a directory. Skipping." % dir_name)
42             else:
43                 for file_name in os.listdir(dir_name):
44                     file_path = os.path.join(dir_name, file_name)
45                     file_base, ext = os.path.splitext(file_path)
46                     
47                     # Skip files that are not XML files
48                     if not ext == '.xml':
49                         continue
50                     
51                     if verbose > 0:
52                         print "Parsing '%s'" % file_path
53                     else:
54                         sys.stdout.write('.')
55                         sys.stdout.flush()
56                     
57                     # Import book files
58                     try:
59                         book = Book.from_xml_file(file_path, overwrite=force)
60                         files_imported += 1
61                         
62                         if os.path.isfile(file_base + '.pdf'):
63                             book.pdf_file.save('%s.pdf' % book.slug, File(file(file_base + '.pdf')))
64                             if verbose:
65                                 print "Importing %s.pdf" % file_base 
66                         if os.path.isfile(file_base + '.odt'):
67                             book.odt_file.save('%s.odt' % book.slug, File(file(file_base + '.odt')))
68                             if verbose:
69                                 print "Importing %s.odt" % file_base
70                         if os.path.isfile(file_base + '.txt'):
71                             book.txt_file.save('%s.txt' % book.slug, File(file(file_base + '.txt')))
72                             if verbose:
73                                 print "Importing %s.txt" % file_base
74                     
75                         book.save()
76                     
77                     except Book.AlreadyExists, msg:
78                         print self.style.ERROR('%s: Book already imported. Skipping. To overwrite use --force.' %
79                             file_path)
80                         files_skipped += 1
81                         
82         # Print results
83         print
84         print "Results: %d files imported, %d skipped, %d total." % (
85             files_imported, files_skipped, files_imported + files_skipped)
86         print
87                         
88         transaction.commit()
89         transaction.leave_transaction_management()
90