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