Commit fa41f13e authored by Michal Čihař's avatar Michal Čihař

Avoid using deprecated optparse

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 5314c581
......@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sites.models import Site
......@@ -26,28 +25,27 @@ from django.contrib.sites.models import Site
class Command(BaseCommand):
help = 'changes default site name'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--set-name',
type='str',
dest='set_name',
default=None,
help='site name to set'
),
make_option(
)
parser.add_argument(
'--site-id',
type='int',
type=int,
dest='site_id',
default=1,
help='site ID to manipulate (1 by default)'
),
make_option(
)
parser.add_argument(
'--get-name',
action='store_true',
dest='get_name',
default=False,
help='just display the site name'
),
)
def handle(self, *args, **options):
......
......@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
import random
import string
......@@ -28,13 +27,13 @@ from django.contrib.auth.models import User
class Command(BaseCommand):
help = 'setups admin user with random password'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--password',
dest='password',
default=None,
help='Password to set, random is generated if not specified'
),
)
@staticmethod
......
......@@ -20,7 +20,7 @@
from __future__ import unicode_literals
from optparse import make_option
import argparse
import json
from django.core.management.base import BaseCommand, CommandError
......@@ -29,13 +29,17 @@ from django.contrib.auth.models import User
class Command(BaseCommand):
help = 'imports users from JSON dump of database'
args = '<json-file>'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--check',
action='store_true',
help='Only check import, do not actually create users'
),
)
parser.add_argument(
'json-file',
type=argparse.FileType('rb'),
help='File to import',
)
def handle(self, *args, **options):
......@@ -43,10 +47,8 @@ class Command(BaseCommand):
Creates default set of groups and optionally updates them and moves
users around to default group.
'''
if len(args) != 1:
raise CommandError('Please specify JSON file to import!')
data = json.load(open(args[0]))
data = json.load(options['json-file'])
for line in data:
if 'fields' in line:
......
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import BaseCommand
from weblate.accounts.models import create_groups, move_users
......@@ -27,21 +25,21 @@ from weblate.accounts.models import create_groups, move_users
class Command(BaseCommand):
help = 'setups default user groups'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--move',
action='store_true',
dest='move',
default=False,
help='Move all users to Users group'
),
make_option(
)
parser.add_argument(
'--no-privs-update',
action='store_false',
dest='update',
default=True,
help='Prevents updates of privileges of existing groups'
),
)
def handle(self, *args, **options):
......
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import BaseCommand
from weblate.lang.models import Language
......@@ -28,14 +26,13 @@ from weblate.lang.models import Language
class Command(BaseCommand):
help = 'Populates language definitions'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--no-update',
action='store_false',
dest='update',
default=True,
help='Prevents updates to existing language definitions'
),
)
def handle(self, *args, **options):
......
......@@ -22,10 +22,11 @@
# Django settings for running testsuite
#
from weblate.settings_example import *
import warnings
import os
from weblate.settings_example import *
if 'CI_DATABASE' in os.environ:
if os.environ['CI_DATABASE'] == 'mysql':
DATABASES['default']['ENGINE'] = 'django.db.backends.mysql'
......
......@@ -21,8 +21,6 @@
Helper classes for management commands.
'''
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
......@@ -33,32 +31,35 @@ class WeblateCommand(BaseCommand):
'''
Command which accepts project/component/--all params to process.
'''
args = '<project/component>'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
parser.add_argument(
'--all',
action='store_true',
dest='all',
default=False,
help='process all components'
),
)
parser.add_argument(
'component',
nargs='*',
help='Slug <project/component> of component to process'
)
def get_units(self, *args, **options):
def get_units(self, **options):
'''
Returns list of units matching parameters.
'''
if options['all']:
return Unit.objects.all()
return Unit.objects.filter(
translation__subproject__in=self.get_subprojects(*args, **options)
translation__subproject__in=self.get_subprojects(**options)
)
def iterate_units(self, *args, **options):
def iterate_units(self, **options):
"""
Memory effective iteration over units.
"""
units = self.get_units(*args, **options).order_by('pk')
units = self.get_units(**options).order_by('pk')
count = units.count()
if not count:
return
......@@ -87,25 +88,25 @@ class WeblateCommand(BaseCommand):
yield unit
self.stdout.write('Operation completed')
def get_translations(self, *args, **options):
def get_translations(self, **options):
'''
Returns list of translations matching parameters.
'''
return Translation.objects.filter(
subproject__in=self.get_subprojects(*args, **options)
subproject__in=self.get_subprojects(**options)
)
def get_subprojects(self, *args, **options):
def get_subprojects(self, **options):
'''
Returns list of components matching parameters.
'''
if options['all']:
# all components
result = SubProject.objects.all()
elif len(args) == 0:
elif len(options['component']) == 0:
# no argumets to filter projects
self.stderr.write(
'Please specify either --all or <project/component>'
'Please specify either --all or at least one <project/component>'
)
raise CommandError('Nothing to process!')
else:
......@@ -113,7 +114,7 @@ class WeblateCommand(BaseCommand):
result = SubProject.objects.none()
# process arguments
for arg in args:
for arg in options['component']:
# do we have also component?
parts = arg.split('/')
......@@ -150,22 +151,21 @@ class WeblateLangCommand(WeblateCommand):
Command accepting additional language parameter to filter
list of languages to process.
'''
option_list = WeblateCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(WeblateLangCommand, self).add_arguments(parser)
parser.add_argument(
'--lang',
action='store',
type='string',
dest='lang',
default=None,
help='Limit only to given languages (comma separated list)'
),
)
def get_units(self, *args, **options):
def get_units(self, **options):
'''
Returns list of units matching parameters.
'''
units = super(WeblateLangCommand, self).get_units(*args, **options)
units = super(WeblateLangCommand, self).get_units(**options)
if options['lang'] is not None:
units = units.filter(
......@@ -174,12 +174,12 @@ class WeblateLangCommand(WeblateCommand):
return units
def get_translations(self, *args, **options):
def get_translations(self, **options):
'''
Returns list of translations matching parameters.
'''
result = super(WeblateLangCommand, self).get_translations(
*args, **options
**options
)
if options['lang'] is not None:
......@@ -199,15 +199,28 @@ class WeblateLangCommand(WeblateCommand):
class WeblateTranslationCommand(BaseCommand):
"""Command with target of one translation."""
args = '<project> <component> <language>'
def get_translation(self, args):
def add_arguments(self, parser):
parser.add_argument(
'project',
help='Slug of project'
)
parser.add_argument(
'component',
help='Slug of component'
)
parser.add_argument(
'language',
help='Slug of language'
)
def get_translation(self, **options):
"""Get translation object"""
try:
return Translation.objects.get(
subproject__project__slug=args[0],
subproject__slug=args[1],
language__code=args[2],
subproject__project__slug=options['project'],
subproject__slug=options['component'],
language__code=options['language'],
)
except Translation.DoesNotExist:
raise CommandError('No matching translation project found!')
......
......@@ -18,7 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
import argparse
from django.core.management.base import CommandError
from django.contrib.auth.models import User
......@@ -33,24 +33,25 @@ class Command(WeblateTranslationCommand):
Command for mass importing suggestions.
"""
help = 'imports suggestions'
args = '<project> <component> <language> <file>'
option_list = WeblateTranslationCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--author',
default='noreply@weblate.org',
help=(
'Email address of author (has to be registered in Weblate)'
)
),
)
parser.add_argument(
'file',
type=argparse.FileType('rb'),
help='File to import',
)
def handle(self, *args, **options):
# Check params
if len(args) != 4:
raise CommandError('Invalid number of parameters!')
# Get translation object
translation = self.get_translation(args)
translation = self.get_translation(**options)
# Get user
try:
......@@ -64,9 +65,8 @@ class Command(WeblateTranslationCommand):
# Process import
try:
with open(args[3], 'rb') as handle:
translation.merge_upload(
request, handle, False, method='suggest',
request, options['file'], False, method='suggest',
author=get_author_name(user),
)
except IOError:
......
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import CommandError
from django.contrib.auth.models import User
......@@ -33,46 +31,43 @@ class Command(WeblateTranslationCommand):
Command for mass automatic translation.
"""
help = 'performs automatic translation based on other components'
option_list = WeblateTranslationCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--user',
default='anonymous',
help=(
'User performing the change'
)
),
make_option(
)
parser.add_argument(
'--source',
default='',
help=(
'Source component <project/component>'
)
),
make_option(
)
parser.add_argument(
'--overwrite',
default=False,
action='store_true',
help=(
'Overwrite existing translations in target component'
)
),
make_option(
)
parser.add_argument(
'--inconsistent',
default=False,
action='store_true',
help=(
'Process only inconsistent translations'
)
),
)
def handle(self, *args, **options):
# Check params
if len(args) != 3:
raise CommandError('Invalid number of parameters!')
# Get translation object
translation = self.get_translation(args)
translation = self.get_translation(**options)
# Get user
try:
......
......@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
import cProfile
import pstats
......@@ -32,28 +31,37 @@ class Command(BaseCommand):
Runs simple project import to perform benchmarks.
'''
help = 'performs import benchmark'
args = '<project> <repo> <mask>'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--profile-sort',
type='str',
dest='profile_sort',
default='cumulative',
help='sort order for profile stats',
),
make_option(
)
parser.add_argument(
'--profile-count',
type='int',
type=int,
dest='profile_count',
default=20,
help='number of profile stats to show',
),
)
parser.add_argument(
'project',
help='Existing project slug for tests',
)
parser.add_argument(
'repo',
help='VCS repository URL',
)
parser.add_argument(
'mask',
help='File mask',
)
def handle(self, *args, **options):
if len(args) < 3:
raise CommandError('Missing arguments!')
project = Project.objects.get(slug=args[0])
project = Project.objects.get(slug=options['project'])
# Delete any possible previous tests
SubProject.objects.filter(
project=project,
......@@ -64,8 +72,8 @@ class Command(BaseCommand):
SubProject.objects.create,
name='Benchmark',
slug='benchmark',
repo=args[1],
filemask=args[2],
repo=options['repo'],
filemask=options['mask'],
project=project
)
stats = pstats.Stats(profiler)
......
......@@ -19,8 +19,6 @@
#
from datetime import timedelta
from optparse import make_option
from django.utils import timezone
from weblate.trans.management.commands import WeblateLangCommand
......@@ -28,22 +26,23 @@ from weblate.trans.management.commands import WeblateLangCommand
class Command(WeblateLangCommand):
help = 'commits pending changes older than given age'
option_list = WeblateLangCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--age',
action='store',
type='int',
type=int,
dest='age',
default=24,
help='Age of changes to commit in hours (default is 24 hours)'
),
)
def handle(self, *args, **options):
age = timezone.now() - timedelta(hours=options['age'])
for translation in self.get_translations(*args, **options):
for translation in self.get_translations(**options):
if not translation.repo_needs_commit():
continue
......
......@@ -19,7 +19,6 @@
#
from glob import glob
from optparse import make_option
import tempfile
import os
import re
......@@ -43,45 +42,46 @@ class Command(BaseCommand):
Command for mass importing of repositories into Weblate.
"""
help = 'imports projects with more components'
args = '<project> <gitrepo> <branch> <filemask>'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--name-template',
default='%s',
help=(
'Python formatting string, transforming the filemask '
'match to a project name'
)
),
make_option(
)
parser.add_argument(
'--component-regexp',
default=None,
help=(
'Regular expression to match component out of filename'
)
),
make_option(
)
parser.add_argument(
'--base-file-template',
default='',
help=(
'Python formatting string, transforming the filemask '
'match to a monolingual base file name'
)
),
make_option(
)
parser.add_argument(
'--file-format',
default='auto',
help='File format type, defaults to autodetection',
),
make_option(
)
parser.add_argument(
'--language-regex',
default=None,
help=(
'Language filter regular expression to be used for created'
' components'
),
),
make_option(
)
parser.add_argument(
'--no-skip-duplicates',
action='store_true',
default=False,
......@@ -90,30 +90,45 @@ class Command(BaseCommand):
'Avoid skipping duplicate component names/slugs. '
'Use this if importing project with long names '
)
),
make_option(
)
parser.add_argument(
'--license',
default=None,
help='License of imported components',
),
make_option(
)
parser.add_argument(
'--license-url',
default=None,
help='License URL of imported components',
),
make_option(
)
parser.add_argument(
'--vcs',
default='git',
help='Version control system to use',
),
make_option(
)
parser.add_argument(
'--main-component',
default=None,
help=(
'Define which component will be used as main - including full'
' VCS repository'
)
),
)
parser.add_argument(
'project',
help='Existing project slug',
)
parser.add_argument(
'repo',
help='VCS repository URL',
)
parser.add_argument(
'branch',
help='VCS repository branch',
)
parser.add_argument(
'filemask',
help='File mask',
)
def __init__(self, *args, **kwargs):
......@@ -219,9 +234,9 @@ class Command(BaseCommand):
'Failed to find suitable name for {0}'.format(name)
)
def parse_options(self, args, options):
def parse_options(self, options):
"""Parses parameters"""
self.filemask = args[3]
self.filemask = options['filemask']
self.vcs = options['vcs']
self.file_format = options['file_format']
self.language_regex = options['language_regex']
......@@ -264,22 +279,18 @@ class Command(BaseCommand):
'''
Automatic import of project.
'''
# Check params count
if len(args) != 4:
raise CommandError('Invalid number of parameters!')
# Read params
repo = args[1]
branch = args[2]
self.parse_options(args, options)
repo = options['repo']
branch = options['branch']
self.parse_options(options)
# Try to get project
try:
project = Project.objects.get(slug=args[0])
project = Project.objects.get(slug=options['project'])
except Project.DoesNotExist:
raise CommandError(
'Project %s does not exist, you need to create it first!' %
args[0]
options['project']
)
if is_repo_link(repo):
......
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import BaseCommand
from weblate.trans.models import Check, get_related_units
......@@ -27,21 +25,22 @@ from weblate.trans.models import Check, get_related_units
class Command(BaseCommand):
help = 'lists top ignored checks'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--count',
type='int',
type=int,
dest='count',
default=100,
help='Number of top checks to list',
),
make_option(
)
parser.add_argument(
'--list-all',
action='store_true',
dest='list_all',
default=False,
help='List all checks (not only ignored)',
),
)
def handle(self, *args, **options):
......
......@@ -18,26 +18,25 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from weblate.trans.management.commands import WeblateLangCommand
class Command(WeblateLangCommand):
help = '(re)loads translations from disk'
option_list = WeblateLangCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--force',
action='store_true',
dest='force',
default=False,
help='Force rereading files even when they should be up to date'
),
)
def handle(self, *args, **options):
langs = None
if options['lang'] is not None:
langs = options['lang'].split(',')
for subproject in self.get_subprojects(*args, **options):
for subproject in self.get_subprojects(**options):
subproject.create_translations(options['force'], langs)
......@@ -18,23 +18,22 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from weblate.trans.management.commands import WeblateCommand
class Command(WeblateCommand):
help = 'pushes all changes to upstream respository'
option_list = WeblateCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--force-commit',
action='store_true',
dest='force_commit',
default=False,
help='Forces commiting pending changes'
),
)
def handle(self, *args, **options):
for subproject in self.get_subprojects(*args, **options):
for subproject in self.get_subprojects(**options):
subproject.do_push(None, force_commit=options['force_commit'])
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from weblate.trans.management.commands import WeblateCommand
from weblate.trans.search import (
get_source_index, get_target_index,
......@@ -31,21 +29,22 @@ from weblate.lang.models import Language
class Command(WeblateCommand):
help = 'rebuilds index for fulltext search'
option_list = WeblateCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--clean',
action='store_true',
dest='clean',
default=False,
help='removes also all words from database'
),
make_option(
)
parser.add_argument(
'--optimize',
action='store_true',
dest='optimize',
default=False,
help='optimize index without rebuilding it'
),
)
def optimize_index(self):
......@@ -72,7 +71,7 @@ class Command(WeblateCommand):
try:
# Process all units
for unit in self.iterate_units(*args, **options):
for unit in self.iterate_units(**options):
lang = unit.translation.language.code
# Lazy open writer
if lang not in target_writers:
......
......@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from optparse import make_option
from django.core.management.base import BaseCommand
from django.db import transaction
......@@ -29,15 +27,16 @@ from weblate.trans.search import update_index, delete_search_unit
class Command(BaseCommand):
help = 'updates index for fulltext search'
option_list = BaseCommand.option_list + (
make_option(
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--limit',
action='store',
type='int',
type=int,
dest='limit',
default=1000,
help='number of updates to process in one run'
),
)
def handle(self, *args, **options):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment