testing some stuff

This commit is contained in:
brent s. 2018-05-15 08:41:58 -04:00
parent ed7ccdeeaf
commit 1df5bd87e0

View File

@ -37,9 +37,7 @@ class XPathFmt(string.Formatter):
def get_field(self, field_name, args, kwargs): def get_field(self, field_name, args, kwargs):
vals = self.get_value(field_name, args, kwargs), field_name vals = self.get_value(field_name, args, kwargs), field_name
if not vals[0]: if not vals[0]:
print(vals)
vals = ('{{{0}}}'.format(vals[1]), vals[1]) vals = ('{{{0}}}'.format(vals[1]), vals[1])
print(vals)
return(vals) return(vals)


class detect(object): class detect(object):
@ -426,9 +424,11 @@ class xml_supplicant(object):
def __init__(self, cfg, profile = None, max_recurse = 5): def __init__(self, cfg, profile = None, max_recurse = 5):
raw = self._detect_cfg(cfg) raw = self._detect_cfg(cfg)
xmlroot = lxml.etree.fromstring(raw) xmlroot = lxml.etree.fromstring(raw)
self.root = lxml.etree.ElementTree(xmlroot) self.btags = {'xpath': {},
'regex': {}}
self.max_recurse = max_recurse self.max_recurse = max_recurse
self.ptrn = re.compile('(?<=(?<!\{)\{)[^{}]*(?=\}(?!\}))') self.ptrn = re.compile('(?<=(?<!\{)\{)[^{}]*(?=\}(?!\}))')
self.root = lxml.etree.ElementTree(xmlroot)
self.substitutions = {} self.substitutions = {}
if not profile: if not profile:
self.profile = xmlroot.xpath('/bdisk/profile[1]')[0] self.profile = xmlroot.xpath('/bdisk/profile[1]')[0]
@ -462,7 +462,6 @@ class xml_supplicant(object):
else: else:
raise TypeError('Could not determine the object type.') raise TypeError('Could not determine the object type.')
return(cfg) return(cfg)


def get_path(self, element): def get_path(self, element):
path = element path = element
@ -477,7 +476,7 @@ class xml_supplicant(object):


def substitute(self, element, recurse_count = 0): def substitute(self, element, recurse_count = 0):
if recurse_count >= self.max_recurse: if recurse_count >= self.max_recurse:
return(None) return(element)
if isinstance(element, lxml.etree._Element): if isinstance(element, lxml.etree._Element):
if isinstance(element, lxml.etree._Comment): if isinstance(element, lxml.etree._Comment):
return(element) return(element)
@ -485,16 +484,23 @@ class xml_supplicant(object):
_dictmap = self.xpath_to_dict(element.text) _dictmap = self.xpath_to_dict(element.text)
while _dictmap: while _dictmap:
for elem in _dictmap: for elem in _dictmap:
if _dictmap is None: # if _dictmap is None:
continue # continue
# I still for the life of me cannot figure out why this # # I still for the life of me cannot figure out why this
# is not caught by the above. But it isn't. # # is not caught by the above. But it isn't.
if elem not in _dictmap: # if elem not in _dictmap:
continue # continue
if isinstance(_dictmap[elem], str): if isinstance(_dictmap[elem], str):
try: try:
newpath = element.xpath(_dictmap[elem]) print('bleh')
print(_dictmap[elem])
try:
print(self.get_path(element))
except:
pass
newpath = element.xpath(_dictmap[elem])[0]
except (AttributeError, IndexError, TypeError): except (AttributeError, IndexError, TypeError):
print('blugh')
newpath = element newpath = element
try: try:
self.substitutions[elem] = self.substitute( self.substitutions[elem] = self.substitute(
@ -532,6 +538,7 @@ class xml_supplicant(object):
d = {} d = {}
try: try:
_, xpath_expr = item.split('%', 1) _, xpath_expr = item.split('%', 1)
print(_)
if not _ == 'xpath': if not _ == 'xpath':
continue continue
if item not in self.substitutions: if item not in self.substitutions: