Anonymous avatar Anonymous committed 99a5cf6

#i116350# Change sdf file format

Comments (0)

Files changed (4)

l10ntools/scripts/tool/l10n.py

 import sys, l10nupdate, l10nverify, l10nstatus, l10nimport
 
+class cmd():
+    def __init__(self, func, args, help_text, example_text):
+        self.func = func
+        self.args = args
+        self.help_text = help_text
+        self.example_text = example_text
+
 def usage():
-    print "help!"
+    print "Usage: l10n cmd [opts]"
+    print "Cmds:"
+    for cmd in valid_options:
+        print cmd + "\t"+ valid_options[cmd].help_text
+    sys.exit(13)
 
-if not len(sys.argv) == 1:
+no_options       = lambda opt: len(opt) <= 2 
+has_options      = lambda opt: len(opt) <= 2 or (len(opt) == 3 and opt[2] != "")
+required_options = lambda opt: len(opt) == 3 and opt[2] != None and opt[2] != ""
+
+valid_options = { "update":     cmd(l10nupdate.run, no_options,        "Updates to current l10n",                          "Example: \nl10n update"),
+                  "cleanup":    cmd(l10nupdate.cleanup, no_options,    "Updates to current l10n and removes obsolete l10n","Example: \nl10n cleanup"),
+                  "verify":     cmd(l10nverify.run, no_options,        "Checks the integrity of the l10n",                 "Example: \nl10n verify"),
+                  "status":     cmd(l10nstatus.run, has_options,       "Shows the l10n translation status.",               "Example: \nl10n status\nl10n status de\nl10n status de,fr,pt-BR"),
+                  "new":        cmd(l10nstatus.new, has_options,       "Print all new strings",                            "Example: \nl10n new\nl10n new de\nl10n new de,fr,pt-BR"),
+                  "changed":    cmd(l10nstatus.changed, has_options,   "Print all changed strings",                        "Example: \nl10n changed\nl10n changed de\nl10n changed de,fr,pt-BR"),
+                  "translated": cmd(l10nstatus.translated, has_options,"Print all translated strings",                     "Example: \nl10n translated\nl10n translated de\nl10n translated de,fr,pt-BR"),
+                  "export":     cmd(l10nstatus.export, has_options,    "Print a full export as sdf",                       "Example: \nl10n export\nl10n export de\nl10n export de,fr,pt-BR"),
+                  "import":     cmd(l10nimport.run, required_options,  "Import a sdf file",                                "Example: \nl10n import l10n_de.sdf"),
+                  "help":       cmd(usage, has_options,                "help",                                             "Example: \nl10n help")}
+
+
+if len(sys.argv) >= 1:
     cmd = sys.argv[1]
+    if cmd in valid_options:
+        if cmd == "help":
+            if len(sys.argv) <= 2:
+                usage()
+            if sys.argv[2] in valid_options:
+                print valid_options[sys.argv[2]].help_text + "\n" + valid_options[sys.argv[2]].example_text
+                sys.exit(13)
+        if valid_options[cmd].args(sys.argv):
+            valid_options[cmd].func(sys.argv)
+            sys.exit(0)
+        else:
+            print valid_options[cmd].example_text
+            sys.exit(13)
 else:
     usage()
-    sys.exit(13)
 
-if(cmd == "update"):
-    l10nupdate.run()
-if(cmd == "cleanup"):
-    l10nupdate.cleanup()
-elif(cmd == "verify"):
-    l10nverify.run()
-elif(cmd == "status"):
-    l10nstatus.run(sys.argv)
-elif(cmd == "new"):
-    l10nstatus.new(sys.argv)
-elif(cmd == "changed"):
-    l10nstatus.changed(sys.argv)
-elif(cmd == "translated"):
-    l10nstatus.translated(sys.argv)
-elif(cmd == "export"):
-    l10nstatus.export(sys.argv)
-elif(cmd == "import"):
-    l10nimport.run(sys.argv)
-elif(cmd == "help"):
-    help()
-
-
-

l10ntools/scripts/tool/l10nstatus.py

             for x in status.translated[lang].values():
                 tmplist.append(self.get_l10n_data(x))
             for x in sorted(tmplist, key=lambda a: str(a)):
-                print x
+                try:
+                    print x
+                except IOError:         # if you use l10n status | head -2
+                    sys.exit(0)
 
         if cmd == "new" or cmd == "export":
             for x in sorted(status.new[lang].values(), key=lambda a: str(a)):
                 sdf = sdf2.convert_sdf2idententity_to_sdf(self.ident[x.keyid])
                 sdf.langid = lang
