Commit e0214482 authored by Ales Mrazek's avatar Ales Mrazek

new generator, autodoc/doctest correction, examples/tests correction,...

new generator, autodoc/doctest correction, examples/tests correction, converter ignore comments Closes #2
parent 0c546c5a
Pipeline #41362 passed with stages
in 51 seconds
......@@ -57,30 +57,36 @@ Library Modules
Generator module
----------------
.. automodule:: generator
:members:
:members: Generator
.. autoclass:: Generator
:members:
Import ``gen_unbound`` and ``gen_kresd`` functions to generate Unbound and Knot Resolver configuration strings from valid python dictionary loaded from Json. Returns string.
Import ``Generator`` class which contains ``generate_unbound`` and ``generate_kresd`` functions to generate Unbound and Knot Resolver configuration strings from valid python dictionary loaded from Json. Returns string.
.. doctest::
>>> from resolvers_yang.generator import gen_unbound, gen_kresd
>>> from resolvers_yang.generator import Generator
>>> generator = Generator()
Call generator functions to generate `unbound.conf` and `kresd.conf`.
.. doctest::
>>> unbound_conf = gen_unbound(json_data)
>>> unbound_conf = generator.generate_unbound(json_data)
>>> print(unbound_conf) # doctest: +NORMALIZE_WHITESPACE
server:
interface: 127.0.0.1@53
interface: ::1@53
interface: 198.51.100.1@8853
outgoing-interface: 2001:db8:0:2::1
do-ip6 = yes
do-ip4 = yes
do-ip6: yes
do-ip4: yes
edns-buffer-size: 4096
username: "jetconf"
root-hints: "/etc/resolver/root.hints"
......@@ -106,9 +112,10 @@ Call generator functions to generate `unbound.conf` and `kresd.conf`.
stub-addr: "198.51.100.1@53"
<BLANKLINE>
.. doctest::
>>> kresd_conf = gen_kresd(json_data)
>>> kresd_conf = generator.generate_kresd(json_data)
>>> print(kresd_conf) # doctest: +NORMALIZE_WHITESPACE
net = {'127.0.0.1@53', '::1@53', '198.51.100.1@8853'}
net.outgoing_v6('2001:db8:0:2::1')
......@@ -118,9 +125,9 @@ Call generator functions to generate `unbound.conf` and `kresd.conf`.
user('jetconf','wheel')
STUB('192.0.2.1@53', '198.51.100.1@53')
modules.load('hints')
hints.set("loopback 127.0.0.1")
hints.set("localhost 127.0.0.1")
hints.set("localhost ::1")
hints.set("loopback 127.0.0.1")
hints.add_hosts(/etc/hosts)
hints.root({['a.root-servers.net'] = {'198.41.0.4', '2001:503:ba3e::2:30'}})
hints.root_file('/etc/resolver/root.hints')
......@@ -153,34 +160,38 @@ Call generator functions to generate `unbound.conf` and `kresd.conf`.
>>> with open("unbound.conf", "w") as unb_file:
... unb_file.write(unbound_conf)
689
687
Converter module
----------------
.. automodule:: converter
:members:
:members: Converter
.. autoclass:: Converter
:members:
Import ``from_unbound`` function for converting Unbound configuration string to python dictionary, which can be validate against `resolvers-yang` data model and save as Json file.
Import ``Converter`` class which contains ``from_unbound`` function for converting Unbound configuration string to python dictionary, which can be validate against `resolvers-yang` data model and save as Json file.
.. doctest::
>>> from resolvers_yang.converter import from_unbound
>>> from resolvers_yang.converter import Converter
Input parameter of ``from_unbound`` function is a list of string lines loaded from ``unbound.conf`` file. Returns python dictionary.
.. doctest::
>>> with open("unbound.conf", "r") as unb_file:
... unbconf_data = unb_file.readlines()
... unbconf_data = unb_file.read()
.. doctest::
>>> json_data = from_unbound(unbconf_data)
>>> converter = Converter()
>>> json_data = converter.from_unbound(unbconf_data)
>>> json_data
{'cznic-resolver-common:dns-resolver': {'server': {'user-name': 'jetconf'}, 'network': {'listen-interfaces': [{'name': 'interface0', 'ip-address': '127.0.0.1', 'port': 53}, {'name': 'interface1', 'ip-address': '::1', 'port': 53}, {'name': 'interface2', 'ip-address': '198.51.100.1', 'port': 8853}], 'source-address': {'ipv6': '2001:db8:0:2::1'}, 'udp-payload-size': 4096, 'recursion-transport': {'l2-protocols': 'ipv4 ipv6'}}, 'resolver': {'options': {'glue-checking': 'strict', 'qname-minimisation': True, 'reorder-rrset': True, 'query-loopback': True}, 'stub-zones': [{'domain': 'stub.example.com', 'nameserver': '192.0.2.1', 'port': 53}, {'domain': 'stub.example.net', 'nameserver': '198.51.100.1', 'port': 53}], 'hints': {'root-zone-file': '/etc/resolver/root.hints'}}, 'logging': {'verbosity': 2}, 'dnssec': {'negative-trust-anchors': ['bad.example.com', 'worse.example.com'], 'trust-anchors': {'key-files': [{'domain': 'domain0', 'file': '/var/tmp/root.keys'}]}}, 'cache': {'max-size': 104857600, 'max-ttl': 172800, 'min-ttl': 0}, 'dns64': {'prefix': '64:ff9b::/96'}}}
{'cznic-resolver-common:dns-resolver': {'server': {'user-name': 'jetconf'}, 'network': {'listen-interfaces': [{'name': 'interface0', 'ip-address': '127.0.0.1', 'port': 53}, {'name': 'interface1', 'ip-address': '::1', 'port': 53}, {'name': 'interface2', 'ip-address': '198.51.100.1', 'port': 8853}], 'source-address': {'ipv6': '2001:db8:0:2::1'}, 'udp-payload-size': 4096, 'recursion-transport': {'l2-protocols': 'ipv4 ipv6'}}, 'resolver': {'stub-zones': [{'domain': 'stub.example.com', 'nameserver': '192.0.2.1', 'port': 53}, {'domain': 'stub.example.net', 'nameserver': '198.51.100.1', 'port': 53}], 'options': {'glue-checking': 'strict', 'qname-minimisation': True, 'reorder-rrset': True, 'query-loopback': True}, 'hints': {'root-zone-file': '/etc/resolver/root.hints'}}, 'logging': {'verbosity': 2}, 'dnssec': {'trust-anchors': {'key-files': [{'domain': 'domain0', 'file': '/var/tmp/root.keys'}]}, 'negative-trust-anchors': ['bad.example.com', 'worse.example.com']}, 'cache': {'max-size': 104857600, 'max-ttl': 172800, 'min-ttl': 0}, 'dns64': {'prefix': '64:ff9b::/96'}}}
``json_data`` variable is python dictionary, which can be easily validate against data model by ``Yangson`` library and save to Json-encoded file.
......@@ -197,4 +208,7 @@ Input parameter of ``from_unbound`` function is a list of string lines loaded fr
.. testcleanup::
os.remove("unbound-data.json")
os.remove("unbound.conf")
os.remove("kresd.conf")
os.chdir("../..")
import sys
from yangson.datamodel import DataModel
from json import load
from resolvers_yang.generator import gen_kresd, gen_unbound
from resolvers_yang.generator import Generator
json_path = sys.argv[1]
......@@ -36,8 +36,9 @@ mock_begin = mock_data.find("SCENARIO_BEGIN")
mock_data = mock_data[mock_begin:]
# generate configuration strings
unb_conf = gen_unbound(data_defaults)
kresd_conf = gen_kresd(data_defaults)
generator = Generator()
unb_conf = generator.generate_unbound(data_defaults)
kresd_conf = generator.generate_kresd(data_defaults)
# write kresd.conf
knot_file = open(kresd_path, "w+")
......
......@@ -2,7 +2,7 @@ import sys
from json import load
from yangson.datamodel import DataModel
from resolvers_yang.generator import gen_unbound, gen_kresd
from resolvers_yang.generator import Generator
json_path = sys.argv[1]
......@@ -30,8 +30,9 @@ data.validate()
data_defaults = data.add_defaults()
# generate configuration strings
unb_conf = gen_unbound(data_defaults)
kresd_conf = gen_kresd(data_defaults)
generator = Generator()
unb_conf = generator.generate_unbound(data_defaults)
kresd_conf = generator.generate_kresd(data_defaults)
# write Knot Resolver configuration
with open(kresd_path, "w") as knot_file:
......
......@@ -58,24 +58,26 @@ class Converter:
def _ignore_comments(line: str):
if "#" in line and ";" not in line:
value, comment = line.split("#")
nocomment_line, comment = line.split("#")
elif "#" not in line and ";" in line:
value, comment = line.split(";")
nocomment_line, comment = line.split(";")
elif "#" in line and ";" in line:
if line.find("#") < line.find(";"):
value, comment = line.split("#")
nocomment_line, comment = line.split("#")
else:
value, comment = line.split(";")
nocomment_line, comment = line.split(";")
else:
value = line
nocomment_line = line
return value
return nocomment_line
@staticmethod
def _parse_line(line: str):
# remove comments ofter # or ;
line = Converter._ignore_comments(line)
# separation of key and value
(key, separator, value) = line.partition(":")
return key.strip(), value.strip(' "')
......
This diff is collapsed.
......@@ -4,11 +4,10 @@ and again unbound-test.conf is generated using this JSON.
Then unbound.conf and unbound-test.conf are compared.
"""
import pytest
from json import load, dump
from yangson.datamodel import DataModel
from resolvers_yang.generator import gen_unbound, gen_kresd
from resolvers_yang.generator import Generator
from resolvers_yang.converter import Converter
examample_path = "../examples/example-data.json"
......@@ -42,7 +41,8 @@ data.validate()
data_defaults = data.add_defaults()
# generate configuration strings
unb_conf = gen_unbound(data_defaults)
generator1 = Generator()
unb_conf = generator1.generate_unbound(data_defaults)
# write Unbound configurationwith
with open(unb_path, "w") as unb_file:
......@@ -86,7 +86,8 @@ data.validate()
data_defaults = data.add_defaults()
# generate configuration strings
unbtest_conf = gen_unbound(data_defaults)
generator2 = Generator()
unbtest_conf = generator2.generate_unbound(data_defaults)
# write Unbound configuration
unbtest_file = open(unbtest_path, "w+")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment