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

Unify command line processing

parent d8c55c00
...@@ -22,9 +22,9 @@ from django.core.management.base import BaseCommand ...@@ -22,9 +22,9 @@ from django.core.management.base import BaseCommand
from optparse import make_option from optparse import make_option
from weblate.trans.models import Unit, SubProject from weblate.trans.models import Unit, SubProject
class UnitCommand(BaseCommand): class WeblateCommand(BaseCommand):
''' '''
Command which accepts project/subproject/--all params to process units. Command which accepts project/subproject/--all params to process.
''' '''
args = '<project/subproject>' args = '<project/subproject>'
option_list = BaseCommand.option_list + ( option_list = BaseCommand.option_list + (
...@@ -32,62 +32,49 @@ class UnitCommand(BaseCommand): ...@@ -32,62 +32,49 @@ class UnitCommand(BaseCommand):
action='store_true', action='store_true',
dest='all', dest='all',
default=False, default=False,
help='work on all projects'), help='process all subprojects'),
) )
def get_units(self, *args, **options): def get_units(self, *args, **options):
''' '''
Returns list of units matching parameters. Returns list of units matching parameters.
''' '''
if options['all']: subprojects = self.get_subprojects(*args, **options)
base = Unit.objects.all() return Unit.objects.filter(translation__subproject__in = subprojects)
else:
base = Unit.objects.none()
for arg in args:
parts = arg.split('/')
if len(parts) == 2:
prj, subprj = parts
base |= Unit.objects.filter(
translation__subproject__slug = subprj,
translation__subproject__project__slug = prj
)
else:
prj = parts[0]
base |= Unit.objects.filter(translation__subproject__project__slug = prj)
if len(args) == 0 or base.count() == 0:
print 'Nothing to process, please use either --all or <project/subproject>'
return base
class SubProjectCommand(BaseCommand):
'''
Command which accepts project/subproject/--all params to process subprojects.
'''
args = '<project/subproject>'
option_list = BaseCommand.option_list + (
make_option('--all',
action='store_true',
dest='all',
default=False,
help='work on all projects'),
)
def get_subprojects(self, *args, **options): def get_subprojects(self, *args, **options):
''' '''
Returns list of units matching parameters. Returns list of units matching parameters.
''' '''
if options['all']: if options['all']:
base = SubProject.objects.all() # all subprojects
result = SubProject.objects.all()
elif len(args) == 0:
# no argumets to filter projects
print 'WARNING: nothing to process, please use either --all or <project/subproject>'
result = SubProject.objects.none()
else: else:
base = SubProject.objects.none() # start with none and add found
result = SubProject.objects.none()
# process arguments
for arg in args: for arg in args:
# do we have also subproject?
parts = arg.split('/') parts = arg.split('/')
# filter by project
found = SubProject.objects.filter(project__slug = parts[0])
# filter by subproject if available
if len(parts) == 2: if len(parts) == 2:
prj, subprj = parts found = found.filter(project__slug = parts[1])
base |= SubProject.objects.filter(slug = subprj, project__slug = prj)
else: # warn on no match
prj = parts[0] if found.count() == 0:
base |= SubProject.objects.filter(project__slug = prj) print 'WARNING: "%s" did not match any subproject' % arg
if len(args) == 0 or base.count() == 0:
print 'Nothing to process, please use either --all or <project/subproject>' # merge results
return base result|= found
return result
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import SubProjectCommand from weblate.trans.management.commands import WeblateCommand
class Command(SubProjectCommand): class Command(WeblateCommand):
help = 'checks status of git repo' help = 'checks status of git repo'
def handle(self, *args, **options): def handle(self, *args, **options):
......
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import SubProjectCommand from weblate.trans.management.commands import WeblateCommand
class Command(SubProjectCommand): class Command(WeblateCommand):
help = 'forces commiting changes to git repo' help = 'forces commiting changes to git repo'
def handle(self, *args, **options): def handle(self, *args, **options):
......
...@@ -18,12 +18,12 @@ ...@@ -18,12 +18,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import SubProjectCommand from weblate.trans.management.commands import WeblateCommand
from optparse import make_option from optparse import make_option
class Command(SubProjectCommand): class Command(WeblateCommand):
help = '(re)loads translations from disk' help = '(re)loads translations from disk'
option_list = SubProjectCommand.option_list + ( option_list = WeblateCommand.option_list + (
make_option('--force', make_option('--force',
action='store_true', action='store_true',
dest='force', dest='force',
......
...@@ -18,15 +18,15 @@ ...@@ -18,15 +18,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import UnitCommand from weblate.trans.management.commands import WeblateCommand
from weblate.trans.models import Unit from weblate.trans.models import Unit
from weblate.lang.models import Language from weblate.lang.models import Language
from weblate.trans.search import FULLTEXT_INDEX, create_source_index, create_target_index from weblate.trans.search import FULLTEXT_INDEX, create_source_index, create_target_index
from optparse import make_option from optparse import make_option
class Command(UnitCommand): class Command(WeblateCommand):
help = 'rebuilds index for fulltext search' help = 'rebuilds index for fulltext search'
option_list = UnitCommand.option_list + ( option_list = WeblateCommand.option_list + (
make_option('--clean', make_option('--clean',
action='store_true', action='store_true',
dest='clean', dest='clean',
......
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import UnitCommand from weblate.trans.management.commands import WeblateCommand
class Command(UnitCommand): class Command(WeblateCommand):
help = 'updates checks for units' help = 'updates checks for units'
def handle(self, *args, **options): def handle(self, *args, **options):
......
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from weblate.trans.management.commands import SubProjectCommand from weblate.trans.management.commands import WeblateCommand
class Command(SubProjectCommand): class Command(WeblateCommand):
help = 'updates git repos' help = 'updates git repos'
def handle(self, *args, **options): 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