+
+ def import_from_dir(self, abs_dir):
+ verbose = self.options.get('verbose')
+ files_imported = 0
+ files_skipped = 0
+ att_dir = os.path.join(abs_dir, self.options['attachments'])
+ attachments = self.all_attachments(att_dir)
+ # files queue
+ files = sorted(os.listdir(abs_dir))
+ postponed = {}
+ ignore_incomplete = set()
+ while files:
+ file_name = files.pop(0)
+ file_path = os.path.join(abs_dir, file_name)
+ file_base, ext = os.path.splitext(file_path)
+
+ # Skip files that are not XML files
+ if not ext == '.xml':
+ continue
+
+ if verbose > 0:
+ print "Parsing '%s'" % file_path
+ else:
+ sys.stdout.write('.')
+ sys.stdout.flush()
+
+ try:
+ iofile = IOFile.from_filename(file_path)
+ iofile.attachments = attachments
+ lesson = Lesson.publish(iofile, file_name in ignore_incomplete)
+ except Section.IncompleteError:
+ if file_name not in postponed or postponed[file_name] < files_imported:
+ # Push it back into the queue, maybe the missing lessons will show up.
+ if verbose > 0:
+ print self.style.NOTICE('Waiting for missing lessons.')
+ files.append(file_name)
+ postponed[file_name] = files_imported
+ elif self.options['ignore_incomplete'] and file_name not in ignore_incomplete:
+ files.append(file_name)
+ ignore_incomplete.add(file_name)
+ postponed[file_name] = files_imported
+ else:
+ # We're in a loop, nothing's being imported - some lesson is really missing.
+ raise
+ except BaseException:
+ import traceback
+ traceback.print_exc()
+ files_skipped += 1
+ else:
+ files_imported += 1
+ if hasattr(lesson, 'level'):
+ self.levels.add(lesson.level)
+ finally:
+ if verbose > 0:
+ print
+ return files_imported, files_skipped