Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_structured_data(rawoutput, module):
index_file = module.params['index_file']
template_dir = module.params['template_dir']
cli_table = clitable.CliTable(index_file, template_dir)
attrs = dict(
Command=module.params['command'],
Platform=module.params['platform']
)
try:
cli_table.ParseCmd(rawoutput, attrs)
structured_data = clitable_to_dict(cli_table)
except CliTableError as e:
# Invalid or Missing template
# module.fail_json(msg='parsing error', error=str(e))
# rather than fail, fallback to return raw text
structured_data = [rawoutput]
return structured_data
attributes: Dict, attribute that further refine matching template.
templates: String list of templates to parse with. If None, uses index
Raises:
CliTableError: A template was not found for the given command.
"""
# Store raw command data within the object.
self.raw = cmd_input
if not templates:
# Find template in template index.
row_idx = self.index.GetRowMatch(attributes)
if row_idx:
templates = self.index.index[row_idx]['Template']
else:
raise CliTableError('No template found for attributes: "%s"' %
attributes)
template_files = self._TemplateNamesToFiles(templates)
try:
# Re-initialise the table.
self.Reset()
self._keys = set()
self.table = self._ParseCmdItem(self.raw, template_file=template_files[0])
# Add additional columns from any additional tables.
for tmplt in template_files[1:]:
self.extend(self._ParseCmdItem(self.raw, template_file=tmplt),
set(self._keys))
finally:
for f in template_files:
Raises:
CliTableError: A template column was not found in the table.
"""
self.index_file = index_file or self.index_file
fullpath = os.path.join(self.template_dir, self.index_file)
if self.index_file and fullpath not in self.INDEX:
self.index = IndexTable(self._PreParse, self._PreCompile, fullpath)
self.INDEX[fullpath] = self.index
else:
self.index = self.INDEX[fullpath]
# Does the IndexTable have the right columns.
if 'Template' not in self.index.index.header: # pylint: disable=E1103
raise CliTableError("Index file does not have 'Template' column.")
def get_structured_data(output, command, platform, index_file, template_dir, data_model):
cli_table = clitable.CliTable(index_file, template_dir)
attrs = dict(
Command=command,
Platform=platform
)
try:
cli_table.ParseCmd(output, attrs)
if data_model == "textfsm":
structured_data = clitable_to_dict(cli_table)
else:
# Only textfsm is supported right now.
structured_data = [output]
except CliTableError:
# Invalid or Missing template
# rather than fail, fallback to return raw text
structured_data = [output]
return structured_data
def get_structured_data(raw_output, platform, command):
"""Convert raw CLI output to structured data using TextFSM template."""
template_dir = get_template_dir()
index_file = os.path.join(template_dir, 'index')
textfsm_obj = clitable.CliTable(index_file, template_dir)
attrs = {'Command': command, 'Platform': platform}
try:
# Parse output through template
textfsm_obj.ParseCmd(raw_output, attrs)
return clitable_to_dict(textfsm_obj)
except CliTableError:
return raw_output