<commit_before>"""Classes for writing BIND configuration files."""

__version__ = '$Revision: 1.11 $'
# $Source: /home/blb/pybind/RCS/bindconf.py,v $

import ipaddr

import iscconf

class BINDConf(iscconf.ISCConf):

    """Class for BIND configuration."""

    def __init__(self):
        super(BINDConf, self).__init__()

    def add_acl(self, acl):
        if type(acl) is not ACL:
            raise TypeError('element is not an ACL')
        self.add_element(acl)

    def add_view(self, view):
        if type(view) is not View:
            raise TypeError('element is not a View')
        self.add_element(view)

class ACL(iscconf.Statement):

    """Class for BIND acl statement."""

    def __init__(self, acl_name, *addresses):
        super(ACL, self).__init__('acl', ('"%s"' % acl_name,), stanza=addresses)

class View(iscconf.Clause):

    """Class for BIND view clause."""

    def __init__(self, view_name, class_='IN'):
        super(View, self).__init__('view', '"%s"' % view_name, class_)

    def add_zone(self, zone):
        if type(zone) is not Zone:
            raise TypeError('element is not a Zone')
        self.add_element(zone)

    def set_match_destinations(self, *addresses):
        self.remove_elements('match-destinations')
        stmt = iscconf.Statement('match-destinations', stanza=addresses)
        self.add_element(stmt)

    def set_notify_source(self, ip, port=None):
        if ipaddr.IPAddress(ip).version == 4:
            label = 'notify-source'
        else:
            label = 'notify-source-v6'
        self.remove_elements(label)
        if port:
            value = (ip, 'port', port)
        else:
            value = (ip,)
        stmt = iscconf.Statement(label, value)
        self.add_element(stmt)

    def set_transfer_source(self, ip, port=None):
        if ipaddr.IPAddress(ip).version == 4:
            label = 'transfer-source'
        else:
            label = 'transfer-source-v6'
        self.remove_elements(label)
        if port:
            value = (ip, 'port', port)
        else:
            value = (ip,)
        stmt = iscconf.Statement(label, value)
        self.add_element(stmt)

class Zone(iscconf.Clause):

    """Class for BIND zone clause."""

    def __init__(self, zone_name, type_=None, file_=None, class_='IN'):
        super(Zone, self).__init__('zone', '"%s"' % zone_name, class_)
        if type_ is not None:
            self.set_type(type_)
        if file_ is not None:
            self.set_file(file_)

    def set_type(self, type_):
        self.remove_elements('type')
        stmt = iscconf.Statement('type', ('%s' % type_,))
        self.add_element(stmt)

    def set_file(self, file_):
        self.remove_elements('file')
        stmt = iscconf.Statement('file', ('"%s"' % file_,))
        self.add_element(stmt)

    def set_allow_update(self, *addresses):
        self.remove_elements('allow-update')
        stmt = iscconf.Statement('allow-update', stanza=addresses)
        self.add_element(stmt)

    def add_master(self, ip, port=None, key=None):
        # supports only IP addresses and not masters lists
        master = ip
        if port:
            master += ' port %s' % port
        if key:
            master += ' key %s' % key
        try:
            # get existing masters statement
            stmt = self.get_elements('masters')[0]
        except IndexError:
            # make new masters statement
            stmt = iscconf.Statement('masters')
            self.add_element(stmt)
        finally:
            stmt.stanza.append(master)

def run_tests():
    c = BINDConf()
    a = ACL('example_acl', '1.1.1.1', '2.2.2.2')
    c.add_acl(a)
    v = View('example_view')
    c.add_view(v)
    z = Zone('example.com', 'master', 'example.com.hosts')
    z.set_allow_update('none')
    z.add_master('1.2.3.4')
    z.add_master('1.2.3.5', 5353)
    v.add_zone(z)
    v.set_match_destinations('1.1.1.1', '2.2.2.2')
    v.set_notify_source('3.3.3.3')

    # v.add_element(Statement('notify-source', '192.168.1.1'))

    # z = Clause('zone', 'example.com')
    # z.add_element(Statement('also-notify', ('192.168.1.2', '192.168.1.3')))
    # z.add_element(Statement('type', 'master'))
    # z.add_element(Statement('file', '"example.com.hosts"'))
    # s = Clause('server', '10.1.2.3')
    # v.add_element(s)
    # v.add_element(z)

    # c.add_element(v)

    c.write_file('named.conf')

if __name__ == '__main__':
    run_tests()
<commit_msg>remove gratuitous tests for type_ and file_ in Zone.__init__()<commit_after>"""Classes for writing BIND configuration files."""

__version__ = '$Revision: 1.11 $'
# $Source: /home/blb/pybind/RCS/bindconf.py,v $

import ipaddr

import iscconf

class BINDConf(iscconf.ISCConf):

    """Class for BIND configuration."""

    def __init__(self):
        super(BINDConf, self).__init__()

    def add_acl(self, acl):
        if type(acl) is not ACL:
            raise TypeError('element is not an ACL')
        self.add_element(acl)

    def add_view(self, view):
        if type(view) is not View:
            raise TypeError('element is not a View')
        self.add_element(view)

class ACL(iscconf.Statement):

    """Class for BIND acl statement."""

    def __init__(self, acl_name, *addresses):
        super(ACL, self).__init__('acl', ('"%s"' % acl_name,), stanza=addresses)

class View(iscconf.Clause):

    """Class for BIND view clause."""

    def __init__(self, view_name, class_='IN'):
        super(View, self).__init__('view', '"%s"' % view_name, class_)

    def add_zone(self, zone):
        if type(zone) is not Zone:
            raise TypeError('element is not a Zone')
        self.add_element(zone)

    def set_match_destinations(self, *addresses):
        self.remove_elements('match-destinations')
        stmt = iscconf.Statement('match-destinations', stanza=addresses)
        self.add_element(stmt)

    def set_notify_source(self, ip, port=None):
        if ipaddr.IPAddress(ip).version == 4:
            label = 'notify-source'
        else:
            label = 'notify-source-v6'
        self.remove_elements(label)
        if port:
            value = (ip, 'port', port)
        else:
            value = (ip,)
        stmt = iscconf.Statement(label, value)
        self.add_element(stmt)

    def set_transfer_source(self, ip, port=None):
        if ipaddr.IPAddress(ip).version == 4:
            label = 'transfer-source'
        else:
            label = 'transfer-source-v6'
        self.remove_elements(label)
        if port:
            value = (ip, 'port', port)
        else:
            value = (ip,)
        stmt = iscconf.Statement(label, value)
        self.add_element(stmt)

class Zone(iscconf.Clause):

    """Class for BIND zone clause."""

    def __init__(self, zone_name, type_=None, file_=None, class_='IN'):
        super(Zone, self).__init__('zone', '"%s"' % zone_name, class_)
        self.set_type(type_)
        self.set_file(file_)

    def set_type(self, type_):
        self.remove_elements('type')
        stmt = iscconf.Statement('type', ('%s' % type_,))
        self.add_element(stmt)

    def set_file(self, file_):
        self.remove_elements('file')
        stmt = iscconf.Statement('file', ('"%s"' % file_,))
        self.add_element(stmt)

    def set_allow_update(self, *addresses):
        self.remove_elements('allow-update')
        stmt = iscconf.Statement('allow-update', stanza=addresses)
        self.add_element(stmt)

    def add_master(self, ip, port=None, key=None):
        # supports only IP addresses and not masters lists
        master = ip
        if port:
            master += ' port %s' % port
        if key:
            master += ' key %s' % key
        try:
            # get existing masters statement
            stmt = self.get_elements('masters')[0]
        except IndexError:
            # make new masters statement
            stmt = iscconf.Statement('masters')
            self.add_element(stmt)
        finally:
            stmt.stanza.append(master)

def run_tests():
    c = BINDConf()
    a = ACL('example_acl', '1.1.1.1', '2.2.2.2')
    c.add_acl(a)
    v = View('example_view')
    c.add_view(v)
    z = Zone('example.com', 'master', 'example.com.hosts')
    z.set_allow_update('none')
    z.add_master('1.2.3.4')
    z.add_master('1.2.3.5', 5353)
    v.add_zone(z)
    v.set_match_destinations('1.1.1.1', '2.2.2.2')
    v.set_notify_source('3.3.3.3')

    # v.add_element(Statement('notify-source', '192.168.1.1'))

    # z = Clause('zone', 'example.com')
    # z.add_element(Statement('also-notify', ('192.168.1.2', '192.168.1.3')))
    # z.add_element(Statement('type', 'master'))
    # z.add_element(Statement('file', '"example.com.hosts"'))
    # s = Clause('server', '10.1.2.3')
    # v.add_element(s)
    # v.add_element(z)

    # c.add_element(v)

    c.write_file('named.conf')

if __name__ == '__main__':
    run_tests()

{'fraction_non_alphanumeric': 0.14838564517943525, 'fraction_numerical': 0.015524805939925751, 'mean_word_length': 2.4780907668231613, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 22, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2516844', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2992, 'n_words': 618}