Skip to content

Instantly share code, notes, and snippets.

@la4gia
Created April 17, 2023 03:44
Show Gist options
  • Save la4gia/391970eabdd84fb345df819fad3e3dd9 to your computer and use it in GitHub Desktop.
Save la4gia/391970eabdd84fb345df819fad3e3dd9 to your computer and use it in GitHub Desktop.
Python Plugin Framework
from plugin_ingestor import PluginIngestor
class FirstPlugin(PluginIngestor):
ATTRIBUTES = {
'name': 'John Doe',
'age': 22,
'languages': ['English', 'Japanese'],
'clients': {'joe': 'somebody'}
}
def run(self):
self.execute()
if __name__ == "__main__":
go = FirstPlugin()
go.run()
from cerberus import Validator
import yaml
with open('plugin_schema.yml', 'r') as h:
schema = yaml.safe_load(h.read())
class PluginIngestor:
ATTRIBUTES = {}
ATTRIBUTE_SCHEMA = schema
def __init__(self):
self._validate_schema()
def _validate_schema(self):
v = Validator(self.ATTRIBUTE_SCHEMA, require_all=True)
v.validate(self.ATTRIBUTES)
if v.errors:
exit(f"Attributes does not match schema. Fix: {v.errors}")
def execute(self):
print(f'Created character:', self.ATTRIBUTES)
name:
type: string
age:
type: integer
languages:
type: [string, list]
clients:
type: dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment