diff --git a/README.md b/README.md index 2f208e2..eb6c3bc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/source/TTP Templates Collection.rst b/docs/source/TTP Templates Collection.rst new file mode 100644 index 0000000..175ee72 --- /dev/null +++ b/docs/source/TTP Templates Collection.rst @@ -0,0 +1,50 @@ +TTP Templates Collection +======================== + +`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://`` format, where ``path`` is an OS path to template text file within ``ttp_templates`` repository. + +Sample code:: + + from ttp import ttp + import pprint + + data = """ + + interface Lo0 + ip address 124.171.238.50 32 + ! + interface Lo1 + description this interface has description + ip address 1.1.1.1 32 + + """ + + 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:: + + + interface {{ interface }} + description {{ description | re(".+") }} + encapsulation dot1q {{ dot1q }} + ip address {{ ip }} {{ mask }} + shutdown {{ disabled | set(True) }} + \ No newline at end of file diff --git a/docs/source/index.rst b/docs/source/index.rst index c48cd60..36bebf6 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -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 \ No newline at end of file diff --git a/test/pytest/Output/excel_out_test_excel_formatter.xlsx b/test/pytest/Output/excel_out_test_excel_formatter.xlsx index ab11c49..fad6624 100644 Binary files a/test/pytest/Output/excel_out_test_excel_formatter.xlsx and b/test/pytest/Output/excel_out_test_excel_formatter.xlsx differ diff --git a/test/pytest/test_ttp_parser_methods.py b/test/pytest/test_ttp_parser_methods.py index 8774171..0d6c98b 100644 --- a/test/pytest/test_ttp_parser_methods.py +++ b/test/pytest/test_ttp_parser_methods.py @@ -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 = """ @@ -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() \ No newline at end of file +# test_add_input_structured_data_list() + +def test_loading_template_from_ttp_templates(): + data1 = """ + +interface Lo0 + ip address 124.171.238.50 32 +! +interface Lo1 + description this interface has description + ip address 1.1.1.1 32 + + """ + 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() \ No newline at end of file diff --git a/ttp/utils/loaders.py b/ttp/utils/loaders.py index d4f1ee8..9b5dce0 100644 --- a/ttp/utils/loaders.py +++ b/ttp/utils/loaders.py @@ -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: