Update: schema management across plugins, trailing spaces cleanup

This commit is contained in:
mrjk 2022-01-15 17:44:02 -05:00
parent 50c7af2f58
commit 4854bb240f
8 changed files with 212 additions and 139 deletions

View File

@ -22,15 +22,15 @@ log = logging.getLogger(__name__)
class App(): class App():
schema = { schema = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"type": "object", "type": "object",
"additionalProperties": False, "additionalProperties": False,
"default": {}, "default": {},
"$def" :{ "$def" :{
'backends_items': None, 'backends_items': {},
'backends_config': None, 'backends_config': {},
'rules_items': None, 'rules_items': {},
'rules_config': None, 'rules_config': {},
}, },
"patternProperties": { "patternProperties": {
".*": { ".*": {
@ -70,12 +70,15 @@ class App():
"tree": { "tree": {
"type": "array", "type": "array",
"default": [], "default": [],
"arrayItem": { "$ref": "#/$defs/backends_items" }, "items": {
"type": "object",
"properties": { "$ref": "#/$defs/backends_items" },
},
}, },
"rules": { "rules": {
"type": "array", "type": "array",
"default": [], "default": [],
"arrayItem": { "$ref": "#/$defs/rules_items" }, # "arrayItem": { "$ref": "#/$defs/rules_items" },
}, },
}, },
}, },
@ -122,12 +125,9 @@ class App():
r2 = RulesManager.get_schema(AlberoPlugins) r2 = RulesManager.get_schema(AlberoPlugins)
d = self.schema d = self.schema
d['$def']['backends_items'] = r1 d['patternProperties']['.*']['properties'] ['tree']['items']['properties'] = r1
d['$def']['rules_items'] = r2 d['patternProperties']['.*']['properties'] ['tree']['items'] = r2
d['$def']['backends_config'] = None
d['$def']['rules_config'] = None
#pprint(d)
print(json.dumps(d, indent=2)) print(json.dumps(d, indent=2))

View File

@ -1,4 +1,6 @@
import dpath.util
import copy import copy
import json import json
import textwrap import textwrap
@ -47,37 +49,32 @@ class LoadPlugin():
class Manager(): class Manager():
plugins_kind = [] plugins_kind = []
_schema_props_default = None
_schema_props_default = {}
_schema_props_default = {}
@classmethod @classmethod
def get_schema(cls, plugins_db): def get_schema(cls, plugins_db):
pprint (cls.plugins_kind)
ret = {}
ret3 = []
# Properties
ret3 = {}
for kind in cls.plugins_kind: for kind in cls.plugins_kind:
ret[kind] = {} # ret[kind] = {}
plugin_kind = getattr(plugins_db, kind) plugin_kind = getattr(plugins_db, kind)
for plugin_name in [i for i in dir(plugin_kind) if not i.startswith('_')]: for plugin_name in [i for i in dir(plugin_kind) if not i.startswith('_')]:
plugin = getattr(plugin_kind, plugin_name) plugin = getattr(plugin_kind, plugin_name)
print (plugin.Plugin)
#pprint (dir(plugin))
plugin_cls = getattr(plugin, 'Plugin', None) plugin_cls = getattr(plugin, 'Plugin', None)
if plugin_cls: if plugin_cls:
schema_props = getattr(plugin_cls, '_schema_props_new', 'MISSING ITEM') schema_props = getattr(plugin_cls, '_schema_props_new', 'MISSING ITEM')
if schema_props: if schema_props:
ret[kind][plugin_name] = schema_props # ret[kind][plugin_name] = schema_props
print (plugin_name) ret3.update( schema_props )
ret3.append( schema_props ) ret3.update( cls._schema_props_new )
ret3.append( cls._schema_props_new )
# Injection
ret1 = cls._schema_props_default ret1 = cls._schema_props_default
ret1["$def"]["items"] = ret3 position = cls._props_position
dpath.util.set(ret1, position, ret3)
return ret1 return ret1
@ -95,25 +92,30 @@ class BackendsManager(Manager):
"default": 'UNSET', "default": 'UNSET',
"optional": False, "optional": False,
}, },
#### INSERT HERE SUBSCHEMA !!!!!
} }
_props_position = 'oneOf/0/properties'
_schema_props_default = { _schema_props_default = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"default": "", "default": "",
"$def": { # This does not work :(
"items": {}, #"$def": {
}, # "props": {},
# },
"oneOf": [ "oneOf": [
{
"type": "string",
"default": "BLAAAAHHH"
},
{ {
"type": "object", "type": "object",
"additionalProperties": True, "additionalProperties": True,
"default": {}, "default": {},
"properties": { "$ref": "#/$defs/name" }, "title": "object",
"properties": {},
"description": "Object to configure a bacjend item",
},
{
"type": "string",
"default": "BLAAAAHHH",
"title": "string",
"description": "Enter a simple string configuration value for default engine",
}, },
] ]
} }
@ -251,8 +253,9 @@ class RulesManager(Manager):
}, },
} }
_props_position = 'oneOf/1/properties'
_schema_props_default = { _schema_props_default = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"default": "", "default": "",
"$def": { "$def": {
"items": {}, "items": {},
@ -272,7 +275,7 @@ class RulesManager(Manager):
} }
OLD_rule_schema = { OLD_rule_schema = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"type": "object", "type": "object",
"additionalProperties": False, "additionalProperties": False,
"properties": { "properties": {

View File

@ -14,38 +14,107 @@ import textwrap
class Plugin(PluginBackendClass): class Plugin(PluginBackendClass):
_plugin_name = "loop" _plugin_name = "loop"
_plugin_help = """
This module helps to loop over a backend
""",
_schema_props_new = { _schema_props_new = {
"loop": { "loop": {
"description": _plugin_help,
"default": None, "default": None,
"optional": True, "optional": True,
"examples": [
{
"value": "site/{{ loop_env }}/config/{{ os }}",
"loop": {
"var": "loop_env",
"data": [
"dev",
"preprod",
"prod",
],
},
"comment": "The module will loop three time over the value, and the variable `loop_env` will consecutely have `dev`, `preprod` and `prod` as value",
},
{
"value": "site/{{ loop_env2 }}/config/{{ os }}",
"loop": {
"var": "loop_env2",
"data": "my_scope_var",
},
"comment": "Like the previous example, but it will fetch the list from any scope variables",
},
{
"loop": None,
"comment": "Disable this module, no loop will operate",
},
# "loop": {
# "var": "my_var",
# },
# },
# "loop": {
# "var": "my_var",
# },
# "example": "",
# },
# "loop": {
# "var": "my_var",
# },
# "example": "",
# },
],
"oneOf": [ "oneOf": [
{
"type": "null",
},
{
"type": "string",
},
{ {
"type": "object", "type": "object",
"additionalProperties": True, "additionalProperties": False,
"default": {}, "default": {},
"title": "Complete config",
"description": "",
"properties": { "properties": {
"data": { "data": {
"default": None, "default": None,
"optional": False, "optional": False,
"title": "Module configuration",
"description": "Data list used for iterations. It only accept lists as type. It disable the module if set to `null`.",
"anyOf":[ "anyOf":[
{"type": "null"}, {
{"type": "string"}, "type": "null",
{"type": "array"}, "title": "Disable Module",
"description": "Disable the module",
},
{
"type": "string",
"title": "Scope variable",
"description": "Will look the value of the loop list from the scope. TOFIX: What if variablle does not exists?",
},
{
"type": "array",
"title": "Hardcoded list",
"description": "Simply enter the list of value to be iterated to.",
},
] ]
}, },
"var": { "var": {
"type": "string", "type": "string",
"default": "loop_item", "default": "loop_item",
"optional": True, "optional": True,
"title": "Module configuration",
"description": "Name of the variable to be used in templating language",
}, },
}, },
}, },
{
"type": "string",
"title": "Short config",
"description": "If set to string, it will define the name of the variable to lookup into the scope.",
},
{
"type": "null",
"title": "Disable",
"description": "If set to null, it disable the module",
},
] ]
} }
} }

View File

@ -149,7 +149,7 @@ class PluginEngineClass(PluginClass):
schema = getattr(self, key) schema = getattr(self, key)
props.update(schema) props.update(schema)
self.schema = { self.schema = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"type": "object", "type": "object",
"additionalProperties": True, "additionalProperties": True,
"properties": props, "properties": props,

View File

@ -54,7 +54,7 @@ class Plugin(PluginStrategyClass):
} }
default_merge_schema = { default_merge_schema = {
"$schema": 'http://json-schema.org/draft-04/schema#', "$schema": 'http://json-schema.org/draft-07/schema#',
"oneOf": [ "oneOf": [
{ {
"type": "array", "type": "array",

View File

@ -12,6 +12,7 @@ jsonmerge = "^1.8.0"
anyconfig = "^0.12.0" anyconfig = "^0.12.0"
python-box = "^5.4.1" python-box = "^5.4.1"
prettytable = "^3.0.0" prettytable = "^3.0.0"
dpath = "^2.0.5"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
json-schema-for-humans = "^0.40" json-schema-for-humans = "^0.40"