Add new command reclass-remove-key
Example:
reclass-remove-key -r parameters.linux.network.interface /srv/
diff --git a/reclass_tools/cli.py b/reclass_tools/cli.py
index b9ad985..86eceed 100644
--- a/reclass_tools/cli.py
+++ b/reclass_tools/cli.py
@@ -53,4 +53,39 @@
args = ['-h']
params = parser.parse_args(args)
- execute(params)
+ results = walk_models.get_all_reclass_params(
+ params.paths,
+ identity_files=params.identity_files,
+ verbose=params.verbose)
+
+ print(yaml.dump(results))
+
+
+def remove_key(args=None):
+ if args is None:
+ args = sys.argv[1:]
+
+ parser = argparse.ArgumentParser(
+ formatter_class=argparse.RawTextHelpFormatter,
+ description="")
+ parser.add_argument('-i', dest='identity_files',
+ help=('For SSH connections, selects a file from which \n'
+ 'the identity (private key) for public key \n'
+ 'authentication is read. It is possible to have \n'
+ 'multiple -i options.'),
+ action='append')
+ parser.add_argument('--verbose', dest='verbose', action='store_const', const=True,
+ help='Show verbosed output.', default=False)
+ parser.add_argument('paths', help='Paths to search for *.yml files.', nargs='+')
+ parser.add_argument('--remove-key', '-r', dest='key',
+ help=('Remove key from reclass model, for example:'
+ ' reclass-remove-key -r parameters.linux.network.interface /path/to/model/'))
+ if len(args) == 0:
+ args = ['-h']
+
+ params = parser.parse_args(args)
+ results = walk_models.remove_reclass_parameter(
+ params.paths,
+ params.key,
+ identity_files=params.identity_files,
+ verbose=params.verbose)
diff --git a/reclass_tools/walk_models.py b/reclass_tools/walk_models.py
index a48c753..024b3cf 100644
--- a/reclass_tools/walk_models.py
+++ b/reclass_tools/walk_models.py
@@ -131,6 +131,7 @@
return None
return data
+
def remove_nested_key(data, path=None):
if type(path) is not list:
raise("Use 'list' object with key names for 'path'")
@@ -152,7 +153,6 @@
def get_all_reclass_params(paths, identity_files=None, verbose=False):
"""Return dict with all used values for each param"""
- #path = '/srv/salt/reclass/classes'
_params = dict()
for path in paths:
for log in walkfiles(path, identity_files, verbose):
@@ -167,52 +167,36 @@
_params[key].append(val)
else:
_params[key] = [val]
-
return _params
- #print(yaml.dump(_params))
-def remove_reclass_parameter(path, parameter, verbose=False):
- """Removes specified key from parameters from all reclass models"""
- #path = '/srv/salt/reclass/classes'
- _params = dict()
- for log in walkfiles(path, verbose=verbose):
- if log.fname.endswith('.yml'):
- model = yaml_read(log.fname)
- if model is not None:
+def remove_reclass_parameter(paths, key,
+ identity_files=None, verbose=False):
+ """Removes specified key from parameters from all reclass models
- # Clear linux.network.interfaces
- interfaces = get_nested_key(model, ['parameters', 'linux', 'network', 'interface'])
- if interfaces:
- print(log.fname)
- print(interfaces.keys())
+ :param key: string with point-separated nested objects, for
+ example: parameters.linux.network.interface
+ """
+ remove_key = key.split('.')
- remove_nested_key(model, ['parameters', 'linux', 'network', 'interface'])
+ for path in paths:
+ for fyml in walkfiles(path, verbose=verbose):
+ if fyml.fname.endswith('.yml'):
+ model = yaml_read(fyml.fname)
+ if model is not None:
- print(model)
- with open(log.fname, 'w') as f:
- f.write(
- yaml.dump(
- model, default_flow_style=False
+ # Clear linux.network.interfaces
+ interfaces = get_nested_key(model, remove_key)
+ if interfaces:
+ print(fyml.fname)
+ print(interfaces.keys())
+
+ remove_nested_key(model, remove_key)
+
+ print(model)
+ with open(fyml.fname, 'w') as f:
+ f.write(
+ yaml.dump(
+ model, default_flow_style=False
+ )
)
- )
-
-# #print(yaml.dump(interfaces, default_flow_style=False))
-
-# lvm = get_nested_key(model, ['parameters', 'linux', 'storage', 'lvm'])
-# if lvm:
-# print(log.fname)
-# print(lvm.keys())
-# #print(yaml.dump(lvm, default_flow_style=False))
-
-# mount = get_nested_key(model, ['parameters', 'linux', 'storage', 'mount'])
-# if mount:
-# print(log.fname)
-# print(mount.keys())
-# #print(yaml.dump(mount, default_flow_style=False))
-
-# swap = get_nested_key(model, ['parameters', 'linux', 'storage', 'swap'])
-# if swap:
-# print(log.fname)
-# print(swap.keys())
-# #print(yaml.dump(swap, default_flow_style=False))
diff --git a/setup.cfg b/setup.cfg
index 3da6344..3e4ece9 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -27,3 +27,4 @@
[entry_points]
console_scripts =
reclass-dump-params = reclass_tools.cli:dump_params
+ reclass-remove-key = reclass_tools.cli:remove_key