tag: make compatible with py2 and py3
authorDan White <dan@whiteaudio.com>
Tue, 30 Aug 2016 03:06:45 +0000 (22:06 -0500)
committerDan White <dan@whiteaudio.com>
Tue, 30 Aug 2016 03:06:45 +0000 (22:06 -0500)
tag

diff --git a/tag b/tag
index c4e7a801d18fda3974eb8b4ed4c55280c0cd5a69..7d6215c041a0037cd3a7542c1d445f0af641bd5c 100755 (executable)
--- a/tag
+++ b/tag
@@ -1,20 +1,19 @@
 #!/usr/bin/env python
 
+from __future__ import print_function
+
 # Dan White <dan@whiteaudio.com>
 
 """Recurse from current or given directory and display all folders with
 tags."""
 
 import datetime
+import io
 import optparse
 import os
-import sys
 import re
+import sys
 
-try:
-    import cStringIO as StringIO
-except ImportError:
-    import StringIO
 
 try:
     import optcomplete
@@ -73,12 +72,12 @@ def listTags(baseDir):
 
 
 def findTag(tag, baseDir):
-    s = StringIO.StringIO()
+    s = io.StringIO()
     for (path, dirs, files) in os.walk(baseDir):
         dirs[:] = [d for d in dirs if not RE_DO_NOT_FOLLOW_DIR.match(d)]
         tags = [t.replace(TAG_PREFIX, '') for t in files if t.startswith(TAG_PREFIX)]
         if tag in tags:
-            print>>s, path
+            print(path, file=s)
     return s.getvalue().rstrip()
 
 
@@ -86,7 +85,7 @@ def addTag(tag, baseDir):
     fname = baseDir + '/' + TAG_PREFIX + tag
     #if os.path.exists(fname):
     fd = open(fname, 'a')
-    print >>fd, datetime.datetime.now().strftime('%F %T')
+    print(datetime.datetime.now().strftime('%F %T'), file=fd)
     #try:
         #open(baseDir + '/' + TAG_PREFIX + tag, 'r')
         #print "ee's already got one (%s)" % tag
@@ -99,7 +98,7 @@ def deleteTag(tag, baseDir):
     try:
         os.remove(baseDir + '/' + TAG_PREFIX + tag)
     except OSError:
-        print 'tag already not present (%s)' % tag
+        print('tag already not present (%s)' % tag)
 
 
 
@@ -138,9 +137,9 @@ def main():
                             alltags[t].append(path)
                         else:
                             alltags[t] = [path]
-                    print '%s:' % herePath(path, walkDir)
-                    print '\n'.join(tags)
-                    print
+                    print('%s:' % herePath(path, walkDir))
+                    print('\n'.join(tags))
+                    print()
             #usedtags = alltags.keys()
             #usedtags.sort()
             #print
@@ -149,13 +148,13 @@ def main():
                 #print '%s:' % t
                 #print '\n'.join(alltags[t])
         else:
-            print '\n'.join(listTags(walkDir))
+            print('\n'.join(listTags(walkDir)))
     elif opt.search:
         for t in opt.search.split(',') + taglist:
-            print findTag(t, walkDir)
+            print(findTag(t, walkDir))
     elif opt.add:
         for t in opt.add.split(',') + taglist:
-            print 'adding', t
+            print('adding', t)
             addTag(t, walkDir)
     elif opt.delete:
         for t in opt.delete.split(','):