code style
[edumed.git] / catalogue / management / commands / importlessons.py
1 # -*- coding: utf-8 -*-
2 # This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 import os
6 import sys
7 from optparse import make_option
8
9 from django.core.management.base import BaseCommand
10 from django.core.management.color import color_style
11 from django.db import transaction
12
13 from catalogue.models import Lesson, Section
14 from librarian import IOFile
15
16 # from search import Index
17
18
19 class Command(BaseCommand):
20     option_list = BaseCommand.option_list + (
21         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
22                     help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
23         make_option('-a', '--attachments', dest='attachments', metavar="PATH", default='materialy',
24                     help='Attachments dir path.'),
25         make_option('--ignore-incomplete', action='store_true', dest='ignore_incomplete', default=False,
26                     help='Attachments dir path.'),
27     )
28     help = 'Imports lessons from the specified directories.'
29     args = 'directory [directory ...]'
30
31     def import_book(self, file_path, options, attachments, ignore_incomplete=False):
32         verbose = options.get('verbose')
33         iofile = IOFile.from_filename(os.path.join(self.curdir, file_path))
34         iofile.attachments = attachments
35         return Lesson.publish(iofile, ignore_incomplete)
36
37     @staticmethod
38     def all_attachments(path):
39         files = {}
40
41         def read_dir(path):
42             for name in os.listdir(path):
43                 fullname = os.path.join(path, name)
44                 if os.path.isdir(fullname):
45                     read_dir(fullname)
46                 else:
47                     f = IOFile.from_filename(fullname)
48                     files[name.decode('utf-8')] = f
49                     files.setdefault(name.replace(" ", "").decode('utf-8'), f)
50
51         read_dir(path)
52         return files
53
54     @transaction.atomic
55     def handle(self, *directories, **options):
56
57         levels = set()
58         self.style = color_style()
59         
60         verbose = options.get('verbose')
61         self.curdir = os.path.abspath(os.curdir)
62
63         files_imported = 0
64         files_skipped = 0
65
66         for dir_name in directories:
67             abs_dir = os.path.join(self.curdir, dir_name)
68             if not os.path.isdir(abs_dir):
69                 print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir)
70             else:
71                 att_dir = os.path.join(abs_dir, options['attachments'])
72                 attachments = self.all_attachments(att_dir)
73
74                 # files queue
75                 files = sorted(os.listdir(abs_dir))
76                 postponed = {}
77                 ignore_incomplete = set()
78                 while files:
79                     file_name = files.pop(0)
80                     file_path = os.path.join(abs_dir, file_name)
81                     file_base, ext = os.path.splitext(file_path)
82
83                     # Skip files that are not XML files
84                     if not ext == '.xml':
85                         continue
86
87                     if verbose > 0:
88                         print "Parsing '%s'" % file_path
89                     else:
90                         sys.stdout.write('.')
91                         sys.stdout.flush()
92
93                     # Import book files
94                     try:
95                         lesson = self.import_book(
96                             file_path, options, attachments,
97                             ignore_incomplete=file_name in ignore_incomplete)
98                     except Section.IncompleteError, e:
99                         if file_name not in postponed or postponed[file_name] < files_imported:
100                             # Push it back into the queue, maybe the missing lessons will show up.
101                             if verbose > 0:
102                                 print self.style.NOTICE('Waiting for missing lessons.')
103                             files.append(file_name)
104                             postponed[file_name] = files_imported
105                         elif options['ignore_incomplete'] and file_name not in ignore_incomplete:
106                             files.append(file_name)
107                             ignore_incomplete.add(file_name)
108                             postponed[file_name] = files_imported
109                         else:
110                             # We're in a loop, nothing's being imported - some lesson is really missing.
111                             raise e
112                     except BaseException:
113                         import traceback
114                         traceback.print_exc()
115                         files_skipped += 1
116                     else:
117                         files_imported += 1
118                         if hasattr(lesson, 'level'):
119                             levels.add(lesson.level)
120                     finally:
121                         if verbose > 0:
122                             print
123
124         if levels:
125             print "Rebuilding level packages:"
126             for level in levels:
127                 print level.name
128                 level.build_packages()
129
130         # Print results
131         print
132         print "Results: %d files imported, %d skipped, %d total." % (
133             files_imported, files_skipped, files_imported + files_skipped)
134         print