'''Rename a tag in all tagfiles.''' import sys import os from lib.tagfiles import nicesplit def edit_tagfile(tagfilepath, old, new): '''Update a single tagfile.''' infile = open(tagfilepath, 'rU') newdata = "" for line in infile: if not line.strip(): continue parts = line.split(':', 1) tags = nicesplit(parts[0]) if old in tags: tags[tags.index(old)] = new parts[0] = ' '.join(tags) newdata += ':'.join(parts) else: newdata += line infile.close() open(tagfilepath, 'w').write(newdata) def edit_recursively(basepath, tagfilename, old, new): '''Update all tagfiles recursively''' def callback(arg, dirname, fnames): '''os.walk callback function to read tagfiles.''' if tagfilename in fnames: tagfilepath = os.path.join(dirname, tagfilename) edit_tagfile(tagfilepath, old, new) os.path.walk(basepath, callback, None) if __name__ == '__main__': if len(sys.argv) != 3: sys.stderr.write("Usage: " + sys.argv[0] + " oldtag newtag\n") edit_recursively(os.getcwd(), 'Merkinnat.txt', sys.argv[1], sys.argv[2])