Esempio n. 1
0
def _write(lines, outfile):
    """
    Writes a re-assembled file. This file is basically the original template
    with replaced (translated) strings.

    @param lines: The re-assembled (translated) lines from the original
        template.
    @param outfile: The output file name or None for writing to stdout.
    """

    if outfile is not None:
        # check if the file is writable
        if not os.access(os.path.dirname(outfile), os.W_OK):
            print_error_message("PO file not writable")
            sys.exit()
        out = file(outfile, 'w')
    else:
        out = sys.stdout

    for curl in lines:
        out.write(curl + '\n')

    if not outfile is None:
        out.close()

    return
Esempio n. 2
0
def _write(lines, outfile):
    """
    Writes a re-assembled file. This file is basically the original template
    with replaced (translated) strings.

    @param lines: The re-assembled (translated) lines from the original
        template.
    @param outfile: The output file name or None for writing to stdout.
    """

    if outfile is not None:
        # check if the file is writable
        if not os.access(os.path.dirname(outfile), os.W_OK):
            print_error_message("PO file not writable")
            sys.exit( )
        out = file(outfile, 'w')
    else:
        out = sys.stdout

    for curl in lines:
        out.write(curl + '\n')

    if not outfile is None:
        out.close( )

    return
Esempio n. 3
0
def main():
    """
    The main function.

    @author: Marco Wegner
    """

    usage = "usage: %prog [options] template1 [template2, ...]"
    parser = OptionParser(usage, conflict_handler="resolve")
    parser.add_options(support.get_global_options())
    parser.add_options(_get_local_options())

    (options, args) = parser.parse_args()

    # the script has been called without any options or arguments
    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit()

    delimiters = support.extract_delimiters(options)

    # check if the template file is defined
    if len(args) == 0:
        support.print_error_message("no template file(s) specified")
        sys.exit()

    if options.verbose:
        print "\nExtracting translatable strings from the template files:"

    super_store = UT3Store()

    count = 0
    for tmpl_file in args:
        tmpl = UT3Template(tmpl_file, delimiters)
        tmpl_store = tmpl.parse()
        num_rem = len(tmpl_store.get_all())
        count = count + num_rem

        super_store.extend(tmpl_store)

        if options.verbose:
            tname = os.path.basename(tmpl_file)
            print " * " + tname + ": " + str(num_rem) + " messages"

    if options.verbose:
        num_rem = len(super_store.get_all())
        print "\nHandling duplicates: removing %i strings" % (count - num_rem)
        print "\nRemaining to be written: %i strings" % (num_rem)
        print "\nWriting the template...\n"

    pofile = Ut3PoFile(options.outfile)
    pofile.write(super_store, True)

    if options.verbose:
        print "\nDone."

    return
Esempio n. 4
0
    def write(self, tstore, pot_mode = False):
        """
        Actually writes the POT file.

        @param tstore: The UT3 translation store.
        @param pot_mode: If true then write a POT file.
        """

        if not isinstance(tstore, UT3Store):
            raise TypeError

        if self.__name is not None:
            # check if the file is writable
            if not os.access(os.path.dirname(self.__name), os.W_OK):
                print_error_message("PO file not writable")
                sys.exit( )
            out = file(self.__name, 'w')
        else:
            out = sys.stdout

        self._write_header(out, pot_mode)

        for ent in tstore.get_all( ):
            out.write('\n')

            if ent.comment is not None:
                out.write('#. %s\n' % (ent.comment))

            sources = ent.get_sources( )
            if len(sources) > 1 or (len(sources) == 1 and not "" in sources):
                out.write('#: %s\n' % (", ".join(sources)))

            if ent.translation is not None and ent.translation != "" and ent.status == store.STATUS_UNFINISHED:
                out.write('#, fuzzy\n')

            out.write('msgid \"%s\"\n' % (ent.message.replace('\"', '\\"')))
            tr = ""
            if ent.translation is not None:
                tr = ent.translation.replace('\"', '\\"')
            out.write('msgstr \"%s\"\n' % (tr.encode("utf-8")))

        if not self.__name is None:
            out.close( )

        return
Esempio n. 5
0
    def _get_file_lines(self):
        """
        Extract the lines of the PO file and return it as string list.

        @return: The file's lines as string list
        """

        # check if the PO/POT file is readable
        if self.__name is None or not os.access(self.__name, os.R_OK):
            print_error_message("PO file does not exist or is not readable")
            sys.exit( )

        # read the PO file
        pofile = file(self.__name, 'r')
        lines = pofile.readlines( )
        pofile.close( )

        return lines
Esempio n. 6
0
    def __init__(self, name, delims):
        """
        Creates a new UT3 template file instance.

        @param name: The template file name.
        @param delims: The delimiters options as a dictionary.
        """

        # check if the template is readable
        if name is None or not os.access(name, os.R_OK):
            print_error_message("specified template file does not exist " \
                + "or is not readable")
            sys.exit()

        self.__name = name
        self.__delims = delims

        return
Esempio n. 7
0
    def __init__(self, name, delims):
        """
        Creates a new UT3 template file instance.

        @param name: The template file name.
        @param delims: The delimiters options as a dictionary.
        """

        # check if the template is readable
        if name is None or not os.access(name, os.R_OK):
            print_error_message("specified template file does not exist " \
                + "or is not readable")
            sys.exit( )

        self.__name = name
        self.__delims = delims

        return
