Hey Aarnav, On Mon, Nov 5, 2018 at 5:55 PM Aarnav Bos <[email protected]> wrote:
> As the issue https://github.com/openwisp/netjsonconfig/issues/112 goes, > It'd be ideal to be able to easily extend the backends. Since the use-case > of adding support for an OpenWRT package is very common, this > implementation primarily focuses on that. > > Adding a method to BaseBackend to allow insertions of converters and > schemas is an approach > > class BaseBackend(object): > ....... > ....... > def add_plugin(self, key, schema, converter): > schema.pop('$schema', None) > # add to backend's schema, this could have an option to raise an > error if the value is being overridden > self.schema['properties'][key] = schema > if getattr(self, 'converters'): > self.converters.append(converter) > else: > self.converters = [converter] > > Tracking the 'plugins' can become simpler too, by adding a an attribute to > the converter, if needed. > I think we are on the right path. Can we find a way to add one single Class which comprises converter, renderer and related schema? That may result in a very clean implementation and easy to work with. > Let's say I was to create a plugin for Coova-Chilli: > > coova_schema = { > "$schema": "http://json-schema.org/draft-04/schema#", > "type": "object", > "properties" : { > "disabled" : {"type" : "integer"}, > }, > } > > > class CoovaConverter(OpenWrtConverter): > def to_intermediate_loop(self, block, result, index=None): > if block: > block.update({ > '.type': 'chilli', > '.name': 'chilli' > }) > result['chilli'] = [self.sorted_dict(block)] > return result > ........ > ........ > > > o = OpenWrt({ > 'chilli': { > "disabled": 1, > } > }) > o.add_plugin(key='chilli', schema=coova_schema, converter=CoovaConverter) > o.render() > ''' > package chilli > > config chilli 'chilli' > option disabled '1' > ''' > This is incomplete code, obviously, but gives an idea of how the > implementation goes. > > Thoughts? > Great work. I think the schema you cite in this example could be simplified (or probably has to) because if you are adding a key to an existing schema you don't need to repeat the initial definitions, but this kind of stuff will become clearer as we move ahead with the implementation. Start pushing something on github so we can start playing with it. Federico -- You received this message because you are subscribed to the Google Groups "OpenWISP" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