-                print str(sdf)
-        
+                try:
+                    print str(sdf)
+                except IOError:   # if you use l10n status | head -2
+                    sys.exit(0)
+
         if cmd == "changed" or cmd == "export":
             tmplist = []
             for x in status.changed[lang].values():
                 tmplist.append(self.get_l10n_data(x))
             for x in sorted(tmplist, key=lambda a: str(a)):
-                print x
+                try:
+                    print x
+                except IOError: # if you use l10n status | head -2
+                    sys.exit(0)
 
         if cmd == "status":
             print "%(lang)7s" % { "lang": lang} +":"+" |%(num)7d/%(per)3g" % {"num": len(status.new[lang].values()), "per": len(status.new[lang].values()) *100 / len(self.lines)} + "% new "  \

l10ntools/scripts/tool/l10nupdate.py

 # run localize
 # add all new strings to sourcedb
 
-def run():
+def run(opts):
     a = l10nupdate()
     a.update_ident_file("update")
 
-def cleanup():
+def cleanup(opts):
     a = l10nupdate()
     a.update_ident_file("cleanup")
 
                 obsolete_dict[str(line)] = line
                 obsolete_cnt += 1 
             elif line.get_l10n_status() == "" and cmd == "cleanup":
+                obsolete_dict[str(line)] = line
+                obsolete_cnt += 1 
                 cleanup_list.append(line)
         
-        if cmd == "cleanup":
-            print "Removeing obsolete strings from sdf2 ident file"
-            for line in cleanup_list:
-                del ident.ident[line.get_identifier()]
-            self.clean_sdf2_data(cleanup_list)
-            
         for key,line in new_dict.iteritems():
             candidate = self.detect_moved(obsolete_dict, line)
             if not candidate == None:
                 new_cnt -= 1
                 line.keyid = candidate.keyid
                 ident.ident[candidate.get_identifier()] = line
-                del ident.ident[line.get_identifier()]
- 
+                if line.get_identifier() in ident.ident:
+                    del ident.ident[line.get_identifier()]
+                if str(candidate) in obsolete_dict:
+                    del obsolete_dict[str(candidate)]
+
+        if cmd == "cleanup":
+            print "Removeing obsolete strings from sdf2 ident file"
+            for line in cleanup_list:
+                if line.get_identifier() in ident.ident:
+                    del ident.ident[line.get_identifier()]
+            self.clean_sdf2_data(cleanup_list)
+            obsolete_cnt = 0
+
         ident.write(self.default_ident_location)
         #print "Update:"
         #print "new strings: "+str(new_cnt)+"moved strings: "+str(moved_cnt)+"changed strings: "+str(changed_cnt)+
     def clean_sdf2_data(self, cleanup_list):
         print "Removeing obsolete strings from sdf2 data files"
         default_localize_location =  os.environ["SOURCE_ROOT"]+"/"+os.environ["WORK_STAMP"]+"/ooo/l10n/source/*/localize.sdf2"
-        fh_list = []
+        file_list = []
         for file in glob.glob(default_localize_location):
-            fh_list.append(open(file,"r+"))
+            file_list.append(file)
 
-        for fh in fh_list:
+        for file in file_list:
             sdf_dict = dict()
-            lines = [line.strip("\n") for line in fh.readlines()]
+            try:
+                fh = open(file, "r")
+                lines = [line.strip("\n") for line in fh.readlines()]
+            except IOError:
+                print "Can not read file "+file
+                sys.exit(13)
+            fh.close()
             for line in lines:
                 sd = sdf2.Sdf2DataEntity()
                 sd.set_properties(line)
             for item in cleanup_list:
                 if item.keyid in sdf_dict:
                     del sdf_dict[item.keyid]
-            fh.truncate(0)
-            for val in sorted(sdf_dict.values(), key=lambda a: str(a)):
-                fh.write(str(val)+"\n")
+            try:
+                fh = open(file, "w")
+                for val in sorted(sdf_dict.values(), key=lambda a: str(a)):
+                    fh.write(str(val)+"\n")
+            except IOError:
+                print "Can not write to file "+file
+                sys.exit(13)
             fh.close()

l10ntools/scripts/tool/sdffile.py

     def write(self, file):
         try:
             fd, tmpfile = tempfile.mkstemp()
-	    fh = os.fdopen(fd, "w")
+            fh = os.fdopen(fd, "w")
             mylist = list()
             self.rehash()
             for line in self.ident.values():
             mylist.sort(key=str.lower)
             for line in mylist:
                 fh.write(line+"\n")
-		#print line+"\n"
- 	    fh.close()
-	    shutil.move(tmpfile, file)
+            fh.close()
+            shutil.move(tmpfile, file)
         except IOError:
             print "Can not write to file "+file
             sys.exit(-1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.