]> Frank Brehm's Git Trees - my-stuff/python.git/commitdiff
Added klammerkomma.py
authorFrank Brehm <frank@brehm-online.com>
Mon, 17 Jun 2013 20:43:36 +0000 (22:43 +0200)
committerFrank Brehm <frank@brehm-online.com>
Mon, 17 Jun 2013 20:43:36 +0000 (22:43 +0200)
klammerkomma.py [new file with mode: 0755]

diff --git a/klammerkomma.py b/klammerkomma.py
new file mode 100755 (executable)
index 0000000..ec735ad
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import tempfile
+import re
+import shutil
+
+re_komma = re.compile(r',(\s*[\)\]\}])')
+
+i = 0
+for fname in sys.argv:
+    if i == 0:
+        i = 1
+        continue
+    if not os.path.exists(fname):
+        sys.stderr.write("Datei %r existiert nicht.\n" % (fname))
+        continue
+    (ofd, ofname) = tempfile.mkstemp()
+    print "Mangling %r -> %r" % (fname, ofname)
+    try:
+        try:
+            ifile = open(fname)
+            lines = ifile.read()
+            nlines = re_komma.sub(r'\1', lines)
+            os.write(ofd, nlines)
+        finally:
+            os.close(ofd)
+            ifile.close()
+        print "Moving %r -> %r" % (ofname, fname)
+        shutil.move(ofname, fname)
+    finally:
+        if os.path.exists(ofname):
+            print "Removing %r ..." % (ofname)
+            os.remove(ofname)
+
+# vim: ts=4 et