Skip to content

Instantly share code, notes, and snippets.

@elyezer
Created March 23, 2017 20:55
Show Gist options
  • Save elyezer/6baa79ec8e0d1ec6357453e909d1363d to your computer and use it in GitHub Desktop.
Save elyezer/6baa79ec8e0d1ec6357453e909d1363d to your computer and use it in GitHub Desktop.
import os
import re
import textwrap
def edit_expected_results():
for dirpath, _, filenames in os.walk('tests/foreman'):
for filename in filenames:
path = os.path.join(dirpath, filename)
if path.endswith('.py') and '__init__.py' not in path:
print(path)
diff = 0
with open(path) as handler:
edited = handler.readlines()
diff = 0
for index, line in enumerate(tuple(edited)):
if ':assert:' in line.lower():
start = index + diff
end = start + 1
while not re.search(r'(^\n$|^\s+"""\n$)', edited[end]):
end += 1
ident = len(edited[start].split(':')[0])
new = ' '.join([l.strip() for l in edited[start:end]])
new = re.sub(
r':assert:',
':expectedresults:',
new,
flags=re.IGNORECASE
)
new = '\n'.join(textwrap.wrap(
new,
width=79,
initial_indent=ident * ' ',
subsequent_indent=(ident + 4) * ' ',
)) + '\n'
for _ in range(start, end):
edited.pop(start)
diff -= 1
edited.insert(start, new)
diff += 1
with open(path, 'w') as handler:
handler.write(''.join(edited))
if __name__ == "__main__":
edit_expected_results()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment