Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes dependency on OrderedDict in netcfg #16483

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 29 additions & 32 deletions lib/ansible/module_utils/netcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#

import re
import collections
import itertools
import shlex

Expand Down Expand Up @@ -109,10 +108,9 @@ def items(self):
return self._config

def __str__(self):
config = collections.OrderedDict()
for item in self._config:
self.expand(item, config)
return '\n'.join(self.flatten(config))
if self._device_os == 'junos':
return self.to_lines(self.expand(self.items))
return self.to_block(self.expand(self.items))

def load(self, contents):
self._config = parse(contents, indent=self.indent)
Expand Down Expand Up @@ -154,26 +152,29 @@ def findall(self, regexp):
regexp = r'%s' % regexp
return re.findall(regexp, str(self))

def expand(self, obj, items):
block = [item.raw for item in obj.parents]
block.append(obj.raw)

current_level = items
for b in block:
if b not in current_level:
current_level[b] = collections.OrderedDict()
current_level = current_level[b]
for c in obj.children:
if c.raw not in current_level:
current_level[c.raw] = collections.OrderedDict()

def flatten(self, data, obj=None):
if obj is None:
obj = list()
for k, v in data.items():
obj.append(k)
self.flatten(v, obj)
return obj
def to_lines(self, section):
lines = list()
for entry in section[1:]:
line = ['set']
line.extend([p.text for p in entry.parents])
line.append(entry.text)
lines.append(' '.join(line))
return lines

def to_block(self, section):
return '\n'.join([item.raw for item in section])

def expand(self, objs):
visited = set()
expanded = list()
for o in objs:
for p in o.parents:
if p not in visited:
visited.add(p)
expanded.append(p)
expanded.append(o)
visited.add(o)
return expanded

def get_object(self, path):
for item in self.items:
Expand Down Expand Up @@ -226,16 +227,12 @@ def difference(self, other, path=None, match='line', replace='line'):
updates.extend(config)
break

updates = self.expand(updates)

if self._device_os == 'junos':
return updates

diffs = collections.OrderedDict()
for update in updates:
if replace == 'block' and update.parents:
update = update.parents[-1]
self.expand(update, diffs)

return self.flatten(diffs)
return [item.text for item in self.expand(updates)]

def _build_children(self, children, parents=None, offset=0):
for item in children:
Expand Down