From b9f71ad8a238ed93c7e515cc9602d11700f183ae Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Wed, 10 Oct 2007 17:31:08 -0300 Subject: [PATCH 1/1] Update services to use SubHandler and DictSubHandler. --- pymin/services/dhcp/__init__.py | 100 ++++---------------------- pymin/services/dns/__init__.py | 24 ++----- pymin/services/firewall/__init__.py | 8 +-- pymin/services/ip/__init__.py | 14 ++-- pymin/services/proxy/__init__.py | 106 +++++++--------------------- 5 files changed, 50 insertions(+), 202 deletions(-) diff --git a/pymin/services/dhcp/__init__.py b/pymin/services/dhcp/__init__.py index 927f801..dd2fa9d 100644 --- a/pymin/services/dhcp/__init__.py +++ b/pymin/services/dhcp/__init__.py @@ -5,10 +5,10 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler + TransactionalHandler, ParametersHandler, \ + DictSubHandler -__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError', - 'HostNotFoundError') +__ALL__ = ('DhcpHandler', 'Error') class Error(HandlerError): r""" @@ -21,41 +21,6 @@ class Error(HandlerError): """ pass -class HostError(Error, KeyError): - r""" - HostError(hostname) -> HostError instance - - This is the base exception for all host related errors. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host error: "%s"' % hostname - -class HostAlreadyExistsError(HostError): - r""" - HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance - - This exception is raised when trying to add a hostname that already exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host already exists: "%s"' % hostname - -class HostNotFoundError(HostError): - r""" - HostNotFoundError(hostname) -> HostNotFoundError instance - - This exception is raised when trying to operate on a hostname that doesn't - exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host not found: "%s"' % hostname - - class Host(Sequence): r"""Host(name, ip, mac) -> Host instance :: Class representing a host. @@ -74,62 +39,23 @@ class Host(Sequence): r"Return a tuple representing the host." return (self.name, self.ip, self.mac) -class HostHandler(Handler): - r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts. + def update(self, ip=None, mac=None): + if ip is not None: + self.ip = ip + if mac is not None: + self.mac = mac + +class HostHandler(DictSubHandler): + r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts. This class is a helper for DhcpHandler to do all the work related to hosts administration. - - hosts - A dictionary with string keys (hostnames) and Host instances values. """ handler_help = u"Manage DHCP hosts" - def __init__(self, parent): - r"Initialize HostHandler object, see class documentation for details." - self.parent = parent - - @handler(u'Add a new host') - def add(self, name, ip, mac): - r"add(name, ip, mac) -> None :: Add a host to the hosts list." - if name in self.parent.hosts: - raise HostAlreadyExistsError(name) - self.parent.hosts[name] = Host(name, ip, mac) - - @handler(u'Update a host') - def update(self, name, ip=None, mac=None): - r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list." - if not name in self.parent.hosts: - raise HostNotFoundError(name) - if ip is not None: - self.parent.hosts[name].ip = ip - if mac is not None: - self.parent.hosts[name].mac = mac - - @handler(u'Delete a host') - def delete(self, name): - r"delete(name) -> None :: Delete a host of the hosts list." - if not name in self.parent.hosts: - raise HostNotFoundError(name) - del self.parent.hosts[name] - - @handler(u'Get information about a host') - def get(self, name): - r"get(name) -> Host :: List all the information of a host." - if not name in self.parent.hosts: - raise HostNotFoundError(name) - return self.parent.hosts[name] - - @handler(u'List hosts') - def list(self): - r"list() -> tuple :: List all the hostnames." - return self.parent.hosts.keys() - - @handler(u'Get information about all hosts') - def show(self): - r"show() -> list of Hosts :: List all the complete hosts information." - return self.parent.hosts.values() - + _dict_subhandler_attr = 'hosts' + _dict_subhandler_class = Host class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, ParametersHandler): diff --git a/pymin/services/dns/__init__.py b/pymin/services/dns/__init__.py index 3736a25..13aaa08 100644 --- a/pymin/services/dns/__init__.py +++ b/pymin/services/dns/__init__.py @@ -8,7 +8,8 @@ from new import instancemethod from pymin.seqtools import Sequence from pymin.dispatcher import handler, HandlerError, Handler from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler, call + TransactionalHandler, ParametersHandler, \ + SubHandler, call __ALL__ = ('DnsHandler', 'Error', 'ZoneError', 'ZoneNotFoundError', 'ZoneAlreadyExistsError', @@ -177,13 +178,10 @@ class Host(Sequence): def as_tuple(self): return (self.name, self.ip) -class HostHandler(Handler): +class HostHandler(SubHandler): handler_help = u"Manage DNS hosts" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds a host to a zone') def add(self, name, hostname, ip): if not name in self.parent.zones: @@ -229,13 +227,10 @@ class MailExchange(Sequence): def as_tuple(self): return (self.mx, self.prio) -class MailExchangeHandler(Handler): +class MailExchangeHandler(SubHandler): handler_help = u"Manage DNS mail exchangers (MX)" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds a mail exchange to a zone') def add(self, zonename, mx, prio): if not zonename in self.parent.zones: @@ -280,13 +275,10 @@ class NameServer(Sequence): def as_tuple(self): return (self.name) -class NameServerHandler(Handler): +class NameServerHandler(SubHandler): handler_help = u"Manage DNS name servers (NS)" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds a name server to a zone') def add(self, zone, ns): if not zone in self.parent.zones: @@ -327,7 +319,7 @@ class Zone(Sequence): def as_tuple(self): return (self.name, self.hosts, self.mxs, self.nss) -class ZoneHandler(Handler): +class ZoneHandler(SubHandler): r"""ZoneHandler(parent.zones) -> ZoneHandler instance :: Handle a list of zones. This class is a helper for DnsHandler to do all the work related to zone @@ -338,9 +330,6 @@ class ZoneHandler(Handler): handler_help = u"Manage DNS zones" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds a zone') def add(self, name): if name in self.parent.zones: @@ -352,7 +341,6 @@ class ZoneHandler(Handler): self.parent.zones[name].mod = True self.parent.zones[name].new = True - @handler(u'Deletes a zone') def delete(self, name): r"delete(name) -> None :: Delete a zone from the zone list." diff --git a/pymin/services/firewall/__init__.py b/pymin/services/firewall/__init__.py index 497087d..d3ae1ed 100644 --- a/pymin/services/firewall/__init__.py +++ b/pymin/services/firewall/__init__.py @@ -8,7 +8,7 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, ServiceHandler, \ - TransactionalHandler + TransactionalHandler, SubHandler __ALL__ = ('FirewallHandler', 'Error', 'RuleError', 'RuleAlreadyExistsError', 'RuleNotFoundError') @@ -114,7 +114,7 @@ class Rule(Sequence): return (self.chain, self.target, self.src, self.dst, self.protocol, self.src_port, self.dst_port) -class RuleHandler(Handler): +class RuleHandler(SubHandler): r"""RuleHandler(rules) -> RuleHandler instance :: Handle a list of rules. This class is a helper for FirewallHandler to do all the work related to rules @@ -125,10 +125,6 @@ class RuleHandler(Handler): handler_help = u"Manage firewall rules" - def __init__(self, parent): - r"Initialize the object, see class documentation for details." - self.parent = parent - @handler(u'Add a new rule') def add(self, *args, **kwargs): r"add(rule) -> None :: Add a rule to the rules list (see Rule doc)." diff --git a/pymin/services/ip/__init__.py b/pymin/services/ip/__init__.py index db29c77..f967ae2 100644 --- a/pymin/services/ip/__init__.py +++ b/pymin/services/ip/__init__.py @@ -6,7 +6,7 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import handler, HandlerError, Handler from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, call + TransactionalHandler, SubHandler, call __ALL__ = ('IpHandler', 'Error','DeviceError', 'DeviceNotFoundError', 'RouteError', 'RouteNotFoundError', 'RouteAlreadyExistsError', @@ -81,13 +81,10 @@ class Route(Sequence): return 0 return cmp(id(self), id(other)) -class RouteHandler(Handler): +class RouteHandler(SubHandler): handler_help = u"Manage IP routes" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds a route to a device') def add(self, device, net_addr, prefix, gateway): if not device in self.parent.devices: @@ -143,13 +140,10 @@ class Address(Sequence): def as_tuple(self): return (self.ip, self.prefix, self.broadcast) -class AddressHandler(Handler): +class AddressHandler(SubHandler): handler_help = u"Manage IP addresses" - def __init__(self, parent): - self.parent = parent - @handler(u'Adds an address to a device') def add(self, device, ip, prefix, broadcast='+'): if not device in self.parent.devices: @@ -200,7 +194,7 @@ class Device(Sequence): def as_tuple(self): return (self.name, self.mac) -class DeviceHandler(Handler): +class DeviceHandler(SubHandler): handler_help = u"Manage network devices" diff --git a/pymin/services/proxy/__init__.py b/pymin/services/proxy/__init__.py index 2aa521d..0577938 100644 --- a/pymin/services/proxy/__init__.py +++ b/pymin/services/proxy/__init__.py @@ -5,12 +5,12 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler + TransactionalHandler, ParametersHandler, \ + DictSubHandler import crypt -__ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError', - 'HostNotFoundError') +__ALL__ = ('ProxyHandler', 'Error') class Error(HandlerError): r""" @@ -23,93 +23,37 @@ class Error(HandlerError): """ pass -class HostError(Error, KeyError): - r""" - HostError(hostname) -> HostError instance - - This is the base exception for all host related errors. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host error: "%s"' % hostname - -class HostAlreadyExistsError(HostError): - r""" - HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance - - This exception is raised when trying to add a hostname that already exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host already exists: "%s"' % hostname - -class HostNotFoundError(HostError): - r""" - HostNotFoundError(hostname) -> HostNotFoundError instance - - This exception is raised when trying to operate on a hostname that doesn't - exists. - """ +class Host(Sequence): + def __init__(self,ip): + self.ip = ip + def as_tuple(self): + return (self.ip,) - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = u'Host not found: "%s"' % hostname +# TODO convert to a SetSubHandler +class HostHandler(DictSubHandler): -class Host(Sequence): + handler_help = u"Manage proxy hosts" - def __init__(self,ip): - self.ip = ip + _dict_subhandler_attr = 'hosts' + _dict_subhandler_class = Host +class User(Sequence): + def __init__(self, name, password): + self.name = name + self.password = crypt.crypt(password,'BA') def as_tuple(self): - return (self.ip) + return (self.name, self.password) + def update(self, password=None): + if password is not None: + self.password = crypt.crypt(password,'BA') -class HostHandler(Handler): +class UserHandler(DictSubHandler): - handler_help = u"Manage proxy hosts" + handler_help = u"Manage proxy users" - def __init__(self, parent): - self.parent = parent - - @handler(u'Adds a host') - def add(self, ip): - if ip in self.parent.hosts: - raise HostAlreadyExistsError(ip) - self.parent.hosts[ip] = Host(ip) - - @handler(u'Deletes a host') - def delete(self, ip): - if not ip in self.parent.hosts: - raise HostNotFoundError(ip) - del self.parent.hosts[ip] - - @handler(u'Shows all hosts') - def list(self): - return self.parent.hosts.keys() - - @handler(u'Get information about all hosts') - def show(self): - return self.parent.hosts.items() - - -class UserHandler(Handler): - - def __init__(self, parent): - self.parent = parent - - @handler('Adds a user') - def add(self, user, password): - if user in self.parent.users: - raise UserAlreadyExistsError(user) - self.parent.users[user] = crypt.crypt(password,'BA') - - @handler('Deletes a user') - def delete(self, user): - if not user in self.parent.users: - raise UserNotFound(user) - del self.parent.users[user] + _dict_subhandler_attr = 'users' + _dict_subhandler_class = User class ProxyHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, ParametersHandler): -- 2.43.0