Module: check_mk
Branch: master
Commit: e084d75d3d95590d3dbdad0e8077c69bb68bf3b3
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e084d75d3d9559…
Author: Roland Halbig <rh(a)mathias-kettner.de>
Date: Thu Dec 15 12:59:10 2016 +0100
Autorefactor: got auto_refactor.py von rh-refactoring-valuespec
Change-Id: I8263eb9c5594dc959afc81bde4b2f8174aec18a3
Autorefactor: Added try catch block to return statement.
Change-Id: Ia710e58108614c8aa34737c4dfa140b6ec68b702
---
tests/web/auto_refactor.py | 155 +++++++++++++++++++++++++++++++--------------
1 file changed, 106 insertions(+), 49 deletions(-)
diff --git a/tests/web/auto_refactor.py b/tests/web/auto_refactor.py
index f5b0063..7660e65 100644
--- a/tests/web/auto_refactor.py
+++ b/tests/web/auto_refactor.py
@@ -4,17 +4,59 @@ from bs4 import BeautifulSoup as bs
from bs4 import NavigableString
+def append_to_html(html, indent, addendum):
+ return "%s\n%s%s" %(html, ' ' * indent, "%s" % addendum)
+
+
+def split_html(text):
+ index = 1
+ while index < len(text) and text[index - 1] != "(":
+ index += 1
+ open_braces = 1
+ while index < len(text) and open_braces > 0:
+ if text[index] == "(":
+ open_braces += 1
+ elif text[index] == ")":
+ open_braces -= 1
+ index += 1
+ while index < len(text) and text[index] != "\n":
+ index += 1
+ return text[:index], text[index:]
+
+
+def stripper(x):
+ x = re.sub(r' %\s+\(.*', '', x)
+ index = len(x) - 1
+ while index >= 0 and x[index] in ['n', ' ', ')',
'"', '\'']:
+ if x[index] == 'n' and index > 0 and x[index - 1] == '\\':
+ index -= 1
+ index -= 1
+ return x[:index+1]
+
+
+def preprocess_tag(tag_str):
+ index = 1
+ while index + 1< len(tag_str):
+ if tag_str[index - 1] == '=' and tag_str[index] not in
['\"', '\'']:
+ next_index = index + 1
+ while next_index < len(tag_str) and re.match(r'\w',
tag_str[next_index]) is not None:
+ next_index += 1
+ tag_str = tag_str[:index] + '\"' + tag_str[index:(next_index)] +
'\"' + tag_str[next_index:]
+ index = next_index + 1
+ else:
+ index += 1
+ return tag_str
+
+
def eval_tag(tag_str, next_one, next_inbetween):
+
skip_next = False
children = list(bs(tag_str, 'html5lib').body.children)
if tag_str[1] == '/':
addendum = "html.close_%s()" % re.sub("<|[/]|>",
'' , tag_str)
-# elif '%' + 's' in tag_str:
-# addendum = ''
-
elif children and not isinstance(children[0], NavigableString):
tag_name = ''
attrs = ''
@@ -33,7 +75,7 @@ def eval_tag(tag_str, next_one, next_inbetween):
attrs += "%s=\"%s\"" % (key, val)
# See if we can close the tag right away
- if next_one and next_one[1] == '/' and next_one ==
("</%s>" % tag_name):
+ if next_one and next_one == ("</%s>" % tag_name):
addendum = "html.%s(%s%s)" % (tag_name, next_inbetween, ",
" + attrs if attrs else '')
skip_next = True
elif tag_name in ['br', 'hr', 'img']:
@@ -42,45 +84,48 @@ def eval_tag(tag_str, next_one, next_inbetween):
addendum = "html.open_%s(%s)" % (tag_name, attrs)
else:
tag_name = tag_str.lstrip(' ').lstrip('<').split('
')[0].rstrip("/").rstrip(">")
- attrs = tag_str.lstrip(' ').lstrip('<').rstrip('
').rstrip('>').lstrip(tag_name)
+ attrs = tag_str.lstrip(' ').lstrip('<').rstrip('
').rstrip('>').lstrip(tag_name).lstrip(' ')
+ attrs = re.sub(r'class=', "class_=", attrs)
+ attrs = re.sub(r'id=', "id_=", attrs)
+ attrs = re.sub(r'type=', "type_=", attrs)
+ attrs = re.sub(r'for=', "for_=", attrs)
addendum = "html.open_%s(%s)" % (tag_name, attrs)
return addendum, skip_next
-def append_to_html(html, indent, addendum):
- return "%s\n%s%s" %(html, ' ' * indent, "%s" % addendum)
-
-
-def split_html(text):
- index = 1
- while index < len(text) and text[index - 1] != "(":
- index += 1
- open_braces = 1
- while index < len(text) and open_braces > 0:
- if text[index] == "(":
- open_braces += 1
- elif text[index] == ")":
- open_braces -= 1
- index += 1
- while index < len(text) and text[index] != "\n":
- index += 1
- return text[:index], text[index:]
-
+def replace_inputs(text, inputs):
+ for index, input in enumerate(inputs):
+ text = re.sub("%" + "s", '[[[%s]]]' % index, text,
1)
+ return text
+
+
+def insert_inputs(text, inputs):
+ if not inputs:
+ return text
+ replacements = []
+ text_index = 0
+ for counter, input in enumerate(inputs):
+ if "_(" in input and input.count("(") >
input.count(")"):
+ input += ")"
+ if "[[[%s]]]" % counter in text:
+ index = text.index("[[[%s]]]" % counter)
+ if text[:(index+1)].count('\"') +
text[:(index+1)].count('\'') % 2 == 0:
+ text = re.sub(r"[[]{3}%s[]]{3}" % counter, input, text, 1)
+ else:
+ text = re.sub(r"[[]{3}%s[]]{3}" % counter, '%' +
's' + '\" % ' + input + "\"", text, 1)
+ return text
-def stripper(x):
- x = re.sub(r' %\s+\(.*', '', x)
- index = len(x) - 1
- while index >= 0 and x[index] in ['n', ' ', ')',
'"', '\'']:
- if x[index] == 'n' and index > 0 and x[index - 1] == '\\':
- index -= 1
- index -= 1
- return x[:index+1]
+def test_replace_inputs():
+ text = "<tag class=\"ein %s\" id=%s>%s</tag>"
+ inputs = ["test", "id", "Hallo Welt!"]
+ print replace_inputs(text, 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
@@ -93,24 +138,24 @@ def replace_tags(html, indent = 0):
orig_html = html
# strip all comments
- html = re.sub(r'#.*', '', html)
-
- if len(html.split(" % ")) == 2:
- html, string_input = html.split(" % ")
- string_input =
string_input.lstrip("(").rstrip("$").rstrip("\n").rstrip('
').rstrip(")")
- for input in string_input.split(","):
- if ("%" + "s") in html:
- html = re.sub("%" + "s", input, html, 1)
+ inputs = []
+ if " % " in html or ' %\n' in html:
+ if " % " in html:
+ html, string_input = html.split(" % ")
+ else:
+ html, string_input = html.split(" %\n")
+ string_input = string_input.lstrip('
').lstrip("(").rstrip("$").rstrip("\n").rstrip('
').rstrip(")")
+ inputs = string_input.split(",")
+ html = replace_inputs(html, inputs) + ")"
tags = re.findall(r'<[^<]*>', html)
- if len(tags) == 0:
- return orig_html + rest
-
inbetween = re.split(r'<[^<]*>',
re.sub(r'\s*html\.write\([\'|"]?', '', html, 1))
inbetween = map(stripper, inbetween)
- html = orig_html + "\n(new)"
+ if len(tags) == 0:
+ return orig_html + rest
+ html = ''
if inbetween[0].strip(' ') not in ['', '\n']:
html = append_to_html(html, indent, "html.write(%s)" % inbetween[0])
@@ -118,24 +163,34 @@ def replace_tags(html, indent = 0):
counter = 0
skip_next = False
while counter < len(tags):
- tag_str = tags[counter]
+ tag_str = preprocess_tag(tags[counter])
next_one = tags[counter + 1] if counter + 1 < len(tags) else None
addendum, skip_next = eval_tag(tag_str, next_one, inbetween[counter + 1])
- if not skip_next and addendum.strip(' '):
- html = append_to_html(html, indent, addendum)
+ html = append_to_html(html, indent, addendum)
counter += 1
if not skip_next and inbetween[counter].strip(' ') not in ['',
'\n']:
html = append_to_html(html, indent, "html.write(%s)" %
inbetween[counter])
+ elif skip_next and addendum.strip(' '):
+ counter += 1
+
+ html = insert_inputs(html, inputs)
+ for index, input in enumerate(inputs):
+ if "[[[%s]]]" % index in html:
+ html = re.sub("[[[%s]]]" % index, input, html)
+
try:
- return html + "\n(/new)" + rest
+ 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 html + "\n(/new)" + rest
+ return orig_html + "\n(new)" + html + "\n" + rest
+
import re
html = sys.argv[1]
@@ -156,6 +211,8 @@ if html.endswith('.py'):
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)