[Assorted-commits] SF.net SVN: assorted:[1595] shell-tools/trunk/src/map-syms.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2010-04-01 18:17:42
|
Revision: 1595 http://assorted.svn.sourceforge.net/assorted/?rev=1595&view=rev Author: yangzhang Date: 2010-04-01 18:17:35 +0000 (Thu, 01 Apr 2010) Log Message: ----------- use single space-delimited file for mappings and a file for the list of files to be processed; reindented Modified Paths: -------------- shell-tools/trunk/src/map-syms.py Modified: shell-tools/trunk/src/map-syms.py =================================================================== --- shell-tools/trunk/src/map-syms.py 2010-04-01 18:16:18 UTC (rev 1594) +++ shell-tools/trunk/src/map-syms.py 2010-04-01 18:17:35 UTC (rev 1595) @@ -1,34 +1,27 @@ -#!/usr/bin/env python -# vim:et:sw=4 - -import common, sys, re - -# TODO add option to switch between regex & literal -# TODO add option to specify the mappings - -def main( argv = sys.argv ): - patternFile = file('pattern') - replaceFile = file('replace') - zipped = zip(patternFile.readlines(), replaceFile.readlines()) - - filePaths = sys.argv[2:] - - for filePath in filePaths: - file = file(filePath, 'r+') - f = file.read() - for pattern, replace in zipped: - pattern = pattern.rstrip() - replace = replace.rstrip() - if pattern == replace: - continue - pattern = r'\b' + re.escape(pattern) + r'\b' -# pattern = r'\b' + pattern + r'\b' - f = re.sub(pattern, - replace, - f) - file.seek(0) - file.write(f) - file.truncate(); - file.close() - -common.run_main() +#!/usr/bin/env python + +import re, sys + +def main(argv): + subs, paths = argv[1], argv[2:] + with open(subs) as f: + subs = [tuple(line.split()) for line in f.read().strip().split('\n')] + for path in paths: + with open(path, 'r+') as f: + cnt = f.read() + for pat, rep in subs: + pat = pat.rstrip() + rep = rep.rstrip() + if pat == rep: + continue + pat = r'\b' + re.escape(pat) + r'\b' + cnt = re.sub(pat, rep, cnt) + f.seek(0) + f.write(cnt) + f.truncate() + f.close() + +if __name__ == '__main__': + sys.exit(main(sys.argv)) + +# vim:et:sw=2 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |