Commit 486582ec authored by Michal Čihař's avatar Michal Čihař

Allow to specify base file for monolingual translations on import (issue #324)

parent 6e8619b3
......@@ -87,6 +87,9 @@ To customise the subproject's name, use the `--name-template` option.
Its parameter is a python formatting string, which will expect the
match from `<filemask>`.
By format string passed by the `--base-file-template` option you can customize
base file for monolingual translations.
You can also specify file format to use (see :ref:`formats`) by the
`--file-format` parameter. The default is autodetection.
......
......@@ -45,6 +45,12 @@ class Command(BaseCommand):
help='Python formatting string, transforming the filemask '
'match to a project name'
),
make_option(
'--base-file-template',
default='%s',
help='Python formatting string, transforming the filemask '
'match to a monolingual base file name'
),
make_option(
'--file-format',
default='auto',
......@@ -52,6 +58,14 @@ class Command(BaseCommand):
),
)
def format_string(self, template, match):
'''
Formats template string with match.
'''
if '%s' in template:
return template % match
return template
def get_name(self, maskre, path):
matches = maskre.match(path)
return matches.group(1)
......@@ -154,12 +168,12 @@ class Command(BaseCommand):
else:
matches, sharedrepo = self.import_initial(
project, repo, branch, filemask, options['name_template'],
options['file_format']
options['file_format'], options['base_file_template']
)
# Create remaining subprojects sharing git repository
for match in matches:
name = options['name_template'] % match
name = self.format_string(options['name_template'], match)
slug = slugify(name)
subprojects = SubProject.objects.filter(
Q(name=name) | Q(slug=slug),
......@@ -180,11 +194,12 @@ class Command(BaseCommand):
repo=sharedrepo,
branch=branch,
file_format=options['file_format'],
template=self.format_string(options['base_file_template'], match),
filemask=filemask.replace('**', match)
)
def import_initial(self, project, repo, branch, filemask, name_template,
file_format):
file_format, base_file_template):
'''
Import the first repository of a project
'''
......@@ -194,7 +209,7 @@ class Command(BaseCommand):
# Create first subproject (this one will get full git repo)
match = matches.pop()
name = name_template % match
name = self.format_string(name_template, match)
slug = slugify(name)
if SubProject.objects.filter(project=project, slug=slug).exists():
......@@ -221,6 +236,7 @@ class Command(BaseCommand):
repo=repo,
branch=branch,
file_format=file_format,
template=self.format_string(base_file_template, match),
filemask=filemask.replace('**', match)
)
......
......@@ -61,6 +61,34 @@ class ImportProjectTest(RepoTestCase):
# We should have loaded three subprojects
self.assertEqual(project.subproject_set.count(), 4)
def test_import_aresource(self):
project = self.create_project()
call_command(
'import_project',
'test',
self.repo_path,
'master',
'**/values-*/strings.xml',
file_format='aresource',
base_file_template='android/values/strings.xml',
)
# We should have loaded one subproject
self.assertEqual(project.subproject_set.count(), 1)
def test_import_aresource_format(self):
project = self.create_project()
call_command(
'import_project',
'test',
self.repo_path,
'master',
'**/values-*/strings.xml',
file_format='aresource',
base_file_template='%s/values/strings.xml',
)
# We should have loaded one subproject
self.assertEqual(project.subproject_set.count(), 1)
def test_re_import(self):
project = self.create_project()
call_command(
......
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