Exemple #1
0
def convertprop(
    inputfile,
    outputfile,
    templatefile,
    personality="java",
    pot=False,
    duplicatestyle="msgctxt",
    encoding=None,
):
    """reads in inputfile using properties, converts using prop2po, writes to
    outputfile
    """
    inputstore = properties.propfile(inputfile, personality, encoding)
    convertor = prop2po(personality=personality,
                        blankmsgstr=pot,
                        duplicatestyle=duplicatestyle)
    if templatefile is None:
        outputstore = convertor.convertstore(inputstore)
    else:
        templatestore = properties.propfile(templatefile, personality,
                                            encoding)
        outputstore = convertor.mergestore(templatestore, inputstore)
    if outputstore.isempty():
        return 0
    outputstore.serialize(outputfile)
    return 1
Exemple #2
0
 def prop2po(self, propsource, proptemplate=None):
     """helper that converts .properties source to po source without requiring files"""
     inputfile = wStringIO.StringIO(propsource)
     inputprop = properties.propfile(inputfile)
     convertor = prop2po.prop2po()
     if proptemplate:
         templatefile = wStringIO.StringIO(proptemplate)
         templateprop = properties.propfile(templatefile)
         outputpo = convertor.mergestore(templateprop, inputprop)
     else:
         outputpo = convertor.convertstore(inputprop)
     return outputpo
Exemple #3
0
 def prop2po(self, propsource, proptemplate=None, personality="java"):
     """helper that converts .properties source to po source without requiring files"""
     inputfile = BytesIO(propsource.encode())
     inputprop = properties.propfile(inputfile, personality=personality)
     convertor = prop2po.prop2po(personality=personality)
     if proptemplate:
         templatefile = BytesIO(proptemplate.encode())
         templateprop = properties.propfile(templatefile)
         outputpo = convertor.mergestore(templateprop, inputprop)
     else:
         outputpo = convertor.convertstore(inputprop)
     return outputpo
Exemple #4
0
 def prop2po(self, propsource, proptemplate=None):
     """helper that converts .properties source to po source without requiring files"""
     inputfile = wStringIO.StringIO(propsource)
     inputprop = properties.propfile(inputfile)
     convertor = prop2po.prop2po()
     if proptemplate:
         templatefile = wStringIO.StringIO(proptemplate)
         templateprop = properties.propfile(templatefile)
         outputpo = convertor.mergestore(templateprop, inputprop)
     else:
         outputpo = convertor.convertstore(inputprop)
     return outputpo
Exemple #5
0
def convertprop(inputfile, outputfile, templatefile, personality="java", pot=False, duplicatestyle="msgctxt"):
    """reads in inputfile using properties, converts using prop2po, writes to outputfile"""
    inputstore = properties.propfile(inputfile, personality)
    convertor = prop2po()
    if templatefile is None:
        outputstore = convertor.convertstore(inputstore, personality, duplicatestyle=duplicatestyle)
    else:
        templatestore = properties.propfile(templatefile, personality)
        outputstore = convertor.mergestore(templatestore, inputstore, personality, blankmsgstr=pot, duplicatestyle=duplicatestyle)
    if outputstore.isempty():
        return 0
    outputfile.write(str(outputstore))
    return 1
Exemple #6
0
def po2it(inputfile,
          outputfile,
          templatefile,
          encoding="cp1252",
          includefuzzy=False):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = StringIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = [
            line
            for line in mozfunny2prop.it2prop(templatelines, encoding=encoding)
        ]
        templatepropfile = StringIO("".join(templateproplines))
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(inputfile,
                                        outputpropfile,
                                        templatepropfile,
                                        includefuzzy=includefuzzy)
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2it(pf)
        for line in outputlines:
            line = line.decode("utf-8").encode(encoding)
            outputfile.write(line)
    return result
Exemple #7
0
def po2inc(inputfile,
           outputfile,
           templatefile,
           encoding=None,
           includefuzzy=False):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = StringIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = [
            line for line in mozfunny2prop.inc2prop(templatelines)
        ]
        templatepropfile = StringIO("".join(templateproplines))
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(inputfile,
                                        outputpropfile,
                                        templatepropfile,
                                        includefuzzy=includefuzzy)
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2inc(pf)
        outputfile.writelines(outputlines)
    return result
def po2inc(
    inputfile,
    outputfile,
    templatefile,
    encoding=None,
    includefuzzy=False,
    remove_untranslated=False,
    outputthreshold=None,
):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = StringIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = [line for line in mozfunny2prop.inc2prop(templatelines)]
        templatepropfile = StringIO("".join(templateproplines))
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(
        inputfile,
        outputpropfile,
        templatepropfile,
        includefuzzy=includefuzzy,
        remove_untranslated=remove_untranslated,
        outputthreshold=outputthreshold,
    )
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2inc(pf)
        outputfile.writelines([line.encode("utf-8") for line in outputlines])
    return result
Exemple #9
0
def po2it(
    inputfile,
    outputfile,
    templatefile,
    encoding="cp1252",
    includefuzzy=False,
    remove_untranslated=False,
    outputthreshold=None,
):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = BytesIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = list(
            mozfunny2prop.it2prop(templatelines, encoding=encoding))
        templatepropfile = BytesIO("".join(templateproplines).encode())
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(
        inputfile,
        outputpropfile,
        templatepropfile,
        includefuzzy=includefuzzy,
        remove_untranslated=remove_untranslated,
        outputthreshold=outputthreshold,
    )
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2it(pf)
        for line in outputlines:
            line = line.encode(encoding)
            outputfile.write(line)
    return result
Exemple #10
0
def po2inc(inputfile,
           outputfile,
           templatefile,
           encoding=None,
           includefuzzy=False,
           remove_untranslated=False,
           outputthreshold=None):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = BytesIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = [
            line for line in mozfunny2prop.inc2prop(templatelines)
        ]
        templatepropfile = BytesIO("".join(templateproplines).encode())
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(
        inputfile,
        outputpropfile,
        templatepropfile,
        includefuzzy=includefuzzy,
        remove_untranslated=remove_untranslated,
        outputthreshold=outputthreshold)
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2inc(pf)
        outputfile.writelines([line.encode('utf-8') for line in outputlines])
    return result
Exemple #11
0
    def test_accel_header(self):
        """Test that we correctly create the header entry for accelerators."""
        propsource = '''prop=value\n'''
        convertor = prop2po.prop2po()

        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="mozilla")
        outputpo = convertor.convertstore(inputprop, personality="mozilla")
        assert "X-Accelerator-Marker" in str(outputpo)

        # Even though the gaia flavour inherrits from mozilla, it should not
        # get the header
        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="gaia")
        outputpo = convertor.convertstore(inputprop, personality="gaia")
        assert "X-Accelerator-Marker" not in str(outputpo)
Exemple #12
0
def prop2funny(src, itencoding="cp1252"):
    lines = src.split("\n")
    header = lines[0]
    if not header.startswith("# converted from "):
        waspseudoprops = len(
            [line for line in lines if line.startswith("# section:")])
        wasdefines = len([
            line for line in lines
            if line.startswith("#filter") or line.startswith("#unfilter")
        ])
    else:
        waspseudoprops = "pseudo-properties" in header
        wasdefines = "#defines" in header
        lines = lines[1:]
    if not (waspseudoprops ^ wasdefines):
        raise ValueError(
            "could not determine file type as pseudo-properties or defines file"
        )
    pf = properties.propfile(personality="mozilla")
    pf.parse("\n".join(lines))
    if wasdefines:
        for line in prop2inc(pf):
            yield line + "\n"
    elif waspseudoprops:
        for line in prop2it(pf):
            yield (line + "\n").encode(itencoding)
Exemple #13
0
    def test_x_header(self):
        """Test that we correctly create the custom header entries
        (accelerators, merge criterion).
        """
        propsource = '''prop=value\n'''
        convertor = prop2po.prop2po()

        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="mozilla")
        outputpo = convertor.convertstore(inputprop, personality="mozilla")
        assert "X-Accelerator-Marker" in str(outputpo)
        assert "X-Merge-On" in str(outputpo)

        # Even though the gaia flavour inherrits from mozilla, it should not
        # get the header
        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="gaia")
        outputpo = convertor.convertstore(inputprop, personality="gaia")
        assert "X-Accelerator-Marker" not in str(outputpo)
        assert "X-Merge-On" not in str(outputpo)
Exemple #14
0
 def propparse(self, propsource, personality="gwt", encoding=None, sourcelanguage=None, targetlanguage=None):
     """helper that parses properties source without requiring files"""
     dummyfile = BytesIO(propsource.encode())
     propfile = properties.propfile(None, personality, encoding)
     if sourcelanguage:
         propfile.sourcelanguage = sourcelanguage
     if targetlanguage:
         propfile.targetlanguage = targetlanguage
     propsrc = dummyfile.read()
     dummyfile.close()
     propfile.parse(propsrc)
     propfile.makeindex()
     return propfile