Esempio n. 8
0
    def write(self, tstore):
        """
        Writes a Qt TS file.

        @param tstore: The UT3 translation store with the items to be written.
        """

        if not isinstance(tstore, UT3Store):
            raise TypeError

        if self.__name is not None:
            # check if the file is writable
            if not os.access(os.path.dirname(self.__name), os.W_OK):
                support.print_error_message("PO file not writable")
                sys.exit()
            out = file(self.__name, 'w')
        else:
            out = sys.stdout

        impl = xml.dom.getDOMImplementation()
        doctype = impl.createDocumentType('TS', None, None)
        doc = impl.createDocument(None, "TS", doctype)
        root = doc.documentElement

        source_list = tstore.get_sources()
        if len(source_list) == 0:
            context_entries = tstore.get_by_source("")
            root.appendChild(
                _generate_context_element(doc, "", context_entries))
        else:
            for context_name in source_list:
                context_entries = tstore.get_by_source(context_name)
                root.appendChild(
                    _generate_context_element(doc, context_name,
                                              context_entries))

        PrettyPrint(root, out)

        if not self.__name is None:
            out.close()

        doc.unlink()

        return
Esempio n. 9
0
    def write(self, tstore):
        """
        Writes a Qt TS file.

        @param tstore: The UT3 translation store with the items to be written.
        """

        if not isinstance(tstore, UT3Store):
            raise TypeError

        if self.__name is not None:
            # check if the file is writable
            if not os.access(os.path.dirname(self.__name), os.W_OK):
                support.print_error_message("PO file not writable")
                sys.exit( )
            out = file(self.__name, 'w')
        else:
            out = sys.stdout

        impl = xml.dom.getDOMImplementation( )
        doctype = impl.createDocumentType('TS', None, None)
        doc = impl.createDocument(None, "TS", doctype)
        root = doc.documentElement

        source_list = tstore.get_sources( )
        if len(source_list) == 0:
            context_entries = tstore.get_by_source("")
            root.appendChild(_generate_context_element(doc, "", context_entries))
        else:
            for context_name in source_list:
                context_entries = tstore.get_by_source(context_name)
                root.appendChild(_generate_context_element(doc, context_name, context_entries))

        PrettyPrint(root, out)

        if not self.__name is None:
            out.close( )

        doc.unlink( )

        return
Esempio n. 10
0
    def parse(self):
        """
        Parse the TS file.

        @return: A translation entry store containing the parsed entries.
        """

        # check if the PO/POT file is readable
        if self.__name is None or not os.access(self.__name, os.R_OK):
            support.print_error_message("TS file does not exist or is not readable")
            sys.exit( )

        doc = minidom.parse(self.__name).documentElement

        tstore = UT3Store( )

        context_elems = doc.getElementsByTagName('context')
        entry_list = _handle_context_elements(context_elems)
        tstore.append_entries(entry_list)

        doc.unlink( )

        return tstore
Esempio n. 11
0
    def parse(self):
        """
        Parse the TS file.

        @return: A translation entry store containing the parsed entries.
        """

        # check if the PO/POT file is readable
        if self.__name is None or not os.access(self.__name, os.R_OK):
            support.print_error_message(
                "TS file does not exist or is not readable")
            sys.exit()

        doc = minidom.parse(self.__name).documentElement

        tstore = UT3Store()

        context_elems = doc.getElementsByTagName('context')
        entry_list = _handle_context_elements(context_elems)
        tstore.append_entries(entry_list)

        doc.unlink()

        return tstore
Esempio n. 12
0
def main():
    """
    The main function.

    @author: Marco Wegner
    """

    parser = OptionParser(usage="usage: %prog [options]",
                          conflict_handler="resolve")
    parser.add_options(support.get_global_options())
    parser.add_options(_get_local_options())

    (options, args) = parser.parse_args()

    num_args = len(args)

    # there are arguments present - but we cannot do anything with them
    if num_args > 0:
        support.print_warning_message("unused argument(s) %s" %
                                      (" ".join(args)))

    # the script has been called without any options (but perhaps with
    # ususable arguments)
    if (len(sys.argv) - num_args) == 1:
        parser.print_help()
        sys.exit()

    delimiters = support.extract_delimiters(options)

    # check if the input file is specified
    if options.infile is None:
        support.print_error_message("no input file specified")
        sys.exit()

    pofile = Ut3PoFile(options.infile)
    store = pofile.parse()

    if (options.verbose):
        print "\nProcessing %s:" % (os.path.basename(options.infile))
        po_count = len(store.get_all())
        po_untr = len(store.get_unfinished())
        print " * %i messages (%i translated, %i untranslated)" \
            % (po_count, po_count-po_untr,po_untr)

    # check if the template file is defined
    if options.template is None:
        support.print_error_message("no template file specified")
        sys.exit()

    oname = "output"
    if options.outfile is not None:
        oname = os.path.basename(options.outfile)

    if options.verbose:
        print "\nAssembling %s:" % (oname)

    tmpl = UT3Template(options.template, delimiters)
    tmpl_count, tmpl_untr = tmpl.convert(store, options.outfile)

    if options.verbose:
        print " * %i messages (%i translated, %i untranslated)" \
            % (tmpl_count, tmpl_count-tmpl_untr, tmpl_untr)
        print "\n" + oname + " written."
        print "\nDone."

    return