Module: check_mk
Branch: master
Commit: c8010feea54cadf0a5947bd186ca83b115bd9dd8
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c8010feea54cad…
Author: Roland Halbig <rh(a)mathias-kettner.de>
Date: Thu Dec 15 12:59:10 2016 +0100
Refactor: got auto_refactor von rh-refactoring-valuespec/
Change-Id: I8263eb9c5594dc959afc81bde4b2f8174aec18a3
---
tests/web/auto_refactor.py | 57 +++++++++++++++++++---------------------------
1 file changed, 23 insertions(+), 34 deletions(-)
diff --git a/tests/web/auto_refactor.py b/tests/web/auto_refactor.py
index 895ff96..e72a547 100644
--- a/tests/web/auto_refactor.py
+++ b/tests/web/auto_refactor.py
@@ -125,13 +125,13 @@ def test_replace_inputs():
# this function does a big chunk of the refactoring for me
def replace_tags(html, indent = 0):
+
# I want to refactor only lines with html.write
if not html.lstrip(' ').startswith('html.write('):
return html
# unbalanced paranthesis indicates sth that goes across line border
- no_comments = '\n'.join(re.sub('#.*', '', line) for line in
html.split('\n'))
- if no_comments.count('(') != no_comments.count(')'):
+ if html.count('(') != html.count(')'):
return html
html, rest = split_html(html)
@@ -180,41 +180,30 @@ def replace_tags(html, indent = 0):
if "[[[%s]]]" % index in html:
html = re.sub("[[[%s]]]" % index, input, html)
-
- try:
- return orig_html + "\n(new)" + html + "\n" + rest
- except:
- print "orig_html:\n", orig_html
- print "__________________________________________"
- print "html:\n", html
- print "__________________________________________"
- print "rest:\n", rest
- return orig_html + "\n(new)" + html + "\n" + rest
-
+ return orig_html + "\n(new)" + html + "\n" + rest
-if __name__ == "__main__":
-
- html = sys.argv[1]
- if html.endswith('.py'):
- whole_text = ''
- with open(html, 'r') as rfile:
- whole_text = "".join(line for line in rfile)
- parts = whole_text.split("html.write(")
- with open("refactored_file.py", "w") as wfile:
- part = parts.pop(0)
- wfile.write(part)
+import re
+html = sys.argv[1]
+if html.endswith('.py'):
+ whole_text = ''
+ with open(html, 'r') as rfile:
+ whole_text = "".join(line for line in rfile)
+ parts = whole_text.split("html.write(")
+ with open("refactored_file.py", "w") as wfile:
+ part = parts.pop(0)
+ wfile.write(part)
+ indent = 0
+ while indent < len(part) and part[len(part) - 1 - indent] == ' ':
+ indent += 1
+ for part in parts:
+ part = "html.write(" + part
+ wfile.write(replace_tags(part, indent))
indent = 0
while indent < len(part) and part[len(part) - 1 - indent] == ' ':
indent += 1
- for part in parts:
- part = "html.write(" + part
- wfile.write(replace_tags(part, indent))
- indent = 0
- while indent < len(part) and part[len(part) - 1 - indent] == '
':
- indent += 1
- elif html == "test":
- test_replace_inputs()
- else:
- print replace_tags(html)
+elif html == "test":
+ test_replace_inputs()
+else:
+ print replace_tags(html)