1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from cPickle import load, dump
7 from optparse import make_option
9 from django.core.management.base import BaseCommand
10 from django.core.management.color import color_style
11 from django.conf import settings
13 from catalogue.models import Book, Tag
16 class Command(BaseCommand):
17 option_list = BaseCommand.option_list + (
18 make_option('-t', '--tags', dest='tags', metavar='SLUG,...',
19 help='Use only books tagged with this tags'),
20 make_option('-i', '--include', dest='include', metavar='SLUG,...',
21 help='Include specific books by slug'),
22 make_option('-e', '--exclude', dest='exclude', metavar='SLUG,...',
23 help='Exclude specific books by slug')
25 help = 'Prepare data for Lesmianator.'
27 def handle(self, *args, **options):
28 self.style = color_style()
29 verbose = int(options.get('verbosity'))
30 tags = options.get('tags')
31 include = options.get('include')
32 exclude = options.get('exclude')
35 path = settings.LESMIANATOR_PICKLE
37 print self.style.ERROR('LESMIANATOR_PICKLE not set in the settings.')
43 books += list(Book.objects.filter(slug__in=include.split(',')).only('slug', 'txt_file'))
46 books += list(Book.tagged.with_all(Tag.objects.filter(slug__in=tags.split(','))).only('slug', 'txt_file'))
48 books = list(Book.objects.all().only('slug', 'txt_file'))
51 books = [book for book in books if book.slug not in exclude.split(',')]
56 processed = skipped = 0
59 print 'Parsing', book.slug
62 print self.style.NOTICE('%s has no TXT file' % book.slug)
67 for number, line in enumerate(book.txt_file):
70 line = unicode(line, 'utf-8').lower()
72 mydict = lesmianator.setdefault(last_word, {})
73 myval = mydict.setdefault(letter, 0)
75 last_word = last_word[-2:] + letter
79 print self.style.ERROR("No books with TXT files found")
81 print self.style.ERROR("No books found")
85 dump(lesmianator, open(path, 'w'))
87 print self.style.ERROR("Counldn't write to $s" % path)
90 dump(lesmianator, open(path, 'w'))
92 print "%d processed, %d skipped" % (processed, skipped)
93 print "Results dumped do %s" % path