def po2it(inputfile, outputfile, templatefile, encoding="cp1252", includefuzzy=False):
    """wraps po2prop but converts outputfile to properties first"""
    outputpropfile = StringIO()
    if templatefile is not None:
        templatelines = templatefile.readlines()
        templateproplines = [line for line in mozfunny2prop.it2prop(templatelines, encoding=encoding)]
        templatepropfile = StringIO("".join(templateproplines))
    else:
        templatepropfile = None
    result = po2prop.convertmozillaprop(inputfile, outputpropfile, templatepropfile, includefuzzy=includefuzzy)
    if result:
        outputpropfile.seek(0)
        pf = properties.propfile(outputpropfile, personality="mozilla")
        outputlines = prop2it(pf)
        for line in outputlines:
            line = line.decode("utf-8").encode(encoding)
            outputfile.write(line)
    return result
def prop2funny(src, itencoding="cp1252"):
    lines = src.split("\n")
    header = lines[0]
    if not header.startswith("# converted from "):
        waspseudoprops = len([line for line in lines if line.startswith("# section:")])
        wasdefines = len([line for line in lines if line.startswith("#filter") or line.startswith("#unfilter")])
    else:
        waspseudoprops = "pseudo-properties" in header
        wasdefines = "#defines" in header
        lines = lines[1:]
    if not (waspseudoprops ^ wasdefines):
        raise ValueError("could not determine file type as pseudo-properties or defines file")
    pf = properties.propfile(personality="mozilla")
    pf.parse("\n".join(lines))
    if wasdefines:
        for line in prop2inc(pf):
            yield line + "\n"
    elif waspseudoprops:
        for line in prop2it(pf):
            yield line.decode("utf-8").encode(itencoding) + "\n"
Exemple #17
0
    def test_gaia_plurals(self):
        """Test conversion of gaia plural units."""
        propsource = '''
message-multiedit-header={[ plural(n) ]}
message-multiedit-header[zero]=Edit
message-multiedit-header[one]={{ n }} selected
message-multiedit-header[two]={{ n }} selected
message-multiedit-header[few]={{ n }} selected
message-multiedit-header[many]={{ n }} selected
message-multiedit-header[other]={{ n }} selected
'''
        convertor = prop2po.prop2po()
        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="gaia")
        outputpo = convertor.convertstore(inputprop, personality="gaia")
        pounit = outputpo.units[-1]
        assert pounit.hasplural()
        assert pounit.getlocations() == [u'message-multiedit-header']

        print outputpo
        zero_unit = outputpo.units[-2]
        assert not zero_unit.hasplural()
        assert zero_unit.source == u"Edit"
Exemple #18
0
    def test_gaia_plurals(self):
        """Test conversion of gaia plural units."""
        propsource = """
message-multiedit-header={[ plural(n) ]}
message-multiedit-header[zero]=Edit
message-multiedit-header[one]={{ n }} selected
message-multiedit-header[two]={{ n }} selected
message-multiedit-header[few]={{ n }} selected
message-multiedit-header[many]={{ n }} selected
message-multiedit-header[other]={{ n }} selected
"""
        convertor = prop2po.prop2po()
        inputfile = wStringIO.StringIO(propsource)
        inputprop = properties.propfile(inputfile, personality="gaia")
        outputpo = convertor.convertstore(inputprop, personality="gaia")
        pounit = outputpo.units[-1]
        assert pounit.hasplural()
        assert pounit.getlocations() == [u"message-multiedit-header"]

        print outputpo
        zero_unit = outputpo.units[-2]
        assert not zero_unit.hasplural()
        assert zero_unit.source == u"Edit"
Exemple #19
0
 def propparse(self, propsource, personality="java", encoding=None):
     """helper that parses properties source without requiring files"""
     dummyfile = BytesIO(propsource.encode() if isinstance(propsource, str) else propsource)
     propfile = properties.propfile(dummyfile, personality, encoding)
     return propfile
Exemple #20
0
 def propparse(self, propsource, personality="java", encoding=None):
     """helper that parses properties source without requiring files"""
     dummyfile = wStringIO.StringIO(propsource)
     propfile = properties.propfile(dummyfile, personality, encoding)
     return propfile
Exemple #21
0
 def propparse(self, propsource, personality="java", encoding=None):
     """helper that parses properties source without requiring files"""
     dummyfile = wStringIO.StringIO(propsource)
     propfile = properties.propfile(dummyfile, personality, encoding)
     return propfile