# HG changeset patch
# User Ivo Hinkelmann <ihi@openoffice.org>
# Date 1295028409 -3600
# Node ID 99a5cf6d10b61678bde5117e380da725488109a2
# Parent  9cf541f20652b9218d4b93449607b5ba72aa7abb
#i116350# Change sdf file format

diff --git a/l10ntools/scripts/tool/l10n.py b/l10ntools/scripts/tool/l10n.py
--- a/l10ntools/scripts/tool/l10n.py
+++ b/l10ntools/scripts/tool/l10n.py
@@ -1,34 +1,50 @@
 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()
-
-
-
diff --git a/l10ntools/scripts/tool/l10nstatus.py b/l10ntools/scripts/tool/l10nstatus.py
--- a/l10ntools/scripts/tool/l10nstatus.py
+++ b/l10ntools/scripts/tool/l10nstatus.py
@@ -94,20 +94,29 @@
             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 "  \
diff --git a/l10ntools/scripts/tool/l10nupdate.py b/l10ntools/scripts/tool/l10nupdate.py
--- a/l10ntools/scripts/tool/l10nupdate.py
+++ b/l10ntools/scripts/tool/l10nupdate.py
@@ -3,11 +3,11 @@
 # 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")
 
@@ -77,14 +77,10 @@
                 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:
@@ -98,8 +94,19 @@
                 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)+
@@ -140,13 +147,19 @@
     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)
@@ -154,7 +167,11 @@
             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()
diff --git a/l10ntools/scripts/tool/sdffile.py b/l10ntools/scripts/tool/sdffile.py
--- a/l10ntools/scripts/tool/sdffile.py
+++ b/l10ntools/scripts/tool/sdffile.py
@@ -26,7 +26,7 @@
     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():
@@ -34,9 +34,8 @@
             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)
