diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2013-05-26 19:33:15 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2013-05-26 19:33:15 (GMT) |
commit | 2a37ffeb58fa954472e5901a4ee0b42fdbfa27f4 (patch) | |
tree | bea1de8e55113ea2f9dce6347d5bc8da5dd4ad31 | |
parent | e1d74619f13f9665e311d7f1d4acf7a4e243bf11 (diff) |
Read activity metadata from .mo files instead of .linfo
For now, .linfo files contain inapropriate i18n strings.
-rwxr-xr-x | misc/aslo-sync | 81 | ||||
-rw-r--r-- | sugar_network/client/bundle.py | 34 | ||||
-rw-r--r-- | sugar_network/db/directory.py | 3 |
3 files changed, 75 insertions, 43 deletions
diff --git a/misc/aslo-sync b/misc/aslo-sync index 766d6ca..0ba1a67 100755 --- a/misc/aslo-sync +++ b/misc/aslo-sync @@ -17,8 +17,10 @@ import os import time +import shutil import getpass -from ConfigParser import ConfigParser +import gettext +import tempfile from os.path import join import MySQLdb as mdb @@ -121,11 +123,9 @@ LICENSES_MAP = { 'au.net.acid.Jam2Jam1': ['GPLv2+'], } -ACTIVITY_I18N_PROPS = [ - ('title', 'name'), - ('summary', 'summary'), - ('description', 'description'), - ] + +class AbsDict(dict): + pass class Application(application.Application): @@ -425,7 +425,7 @@ class Application(application.Application): props[prop] = spec[prop] try: - svg = bundle.extractfile(join(bundle.extract, spec['icon'])) + svg = bundle.extractfile(join(bundle.rootdir, spec['icon'])) icon = props['artifact_icon'] = svg.read() png = svg_to_png(icon, '--width=55', '--height=55') if png: @@ -434,23 +434,54 @@ class Application(application.Application): if png: props['preview'] = png except Exception, error: - print '-- Cannot find activity icon: %s' % error - - for prop, confname in ACTIVITY_I18N_PROPS: + print '-- Cannot find activity icon in %r: %s' % (filename, error) + + msgids = {} + for prop, confname in [ + ('title', 'name'), + ('summary', 'summary'), + ('description', 'description'), + ]: if spec[confname]: - props[prop] = {'en': spec[confname]} - for path in bundle.get_names(): - locale_path = path.split(os.sep) - if len(locale_path) < 3 or locale_path[-3] != 'locale' or \ - locale_path[-1] != 'activity.linfo': - continue - config = ConfigParser() - config.readfp(bundle.extractfile(path)) - lang = locale_path[-2].replace('_', '-').lower() - for prop, confname in ACTIVITY_I18N_PROPS: - if config.has_option('Activity', confname): - value = config.get('Activity', confname) - props.setdefault(prop, {})[lang] = value + msgids[prop] = spec[confname] + + title, summary, description = self.sqlexec(""" + SELECT + addons.name, + addons.summary, + addons.description + FROM + addons + WHERE + addons.id = %s + """ % addon_id)[0] + if 'title' not in msgids: + props['title'] = AbsDict(self.get_i18n_field(title)) + if 'summary' not in msgids: + props['summary'] = AbsDict(self.get_i18n_field(summary)) + if 'description' not in msgids: + props['description'] = AbsDict(self.get_i18n_field(description)) + + tmpdir = tempfile.mkdtemp() + try: + for path in bundle.get_names(): + if not path.endswith('.mo'): + continue + locale_path = path.strip(os.sep).split(os.sep) + if len(locale_path) != 5 or locale_path[1] != 'locale': + continue + lang = locale_path[2] + bundle.extract(path, tmpdir) + i18n = gettext.translation(bundle_id, + join(tmpdir, *locale_path[:2]), [lang]) + for prop, value in msgids.items(): + msgstr = i18n.gettext(value) + if msgstr != value or lang == 'en': + props.setdefault(prop, AbsDict())[lang] = msgstr + except Exception, error: + print '-- Failed to read locales from %r: %s' % (filename, error) + finally: + shutil.rmtree(tmpdir) print '-- Update %r metadata from %r' % (bundle_id, filename) self.volume['context'].update(bundle_id, **props) @@ -560,7 +591,7 @@ class Application(application.Application): spec={'*-*': { 'commands': spec.commands, 'requires': spec.requires, - 'extract': bundle.extract, + 'extract': bundle.rootdir, }}, ctime=time.time(), mtime=time.time(), author=self.authors(), @@ -598,7 +629,7 @@ class Application(application.Application): translations WHERE id = %s""" % an_id): - result[locale] = value + result[locale.lower()] = value return result def sqlexec(self, text): diff --git a/sugar_network/client/bundle.py b/sugar_network/client/bundle.py index 956619b..0e9bbca 100644 --- a/sugar_network/client/bundle.py +++ b/sugar_network/client/bundle.py @@ -28,7 +28,7 @@ class BundleError(Exception): class Bundle(object): def __init__(self, bundle, mime_type=None): - self._extract = False + self._rootdir = False if mime_type is None: mime_type = _detect_mime_type(bundle) or '' @@ -37,10 +37,12 @@ class Bundle(object): self._bundle = zipfile.ZipFile(bundle) self._do_get_names = self._bundle.namelist self._do_extractfile = self._bundle.open + self._do_extract = self._bundle.extract elif mime_type.split('/')[-1].endswith('-tar'): self._bundle = tarfile.open(bundle) self._do_get_names = self._bundle.getnames self._do_extractfile = self._bundle.extractfile + self._do_extract = self._bundle.extract else: raise BundleError('Unsupported bundle type for "%s" file, ' 'it can be either tar or zip.' % bundle) @@ -55,10 +57,8 @@ class Bundle(object): def get_names(self): return self._do_get_names() - def extractfileto(self, name, dst_path): - f = file(dst_path, 'w') - f.write(self._do_extractfile(name).read()) - f.close() + def extract(self, name, dst_path): + return self._do_extract(name, dst_path) def extractfile(self, name): return self._do_extractfile(name) @@ -67,26 +67,26 @@ class Bundle(object): self._bundle.extractall(path=path, members=members) @property - def extract(self): - if self._extract is not False: - return self._extract - self._extract = None + def rootdir(self): + if self._rootdir is not False: + return self._rootdir + self._rootdir = None for arcname in self.get_names(): parts = arcname.split(os.sep) if len(parts) > 1: - if self._extract is None: - self._extract = parts[0] - elif parts[0] != self._extract: - self._extract = None + if self._rootdir is None: + self._rootdir = parts[0] + elif parts[0] != self._rootdir: + self._rootdir = None break - return self._extract + return self._rootdir def get_spec(self): - if self.extract: - specs = (join(self.extract, 'sweets.recipe'), - join(self.extract, 'activity', 'activity.info')) + if self.rootdir: + specs = (join(self.rootdir, 'sweets.recipe'), + join(self.rootdir, 'activity', 'activity.info')) else: specs = ('sweets.recipe', join('activity', 'activity.info')) diff --git a/sugar_network/db/directory.py b/sugar_network/db/directory.py index 91136d2..8eb36d4 100644 --- a/sugar_network/db/directory.py +++ b/sugar_network/db/directory.py @@ -392,7 +392,8 @@ class Directory(object): if prop.localized: if not isinstance(value, dict): value = {toolkit.default_lang(): value} - if existed: + if existed and \ + type(value) is dict: # TODO To reset `value` meta = record.get(name) if meta is not None: meta['value'].update(value) |