summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2009-11-20 14:09:32 -0500
committerDavid Malcolm <dmalcolm@redhat.com>2009-11-20 14:09:32 -0500
commit0ff8884ac72ac8cd94a8b448cbbbe716d9700c0f (patch)
treeda063e0561cc705f2fc241cc7da4ec158b52a0ed
parent237a05d5ee8ce68be6326750de6ee3e812e35e69 (diff)
download2to3c-0ff8884ac72ac8cd94a8b448cbbbe716d9700c0f.tar.gz
2to3c-0ff8884ac72ac8cd94a8b448cbbbe716d9700c0f.tar.xz
2to3c-0ff8884ac72ac8cd94a8b448cbbbe716d9700c0f.zip
Introduce "2to3c" command, working towards pluggable fixes
-rwxr-xr-x2to3c45
-rw-r--r--typeobject.py18
2 files changed, 46 insertions, 17 deletions
diff --git a/2to3c b/2to3c
new file mode 100755
index 0000000..c31d991
--- /dev/null
+++ b/2to3c
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+
+def get_fixers():
+ from typeobject import fixup_typeobject_initializers
+ return [fixup_typeobject_initializers]
+
+def fixup_content(content):
+ # Apply the various fixers, in turn:
+ for fixer in get_fixers():
+ content = fixer(content)
+ return content
+
+
+def fixup_file(filename, options):
+ from difflib import unified_diff
+
+ content = open(filename, 'r').read()
+ fixed_content = fixup_content(content)
+ if content != fixed_content:
+ for line in unified_diff(content.splitlines(),
+ fixed_content.splitlines(),
+ fromfile = filename+'.orig',
+ tofile = filename,
+ lineterm=''):
+ print line
+
+ if options.write:
+ open(filename, 'w').write(fixed_content)
+
+def main():
+ from optparse import OptionParser
+ usage = "usage: %prog [options] filenames..."
+ parser = OptionParser(usage=usage)
+ parser.add_option('-w', '--write',
+ action="store_true", dest="write", default=False,
+ help="Write back modified files")
+ (options, args) = parser.parse_args()
+ # print (options, args)
+ for filename in args:
+ fixup_file(filename, options)
+
+if __name__ == '__main__':
+ main()
+
+
diff --git a/typeobject.py b/typeobject.py
index 9967022..1710a74 100644
--- a/typeobject.py
+++ b/typeobject.py
@@ -61,22 +61,6 @@ def fixup_file(filename, options):
if options.write:
open(filename, 'w').write(fixed_content)
-
-def main():
- from optparse import OptionParser
- usage = "usage: %prog [options] filenames..."
- parser = OptionParser(usage=usage)
- parser.add_option('-w', '--write',
- action="store_true", dest="write", default=False,
- help="Write back modified files")
- (options, args) = parser.parse_args()
- # print (options, args)
- for filename in args:
- fixup_file(filename, options)
if __name__ == '__main__':
- if len(sys.argv) == 1:
- unittest.main()
- else:
- main()
-
+ unittest.main()