Skip to content

Commit

Permalink
refactored file read function into file_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
p committed May 25, 2010
1 parent 6ab96c2 commit 1936ff0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
12 changes: 3 additions & 9 deletions test/tests.py
@@ -1,23 +1,17 @@
import os.path import os.path
import aml import aml
import file_utils


self_dir = os.path.dirname(__file__) self_dir = os.path.dirname(__file__)
input_dir = os.path.join(self_dir, 'fixtures', 'input') input_dir = os.path.join(self_dir, 'fixtures', 'input')
output_dir = os.path.join(self_dir, 'fixtures', 'output') output_dir = os.path.join(self_dir, 'fixtures', 'output')


tests = ['singleline-text', 'multiline-text'] tests = ['singleline-text', 'multiline-text']


def read_file(path):
file = open(path)
try:
return file.read()
finally:
file.close()

for test in tests: 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) 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 actual_output
#print expected_output #print expected_output
assert actual_output == expected_output assert actual_output == expected_output
13 changes: 3 additions & 10 deletions tools/build-aml.py
@@ -1,14 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python


import sys, os.path, re import sys, os.path, re

import file_utils
def read(source):
f = open(source)
try:
text = f.read()
finally:
f.close()
return text


main_re_text = r'if __name__ == ([\'"])__main__\1:(\n(\s+).*)+\n' main_re_text = r'if __name__ == ([\'"])__main__\1:(\n(\s+).*)+\n'
main_re = re.compile(main_re_text) main_re = re.compile(main_re_text)
Expand Down Expand Up @@ -46,9 +39,9 @@ def strip_modules(text, modules):


merged_modules = [re.sub(r'\.py$', '', os.path.basename(path)) for path in sources[1:]] 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:]: 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)) text = move_main(strip_modules(text, merged_modules))


f = open(target, 'w') f = open(target, 'w')
Expand Down
7 changes: 7 additions & 0 deletions 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.