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.
 
   7 from optparse import make_option
 
   9 from django.core.management.base import BaseCommand
 
  10 from django.db import transaction
 
  12 from catalogue.models import Lesson, Section
 
  13 from librarian import IOFile
 
  16 class Command(BaseCommand):
 
  17     option_list = BaseCommand.option_list + (
 
  18         make_option('-q', '--quiet', action='store_false', dest='verbose', default=True,
 
  19                     help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
 
  20         make_option('-a', '--attachments', dest='attachments', metavar="PATH", default='materialy',
 
  21                     help='Attachments dir path.'),
 
  22         make_option('--ignore-incomplete', action='store_true', dest='ignore_incomplete', default=False,
 
  23                     help='Attachments dir path.'),
 
  24         make_option('--dont-repackage', action='store_false', dest='repackage', default=True,
 
  25                     help='Don\'t refresh level packages.'),
 
  27     help = 'Imports lessons from the specified directories.'
 
  28     args = 'directory [directory ...]'
 
  31         super(Command, self).__init__()
 
  36     def all_attachments(path):
 
  38         if not os.path.isdir(path):
 
  42             for name in os.listdir(path):
 
  43                 fullname = os.path.join(path, name)
 
  44                 if os.path.isdir(fullname):
 
  47                     f = IOFile.from_filename(fullname)
 
  48                     files[name.decode('utf-8')] = f
 
  49                     files.setdefault(name.replace(" ", "").decode('utf-8'), f)
 
  55     def handle(self, *directories, **options):
 
  57         repackage = options.get('repackage')
 
  61         curdir = os.path.abspath(os.curdir)
 
  62         self.options = options
 
  67         for dir_name in directories:
 
  68             abs_dir = os.path.join(curdir, dir_name)
 
  69             if not os.path.isdir(abs_dir):
 
  70                 print self.style.ERROR("%s: Not a directory. Skipping." % abs_dir)
 
  72                 files_imported_dir, files_skipped_dir = self.import_from_dir(abs_dir)
 
  73                 files_imported += files_imported_dir
 
  74                 files_skipped += files_skipped_dir
 
  76         if self.levels and repackage:
 
  77             print "Rebuilding level packages:"
 
  78             for level in self.levels:
 
  80                 level.build_packages()
 
  84         print "Results: %d files imported, %d skipped, %d total." % (
 
  85             files_imported, files_skipped, files_imported + files_skipped)
 
  88     def import_from_dir(self, abs_dir):
 
  89         verbose = self.options.get('verbose')
 
  92         att_dir = os.path.join(abs_dir, self.options['attachments'])
 
  93         attachments = self.all_attachments(att_dir)
 
  95         files = sorted(os.listdir(abs_dir))
 
  97         ignore_incomplete = set()
 
  99             file_name = files.pop(0)
 
 100             file_path = os.path.join(abs_dir, file_name)
 
 101             file_base, ext = os.path.splitext(file_path)
 
 103             if os.path.isdir(file_path):
 
 104                 dir_imported, dir_skipped = self.import_from_dir(file_path)
 
 105                 files_imported += dir_imported
 
 106                 files_skipped += files_skipped
 
 109             # Skip files that are not XML files
 
 110             if not ext == '.xml':
 
 114                 print "Parsing '%s'" % file_path
 
 116                 sys.stdout.write('.')
 
 120                 iofile = IOFile.from_filename(file_path)
 
 121                 iofile.attachments = attachments
 
 122                 lesson = Lesson.publish(iofile, file_name in ignore_incomplete)
 
 123             except Section.IncompleteError:
 
 124                 if file_name not in postponed or postponed[file_name] < files_imported:
 
 125                     # Push it back into the queue, maybe the missing lessons will show up.
 
 127                         print self.style.NOTICE('Waiting for missing lessons.')
 
 128                     files.append(file_name)
 
 129                     postponed[file_name] = files_imported
 
 130                 elif self.options['ignore_incomplete'] and file_name not in ignore_incomplete:
 
 131                     files.append(file_name)
 
 132                     ignore_incomplete.add(file_name)
 
 133                     postponed[file_name] = files_imported
 
 135                     # We're in a loop, nothing's being imported - some lesson is really missing.
 
 137             except BaseException:
 
 139                 traceback.print_exc()
 
 143                 if hasattr(lesson, 'level'):
 
 144                     self.levels.add(lesson.level)
 
 148         return files_imported, files_skipped