ttp-templates


Namettp-templates JSON
Version 0.3.7 PyPI version JSON
download
home_pagehttps://github.com/dmulyalin/ttp_templates
SummaryTemplate Text Parser Templates collections
upload_time2024-11-07 09:41:52
maintainerDenis Mulyalin
docs_urlNone
authorDenis Mulyalin
requires_python<4.0,>=3.6
licenseMIT
keywords parsing ttp regex
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Downloads](https://pepy.tech/badge/ttp_templates)](https://pepy.tech/project/ttp_templates)
[![PyPI versions](https://img.shields.io/pypi/pyversions/ttp.svg)](https://pypi.python.org/pypi/ttp_templates/)

# Template Text Parser Templates

This repository contains a collection of [TTP](https://github.com/dmulyalin/ttp) templates.

If you solved a problem using TTP and feel that your work can be useful to other people, feel
free to raise an issue or submit pull request to include your template(s) in this repository.
Refer to [Contribute Guide](https://dmulyalin.github.io/ttp_templates/contribute/) for details.

Documentation: [https://dmulyalin.github.io/ttp_templates/](https://dmulyalin.github.io/ttp_templates/)

Repository: [https://github.com/dmulyalin/ttp_templates](https://github.com/dmulyalin/ttp_templates)

TTP: [https://ttp.readthedocs.io/](https://ttp.readthedocs.io/)

## Installation

From PyPi:

`pip install ttp-templates`

or latest from GitHub master branch (need Git installed on the system):

`pip install git+https://github.com/dmulyalin/ttp_templates.git`

## Sample usage

This example demonstrates how to parse `Test Platform` output for `show run | sec interface` command using `platform/test_platform_show_run_pipe_sec_interface.txt` template.
<details><summary>Code</summary>

```python
from ttp_templates import parse_output
import pprint

data = """
interface GigabitEthernet1/3.251
 description Customer #32148
 encapsulation dot1q 251
 ip address 172.16.33.10 255.255.255.128
 shutdown
!
interface GigabitEthernet1/3.251
 description Customer #32148
 encapsulation dot1q 251
 ip address 172.16.33.10 255.255.255.128
 shutdown
"""

result = parse_output(
    data=data,
    platform="Test Platform",
    command="show run | sec interface"
)

pprint.pprint(result)

# prints:
# [[[{'description': 'Customer #32148',
#     'disabled': True,
#     'dot1q': '251',
#     'interface': 'GigabitEthernet1/3.251',
#     'ip': '172.16.33.10',
#     'mask': '255.255.255.128'},
#    {'description': 'Customer #32148',
#     'disabled': True,
#     'dot1q': '251',
#     'interface': 'GigabitEthernet1/3.251',
#     'ip': '172.16.33.10',
#     'mask': '255.255.255.128'}]]]
```
</details>

Sample code to parse `Cisco IOS` output in a structure compatible with `ietf-interfaces` YANG module.
<details><summary>Code</summary>

```python
from ttp_templates import get_template
from ttp import ttp
import pprint

data1 = """
interface GigabitEthernet1/3.251
 description Customer #32148
 encapsulation dot1q 251
 ip address 172.16.33.10 255.255.255.128
 shutdown
!
interface GigabitEthernet1/4
 description vCPEs access control
 ip address 172.16.33.10 255.255.255.128
!
"""
data2 = """
interface GigabitEthernet1/5
 description Works data
 ip mtu 9000
!
interface GigabitEthernet1/7
 description Works data v6
 ipv6 address 2001::1/64
 ipv6 address 2001:1::1/64
"""

template = get_template(yang="ietf-interfaces", platform="cisco_ios")
parser = ttp(template=template)

parser.add_input(data1)
parser.add_input(data2)

parser.parse()
res = parser.result()
pprint.pprint(res)

# prints:
# [{'comment': '',
#   'exception': {},
#   'result': [{'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'down',
#                                                             'description': 'Customer '
#                                                                            '#32148',
#                                                             'enabled': False,
#                                                             'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10',
#                                                                                           'netmask': '255.255.255.128',
#                                                                                           'origin': 'static'}]},
#                                                             'if-index': 1,
#                                                             'link-up-down-trap-enable': 'enabled',
#                                                             'name': 'GigabitEthernet1/3.251',
#                                                             'oper-status': 'unknown',
#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},
#                                                             'type': 'iana-if-type:ethernetCsmacd'},
#                                                            {'admin-status': 'up',
#                                                             'description': 'vCPEs '
#                                                                            'access '
#                                                                            'control',
#                                                             'enabled': True,
#                                                             'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10',
#                                                                                           'netmask': '255.255.255.128',
#                                                                                           'origin': 'static'}]},
#                                                             'if-index': 1,
#                                                             'link-up-down-trap-enable': 'enabled',
#                                                             'name': 'GigabitEthernet1/4',
#                                                             'oper-status': 'unknown',
#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},
#                                                             'type': 'iana-if-type:ethernetCsmacd'}]}},
#              {'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'up',
#                                                             'description': 'Works '
#                                                                            'data',
#                                                             'enabled': True,
#                                                             'ietf-ip:ipv4': {'mtu': 9000},
#                                                             'if-index': 1,
#                                                             'link-up-down-trap-enable': 'enabled',
#                                                             'name': 'GigabitEthernet1/5',
#                                                             'oper-status': 'unknown',
#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},
#                                                             'type': 'iana-if-type:ethernetCsmacd'},
#                                                            {'admin-status': 'up',
#                                                             'description': 'Works '
#                                                                            'data '
#                                                                            'v6',
#                                                             'enabled': True,
#                                                             'ietf-ip:ipv6': {'address': [{'ip': '2001::1',
#                                                                                           'origin': 'static',
#                                                                                           'prefix-length': 64},
#                                                                                          {'ip': '2001:1::1',
#                                                                                           'origin': 'static',
#                                                                                           'prefix-length': 64}]},
#                                                             'if-index': 1,
#                                                             'link-up-down-trap-enable': 'enabled',
#                                                             'name': 'GigabitEthernet1/7',
#                                                             'oper-status': 'unknown',
#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},
#                                                             'type': 'iana-if-type:ethernetCsmacd'}]}}],
#   'valid': {0: True, 1: True}}]
```
</details>

## How templates collections structured

This repository contains three collections of templates corresponding to folder names:

* `platform` collection - mimics [ntc-templates](https://github.com/networktocode/ntc-templates) API and follows same naming rule
* `yang` collection - contains templates capable of producing YANG compatible structures out of text data
* `misc` collection - miscellaneous templates for various use cases organized in folders

### Platform collection templates files naming rule

`{{ vendor_os }}_{{ command_with_underscores }}.txt` - lower case only.

Naming rules details:

* All space symbols `' '` replaced with underscores.
* Pipe symbol `|` replaced with `pipe` in template name. For example,
  template to parse Cisco IOS `show run | section interface` command output
  must be named `cisco_ios_show_running_config_pipe_section_interface.txt`
* Dash symbols `-` replaced with underscores. For example, template to parse
  Huawei `display current-configuration interface` command output  must be
  named `huawei_display_current_configuration_interface.txt`

### YANG collection templates files naming rule

`{{ YANG module name }}_{{ platform_name }}.txt` - lower case only

Naming rules details:

* All space symbols `' '` replaced with underscore.
* Dash symbol `-` **does not** replaced with underscore. For example, template
  to produce output compatible with openconfig-lldp YANG model for Cisco IOS
  must be named `openconfig-lldp_cisco_xr.txt`

### Misc collection templates files naming rule

`{{ usecase folder }}/{{ template name }}.txt` - upper or lower case

Naming rules details:

* Nothing replaced with anything, provided template name used as is.

## Additional Templates Resources

List of resources with TTP templates:

- TTP SrosParser - https://pypi.org/project/ttp-sros-parser/ by [h4ndzdatm0ld](https://github.com/h4ndzdatm0ld)
- Template for parsing "show run" for Cisco IOS - https://github.com/tbotnz/ios-show-run-ttp by [tbotnz](https://github.com/tbotnz)
- Template for Cisco ASA configuration - https://gist.github.com/consentfactory/85872fc83453d1735b15aed3e47a9763 by [consentfactory](https://gist.github.com/consentfactory)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dmulyalin/ttp_templates",
    "name": "ttp-templates",
    "maintainer": "Denis Mulyalin",
    "docs_url": null,
    "requires_python": "<4.0,>=3.6",
    "maintainer_email": "d.mulyalin@gmail.com",
    "keywords": "Parsing, TTP, regex",
    "author": "Denis Mulyalin",
    "author_email": "d.mulyalin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4f/24/3980b51a98a061790d1a900b8b0f7a786d543436ac2e08c6aba2c2f43aff/ttp_templates-0.3.7.tar.gz",
    "platform": null,
    "description": "[![Downloads](https://pepy.tech/badge/ttp_templates)](https://pepy.tech/project/ttp_templates)\n[![PyPI versions](https://img.shields.io/pypi/pyversions/ttp.svg)](https://pypi.python.org/pypi/ttp_templates/)\n\n# Template Text Parser Templates\n\nThis repository contains a collection of [TTP](https://github.com/dmulyalin/ttp) templates.\n\nIf you solved a problem using TTP and feel that your work can be useful to other people, feel\nfree to raise an issue or submit pull request to include your template(s) in this repository.\nRefer to [Contribute Guide](https://dmulyalin.github.io/ttp_templates/contribute/) for details.\n\nDocumentation: [https://dmulyalin.github.io/ttp_templates/](https://dmulyalin.github.io/ttp_templates/)\n\nRepository: [https://github.com/dmulyalin/ttp_templates](https://github.com/dmulyalin/ttp_templates)\n\nTTP: [https://ttp.readthedocs.io/](https://ttp.readthedocs.io/)\n\n## Installation\n\nFrom PyPi:\n\n`pip install ttp-templates`\n\nor latest from GitHub master branch (need Git installed on the system):\n\n`pip install git+https://github.com/dmulyalin/ttp_templates.git`\n\n## Sample usage\n\nThis example demonstrates how to parse `Test Platform` output for `show run | sec interface` command using `platform/test_platform_show_run_pipe_sec_interface.txt` template.\n<details><summary>Code</summary>\n\n```python\nfrom ttp_templates import parse_output\nimport pprint\n\ndata = \"\"\"\ninterface GigabitEthernet1/3.251\n description Customer #32148\n encapsulation dot1q 251\n ip address 172.16.33.10 255.255.255.128\n shutdown\n!\ninterface GigabitEthernet1/3.251\n description Customer #32148\n encapsulation dot1q 251\n ip address 172.16.33.10 255.255.255.128\n shutdown\n\"\"\"\n\nresult = parse_output(\n    data=data,\n    platform=\"Test Platform\",\n    command=\"show run | sec interface\"\n)\n\npprint.pprint(result)\n\n# prints:\n# [[[{'description': 'Customer #32148',\n#     'disabled': True,\n#     'dot1q': '251',\n#     'interface': 'GigabitEthernet1/3.251',\n#     'ip': '172.16.33.10',\n#     'mask': '255.255.255.128'},\n#    {'description': 'Customer #32148',\n#     'disabled': True,\n#     'dot1q': '251',\n#     'interface': 'GigabitEthernet1/3.251',\n#     'ip': '172.16.33.10',\n#     'mask': '255.255.255.128'}]]]\n```\n</details>\n\nSample code to parse `Cisco IOS` output in a structure compatible with `ietf-interfaces` YANG module.\n<details><summary>Code</summary>\n\n```python\nfrom ttp_templates import get_template\nfrom ttp import ttp\nimport pprint\n\ndata1 = \"\"\"\ninterface GigabitEthernet1/3.251\n description Customer #32148\n encapsulation dot1q 251\n ip address 172.16.33.10 255.255.255.128\n shutdown\n!\ninterface GigabitEthernet1/4\n description vCPEs access control\n ip address 172.16.33.10 255.255.255.128\n!\n\"\"\"\ndata2 = \"\"\"\ninterface GigabitEthernet1/5\n description Works data\n ip mtu 9000\n!\ninterface GigabitEthernet1/7\n description Works data v6\n ipv6 address 2001::1/64\n ipv6 address 2001:1::1/64\n\"\"\"\n\ntemplate = get_template(yang=\"ietf-interfaces\", platform=\"cisco_ios\")\nparser = ttp(template=template)\n\nparser.add_input(data1)\nparser.add_input(data2)\n\nparser.parse()\nres = parser.result()\npprint.pprint(res)\n\n# prints:\n# [{'comment': '',\n#   'exception': {},\n#   'result': [{'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'down',\n#                                                             'description': 'Customer '\n#                                                                            '#32148',\n#                                                             'enabled': False,\n#                                                             'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10',\n#                                                                                           'netmask': '255.255.255.128',\n#                                                                                           'origin': 'static'}]},\n#                                                             'if-index': 1,\n#                                                             'link-up-down-trap-enable': 'enabled',\n#                                                             'name': 'GigabitEthernet1/3.251',\n#                                                             'oper-status': 'unknown',\n#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},\n#                                                             'type': 'iana-if-type:ethernetCsmacd'},\n#                                                            {'admin-status': 'up',\n#                                                             'description': 'vCPEs '\n#                                                                            'access '\n#                                                                            'control',\n#                                                             'enabled': True,\n#                                                             'ietf-ip:ipv4': {'address': [{'ip': '172.16.33.10',\n#                                                                                           'netmask': '255.255.255.128',\n#                                                                                           'origin': 'static'}]},\n#                                                             'if-index': 1,\n#                                                             'link-up-down-trap-enable': 'enabled',\n#                                                             'name': 'GigabitEthernet1/4',\n#                                                             'oper-status': 'unknown',\n#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},\n#                                                             'type': 'iana-if-type:ethernetCsmacd'}]}},\n#              {'ietf-interfaces:interfaces': {'interface': [{'admin-status': 'up',\n#                                                             'description': 'Works '\n#                                                                            'data',\n#                                                             'enabled': True,\n#                                                             'ietf-ip:ipv4': {'mtu': 9000},\n#                                                             'if-index': 1,\n#                                                             'link-up-down-trap-enable': 'enabled',\n#                                                             'name': 'GigabitEthernet1/5',\n#                                                             'oper-status': 'unknown',\n#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},\n#                                                             'type': 'iana-if-type:ethernetCsmacd'},\n#                                                            {'admin-status': 'up',\n#                                                             'description': 'Works '\n#                                                                            'data '\n#                                                                            'v6',\n#                                                             'enabled': True,\n#                                                             'ietf-ip:ipv6': {'address': [{'ip': '2001::1',\n#                                                                                           'origin': 'static',\n#                                                                                           'prefix-length': 64},\n#                                                                                          {'ip': '2001:1::1',\n#                                                                                           'origin': 'static',\n#                                                                                           'prefix-length': 64}]},\n#                                                             'if-index': 1,\n#                                                             'link-up-down-trap-enable': 'enabled',\n#                                                             'name': 'GigabitEthernet1/7',\n#                                                             'oper-status': 'unknown',\n#                                                             'statistics': {'discontinuity-time': '1970-01-01T00:00:00+00:00'},\n#                                                             'type': 'iana-if-type:ethernetCsmacd'}]}}],\n#   'valid': {0: True, 1: True}}]\n```\n</details>\n\n## How templates collections structured\n\nThis repository contains three collections of templates corresponding to folder names:\n\n* `platform` collection - mimics [ntc-templates](https://github.com/networktocode/ntc-templates) API and follows same naming rule\n* `yang` collection - contains templates capable of producing YANG compatible structures out of text data\n* `misc` collection - miscellaneous templates for various use cases organized in folders\n\n### Platform collection templates files naming rule\n\n`{{ vendor_os }}_{{ command_with_underscores }}.txt` - lower case only.\n\nNaming rules details:\n\n* All space symbols `' '` replaced with underscores.\n* Pipe symbol `|` replaced with `pipe` in template name. For example,\n  template to parse Cisco IOS `show run | section interface` command output\n  must be named `cisco_ios_show_running_config_pipe_section_interface.txt`\n* Dash symbols `-` replaced with underscores. For example, template to parse\n  Huawei `display current-configuration interface` command output  must be\n  named `huawei_display_current_configuration_interface.txt`\n\n### YANG collection templates files naming rule\n\n`{{ YANG module name }}_{{ platform_name }}.txt` - lower case only\n\nNaming rules details:\n\n* All space symbols `' '` replaced with underscore.\n* Dash symbol `-` **does not** replaced with underscore. For example, template\n  to produce output compatible with openconfig-lldp YANG model for Cisco IOS\n  must be named `openconfig-lldp_cisco_xr.txt`\n\n### Misc collection templates files naming rule\n\n`{{ usecase folder }}/{{ template name }}.txt` - upper or lower case\n\nNaming rules details:\n\n* Nothing replaced with anything, provided template name used as is.\n\n## Additional Templates Resources\n\nList of resources with TTP templates:\n\n- TTP SrosParser - https://pypi.org/project/ttp-sros-parser/ by [h4ndzdatm0ld](https://github.com/h4ndzdatm0ld)\n- Template for parsing \"show run\" for Cisco IOS - https://github.com/tbotnz/ios-show-run-ttp by [tbotnz](https://github.com/tbotnz)\n- Template for Cisco ASA configuration - https://gist.github.com/consentfactory/85872fc83453d1735b15aed3e47a9763 by [consentfactory](https://gist.github.com/consentfactory)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Template Text Parser Templates collections",
    "version": "0.3.7",
    "project_urls": {
        "Documentation": "https://dmulyalin.github.io/ttp_templates/",
        "Homepage": "https://github.com/dmulyalin/ttp_templates",
        "Repository": "https://github.com/dmulyalin/ttp_templates"
    },
    "split_keywords": [
        "parsing",
        " ttp",
        " regex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cb8e53b0d67a394ff742dbfda62543a1c48c0f32ea630acce6ccadeb09194344",
                "md5": "327f7ed58c0c23a1d756329c70c0f733",
                "sha256": "2328304fb4c957ee60db6f301143e8a4556b22a12b3e2f30511e8ef97fc78f7e"
            },
            "downloads": -1,
            "filename": "ttp_templates-0.3.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "327f7ed58c0c23a1d756329c70c0f733",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.6",
            "size": 76514,
            "upload_time": "2024-11-07T09:41:50",
            "upload_time_iso_8601": "2024-11-07T09:41:50.359330Z",
            "url": "https://files.pythonhosted.org/packages/cb/8e/53b0d67a394ff742dbfda62543a1c48c0f32ea630acce6ccadeb09194344/ttp_templates-0.3.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f243980b51a98a061790d1a900b8b0f7a786d543436ac2e08c6aba2c2f43aff",
                "md5": "02eaabf60b6058ba563229b37ec282bd",
                "sha256": "f9103041a3683a0cb3811609ad990f679beadfc9a92c3e3fa05d6037414ad2bf"
            },
            "downloads": -1,
            "filename": "ttp_templates-0.3.7.tar.gz",
            "has_sig": false,
            "md5_digest": "02eaabf60b6058ba563229b37ec282bd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.6",
            "size": 43233,
            "upload_time": "2024-11-07T09:41:52",
            "upload_time_iso_8601": "2024-11-07T09:41:52.155879Z",
            "url": "https://files.pythonhosted.org/packages/4f/24/3980b51a98a061790d1a900b8b0f7a786d543436ac2e08c6aba2c2f43aff/ttp_templates-0.3.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-07 09:41:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dmulyalin",
    "github_project": "ttp_templates",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "ttp-templates"
}
        
Elapsed time: 1.49507s