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

Initial implementation of file format classes (issue #184)

parent b12790a1
...@@ -26,17 +26,31 @@ import importlib ...@@ -26,17 +26,31 @@ import importlib
import __builtin__ import __builtin__
FILE_FORMATS = {}
def register_fileformat(fileformat):
'''
Registers fileformat in dictionary.
'''
FILE_FORMATS[fileformat.format_id] = fileformat()
class FileFormat(object): class FileFormat(object):
''' '''
Simple object defining file format loader. Generic object defining file format loader.
''' '''
def __init__(self, name, loader, monolingual=None, mark_fuzzy=False, name = ''
fixups=None): format_id = ''
self.name = name loader = None
self.loader = loader monolingual = None
self.monolingual = monolingual mark_fuzzy = None
self.mark_fuzzy = mark_fuzzy
self.fixups = fixups def fixup(self, store):
'''
Performs optional fixups on store.
'''
return store
def load(self, storefile): def load(self, storefile):
''' '''
...@@ -44,10 +58,6 @@ class FileFormat(object): ...@@ -44,10 +58,6 @@ class FileFormat(object):
''' '''
loader = self.loader loader = self.loader
# If loader is callable call it directly
if callable(loader):
return loader(storefile)
# Tuple style loader, import from translate toolkit # Tuple style loader, import from translate toolkit
module_name, class_name = loader module_name, class_name = loader
if '.' in module_name: if '.' in module_name:
...@@ -70,61 +80,98 @@ class FileFormat(object): ...@@ -70,61 +80,98 @@ class FileFormat(object):
# Parse file # Parse file
store = storeclass.parsefile(storefile) store = storeclass.parsefile(storefile)
# Apply possible fixups # Apply possible fixups and return
if self.fixups is not None: return self.fixup(store)
for fix in self.fixups:
setattr(store, fix, self.fixups[fix])
return store
class AutoFormat(FileFormat):
name = _('Automatic detection')
format_id = 'auto'
FILE_FORMATS = { def load(self, storefile):
'auto': FileFormat( '''
_('Automatic detection'), Directly loads using translate-toolkit.
factory.getobject, '''
), return factory.getobject(storefile)
'po': FileFormat(
_('Gettext PO file'), register_fileformat(AutoFormat)
('po', 'pofile'),
False,
), class PoFormat(FileFormat):
'ts': FileFormat( name = _('Gettext PO file')
_('Qt Linguist Translation File'), format_id = 'po'
('ts2', 'tsfile'), loader = ('po', 'pofile')
), monolingual = False
'xliff': FileFormat(
_('XLIFF Translation File'), register_fileformat(PoFormat)
('xliff', 'xlifffile'),
),
'strings': FileFormat( class TSFormat(FileFormat):
_('OS X Strings'), name = _('Qt Linguist Translation File')
('properties', 'stringsfile'), format_id = 'ts'
False, loader = ('ts2', 'tsfile')
),
'properties': FileFormat( register_fileformat(TSFormat)
_('Java Properties'),
('properties', 'javafile'),
True, class XliffFormat(FileFormat):
# Java properties need to be iso-8859-1, but name = _('XLIFF Translation File')
# ttkit converts them to utf-8 format_id = 'xliff'
fixups={'encoding': 'iso-8859-1'}, loader = ('xliff', 'xlifffile')
),
'properties-utf8': FileFormat( register_fileformat(XliffFormat)
_('Java Properties (UTF-8)'),
('properties', 'javautf8file'),
True, class StringsFormat(FileFormat):
), name = _('OS X Strings')
'php': FileFormat( format_id = 'strings'
_('PHP strings'), loader = ('properties', 'stringsfile')
('php', 'phpfile'), monolingual = False
),
'aresource': FileFormat( register_fileformat(StringsFormat)
_('Android String Resource'),
('aresource', 'AndroidResourceFile'),
True, class PropertiesFormat(FileFormat):
mark_fuzzy=True, name = _('Java Properties')
) format_id = 'properties'
} loader = ('properties', 'javafile')
monolingual = True
def fixup(self, store):
'''
Java properties need to be iso-8859-1, but
ttkit converts them to utf-8.
'''
store.enncoding = 'iso-8859-1'
register_fileformat(PropertiesFormat)
class PropertiesUtf8Format(FileFormat):
name = _('Java Properties (UTF-8)')
format_id = 'properties-utf8'
loader = ('properties', 'javautf8file')
monolingual = 5
register_fileformat(PropertiesUtf8Format)
class PhpFormat(FileFormat):
name = _('PHP strings')
format_id = 'php'
loader = ('php', 'phpfile')
register_fileformat(PhpFormat)
class AndroidFormat(FileFormat):
name = _('Android String Resource')
format_id = 'aresource'
loader = ('aresource', 'AndroidResourceFile')
monolingual = True
mark_fuzzy = True
register_fileformat(AndroidFormat)
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]
......
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