conf_dhcp.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Apr 2009 17:47:43 +0300
changeset 0 257003279747
permissions -rw-r--r--
initial code
"""
    Configuration file output for the ISC DHCP server
"""

import conf

import itertools

class ConfDHCP (conf.File) :
    def __init__ (self, name="dhcpd.conf", path="/etc/dhcp3/dhcpd.conf") :
        """
            Initialize the dhcpd config file, but don't open it yet

            @see conf.ConfFile.__init__
        """
        
        super(ConfDHCP, self).__init__(name, path)

class Statement (conf.Object) :
    """
      A statement is a single line in the config file
    """

    def __init__ (self, name, *args) :
        """
            The statement will be formatted like this:
                <name> [ <arg> [ ... ] ] ";"
        """

        self.name = name
        self.args = args
    
    def _fmt_arg (self, arg) :
        """
            Formats a arg for use in output, the following types are supported:

                list/tuple/iter:    results in a comma-and-space separated list of formatted values
                unicode:            results in an encoded str
                str:                results in the string itself, quoted if needed
                other:              attempt to convert to a str, and then format that
        """
        
        # format lists specially
        # XXX: iterators?
        if isinstance(arg, (list, tuple)) :
            # recurse as a comma-and-space separated list
            return ', '.join(self._fmt_arg(a) for a in arg)

        elif isinstance(arg, Literal) :
            # use what it specifies
            return arg.fmt_arg()

        elif isinstance(arg, unicode) :
            # recurse with the str version
            # XXX: what encoding to use?
            return self._fmt_arg(arg.encode('utf8'))

        elif isinstance(arg, str) :
            # XXX: quoting
            return arg
        
        else :
            # try and use it as a string
            return self._fmt_arg(str(arg))
    
    def _fmt_data (self) :
        """
            Formats the statement name/params as a single line
        """

        return "%s%s" % (self.name, (' ' + ' '.join(self._fmt_arg(a) for a in self.args)) if self.args else '')

class Literal (Statement) :
    """
        A literal is something that goes into the config file as-is, with no formatting or escaping applied.
    """

    def __init__ (self, literal) :
        self.literal = literal
    
    def fmt_arg (self) :
        return self.literal

    def fmt_lines (self) :
        yield self.literal

class Parameter (Statement) :
    """
        A parameter is a single statement that configures the behaviour of something.

        Parameters have a name, and optionally, a number of arguments, and are formatted as statements terminated with
        a semicolon.
    """
    
    def fmt_lines (self) :
        """
            Yields a single ;-terminated line
        """

        yield "%s;" % self._fmt_data()

class Declaration (Statement) :
    """
        A declaration begins like a statement (with name and args), but then contains a block of any number of
        parameters followed by any number of nested declarations.

        <name> [ <args> [ ... ] ] {
            [ <parameters> ]
            [ <declarations> ]
        }
        
    """

    def __init__ (self, name, args=[], params=[], decls=[]) :
        """
            The name/args will be formatted as in Statement, but params should be an iterable of Parameters, and decls
            an iterable of Declarations.
        """
        
        # init the statement bit
        Statement.__init__(self, name, *args)

        # store the iterables
        self.params = params
        self.decls = decls

    def fmt_lines (self) :
        """
            Yields a header line, a series of indented body lines, and the footer line
        """
        
        # the header to open the block
        yield "%s {" % self._fmt_data()
        
        # then output each content line
        for stmt in itertools.chain(self.params, self.decls) :
            # ..indented
            for line in stmt.fmt_lines() :
                yield "\t%s" % line
        
        # and then close the block
        yield "}"

class SharedNetwork (Declaration) :
    """
        A shared-network declaration is used to define a set of subnets that share the same physical network,
        optionally with some shared params.

        shared-network <name> {
            [ parameters ]
            [ declarations ]
        }
    """

    def __init__ (self, name, params=[], decls=[]) :
        """
            @param name the name of the shared-subnet
            @param params optional parameters
            @param decls the iterable of subnets or other declarations in the shared network
        """

        super(SharedNetwork, self).__init__("shared-network", [name], params, decls)

class Subnet (Declaration) :
    """
        A subnet is used to provide the information about a subnet required to identify whether or not an IP address is
        on that subnet, and may also be used to specify parameters/declarations for that subnet.
        
        subnet <subnet-number> netmask <netmask> {
            [ parameters ]
            [ declarations ]
        }
    """

    def __init__ (self, network, params=[], decls=[]) :
        """
            @param network the addr.Network for the subnet
            @param params optional parameters
            @param decls optional decls, e.g. subnets
        """

        super(Subnet, self).__init__("subnet", [network.net(), "netmask", network.netmask()], params, decls)

class Group (Declaration) :
    """
        A group is simply used to apply a set of parameters to a set of declarations.

        group {
            [ parameters ]
            [ declarations ]
        }
    """

    def __init__ (self, params=[], decls=[]) :
        super(Group, self).__init__("group", [], params, decls)


class Host (Declaration) :
    """
        A host is used to match a request against specific host, and then apply settings for that host.

        The "hostname" is the DHCP name to identify the host. 

        If no dhcp-client-identifier option is specified in the parameters, then the host is matched using the
        "hardware" parameter.

        host <hostname> {
            [ parameters ]
            [ declarations ]
        }
    """

    def __init__ (self, hostname, params=[], decls=[]) :
        super(Host, self).__init__("host", [hostname], params, decls)

class Option (Parameter) :
    """
        A generic 'option' parameter for a dhcpd.conf file
    """

    def __init__ (self, name, *args) :
        """
            Formatted as a Satement with a name of "option <name>".
        """

        super(Option, self).__init__("option %s" % name, *args)