Commit 48bf9049 by Alexander Kryklia

converted regexp to lxml

parent ca4b625e
...@@ -6,6 +6,7 @@ understand functional dependencies. ...@@ -6,6 +6,7 @@ understand functional dependencies.
import json import json
import logging import logging
from lxml import etree from lxml import etree
from lxml import html
import xmltodict import xmltodict
import re import re
...@@ -82,104 +83,49 @@ class GraphicalSliderToolModule(XModule): ...@@ -82,104 +83,49 @@ class GraphicalSliderToolModule(XModule):
html_string with control tags replaced by proper divs html_string with control tags replaced by proper divs
(<slider var="a"/> -> <div class="....slider" > </div>) (<slider var="a"/> -> <div class="....slider" > </div>)
""" """
#substitute plot
xml = html.fromstring(html_string)
#substitute plot, if presented
plot_div = '<div class="{element_class}_plot" id="{element_id}_plot" \ plot_div = '<div class="{element_class}_plot" id="{element_id}_plot" \
style="{style}"></div>' style="{style}"></div>'
# extract css style from plot plot_el = xml.xpath('//plot')
plot_def = re.search(r'<plot[^<>]*/>', html_string) if plot_el:
if plot_def: plot_el = plot_el[0]
plot_def = plot_def.group() plot_el.getparent().replace(plot_el, html.fromstring(
style = re.search(r'(?=.*style\=[\"\'](.*)[\"\'])', plot_def, plot_div.format(element_class=self.html_class,
flags=re.UNICODE | re.DOTALL) element_id=self.html_id,
if style: style=plot_el.get('style', ""))))
style = style.groups()[0]
else: # no style parameter
style = ''
replacement = plot_div.format(element_class=self.html_class,
element_id=self.html_id,
style=style)
html_string = re.sub(r'<plot[^<>]*/>', replacement, html_string,
flags=re.UNICODE)
# get variables
if json.loads(self.configuration_json)['root'].get('parameters'):
variables = json.loads(self.configuration_json)['root']['parameters']['param']
if type(variables) == dict:
variables = [variables]
variables = [x['@var'] for x in variables]
else:
return html_string
#substitute sliders #substitute sliders
slider_div = '<div class="{element_class}_slider" \ slider_div = '<div class="{element_class}_slider" \
id="{element_id}_slider_{var}" \ id="{element_id}_slider_{var}" \
data-var="{var}" style="{style}">\ data-var="{var}" style="{style}">\
</div>' </div>'
for var in variables: slider_els = xml.xpath('//slider')
# find <slider var='var' ... > for slider_el in slider_els:
instances = re.findall(r'<slider\s+(?=[^<>]*var\=[\"\']' + var + '[\"\'])' \ slider_el.getparent().replace(slider_el, html.fromstring(
+ r'[^<>]*/>', html_string, flags=re.UNICODE | re.DOTALL) slider_div.format(element_class=self.html_class,
if instances: # if presented, only one slider per var
slider_def = instances[0] # get <slider var='var' ... > string
# extract var for proper style extraction further
var_substring = re.search(r'(var\=[\"\']' + var + r'[\"\'])',
slider_def).group()
slider_def = slider_def.replace(var_substring, '')
# get style
style = re.search(r'(?=[^<>]*style\=[\"\'](.*)[\"\'])', slider_def,
flags=re.UNICODE | re.DOTALL)
if style:
style = style.groups()[0]
else: # no style parameter
style = ''
# substitute parameters to slider div
replacement = slider_div.format(element_class=self.html_class,
element_id=self.html_id, element_id=self.html_id,
var=var, style=style) var=slider_el.get('var', ""),
# subsitute <slider var='var' ... > in html_srting to proper style=slider_el.get('style', ""))))
# html div element
html_string = re.sub(r'<slider\s+(?=[^<>]*var\=[\"\'](' + \ # substitute inputs aka textboxes
var + ')[\"\'])' + r'[^<>]*/>', input_div = '<input class="{element_class}_input" \
replacement, html_string, flags=re.UNICODE | re.DOTALL)
# substitute inputs if we have them
input_el = '<input class="{element_class}_input" \
id="{element_id}_input_{var}_{input_index}" \ id="{element_id}_input_{var}_{input_index}" \
data-var="{var}" style="{style}" \ data-var="{var}" style="{style}" \
data-el_readonly="{readonly}"/>' data-el_readonly="{readonly}"/>'
input_els = xml.xpath('//textbox')
for var in variables: for input_index, input_el in enumerate(input_els):
input_index = 0 # make multiple inputs for same variable have input_el.getparent().replace(input_el, html.fromstring(
# different id input_div.format(element_class=self.html_class,
instances = re.findall(r'<textbox\s+(?=[^<>]*var\=[\"\']' + var + '[\"\'])' \ element_id=self.html_id,
+ r'[^<>]*/>', html_string, flags=re.UNICODE | re.DOTALL) var=input_el.get('var', ""),
for input_def in instances: # for multiple inputs per var readonly=input_el.get('readonly', ''),
input_index += 1 style=input_el.get('style', ""),
# extract var and readonly before style! input_index=input_index)))
var_substring = re.search(r'(var\=[\"\']' + var + r'[\"\'])',
input_def).group() return html.tostring(xml)
input_def = input_def.replace(var_substring, '')
readonly = re.search(r'(?=[^<>]*(readonly\=[\"\'](\w+)[\"\']))',
input_def, flags=re.UNICODE | re.DOTALL)
if readonly:
input_def = input_def.replace(readonly.groups()[0], '')
readonly = readonly.groups()[1]
else:
readonly = ''
style = re.search(r'(?=[^<>]*style\=[\"\'](.*)[\"\'])', input_def,
flags=re.UNICODE | re.DOTALL)
if style:
style = style.groups()[0]
else:
style = ''
replacement = input_el.format(element_class=self.html_class,
element_id=self.html_id,
var=var, readonly=readonly, style=style,
input_index=input_index)
html_string = re.sub(r'<textbox\s+(?=[^<>]*var\=[\"\'](' + \
var + ')[\"\'])' + r'[^<>]*/>',
replacement, html_string, count=1, flags=re.UNICODE | re.DOTALL)
return html_string
def build_configuration_json(self): def build_configuration_json(self):
"""Creates json element from xml element (with aim to transfer later """Creates json element from xml element (with aim to transfer later
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment