Skip to content

Commit

Permalink
Merge pull request bluebanquise#266 from actatux/fix/static_code_anal…
Browse files Browse the repository at this point in the history
…ysis

fix: code cleanup in inventory converter helper
  • Loading branch information
oxedions authored Jun 30, 2020
2 parents 49ad6f9 + 18a32cf commit 7be74d5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions tools/inventory-converter-1.3-network_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import yaml


# Indent list with PyYAML
# From https://web.archive.org/web/20170903201521/https://pyyaml.org/ticket/64#comment:5
class MyDumper(yaml.Dumper):
Expand All @@ -15,6 +16,7 @@ def increase_indent(self, flow=False, indentless=False):
def represent_dict_preserve_order(self, data):
return self.represent_dict(data.items())


# Search and convert network_interfaces from dict to list
#
def search_network_interfaces(dictionary):
Expand All @@ -40,15 +42,17 @@ def search_network_interfaces(dictionary):

return dictionary

def usage():
print(f"Usage: {sys.argv[0]} /etc/bluebanquise/inventory/cluster/nodes/file.yml")

def usage(command):
print(f"Usage: {command} /etc/bluebanquise/inventory/cluster/nodes/file.yml")


def main():

MyDumper.add_representer(dict, MyDumper.represent_dict_preserve_order)

if len(sys.argv) != 2:
usage()
usage(sys.argv[0])
exit(1)

hostsfile = sys.argv[1]
Expand All @@ -57,11 +61,7 @@ def main():
with open(hostsfile, 'r') as fd:
try:
inventory = yaml.load(fd, Loader=yaml.FullLoader)
#print(yaml.dump(inventory))

new_inventory = search_network_interfaces(inventory)
#print(yaml.dump(new_inventory, Dumper=MyDumper))

except yaml.YAMLError as exc:
print(exc)

Expand All @@ -72,5 +72,6 @@ def main():
$ diff -u {hostsfile} {outfile} | less
$ mv {outfile} {hostsfile}''')


if __name__ == "__main__":
main()

0 comments on commit 7be74d5

Please sign in to comment.