Skip to content

Commit

Permalink
draft for loading external converters, references #112
Browse files Browse the repository at this point in the history
  • Loading branch information
edoput committed Jul 20, 2018
1 parent 0f8f098 commit 19375c4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions netjsonconfig/backends/base/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections import OrderedDict
from copy import deepcopy
from io import BytesIO
from pkg_resources import iter_entry_points

import six
from jsonschema import FormatChecker, validate
Expand All @@ -21,6 +22,7 @@ class BaseBackend(object):
schema = None
FILE_SECTION_DELIMITER = '# ---------- files ---------- #'
list_identifiers = []
_converters = []

def __init__(self, config=None, native=None, templates=None, context=None):
"""
Expand Down Expand Up @@ -49,6 +51,20 @@ def __init__(self, config=None, native=None, templates=None, context=None):
raise ValueError('Expecting either config or native argument to be '
'passed during the initialization of the backend')

@property
def converters(self):
converters = []
entry_key = 'netjsonconfig.{}.converter'.format(self.__class__.__name__)
for entry_point in iter_entry_points(entry_key):
try:
converters.append(entry_point.load())
except ImportError as e: # noqa
# TODO: some error handling here
continue

return self._converters + converters


def _load(self, config):
"""
Loads config from string or dict
Expand Down
2 changes: 1 addition & 1 deletion netjsonconfig/backends/openvpn/openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OpenVpn(BaseBackend):
OpenVPN 2.x Configuration Backend
"""
schema = schema
converters = [converters.OpenVpn]
_converters = [converters.OpenVpn]
parser = OpenVpnParser
renderer = OpenVpnRenderer
list_identifiers = ['name']
Expand Down
2 changes: 1 addition & 1 deletion netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OpenWrt(BaseBackend):
OpenWRT / LEDE Configuration Backend
"""
schema = schema
converters = [
_converters = [
converters.General,
converters.Ntp,
converters.Led,
Expand Down

0 comments on commit 19375c4

Please sign in to comment.