Skip to content

Commit

Permalink
Added support to use templates from ttp_templates collection
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulyalin committed Dec 22, 2020
1 parent 737c82e commit 7b013a8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ TTP [Networktocode Slack channel](https://networktocode.slack.com/archives/C018H

Playground - [online TTP tester](http://netpalm.tech:8080/)

Collection of [TTP Templates](https://github.com/dmulyalin/ttp_templates)

## Example - as simple as it can be

Simple interfaces configuration parsing example
Expand Down
50 changes: 50 additions & 0 deletions docs/source/TTP Templates Collection.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
TTP Templates Collection
========================

`TTP Templates <https://github.com/dmulyalin/ttp_templates>`_ repository contains a number of TTP templates.

Install::

pip install ttp_templates
To reference templates from ``ttp_templates``, ttp parser ``template`` argument should be of ``ttp://<path>`` format, where ``path`` is an OS path to template text file within ``ttp_templates`` repository.

Sample code::

from ttp import ttp
import pprint
data = """
<input load="text">
interface Lo0
ip address 124.171.238.50 32
!
interface Lo1
description this interface has description
ip address 1.1.1.1 32
</input>
"""
parser = ttp(data=data, template="ttp://platform/test_platform_show_run_pipe_sec_interface.txt")
parser.parse()
res = parser.result()
pprint.pprint(res)
# prints:
#
# [[[{'interface': 'Lo0', 'ip': '124.171.238.50', 'mask': '32'},
# {'description': 'this interface has description',
# 'interface': 'Lo1',
# 'ip': '1.1.1.1',
# 'mask': '32'}]]]
Where ``platform/test_platform_show_run_pipe_sec_interface.txt`` is a text file from ``ttp_templates`` repository with content::

<group>
interface {{ interface }}
description {{ description | re(".+") }}
encapsulation dot1q {{ dot1q }}
ip address {{ ip }} {{ mask }}
shutdown {{ disabled | set(True) }}
</group>
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Template Text Parser
Doc Tag/Doc Tag
Writing templates/index
CLI tool
TTP Templates Collection
API reference
Performance
TTP Internals/index
Binary file modified test/pytest/Output/excel_out_test_excel_formatter.xlsx
Binary file not shown.
33 changes: 28 additions & 5 deletions test/pytest/test_ttp_parser_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,10 +1150,10 @@ def test_get_input_load_anonymous_template():
# test_get_input_load_anonymous_template()

def to_test_globals_injection(data):
if "_ttp_" in globals():
return data, True
return data, False
if "_ttp_" in globals():
return data, True
return data, False
def test_add_function_method_group_globals_injection():
template_1 = """
<input load="text">
Expand Down Expand Up @@ -1217,4 +1217,27 @@ def pre_process(data):
assert res == [[[{'interface': 'Lo0', 'ip': '124.171.238.50', 'mask': '32'},
{'description': 'this interface has description', 'interface': 'Lo1'}]]]

# test_add_input_structured_data_list()
# test_add_input_structured_data_list()

def test_loading_template_from_ttp_templates():
data1 = """
<input load="text">
interface Lo0
ip address 124.171.238.50 32
!
interface Lo1
description this interface has description
ip address 1.1.1.1 32
</input>
"""
parser = ttp(data=data1, template="ttp://platform/test_platform_show_run_pipe_sec_interface.txt")
parser.parse()
res = parser.result()
# pprint.pprint(res)
assert res == [[[{'interface': 'Lo0', 'ip': '124.171.238.50', 'mask': '32'},
{'description': 'this interface has description',
'interface': 'Lo1',
'ip': '1.1.1.1',
'mask': '32'}]]]

# test_loading_template_from_ttp_templates()
6 changes: 5 additions & 1 deletion ttp/utils/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ def load_files(path, extensions=[], filters=[], read=False):
)
]

# check if path is a reference to template in ttp_templates collection
if path.startswith("ttp://"):
from ttp_templates import get_template
return [("text_data", get_template(path=path.replace("ttp://", "")))]
# check if path is a path to file:
if os.path.isfile(path[:5000]):
elif os.path.isfile(path[:5000]):
if read:
try:
if _ttp_["python_major_version"] == 2:
Expand Down

0 comments on commit 7b013a8

Please sign in to comment.