Did I find the right examples for you? yes no Crawl my project Python Jobs
All Samples(7) | Call(7) | Derive(0) | Import(0)
Read a binary MO file from the given file-like object and return a corresponding `Catalog` object. :param fileobj: the file-like object to read the MO file from :note: The implementation of this function is heavily based on the ``GNUTranslations._parse`` method of the ``gettext`` module in the standard library.
def read_mo(fileobj): """Read a binary MO file from the given file-like object and return a corresponding `Catalog` object. :param fileobj: the file-like object to read the MO file from :note: The implementation of this function is heavily based on the ``GNUTranslations._parse`` method of the ``gettext`` module in the standard library. """ catalog = Catalog() headers = {} filename = getattr(fileobj, 'name', '') buf = fileobj.read() buflen = len(buf) unpack = struct.unpack # Parse the .mo file header, which consists of 5 little endian 32 # bit words. magic = unpack('4I', buf[4:20]) ii = '>II' else: raise IOError(0, 'Bad magic number', filename) # Now put all messages from the .mo file buffer into the catalog # dictionary for i in range_type(0, msgcount): mlen, moff = unpack(ii, buf[origidx:origidx + 8]) mend = moff + mlen tlen, toff = unpack(ii, buf[transidx:transidx + 8]) tend = toff + tlen if mend < buflen and tend < buflen: msg = buf[moff:mend] tmsg = buf[toff:tend] else: raise IOError(0, 'File is corrupt', filename) # See if we're looking at GNU .mo conventions for metadata if mlen == 0: # Catalog description lastkey = key = None for item in tmsg.splitlines(): item = item.strip() if not item: continue if b':' in item: key, value = item.split(b':', 1) lastkey = key = key.strip().lower() headers[key] = value.strip() elif lastkey: headers[lastkey] += b'\n' + item if b'\x04' in msg: # context ctxt, msg = msg.split(b'\x04') else: ctxt = None if b'\x00' in msg: # plural forms msg = msg.split(b'\x00') tmsg = tmsg.split(b'\x00') if catalog.charset: msg = [x.decode(catalog.charset) for x in msg] tmsg = [x.decode(catalog.charset) for x in tmsg] else: if catalog.charset: msg = msg.decode(catalog.charset) tmsg = tmsg.decode(catalog.charset) catalog[msg] = Message(msg, tmsg, context=ctxt) # advance to next entry in the seek tables origidx += 8 transidx += 8 catalog.mime_headers = headers.items() return catalog
def test_basics(self): mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', 'LC_MESSAGES', 'messages.mo')) try: catalog = mofile.read_mo(mo_file)
src/z/e/ZenPacks.zenoss.OpenStack-HEAD/src/Babel-1.3/tests/messages/test_mofile.py ZenPacks.zenoss.OpenStack(Download)
mo_file = open(mo_path, 'rb') try: catalog = mofile.read_mo(mo_file) self.assertEqual(2, len(catalog)) self.assertEqual('TestProject', catalog.project)
src/w/e/webapp-improved-HEAD/lib/babel/babel/messages/tests/mofile.py webapp-improved(Download)
def test_basics(self): mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', 'LC_MESSAGES', 'messages.mo')) try: catalog = mofile.read_mo(mo_file)
src/f/j/fjord-HEAD/vendor/packages/Babel/tests/messages/test_mofile.py fjord(Download)
mo_file = open(mo_path, 'rb') try: catalog = mofile.read_mo(mo_file) self.assertEqual(2, len(catalog)) self.assertEqual('TestProject', catalog.project)
src/b/a/Babel-CLDR-HEAD/babel/messages/tests/mofile.py Babel-CLDR(Download)
def test_basics(self): mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', 'LC_MESSAGES', 'messages.mo')) try: catalog = mofile.read_mo(mo_file)
src/b/a/babel-HEAD/babel/messages/tests/mofile.py babel(Download)
def test_basics(self): mo_file = open(os.path.join(self.datadir, 'project', 'i18n', 'de', 'LC_MESSAGES', 'messages.mo')) try: catalog = mofile.read_mo(mo_file)
src/b/a/Babel-1.3/tests/messages/test_mofile.py Babel(Download)
mo_file = open(mo_path, 'rb') try: catalog = mofile.read_mo(mo_file) self.assertEqual(2, len(catalog)) self.assertEqual('TestProject', catalog.project)