2015-12-12 22:34:08 +00:00
|
|
|
|
2015-12-16 17:41:49 +00:00
|
|
|
import os
|
|
|
|
import click
|
2016-03-21 21:42:39 +00:00
|
|
|
import ipaddress
|
|
|
|
|
|
|
|
def ip_network(j):
|
|
|
|
return ipaddress.ip_network(unicode(j))
|
|
|
|
|
|
|
|
def ip_address(j):
|
|
|
|
return ipaddress.ip_address(unicode(j))
|
2015-12-16 17:41:49 +00:00
|
|
|
|
2015-12-12 22:34:08 +00:00
|
|
|
def expand_paths():
|
|
|
|
"""
|
|
|
|
Prefix '..._path' keyword arguments of target function with 'directory' keyword argument
|
|
|
|
and create the directory if necessary
|
|
|
|
|
|
|
|
TODO: Move to separate file
|
|
|
|
"""
|
|
|
|
def wrapper(func):
|
|
|
|
def wrapped(**arguments):
|
|
|
|
d = arguments.get("directory")
|
|
|
|
for key, value in arguments.items():
|
|
|
|
if key.endswith("_path"):
|
|
|
|
if d:
|
|
|
|
value = os.path.join(d, value)
|
|
|
|
value = os.path.realpath(value)
|
|
|
|
parent = os.path.dirname(value)
|
|
|
|
if not os.path.exists(parent):
|
|
|
|
click.echo("Making directory %s for %s" % (repr(parent), repr(key)))
|
|
|
|
os.makedirs(parent)
|
|
|
|
elif not os.path.isdir(parent):
|
|
|
|
raise Exception("Path %s is not directory!" % parent)
|
|
|
|
arguments[key] = value
|
|
|
|
return func(**arguments)
|
|
|
|
return wrapped
|
|
|
|
return wrapper
|
|
|
|
|