Skip to content

Commit

Permalink
Python v2.6 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferry Boender committed Feb 25, 2017
1 parent 92249d8 commit e69a97c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/ansible-cmdb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ if __name__ == "__main__":

data_dir = util.find_path(data_dir_paths, 'tpl/html_fancy.tpl')
if not data_dir:
sys.stdout.write("Couldn't find the data dir for the templates. I tried: {}\n".format(", ".join(data_dir_paths)))
sys.stdout.write("Couldn't find the data dir for the templates. I tried: {0}\n".format(", ".join(data_dir_paths)))
sys.exit(1)
tpl_dir = os.path.join(data_dir, 'tpl')
static_dir = os.path.join(data_dir, 'static')
version = open(os.path.join(data_dir, 'VERSION')).read().strip()

parser = optparse.OptionParser(version="%prog v{}".format(version))
parser = optparse.OptionParser(version="%prog v{0}".format(version))
parser.set_usage(sys.argv[0] + " [option] <dir> > output.html")
parser.add_option("-t", "--template", dest="template", action="store", default='html_fancy', help="Template to use. Default is 'html_fancy'")
parser.add_option("-i", "--inventory", dest="inventory", action="store", default=None, help="Inventory to read extra info from")
Expand Down
14 changes: 7 additions & 7 deletions src/ansiblecmdb/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def _handle_inventory(self, inventory_path):

self._handle_inventory(os.path.join(inventory_path, fname))
else:
raise IOError("Invalid inventory file / dir: '{}'".format(inventory_path))
raise IOError("Invalid inventory file / dir: '{0}'".format(inventory_path))
self._parse_hostvar_dir(inventory_path)

def _parse_hosts_inventory(self, inventory_path):
Expand Down Expand Up @@ -117,7 +117,7 @@ def _parse_hostvar_dir(self, inventory_path):
# some reason... (psst, the reason is that yaml sucks)
first_line = open(f_path, 'r').readline()
if first_line.startswith('$ANSIBLE_VAULT'):
sys.stderr.write("Skipping encrypted vault file {}\n".format(f_path))
sys.stderr.write("Skipping encrypted vault file {0}\n".format(f_path))
continue

try:
Expand All @@ -126,7 +126,7 @@ def _parse_hostvar_dir(self, inventory_path):
f.close()
self.update_host(fname, {'hostvars': invars})
except Exception as err:
sys.stderr.write("Yaml couldn't load '{}'. Skipping\n".format(f_path))
sys.stderr.write("Yaml couldn't load '{0}'. Skipping\n".format(f_path))

def _parse_fact_dir(self, fact_dir, fact_cache=False):
"""
Expand All @@ -136,7 +136,7 @@ def _parse_fact_dir(self, fact_dir, fact_cache=False):
"""
self.log.debug("Parsing fact dir: {0}".format(fact_dir))
if not os.path.isdir(fact_dir):
raise IOError("Not a directory: '{}'".format(fact_dir))
raise IOError("Not a directory: '{0}'".format(fact_dir))

flist = []
for (dirpath, dirnames, filenames) in os.walk(fact_dir):
Expand Down Expand Up @@ -176,8 +176,8 @@ def _parse_dyn_inventory(self, script):
close_fds=True)
stdout, stderr = proc.communicate(input)
if proc.returncode != 0:
sys.stderr.write("Dynamic inventory script '{}' returned "
"exitcode {}\n".format(script,
sys.stderr.write("Dynamic inventory script '{0}' returned "
"exitcode {1}\n".format(script,
proc.returncode))
for line in stderr:
sys.stderr.write(line)
Expand All @@ -186,7 +186,7 @@ def _parse_dyn_inventory(self, script):
for hostname, key_values in dyninv_parser.hosts.items():
self.update_host(hostname, key_values)
except OSError as err:
sys.stderr.write("Exception while executing dynamic inventory script '{}':\n\n".format(script))
sys.stderr.write("Exception while executing dynamic inventory script '{0}':\n\n".format(script))
sys.stderr.write(str(err) + '\n')

def update_host(self, hostname, key_values):
Expand Down
8 changes: 4 additions & 4 deletions src/ansiblecmdb/data/tpl/markdown.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import datetime
<%def name="r_dict(d, depth=0)"><%
t = ""
for k, v in d.items():
t += (" " * depth) + "* **{}**: ".format(k)
t += (" " * depth) + "* **{0}**: ".format(k)
if type(v) == list:
t += "\n{}".format(r_list(v, depth + 1))
t += "\n{0}".format(r_list(v, depth + 1))
elif type(v) == dict:
t += "\n{}".format(r_dict(v, depth + 1))
t += "\n{0}".format(r_dict(v, depth + 1))
else:
t += "{}\n".format(v)
t += "{0}\n".format(v)
return t
%></%def>\
<%def name="col_main_ip(host)"><%
Expand Down
6 changes: 3 additions & 3 deletions src/ansiblecmdb/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(self, hosts_contents):
self._apply_section(section, self.hosts)
except ValueError:
tb = traceback.format_exc()
sys.stderr.write("Error while parsing hosts contents: '{}'\n"
sys.stderr.write("Error while parsing hosts contents: '{0}'\n"
"Invalid hosts file?\n".format(tb))

def _parse_hosts_contents(self, hosts_contents):
Expand Down Expand Up @@ -357,7 +357,7 @@ def expand_hostdef(self, hostdef):
# Strip port numbers off and return
return [host_name.split(':')[0] for host_name in hosts_done]
except Exception as e:
sys.stderr.write("Couldn't parse host definition '{}': {}\n".format(hostdef, e))
sys.stderr.write("Couldn't parse host definition '{0}': {1}\n".format(hostdef, e))
return []


Expand Down Expand Up @@ -430,7 +430,7 @@ def _parse_group(self, group_name, group):
self._get_host(hostname)['groups'].add(group_name)
else:
sys.stderr.write("Invalid element found in dynamic "
"inventory output: {}".format(type(group)))
"inventory output: {0}".format(type(group)))

def _parse_meta(self, meta):
"""
Expand Down

0 comments on commit e69a97c

Please sign in to comment.