def resources(self, locale=None, domain=None): """Iterator on resources for given language code (or from values dir)""" locale = locale or self.SOURCE_LOCALE values_dir = locale != self.SOURCE_LOCALE and "values-{}".format( locale) or "values" pattern = domain and "{0}.xml".format(domain) or self.search search = os.path.join(self.basepath, values_dir, pattern) for source in glob.glob(search): (_, fname) = os.path.split(source) (domain_file, _) = os.path.splitext(fname) xml = etree.parse(source) for node in xml.getroot(): options = { "domain": unicode(domain_file), "locale": unicode(locale) } if node.tag == 'string': options['message'] = node.text and unicode( node.text) or u"" options['name'] = unicode(node.attrib['name']) yield Resource(**options) elif node.tag == 'plurals': options['plurals'] = plurals_to_dict(node) options['name'] = unicode(node.attrib['name']) yield Resource(**options)
def resources(self, project, locale, domain=None): """Get all resource for project in given locale""" self.login() if domain: options = urlencode({ "reduce": "false", "startkey": '["{0}", "{1}", "{2}"]'.format(project, locale, domain), "endkey": '["{0}", "{1}", "{2}", {{}}]'.format(project, locale, domain) }) else: options = urlencode({ "reduce": "false", "startkey": '["{0}", "{1}"]'.format(project, locale), "endkey": '["{0}", "{1}", {{}}]'.format(project, locale) }) view = os.path.join( self.base_url, "{url}?{options}".format(url=self.RESOURCES_URI, options=options)) self.conn.connect() self.conn.request("GET", view, headers={'Cookie': self._auth_token}) response = json.loads(self.conn.getresponse().read()) self.conn.close() return (Resource(**doc['value']) for doc in response['rows'])
def test_update(self): # write predefinded messages msg_in = Resource(domain="strings-fresh", message="translated ' one", name="fresh_string_one", project="test", locale="ru") self.androidxml.update([msg_in], locale="ru", domain="strings-fresh") # read and checkall translations = self.androidxml.resources(locale="ru") translated = [ msg for msg in translations if msg.domain == "strings-fresh" ] self.assertEqual(translated[0].text, msg_in.text)
def translations(self, resource): """Return translations for given resource""" self.login() resource_id = isinstance(resource, Resource) and resource._id or resource options = urlencode({ 'startkey': '["{id}"]'.format(id=resource_id), 'endkey': '["{id}", {{}}]'.format(id=resource_id) }) url = os.path.join(self.base_url, self.TRANSLATIONS_URI.format(options=options)) self.conn.connect() self.conn.request("GET", url, headers={"Cookie": self._auth_token}) data = json.loads(self.conn.getresponse().read()) self.conn.close() result = {} for row in data['rows']: result[row['key'][1]] = Resource(**row['value']) return result
def resources(self, locale, domain=None): """Yields a resources from php array""" pattern = domain and domain + self.filepattern or self.filepattern search = os.path.join(self.path, locale, pattern) for domain_file in glob(search): (_, domain) = os.path.split(domain_file) if domain in self.exclude: # domain is excluded, so just skip continue (domain, _) = os.path.splitext(domain) if domain in self.exclude: # domain is excluded, so just skip continue entries = parse_array( open(domain_file).read(), self.varname, self.php_bin) entries = entries or {} for (key, val) in flatten(entries).items(): yield Resource(domain=domain, locale=locale, message=val, name=key)
def test_notequality(self): res1 = Resource(domain="test", message="msg", locale="en", name="key") res2 = Resource(domain="test", message="msg", locale="en", name="key1") res3 = Resource(domain="test", message="msg1", locale="en", name="key1") self.assertTrue(res1 != res2 and res2 != res3 and res1 != res3)
def test_dict(self): msg = Resource(**self.test_msg) self.assertEqual(self.test_msg, msg.as_dict)
def test_equality(self): res1 = Resource(**self.test_msg) res2 = Resource(**self.test_msg) res3 = res1 self.assertTrue(res1 == res2 and res1 is not res2 and res3 is res1)
def test_assign(self): msg = Resource(**self.test_msg) msg.message = u'Updated' self.assertEqual(msg.text, u'Updated')
def test_conversions(self): msg = Resource(**self.test_msg) msg_out = json.loads(msg.as_json) self.assertEqual(self.test_msg, msg_out)
def test_plurals(self): msg = Resource( domain=self.domain, plurals=self.plurals, name=self.name, locale=self.locale ) self.assertTrue(msg.text == self.plurals['other'] and msg.is_plural)
def test_string(self): msg = Resource( domain=self.domain, message=self.string, name=self.name, locale=self.locale ) self.assertTrue(msg.text == self.string and msg.is_plural is False)
def create_resource(self, text, name=None): """Utility function for create resource""" return Resource(domain=self.domain, message=text, name=name or self.name, locale=self.locale)