[gimp-help/wip/wormnest/python3-migration: 14/15] tools: add --base option in xml2po to set base path from build to source




commit 154f523392ddbbb7c5279ae6274ed11495cd9315
Author: Jacob Boerema <jgboerema gmail com>
Date:   Wed Jun 2 15:24:29 2021 -0400

    tools: add --base option in xml2po to set base path from build to source
    
    When using separate build and source directories the generated pot and po
    files got comments about source po files including the redirection path
    from build to source directory (e.g. ../../gimp-help).
    
    This can cause a lot of unnecessary changes when committing by someone that
    uses a different build directory path.
    
    To fix this we added an option --base to set the base path to get from
    the build directory to the source directory. This part of the filepath
    is then removed from the filepaths set in po and pot comments.

 tools/xml2po.py          | 13 ++++++++++---
 tools/xml2po/__init__.py | 26 ++++++++++++++++++--------
 2 files changed, 28 insertions(+), 11 deletions(-)
---
diff --git a/tools/xml2po.py b/tools/xml2po.py
index 970713f1b..5740a5d48 100755
--- a/tools/xml2po.py
+++ b/tools/xml2po.py
@@ -57,6 +57,10 @@ OPTIONS may be some of:
     -r    --reuse=FILE         Specify translated XML file with the same structure
     -t    --translation=FILE   Specify MO file containing translation, and merge
     -u    --update-translation=LANG.po   Updates a PO file using msgmerge program
+    -b    --base=PATH          Specify base path to source repository
+                               (e.g. -b ../gimp-help)
+                               If set will remove this part of filenames from
+                               po/pot comments
 
     -l    --language=LANG      Set language of the translation to LANG
           --mark-untranslated  Set 'xml:lang="C"' on untranslated tags
@@ -105,10 +109,11 @@ def main(argv):
     origxml = ''
     mofile = None
     mofile_tmppath = None
+    base = ''
 
-    try: opts, remaining_args = getopt.getopt(argv, 'avhkem:t:o:p:u:r:l:',
+    try: opts, remaining_args = getopt.getopt(argv, 'avhkem:t:o:p:u:r:l:b:',
                                ['automatic-tags','version', 'help', 'keep-entities', 'expand-all-entities', 
'mode=', 'translation=',
-                                'output=', 'po-file=', 'update-translation=', 'reuse=', 'language=', 
'mark-untranslated' ])
+                                'output=', 'po-file=', 'update-translation=', 'reuse=', 'language=', 
'mark-untranslated', 'base=' ])
     except getopt.GetoptError:
         usage(True)
         sys.exit(2)
@@ -155,6 +160,8 @@ def main(argv):
         elif opt in ('-h', '--help'):
             usage(True)
             sys.exit(0)
+        elif opt in ('-b', '--base'):
+            base = arg
 
     if operation == 'update' and output != "-":
         print("Option '-o' is not yet supported when updating translations directly. Ignoring this option.", 
file=sys.stderr)
@@ -165,7 +172,7 @@ def main(argv):
         filenames.append(remaining_args.pop())
 
     try:
-        xml2po_main = Main(default_mode, operation, output, options)
+        xml2po_main = Main(default_mode, operation, output, base, options)
     except IOError:
         print("Error: cannot open file %s for writing." % (output), file=sys.stderr)
         sys.exit(5)
diff --git a/tools/xml2po/__init__.py b/tools/xml2po/__init__.py
index e23bb4cfb..82fd020fa 100644
--- a/tools/xml2po/__init__.py
+++ b/tools/xml2po/__init__.py
@@ -142,11 +142,20 @@ msgstr ""
             out.write("msgstr \"%s\"\n\n" % (translation))
 
 class XMLDocument(object):
-    def __init__(self, filename, app):
+    def __init__(self, filename, base_path, app):
         self.filename = filename
         self.app = app
         self.expand_entities = self.app.options.get('expand_entities')
         self.ignored_tags = self.app.current_mode.getIgnoredTags()
+
+        # Remove the part of the path that redirects from build dir to
+        # source dir since that part depends on personal setup and can
+        # cause a lot of unnecessary changes in commits.
+        self.base_path = base_path
+        self.source_filename = self.filename
+        if self.source_filename.startswith(self.base_path):
+            self.source_filename = self.source_filename[len(self.base_path):]
+
         ctxt = libxml2.createFileParserCtxt(filename)
         ctxt.lineNumbers(1)
         if self.app.options.get('expand_all_entities'):
@@ -162,11 +171,11 @@ class XMLDocument(object):
         if self.doc.name != filename:
             raise Exception("Error: I tried to open '%s' but got '%s' -- how did that happen?" % (filename, 
self.doc.name))
         if self.app.msg:
-            self.app.msg.setFilename(filename)
+            self.app.msg.setFilename(self.source_filename)
         self.isFinalNode = self.app.current_mode.isFinalNode
 
     def generate_messages(self):
-        self.app.msg.setFilename(self.doc.name)
+        self.app.msg.setFilename(self.source_filename)
         self.doSerialize(self.doc)
 
     def normalizeNode(self, node):
@@ -641,13 +650,14 @@ def xml_error_handler(ctxt, error):
     pass
 
 class Main(object):
-    def __init__(self, mode, operation, output, options):
+    def __init__(self, mode, operation, output, base_path, options):
         libxml2.registerErrorHandler(xml_error_handler, None)
         self.operation = operation
         self.options = options
         self.msg = None
         self.gt = None
         self.current_mode = self.load_mode(mode)()
+        self.base_path = base_path
         # Prepare output
         if operation == 'update':
             self.out = tempfile.TemporaryFile(encoding='utf-8')
@@ -675,7 +685,7 @@ class Main(object):
             if not os.access(xmlfile, os.R_OK):
                 raise IOError("Unable to read file '%s'" % xmlfile)
             try:
-                doc = XMLDocument(xmlfile, self)
+                doc = XMLDocument(xmlfile, self.base_path, self)
             except Exception as e:
                 print("Error parsing XML file '%s': %s" % (xmlfile, str(e)), file=sys.stderr)
                 sys.exit(1)
@@ -688,7 +698,7 @@ class Main(object):
         if not os.access(xmlfile, os.R_OK):
             raise IOError("Unable to read file '%s'" % xmlfile)
         try:
-            doc = XMLDocument(xmlfile, self)
+            doc = XMLDocument(xmlfile, self.base_path, self)
         except Exception as e:
             print("Error parsing XML file '%s': %s" % (xmlfile, str(e)), file=sys.stderr)
             sys.exit(1)
@@ -717,7 +727,7 @@ class Main(object):
         if not os.access(origxml, os.R_OK):
             raise IOError("Unable to read file '%s'" % xmlfile)
         try:
-            doc = XMLDocument(xmlfile, self)
+            doc = XMLDocument(xmlfile, self.base_path, self)
         except Exception as e:
             print("Error parsing XML file '%s': %s" % (xmlfile, str(e)), file=sys.stderr)
             sys.exit(1)
@@ -725,7 +735,7 @@ class Main(object):
 
         self.msg.translationsFollow()
         try:
-            doc = XMLDocument(origxml, self)
+            doc = XMLDocument(origxml, self.base_path, self)
         except Exception as e:
             print("Error parsing XML file '%s': %s" % (origxml, str(e)), file=sys.stderr)
             sys.exit(1)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]