|
1 | | -from six import string_types |
2 | | -from six.moves import reduce |
3 | | - |
4 | 1 | from ..base.backend import BaseBackend |
5 | 2 | from .converters import (Aaa, Bridge, Discovery, Dyndns, Ebtables, Gui, Httpd, |
6 | 3 | Igmpproxy, Iptables, Netconf, Netmode, Ntpclient, |
7 | 4 | Pwdog, Radio, Resolv, Route, Snmp, Sshd, Syslog, |
8 | 5 | System, Telnetd, Update, Users, Vlan, Wireless, |
9 | 6 | Wpasupplicant) |
| 7 | +from .intermediate import flatten, intermediate_to_list |
10 | 8 | from .renderers import AirOsRenderer |
11 | 9 | from .schema import schema |
12 | 10 |
|
@@ -56,96 +54,3 @@ def to_intermediate(self): |
56 | 54 | super(AirOs, self).to_intermediate() |
57 | 55 | for k, v in self.intermediate_data.items(): |
58 | 56 | self.intermediate_data[k] = [x for x in flatten(intermediate_to_list(v)) if x != {}] |
59 | | - |
60 | | - |
61 | | -def flatten(elements): |
62 | | - """ |
63 | | - Flatten a list |
64 | | - """ |
65 | | - if elements is not list: |
66 | | - return elements |
67 | | - else: |
68 | | - return reduce(lambda x, y: x + flatten(y), elements, []) |
69 | | - |
70 | | - |
71 | | -def intermediate_to_list(configuration): |
72 | | - """ |
73 | | - Explore the configuration tree and flatten where |
74 | | - possible with the following policy |
75 | | - - list -> prepend the list index to every item key |
76 | | - - dictionary -> prepend the father key to every key |
77 | | -
|
78 | | - configuration :: List[Enum[Dict,List]] |
79 | | - return List[Dict] |
80 | | -
|
81 | | - >>> intermediate_to_list([ |
82 | | - { |
83 | | - 'spam': { |
84 | | - 'eggs': 'spam and eggs' |
85 | | - } |
86 | | - } |
87 | | - ]) |
88 | | - >>> |
89 | | - [{ |
90 | | - 'spam.eggs' : 'spam and eggs' |
91 | | - ]} |
92 | | -
|
93 | | - >>> intermediate_to_list([ |
94 | | - { |
95 | | - 'spam': { |
96 | | - 'eggs': 'spam and eggs' |
97 | | - } |
98 | | - }, |
99 | | - [ |
100 | | - { |
101 | | - 'henry': 'the first' |
102 | | - }, |
103 | | - { |
104 | | - 'jacob' : 'the second' |
105 | | - } |
106 | | - ] |
107 | | - ]) |
108 | | - >>> |
109 | | - [ |
110 | | - { |
111 | | - 'spam.eggs' : 'spam and eggs' |
112 | | - }, |
113 | | - { |
114 | | - '1.henry' : 'the first' |
115 | | - }, |
116 | | - { |
117 | | - '2.jacob' : 'the second' |
118 | | - } |
119 | | - ] |
120 | | - """ |
121 | | - |
122 | | - result = [] |
123 | | - |
124 | | - for element in configuration: |
125 | | - temp = {} |
126 | | - if isinstance(element, list): |
127 | | - for index, el in enumerate(element): |
128 | | - for key, value in el.items(): |
129 | | - temp['{i}.{key}'.format(i=index + 1, key=key)] = value |
130 | | - result = result + intermediate_to_list([temp]) |
131 | | - |
132 | | - elif isinstance(element, dict): |
133 | | - for key, value in element.items(): |
134 | | - if isinstance(value, string_types) or isinstance(value, int): |
135 | | - temp[key] = value |
136 | | - else: |
137 | | - # reduce to atom list |
138 | | - # as value could be dict or list |
139 | | - # enclose it in a flattened list |
140 | | - for child in intermediate_to_list(flatten([value])): |
141 | | - for child_key, child_value in child.items(): |
142 | | - nested_key = '{key}.{subkey}'.format(key=key, subkey=child_key) |
143 | | - temp[nested_key] = child_value |
144 | | - |
145 | | - # now it is atomic, append it to |
146 | | - result.append(temp) |
147 | | - |
148 | | - else: |
149 | | - raise Exception('malformed intermediate representation') |
150 | | - |
151 | | - return result |
0 commit comments