From 0ab9f038925441030a729392f04f95e0641ef97b Mon Sep 17 00:00:00 2001 From: ogenstad Date: Sat, 14 May 2016 14:16:48 +0200 Subject: [PATCH] ntc-templates the package --- HISTORY.rst | 7 +++++ MANIFEST.in | 1 + ntc_templates/__init__.py | 3 ++ ntc_templates/parse.py | 46 ++++++++++++++++++++++++++++++ pylama.ini | 7 +++++ setup.py | 59 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 HISTORY.rst create mode 100644 MANIFEST.in create mode 100644 ntc_templates/__init__.py create mode 100644 ntc_templates/parse.py create mode 100644 pylama.ini create mode 100644 setup.py diff --git a/HISTORY.rst b/HISTORY.rst new file mode 100644 index 0000000000..7f9ac1dd88 --- /dev/null +++ b/HISTORY.rst @@ -0,0 +1,7 @@ +Release History +--------------- + +1.0.0 ++++++ + +* First initial release diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000000..eb83332d83 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.md HISTORY.rst templates/* diff --git a/ntc_templates/__init__.py b/ntc_templates/__init__.py new file mode 100644 index 0000000000..5543705dc8 --- /dev/null +++ b/ntc_templates/__init__.py @@ -0,0 +1,3 @@ +"""ntc_templates - Parse raw output from network devices and return structured data.""" + +__version__ = '1.0.0' diff --git a/ntc_templates/parse.py b/ntc_templates/parse.py new file mode 100644 index 0000000000..b96d9dc17b --- /dev/null +++ b/ntc_templates/parse.py @@ -0,0 +1,46 @@ +"""ntc_templates.parse.""" +import os +import sys +from textfsm.clitable import CliTableError +import textfsm.clitable as clitable + + +def _get_template_dir(): + ntc_template_abspath = os.path.abspath(sys.modules['ntc_templates'].__file__) + base_dir = os.path.dirname(ntc_template_abspath) + template_dir = '%s%s%s' % (base_dir, os.sep, 'templates') + return template_dir + + +def _clitable_to_dict(cli_table): + """Convert TextFSM cli_table object to list of dictionaries.""" + objs = [] + for row in cli_table: + temp_dict = {} + for index, element in enumerate(row): + temp_dict[cli_table.header[index].lower()] = element + objs.append(temp_dict) + + return objs + + +def parse_output(platform=None, command=None, data=None): + """Return the structured data based on the output from a network device.""" + template_dir = _get_template_dir() + cli_table = clitable.CliTable('index', template_dir) + + attrs = dict( + Command=command, + Platform=platform + ) + try: + cli_table.ParseCmd(data, attrs) + structured_data = _clitable_to_dict(cli_table) + except CliTableError as e: + raise Exception('Unable to parse command "%s" on platform %s - %s' % (command, platform, str(e))) + # Invalid or Missing template + # module.fail_json(msg='parsing error', error=str(e)) + # rather than fail, fallback to return raw text + # structured_data = [data] + + return structured_data diff --git a/pylama.ini b/pylama.ini new file mode 100644 index 0000000000..b324c223e8 --- /dev/null +++ b/pylama.ini @@ -0,0 +1,7 @@ +[pylama] +linters = mccabe,pep257,pep8,pyflakes +ignore = D203, +skip = .tox/*,.ntc-modules/* + +[pylama:pep8] +max_line_length = 110 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000000..d1370e0495 --- /dev/null +++ b/setup.py @@ -0,0 +1,59 @@ +"""setup.py file.""" +import re +from codecs import open +from glob import glob +from setuptools import setup +import os +import shutil + +version = '' +with open('ntc_templates/__init__.py', 'r') as fd: + version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', + fd.read(), re.MULTILINE).group(1) + +if not version: + raise RuntimeError('Cannot find version information') + +with open('README.md', 'r', 'utf-8') as f: + readme = f.read() + +with open('HISTORY.rst', 'r', 'utf-8') as f: + history = f.read() + + +long_description = readme + '\n\n' + history + + +template_files = glob('templates/*') + +if os.path.islink('ntc_templates/templates'): + os.unlink('ntc_templates/templates') +elif os.path.isdir('ntc_templates/templates'): + shutil.rmtree('ntc_templates/templates') + +os.symlink('../templates', 'ntc_templates/templates') +config = { + 'name': 'ntc_templates', + # 'package_dir': {'': 'lib'}, + 'packages': ['ntc_templates'], + 'version': version, + 'package_data': {'ntc_templates': template_files}, + 'description': 'Package to return structured data from the output of network devices.', + 'long_description': long_description, + 'author': 'network.toCode()', + 'author_email': 'info@networktocode.com', + 'url': 'https://github.com/networktocode/ntc-templates', + 'install_requires': [ + 'gtextfsm', + 'terminal', + ], + 'classifiers': ['Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'Intended Audience :: System Administrators', + 'Programming Language :: Python :: 2.7'] +} + +setup(**config) + +if os.path.islink('ntc_templates/templates'): + os.unlink('ntc_templates/templates')