1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/hacking/conf2yaml.py
Brian Coca 74842adc07 1st part of ansible config, adds ansible-config to view/manage configs (#12797)
* Start of ansible config project

moved configuration definitions to external yaml file vs hardcoded
 * updated constants to be a data strcutures that are looped over and also return origin of setting
changed to manager/data scheme for base classes
new cli ansible-config to view/manage ansible configuration settings
 * prints green for default/unchanged and yellow for those that have been overriden
 * added list action to show all configurable settings and their associated ini and env var names
 * allows specifying config file to see what result would look like
 * TBD update, edit and view options

removed test for functions that have been removed

env_Vars are now list of dicts
allows for version_added and deprecation in future
added a couple of descriptions for future doc autogeneration
ensure test does not fail if delete_me exists
normalized 'path expansion'
added yaml config to setup packaging
removed unused imports
better encoding handling

updated as per feedback

* pep8
2017-06-14 11:08:34 -04:00

124 lines
2.9 KiB
Python
Executable file

#!/usr/bin/env python
import ast
import yaml
import os
import sys
from ansible.parsing.yaml.dumper import AnsibleDumper
things = {}
stuff = {}
op_map = {
ast.Add: '+',
ast.Sub: '-',
ast.Mult: '*',
ast.Div: '/',
}
def get_values(values):
if not isinstance(values, list):
return get_value(values)
ret = []
for value in values:
ret.append(get_value(value))
return ret
def get_value(value):
if hasattr(value, 'id'):
ret = value.id
elif hasattr(value, 's'):
ret = value.s
elif hasattr(value, 'n'):
ret = value.n
elif hasattr(value, 'left'):
operator = op_map[type(value.op)]
left = get_values(value.left)
right = get_values(value.right)
return '%s %s %s' % (left, operator, right)
elif hasattr(value, 'value'):
ret = value.value
elif hasattr(value, 'elts'):
ret = get_values(value.elts)
elif isinstance(value, ast.Call):
func, args, kwargs = get_call(value)
args[:] = [repr(arg) for arg in args]
for k, v in kwargs.items():
args.append('%s=%s' % (k, repr(v)))
return '%s(%s)' % (func, ', '.join(args))
else:
return value
return get_value(ret)
def get_call(value):
args = []
for arg in value.args:
v = get_value(arg)
try:
v = getattr(C, v, v)
except:
pass
args.append(v)
kwargs = {}
for keyword in value.keywords:
v = get_value(keyword.value)
try:
v = getattr(C, v, v)
except:
pass
kwargs[keyword.arg] = v
func = get_value(value.func)
try:
attr = '.%s' % value.func.attr
except:
attr = ''
return '%s%s' % (func, attr), args, kwargs
with open(sys.argv[1]) as f:
tree = ast.parse(f.read())
for item in tree.body:
if hasattr(item, 'value') and isinstance(item.value, ast.Call):
try:
if item.value.func.id != 'get_config':
continue
except AttributeError:
continue
_, args, kwargs = get_call(item.value)
name = get_value(item.targets[0])
section = args[1].lower()
config = args[2]
# new form
if name not in stuff:
stuff[name] = {}
stuff[name] = {
'desc': 'TODO: write it',
'ini': [{'section': section, 'key': config}],
'env': [args[3]],
'default': args[4] if len(args) == 5 else None,
'yaml': {'key': '%s.%s' % (section, config)},
'vars': []
}
stuff[name].update(kwargs)
## ini like
#if section not in things:
# things[section] = {}
#things[section][config] = {
# 'env_var': args[3],
# 'default': args[4] if len(args) == 5 else 'UNKNOWN'
#}
#things[section][config].update(kwargs)
print(yaml.dump(stuff, Dumper=AnsibleDumper, indent=2, width=170))