X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/357027375ff8867f42ca34bcbfb5a78b5b185fc3..4cbcf8cac5e8cad592d9d84d76408249ccca8112:/src/wolnelektury/management/commands/translation2po.py diff --git a/src/wolnelektury/management/commands/translation2po.py b/src/wolnelektury/management/commands/translation2po.py index 6220006fe..283470b45 100644 --- a/src/wolnelektury/management/commands/translation2po.py +++ b/src/wolnelektury/management/commands/translation2po.py @@ -1,22 +1,18 @@ -# -*- coding: utf-8 -*- # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # import os import time -from optparse import make_option from django.conf import settings from django.core.management.base import BaseCommand -from django.core.management.color import color_style from django.db import models - import polib -import modeltranslation.models from modeltranslation.translator import translator, NotRegistered +from wolnelektury.utils import makedirs def metadata(language=''): - "get metadata for PO, given language code" + """get metadata for PO, given language code""" t = time.strftime('%Y-%m-%d %H:%M%z') return { @@ -33,33 +29,46 @@ def metadata(language=''): def make_po(language=''): - "Create new POFile object for language code" + """Create new POFile object for language code""" po = polib.POFile() po.metadata = metadata(language) return po def get_languages(langs): - if not langs: return settings.LANGUAGES + if not langs: + return settings.LANGUAGES langs = langs.split(',') lm = dict(settings.LANGUAGES) return map(lambda l: (l, lm.get(l, l)), langs) class Command(BaseCommand): - option_list = BaseCommand.option_list + ( - make_option('-d', '--directory', help='Specify which directory should hold generated PO files', dest='directory', default=''), - make_option('-l', '--load', help='load locales back to source', action='store_true', dest='load', default=False), - make_option('-L', '--language', help='locales to load', dest='lang', default=None), - make_option('-n', '--poname', help='name of the po file [no extension]', dest='poname', default=None), - make_option('-k', '--keep-running', help='keep running even when missing an input file', dest='keep_running', default=False, action='store_true'), - ) help = 'Export models from app to po files' - args = 'app' + + def add_arguments(self, parser): + parser.add_argument( + '-d', '--directory', dest='directory', default='', + help='Specify which directory should hold generated PO files') + parser.add_argument( + '-l', '--load', help='load locales back to source', + action='store_true', dest='load', default=False) + parser.add_argument( + '-L', '--language', help='locales to load', + dest='lang', default=None) + parser.add_argument( + '-n', '--poname', help='name of the po file [no extension]', + dest='poname', default=None) + parser.add_argument( + '-k', '--keep-running', dest='keep_running', + help='keep running even when missing an input file', + default=False, action='store_true') + parser.add_argument('app') def get_models(self, app): for mdname in dir(app.models): - if mdname[0] == '_': continue + if mdname[0] == '_': + continue md = getattr(app.models, mdname) try: assert issubclass(md, models.Model) @@ -73,15 +82,17 @@ class Command(BaseCommand): else: yield (md, opts) - def handle(self, appname, **options): - if not options['poname']: options['poname'] = appname + def handle(self, **options): + appname = options['app'] + if not options['poname']: + options['poname'] = appname app = __import__(appname) if options['load']: objects = {} modmod = {} for md, opts in self.get_models(app): - if not md.__name__ in objects: + if md.__name__ not in objects: objects[md.__name__] = {} modmod['model'] = md @@ -119,7 +130,7 @@ class Command(BaseCommand): cur_lang = locfld.language try: po = pofiles[cur_lang] - except: + except KeyError: po = make_po(cur_lang) pofiles[cur_lang] = po v = locfld.value_from_object(obj) or '' @@ -131,5 +142,5 @@ class Command(BaseCommand): directory = options['directory'] for lng, po in pofiles.items(): - os.makedirs(os.path.join(directory, lng)) + makedirs(os.path.join(directory, lng)) po.save(os.path.join(directory, lng, '%s.po' % options['poname']))