def parse_xml(output, tmpl): if not os.path.exists(tmpl): raise AnsibleError('unable to locate parse_cli template: %s' % tmpl) if not isinstance(output, string_types): raise AnsibleError( 'parse_xml works on string input, but given input of : %s' % type(output)) root = fromstring(output) try: template = Template() except ImportError as exc: raise AnsibleError(str(exc)) spec = yaml.safe_load(open(tmpl).read()) obj = {} for name, attrs in iteritems(spec['keys']): value = attrs['value'] try: variables = spec.get('vars', {}) value = template(value, variables) except: pass if 'items' in attrs: obj[name] = _extract_param(template, root, attrs, value) else: obj[name] = value return obj
def filter_parse_columns(val, column_def_file): with open(column_def_file, 'r') as fd: column_def = yaml.safe_load(fd) match_start_block = (re.compile(column_def['start_block']) if 'start_block' in column_def else None) match_start_table = (re.compile(column_def['start_table']) if 'start_table' in column_def else None) match_end_table = (re.compile(column_def['end_table']) if 'end_table' in column_def else None) in_block = match_start_block is None in_table = match_start_table is None lines = val.splitlines() rows = [] template = Template() for line in lines: if not in_block: if match_start_block and match_start_block.match(line): in_block = True continue if not in_table: if match_start_table and match_start_table.match(line): in_table = True continue if match_end_table and match_end_table.match(line): break row = {} for coldef in column_def['columns']: if 'start' in coldef and 'end' in coldef: val1 = line[coldef['start']:coldef['end']] else: val1 = None val2 = template(coldef.get('value', DEFAULT_TEMPLATE), { 'value': val1, 'obj': row }) # template() returns None if the result of template # expansion is empty, which will break the subsequent # re.match. if val2 is None: val2 = '' if 'check' in coldef: if not re.match(coldef['check'], val2): raise ValueError(val2) row[coldef['name']] = val2 rows.append(row) return rows
def parse_cli(output, tmpl): if not isinstance(output, string_types): raise AnsibleError( "parse_cli input should be a string, but was given a input of %s" % (type(output))) if not os.path.exists(tmpl): raise AnsibleError('unable to locate parse_cli template: %s' % tmpl) try: template = Template() except ImportError as exc: raise AnsibleError(to_native(exc)) with open(tmpl) as tmpl_fh: tmpl_content = tmpl_fh.read() spec = yaml.safe_load(tmpl_content) obj = {} for name, attrs in iteritems(spec['keys']): value = attrs['value'] try: variables = spec.get('vars', {}) value = template(value, variables) except Exception: pass if 'start_block' in attrs and 'end_block' in attrs: start_block = re.compile(attrs['start_block']) end_block = re.compile(attrs['end_block']) blocks = list() lines = None block_started = False for line in output.split('\n'): match_start = start_block.match(line) match_end = end_block.match(line) if match_start: lines = list() lines.append(line) block_started = True elif match_end: if lines: lines.append(line) blocks.append('\n'.join(lines)) block_started = False elif block_started: if lines: lines.append(line) regex_items = [re.compile(r) for r in attrs['items']] objects = list() for block in blocks: if isinstance(value, Mapping) and 'key' not in value: items = list() for regex in regex_items: match = regex.search(block) if match: item_values = match.groupdict() item_values['match'] = list(match.groups()) items.append(item_values) else: items.append(None) obj = {} for k, v in iteritems(value): try: obj[k] = template(v, {'item': items}, fail_on_undefined=False) except Exception: obj[k] = None objects.append(obj) elif isinstance(value, Mapping): items = list() for regex in regex_items: match = regex.search(block) if match: item_values = match.groupdict() item_values['match'] = list(match.groups()) items.append(item_values) else: items.append(None) key = template(value['key'], {'item': items}) values = dict([(k, template(v, {'item': items})) for k, v in iteritems(value['values'])]) objects.append({key: values}) return objects elif 'items' in attrs: regexp = re.compile(attrs['items']) when = attrs.get('when') conditional = "{%% if %s %%}True{%% else %%}False{%% endif %%}" % when if isinstance(value, Mapping) and 'key' not in value: values = list() for item in re_matchall(regexp, output): entry = {} for item_key, item_value in iteritems(value): entry[item_key] = template(item_value, {'item': item}) if when: if template(conditional, {'item': entry}): values.append(entry) else: values.append(entry) obj[name] = values elif isinstance(value, Mapping): values = dict() for item in re_matchall(regexp, output): entry = {} for item_key, item_value in iteritems(value['values']): entry[item_key] = template(item_value, {'item': item}) key = template(value['key'], {'item': item}) if when: if template(conditional, {'item': { 'key': key, 'value': entry }}): values[key] = entry else: values[key] = entry obj[name] = values else: item = re_search(regexp, output) obj[name] = template(value, {'item': item}) else: obj[name] = value return obj
def test_template(self): tmpl = Template() self.assertEqual('foo', tmpl('{{ test }}', {'test': 'foo'}))
def __init__(self, lines, tmplt): self._lines = lines self._tmplt = tmplt self._template = Template()
def test_template(): tmpl = Template() assert 'foo' == tmpl('{{ test }}', {'test': 'foo'})
def __init__(self, tmplt): self._tmplt = tmplt self._template = Template()