Commit 2ad61a38 authored by Michal Čihař's avatar Michal Čihař

Add field for storing file format

This is needed for supporting formats which can not be autodetected
like in issue #107 or issue #91.
parent 33001946
This diff is collapsed.
...@@ -58,8 +58,15 @@ from distutils.version import LooseVersion ...@@ -58,8 +58,15 @@ from distutils.version import LooseVersion
logger = logging.getLogger('weblate') logger = logging.getLogger('weblate')
FILE_FORMATS = {
'auto': {
'name': ugettext_lazy('Automatic detection'),
}
}
def ttkit(storefile): FILE_FORMAT_CHOICES = [(fmt, FILE_FORMATS[fmt]['name']) for fmt in FILE_FORMATS]
def ttkit(storefile, file_format = 'auto'):
''' '''
Returns translate-toolkit storage for a path. Returns translate-toolkit storage for a path.
''' '''
...@@ -73,7 +80,9 @@ def ttkit(storefile): ...@@ -73,7 +80,9 @@ def ttkit(storefile):
if not isinstance(storefile, basestring): if not isinstance(storefile, basestring):
storefile.mode = 'r' storefile.mode = 'r'
return factory.getobject(storefile) if file_format == 'auto':
return factory.getobject(storefile)
raise Exception('Not supported file format: %s' % file_format)
def validate_repoweb(val): def validate_repoweb(val):
...@@ -375,6 +384,12 @@ class SubProject(models.Model): ...@@ -375,6 +384,12 @@ class SubProject(models.Model):
blank = True, blank = True,
help_text = ugettext_lazy('Filename of translations template, this is recommended to use for translations which store only translated string like Android resource strings') help_text = ugettext_lazy('Filename of translations template, this is recommended to use for translations which store only translated string like Android resource strings')
) )
file_format = models.CharField(
max_length = 50,
default = 'auto',
choices = FILE_FORMAT_CHOICES,
help_text = ugettext_lazy('Automatic detection might fail for some formats and is slightly slower'),
)
locked = models.BooleanField( locked = models.BooleanField(
default = False, default = False,
help_text = ugettext_lazy('Whether subproject is locked for translation updates') help_text = ugettext_lazy('Whether subproject is locked for translation updates')
...@@ -869,7 +884,7 @@ class SubProject(models.Model): ...@@ -869,7 +884,7 @@ class SubProject(models.Model):
errors = [] errors = []
for match in matches: for match in matches:
try: try:
ttkit(os.path.join(self.get_path(), match)) ttkit(os.path.join(self.get_path(), match), self.file_format)
except ValueError: except ValueError:
notrecognized.append(match) notrecognized.append(match)
except Exception, e: except Exception, e:
...@@ -889,7 +904,7 @@ class SubProject(models.Model): ...@@ -889,7 +904,7 @@ class SubProject(models.Model):
if self.template != '': if self.template != '':
template = os.path.join(self.get_path(), self.template) template = os.path.join(self.get_path(), self.template)
try: try:
ttkit(os.path.join(self.get_path(), match)) ttkit(os.path.join(self.get_path(), match), self.file_format)
except ValueError: except ValueError:
raise ValidationError(_('Format of translation template could not be recognized.')) raise ValidationError(_('Format of translation template could not be recognized.'))
except Exception, e: except Exception, e:
...@@ -1189,7 +1204,7 @@ class Translation(models.Model): ...@@ -1189,7 +1204,7 @@ class Translation(models.Model):
''' '''
Returns ttkit storage object for a translation. Returns ttkit storage object for a translation.
''' '''
store = ttkit(self.get_filename()) store = ttkit(self.get_filename(), self.subproject.file_format)
if hasattr(store, 'set_base_resource') and self.subproject.template != '': if hasattr(store, 'set_base_resource') and self.subproject.template != '':
template = os.path.join(self.subproject.get_path(), self.subproject.template) template = os.path.join(self.subproject.get_path(), self.subproject.template)
store.set_base_resource(template) store.set_base_resource(template)
......
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