Skip to content
Browse files

refactored file read function into file_utils

  • Loading branch information...
1 parent 6ab96c2 commit 1936ff0e38330b49dbbef4fc30dcab9cfbadf431 @p committed May 25, 2010
Showing with 13 additions and 19 deletions.
  1. +3 −9 test/tests.py
  2. +3 −10 tools/build-aml.py
  3. +7 −0 tools/file_utils.py
View
12 test/tests.py
@@ -1,23 +1,17 @@
import os.path
import aml
+import file_utils
self_dir = os.path.dirname(__file__)
input_dir = os.path.join(self_dir, 'fixtures', 'input')
output_dir = os.path.join(self_dir, 'fixtures', 'output')
tests = ['singleline-text', 'multiline-text']
-def read_file(path):
- file = open(path)
- try:
- return file.read()
- finally:
- file.close()
-
for test in tests:
- input = read_file(os.path.join(input_dir, test + '.at'))
+ input = file_utils.read_file(os.path.join(input_dir, test + '.at'))
actual_output = aml.convert_text(input)
- expected_output = read_file(os.path.join(output_dir, test + '.jt'))
+ expected_output = file_utils.read_file(os.path.join(output_dir, test + '.jt'))
#print actual_output
#print expected_output
assert actual_output == expected_output
View
13 tools/build-aml.py
@@ -1,14 +1,7 @@
#!/usr/bin/env python
import sys, os.path, re
-
-def read(source):
- f = open(source)
- try:
- text = f.read()
- finally:
- f.close()
- return text
+import file_utils
main_re_text = r'if __name__ == ([\'"])__main__\1:(\n(\s+).*)+\n'
main_re = re.compile(main_re_text)
@@ -46,9 +39,9 @@ def strip_modules(text, modules):
merged_modules = [re.sub(r'\.py$', '', os.path.basename(path)) for path in sources[1:]]
-text = read(sources[0])
+text = file_utils.read_file(sources[0])
for source in sources[1:]:
- text += strip_main(read(source))
+ text += strip_main(file_utils.read_file(source))
text = move_main(strip_modules(text, merged_modules))
f = open(target, 'w')
View
7 tools/file_utils.py
@@ -0,0 +1,7 @@
+def read_file(path):
+ file = open(path)
+ try:
+ contents = file.read()
+ finally:
+ file.close()
+ return contents

0 comments on commit 1936ff0

Please sign in to comment.
Something went wrong with that request. Please try again.