Esempio n. 1
0
 def test_invalid_pofile_with_abort_flag(self):
     parser = pofile.PoFileParser(None, abort_invalid=True)
     lineno = 10
     line = 'Algo esta mal'
     msg = 'invalid file'
     with self.assertRaises(pofile.PoFileError) as e:
         parser._invalid_pofile(line, lineno, msg)
Esempio n. 2
0
def read_po(po_path):
    """
    Read a Po file
    :param po_path: .po path
    :return: po content
    """
    from babel.messages import pofile

    logger = logging.getLogger('destral.utils.read_po')
    try:
        with open(po_path, 'r') as pot:
            catalog = pofile.read_po(pot)
            return catalog
    except ValueError:
        # If bad formatted data, replace it
        with open(po_path, 'r') as potB:
            data = potB.read()
        from re import sub, findall
        from dateutil.parser import parse as date_parse
        from babel.messages import Catalog
        replace_string = r'\1 {}\2'.format(
            date_parse(findall(r"POT-Creation-Date: (.*)\\n",
                               data)[0]).strftime('%Y-%m-%d %H:%M'))
        data = sub(r"(POT-Creation-Date:).*(\\n)", replace_string, data)
        replace_string = r'\1 {}\2'.format(
            date_parse(findall(r"PO-Revision-Date: (.*)\\n",
                               data)[0]).strftime('%Y-%m-%d %H:%M'))
        data = sub(r"(PO-Revision-Date:).*(\\n)", replace_string, data)
        catalog = Catalog()
        parser = pofile.PoFileParser(catalog)
        parser.parse(data.split('\n'))
        logger.warning('Data of POfile {} has bad formatted '
                       'creation or revision dates'.format(po_path))
        return catalog
Esempio n. 3
0
def read_po(fileobj,
            locale=None,
            domain=None,
            ignore_obsolete=False,
            charset=None):
    if locale in MISSING_LOCALES:
        catalog = PatchedCatalog(locale=MISSING_LOCALES[locale]['plural_rule'],
                                 domain=domain,
                                 charset=charset,
                                 original_locale=locale,
                                 copyright_holder="Mozilla",
                                 project="Focus for Android")
    else:
        catalog = PatchedCatalog(locale=locale, domain=domain, charset=charset)
    parser = pofile.PoFileParser(catalog, ignore_obsolete)
    parser.parse(fileobj)
    return catalog