Commit 45d6d678 authored by Michal Čihař's avatar Michal Čihař

Use object based file format types

parent 608a7756
...@@ -60,64 +60,105 @@ from distutils.version import LooseVersion ...@@ -60,64 +60,105 @@ from distutils.version import LooseVersion
logger = logging.getLogger('weblate') logger = logging.getLogger('weblate')
class FileFormat(object):
'''
Simple object defining file format loader.
'''
def __init__(self, name, loader, monolingual = None, mark_fuzzy = False, fixups = None):
self.name = name
self.loader = loader
self.monolingual = monolingual
self.mark_fuzzy = mark_fuzzy
self.fixups = fixups
def load(self, storefile):
'''
Loads file using defined loader.
'''
loader = self.loader
# If loader is callable call it directly
if callable(loader):
return loader(storefile)
# Tuple style loader, import from translate toolkit
module_name, class_name = loader
if '.' in module_name:
module = importlib.import_module(module_name)
else:
module = importlib.import_module('translate.storage.%s' % module_name)
# Get the class
storeclass = getattr(module, class_name)
# Parse file
store = storeclass.parsefile(storefile)
# Apply possible fixups
if self.fixups is not None:
for fix in self.fixups:
setattr(store, fix, self.fixups[fix])
return store
FILE_FORMATS = { FILE_FORMATS = {
'auto': { 'auto': FileFormat(
'name': ugettext_lazy('Automatic detection'), ugettext_lazy('Automatic detection'),
'loader': factory.getobject, factory.getobject,
'monolingual': None, # Depends on actual format ),
}, 'po': FileFormat(
'po': { ugettext_lazy('Gettext PO file'),
'name': ugettext_lazy('Gettext PO file'), ('po', 'pofile'),
'loader': ('po', 'pofile'), False,
'monolingual': False, ),
}, 'ts': FileFormat(
'ts': { ugettext_lazy('XLIFF Translation File'),
'name': ugettext_lazy('XLIFF Translation File'), ('ts2', 'tsfile'),
'loader': ('ts2', 'tsfile'), ),
'monolingual': None, # Can be both 'xliff': FileFormat(
}, ugettext_lazy('Qt Linguist Translation File'),
'xliff': { ('xliff', 'xlifffile'),
'name': ugettext_lazy('Qt Linguist Translation File'), ),
'loader': ('xliff', 'xlifffile'), 'strings': FileFormat(
'monolingual': None, # Can be both ugettext_lazy('OS X Strings'),
}, ('properties', 'stringsfile'),
'strings': { False,
'name': ugettext_lazy('OS X Strings'), ),
'loader': ('properties', 'stringsfile'), 'properties': FileFormat(
'monolingual': False, ugettext_lazy('Java Properties'),
}, ('properties', 'javafile'),
'properties': { True,
'name': ugettext_lazy('Java Properties'),
'loader': ('properties', 'javafile'),
'monolingual': True,
# Java properties need to be iso-8859-1, but # Java properties need to be iso-8859-1, but
# ttkit converts them to utf-8 # ttkit converts them to utf-8
'fixups': {'encoding': 'iso-8859-1'}, fixups = {'encoding': 'iso-8859-1'},
}, ),
'properties-utf8': { 'properties-utf8': FileFormat(
'name': ugettext_lazy('Java Properties (UTF-8)'), ugettext_lazy('Java Properties (UTF-8)'),
'loader': ('properties', 'javautf8file'), ('properties', 'javautf8file'),
'monolingual': True, True,
}, ),
} }
# Check if there is support for Android resources # Check if there is support for Android resources
# Available as patch at https://github.com/translate/translate/pull/2 # Available as patch at https://github.com/translate/translate/pull/2
try: try:
from translate.storage import aresource from translate.storage import aresource
FILE_FORMATS['aresource'] = { FILE_FORMATS['aresource'] = FileFormat(
'name': ugettext_lazy('Android String Resource'), ugettext_lazy('Android String Resource'),
'loader': ('aresource', 'AndroidResourceFile'), ('aresource', 'AndroidResourceFile'),
'monolingual': True, True,
} mark_fuzzy = True,
)
except ImportError: except ImportError:
FILE_FORMATS['aresource'] = { FILE_FORMATS['aresource'] = FileFormat(
'name': ugettext_lazy('Android String Resource'), ugettext_lazy('Android String Resource'),
'loader': ('ttkit.aresource', 'AndroidResourceFile'), ('ttkit.aresource', 'AndroidResourceFile'),
'monolingual': True, True,
} mark_fuzzy = True,
)
FILE_FORMAT_CHOICES = [(fmt, FILE_FORMATS[fmt]['name']) for fmt in FILE_FORMATS] FILE_FORMAT_CHOICES = [(fmt, FILE_FORMATS[fmt].name) for fmt in FILE_FORMATS]
def ttkit(storefile, file_format = 'auto'): def ttkit(storefile, file_format = 'auto'):
''' '''
...@@ -137,32 +178,9 @@ def ttkit(storefile, file_format = 'auto'): ...@@ -137,32 +178,9 @@ def ttkit(storefile, file_format = 'auto'):
raise Exception('Not supported file format: %s' % file_format) raise Exception('Not supported file format: %s' % file_format)
# Get loader # Get loader
loader = FILE_FORMATS[file_format]['loader'] format_obj = FILE_FORMATS[file_format]
# If loader is callable call it directly
if callable(loader):
return loader(storefile)
# Tuple style loader, import from translate toolkit
module_name, class_name = loader
if '.' in module_name:
module = importlib.import_module(module_name)
else:
module = importlib.import_module('translate.storage.%s' % module_name)
# Get the class
storeclass = getattr(module, class_name)
# Parse file
store = storeclass.parsefile(storefile)
# Apply possible fixups
if 'fixups' in FILE_FORMATS[file_format]:
for fix in FILE_FORMATS[file_format]['fixups']:
setattr(store, fix, FILE_FORMATS[file_format]['fixups'][fix])
return store
return format_obj.load(storefile)
def validate_repoweb(val): def validate_repoweb(val):
try: try:
...@@ -1069,7 +1087,7 @@ class SubProject(models.Model): ...@@ -1069,7 +1087,7 @@ class SubProject(models.Model):
''' '''
Returns true if subproject is using template for translation Returns true if subproject is using template for translation
''' '''
return FILE_FORMATS[self.file_format]['monolingual'] != False and self.template != '' return FILE_FORMATS[self.file_format].monolingual != False and self.template != ''
def get_template_store(self): def get_template_store(self):
''' '''
......
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