Skip to content

API Reference

ttp_templates.parse_output(data: str, platform: Optional[str] = None, command: Optional[str] = None, path: Optional[str] = None, yang: Optional[str] = None, misc: Optional[str] = None, structure: Optional[str] = 'list', template_vars: Optional[Dict] = None) -> Union[Dict, List]

Function to load template text and parse data provided

Valid combinations of template location

path attribute is always more preferred

  • path="./misc/foo/bar.txt"
  • platform="cisco_ios", command="show version"
  • yang="ietf-interfaces", platform="cisco_ios"
  • misc="foo_folder/bar_template.txt"

Parameters:

  • data (str) –

    data to parse

  • path (Optional[str]) –

    OS path to template to load

  • platform (Optional[str]) –

    name of the platform to load template for

  • command (Optional[str]) –

    command to load template for

  • yang (Optional[str]) –

    name of YANG module to load template for

  • misc (Optional[str]) –

    OS path to template within repository misc folder

  • structure (Optional[str]) –

    results structure list, dictionary or flat_list

  • template_vars (Optional[Dict]) –

    variables to load in template object

Source code in ttp_templates\ttp_templates.py
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def parse_output(
    data: str,
    platform: Optional[str] = None,
    command: Optional[str] = None,
    path: Optional[str] = None,
    yang: Optional[str] = None,
    misc: Optional[str] = None,
    structure: Optional[str] = "list",
    template_vars: Optional[Dict] = None,
) -> Union[Dict, List]:
    """
    Function to load template text and parse data provided

    **Valid combinations of template location**

    ``path`` attribute is always more preferred

    * ``path="./misc/foo/bar.txt"``
    * ``platform="cisco_ios", command="show version"``
    * ``yang="ietf-interfaces", platform="cisco_ios"``
    * ``misc="foo_folder/bar_template.txt"``

    :param data: data to parse
    :param path: OS path to template to load
    :param platform: name of the platform to load template for
    :param command: command to load template for
    :param yang: name of YANG module to load template for
    :param misc: OS path to template within repository misc folder
    :param structure: results structure list, dictionary or flat_list
    :param template_vars: variables to load in template object
    """
    template_vars = template_vars or {}
    # get template text
    template = get_template(
        platform=platform, command=command, path=path, yang=yang, misc=misc
    )

    # create parser object
    parser = ttp(data=data, template=template, vars=template_vars)

    # parse and return results
    parser.parse(one=True)
    return parser.result(structure=structure)

ttp_templates.get_template(path: Optional[str] = None, platform: Optional[str] = None, command: Optional[str] = None, yang: Optional[str] = None, misc: Optional[str] = None) -> str

Function to locate template file and return it's content

Valid combinations of template location

path attribute is always more preferred

  • path="./misc/foo/bar.txt"
  • platform="cisco_ios", command="show version"
  • yang="ietf-interfaces", platform="cisco_ios"
  • misc="foo_folder/bar_template.txt"

Parameters:

  • path (Optional[str]) –

    OS path to template to load

  • platform (Optional[str]) –

    name of the platform to load template for

  • command (Optional[str]) –

    command to load template for

  • yang (Optional[str]) –

    name of YANG module to load template for

  • misc (Optional[str]) –

    OS path to template within repository misc folder

Source code in ttp_templates\ttp_templates.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def get_template(
    path: Optional[str] = None,
    platform: Optional[str] = None,
    command: Optional[str] = None,
    yang: Optional[str] = None,
    misc: Optional[str] = None,
) -> str:
    """
    Function to locate template file and return it's content

    **Valid combinations of template location**

    ``path`` attribute is always more preferred

    * ``path="./misc/foo/bar.txt"``
    * ``platform="cisco_ios", command="show version"``
    * ``yang="ietf-interfaces", platform="cisco_ios"``
    * ``misc="foo_folder/bar_template.txt"``

    :param path: OS path to template to load
    :param platform: name of the platform to load template for
    :param command: command to load template for
    :param yang: name of YANG module to load template for
    :param misc: OS path to template within repository misc folder
    """
    # form path to template file
    if path:
        if path.strip().startswith("ttp://"):
            path = path.strip()[6:]
    elif platform and command:
        platform = platform.lower()
        command = command.lower()
        command = command.replace("|", "pipe")
        for symbol in [" ", "-"]:
            platform = platform.replace(symbol, "_")
            command = command.replace(symbol, "_")
        path = "platform/{}_{}.txt".format(platform, command)
    elif platform and yang:
        platform = platform.lower()
        yang = yang.lower()
        for symbol in [" "]:
            platform = platform.replace(symbol, "_")
            yang = yang.replace(symbol, "_")
        path = "yang/{}_{}.txt".format(yang, platform)
    elif misc:
        path = "misc/{}".format(misc)
    else:
        return None

    template_dir = os.path.abspath(os.path.dirname(__file__))
    template_filename = os.path.join(template_dir, path)

    # open template file and return content
    with open(template_filename, mode="r", encoding="utf-8") as f:
        return f.read()

ttp_templates.list_templates(pattern: str = '*') -> dict

Function to list available templates matching given pattern.

Primary usecase for this function is to simplify integration with other applications by providing API interface to list available TTP templates.

Parameters:

  • pattern (str) –

    glob pattern to filter templates by name

  • return

    dictionary with platform, yang and misc keys

Source code in ttp_templates\ttp_templates.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def list_templates(pattern: str = "*") -> dict:
    """
    Function to list available templates matching given pattern.

    Primary usecase for this function is to simplify integration with
    other applications by providing API interface to list available
    TTP templates.

    :param pattern: glob pattern to filter templates by name
    :param return: dictionary with platform, yang and misc keys
    """
    res = {
        "platform": [],
        "yang": [],
        "misc": {},
    }
    skip_files = ["readme.md"]
    paths = ["platform", "yang", "misc"]
    ttp_templates_dir = os.path.abspath(os.path.dirname(__file__))

    for path in paths:
        dirname = os.path.join(ttp_templates_dir, path)
        for dirpath, dirnames, filenames in os.walk(dirname):
            # get a list of parent folders names for current folder
            dirpath_items = dirpath.replace(ttp_templates_dir, "").split(os.sep)[1:]
            # skip if no files in folder
            if not filenames:
                continue
            # filter files
            files = [
                filename
                for filename in filenames
                if (
                    fnmatchcase(filename, pattern)
                    and filename.lower() not in skip_files
                )
            ]
            # descend down dirpath_items in res and save files list
            ref = res
            for index, item in enumerate(dirpath_items):
                # check if last item and save files list
                if index + 1 == len(dirpath_items):
                    ref[item] = files
                # create item key in res and obtain reference to its value
                else:
                    ref = ref.setdefault(item, {})
    return res
Back to top