class Sequence: pass
try:
- from dispatcher import handler, HandlerError
+ from dispatcher import handler, HandlerError, Handler
except ImportError:
class HandlerError(RuntimeError): pass
- def handler(f): return f # NOP for testing
+ class Handler: pass
+ def handler(help):
+ def wrapper(f):
+ return f
+ return wrapper
def as_tuple(self):
return (self.name, self.ip)
-class HostHandler:
+class HostHandler(Handler):
def __init__(self,zones):
self.zones = zones
- @handler
+ @handler(u'Adds a host to a zone')
def add(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
self.zones[name].hosts[hostname] = Host(hostname, ip)
self.zones[name].mod = True
- @handler
+ @handler(u'Updates a host ip in a zone')
def update(self, name, hostname, ip):
if not name in self.zones:
raise ZoneNotFoundError(name)
self.zones[name].hosts[hostname].ip = ip
self.zones[name].mod = True
- @handler
+ @handler(u'Deletes a host from a zone')
def delete(self, name, hostname):
if not name in self.zones:
raise ZoneNotFoundError(name)
del self.zones[name].hosts[hostname]
self.zones[name].mod = True
- @handler
+ @handler(u'Lists hosts')
def list(self):
return self.zones.keys()
- @handler
+ @handler(u'Get insormation about all hosts')
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.mx, self.prio)
-class MailExchangeHandler:
+class MailExchangeHandler(Handler):
def __init__(self, zones):
self.zones = zones
- @handler
+ @handler(u'Adds a mail exchange to a zone')
def add(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
self.zones[zonename].mxs[mx] = MailExchange(mx, prio)
self.zones[zonename].mod = True
- @handler
+ @handler(u'Updates a mail exchange priority')
def update(self, zonename, mx, prio):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
self.zones[zonename].mxs[mx].prio = prio
self.zones[zonename].mod = True
- @handler
+ @handler(u'Deletes a mail exchange from a zone')
def delete(self, zonename, mx):
if not zonename in self.zones:
raise ZoneNotFoundError(zonename)
del self.zones[zonename].mxs[mx]
self.zones[zonename].mod = True
- @handler
+ @handler(u'Lists mail exchangers')
def list(self):
return self.zones.keys()
- @handler
+ @handler(u'Get information about all mail exchangers')
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.name)
-class NameServerHandler:
+class NameServerHandler(Handler):
def __init__(self, zones):
self.zones = zones
- @handler
+ @handler(u'Adds a name server to a zone')
def add(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
self.zones[zone].nss[ns] = NameServer(ns)
self.zones[zone].mod = True
- @handler
+ @handler(u'Deletes a name server from a zone')
def delete(self, zone, ns):
if not zone in self.zones:
raise ZoneNotFoundError(zone)
del self.zones[zone].nss[ns]
self.zones[zone].mod = True
- @handler
+ @handler(u'Lists name servers')
def list(self):
return self.zones.keys()
- @handler
+ @handler(u'Get information about all name servers')
def show(self):
return self.zones.values()
def as_tuple(self):
return (self.name, self.hosts, self.mxs, self.nss)
-class ZoneHandler:
+class ZoneHandler(Handler):
r"""ZoneHandler(zones) -> ZoneHandler instance :: Handle a list of zones.
def __init__(self, zones):
self.zones = zones
- @handler
+ @handler(u'Adds a zone')
def add(self, name):
if name in self.zones:
- if self.zones[name].dele = True:
+ if self.zones[name].dele == True:
self.zones[name].dele = False
else:
raise ZoneAlreadyExistsError(name)
self.zones[name].new = True
- @handler
+ @handler(u'Deletes a zone')
def delete(self, name):
r"delete(name) -> None :: Delete a zone from the zone list."
if not name in self.zones:
raise ZoneNotFoundError(name)
self.zones[name].dele = True
- @handler
+ @handler(u'Lists zones')
def list(self):
return self.zones.keys()
- @handler
+ @handler(u'Get information about all zones')
def show(self):
return self.zones.values()
-class DnsHandler:
+class DnsHandler(Handler):
r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance.
Handles DNS service commands for the dns program.
self.ns = NameServerHandler(self.zones)
self.mod = False
- @handler
+ @handler(u'Set a DNS parameter')
def set(self, param, value):
r"set(param, value) -> None :: Set a DNS parameter."
if not param in self.vars:
self.vars[param] = value
self.mod = True
- @handler
+ @handler(u'Get a DNS parameter')
def get(self, param):
r"get(param) -> None :: Get a DNS parameter."
if not param in self.vars:
raise ParameterNotFoundError(param)
return self.vars[param]
- @handler
+ @handler(u'List DNS parameters')
def list(self):
return self.vars.keys()
- @handler
+ @handler(u'Get all DNS parameters, with their values.')
def show(self):
return self.vars.values()
- @handler
+ @handler(u'Start the service.')
def start(self):
r"start() -> None :: Start the DNS service."
#esto seria para poner en una interfaz
#y seria el hook para arrancar el servicio
pass
- @handler
+ @handler(u'Stop the service.')
def stop(self):
r"stop() -> None :: Stop the DNS service."
#esto seria para poner en una interfaz
#y seria el hook para arrancar el servicio
pass
- @handler
+ @handler(u'Restart the service.')
def restart(self):
r"restart() -> None :: Restart the DNS service."
#esto seria para poner en una interfaz
#y seria el hook para arrancar el servicio
pass
- @handler
+ @handler(u'Reload the service config (without restarting, if possible)')
def reload(self):
r"reload() -> None :: Reload the configuration of the DNS service."
#esto seria para poner en una interfaz
#y seria el hook para arrancar el servicio
pass
- @handler
+ @handler(u'Commit the changes (reloading the service, if necessary).')
def commit(self):
r"commit() -> None :: Commit the changes and reload the DNS service."
#esto seria para poner en una interfaz
self._write_config()
self.reload()
- @handler
+ @handler(u'Discard all the uncommited changes.')
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
# NOP for testing
class Sequence: pass
try:
- from dispatcher import handler, HandlerError
+ from dispatcher import handler, HandlerError, Handler
except ImportError:
# NOP for testing
class HandlerError(RuntimeError): pass
- def handler(f): return f
+ class Handler: pass
+ def handler(help):
+ def wrapper(f):
+ return f
+ return wrapper
-__ALL__ = ('IpHandler',)
+__ALL__ = ('IpHandler','Error','DeviceError','DeviceNotFoundError','RouteError','RouteNotFoundError',
+ 'RouteAlreadyExistsError','AddressError','AddressNotFoundError','AddressAlreadyExistsError')
pickle_ext = '.pkl'
pickle_devices = 'devs'
return 0
return cmp(id(self), id(other))
-class RouteHandler:
+class RouteHandler(Handler):
def __init__(self, devices):
self.devices = devices
- @handler
+ @handler(u'Adds a route to a device')
def add(self, device, net_addr, prefix, gateway):
if not device in self.devices:
raise DeviceNotFoundError(device)
except ValueError:
self.devices[device].routes.append(r)
- @handler
+ @handler(u'Deletes a route from a device')
def delete(self, device, net_addr, prefix, gateway):
if not device in self.devices:
raise DeviceNotFoundError(device)
except ValueError:
raise RouteNotFoundError(net_addr + '/' + prefix + '->' + gateway)
- @handler
+ @handler(u'Flushes routes from a device')
def flush(self, device):
if not device in self.devices:
raise DeviceNotFoundError(device)
self.devices[device].routes = list()
- @handler
+ @handler(u'List routes')
def list(self, device):
try:
k = self.devices[device].routes.keys()
k = list()
return k
- @handler
+ @handler(u'Get information about all routes')
def show(self):
try:
k = self.devices[device].routes.values()
def as_tuple(self):
return (self.ip, self.prefix, self.broadcast)
-class AddressHandler:
+class AddressHandler(Handler):
def __init__(self, devices):
self.devices = devices
- @handler
+ @handler(u'Adds an address to a device')
def add(self, device, ip, prefix, broadcast='+'):
if not device in self.devices:
raise DeviceNotFoundError(device)
raise AddressAlreadyExistsError(ip)
self.devices[device].addrs[ip] = Address(ip, prefix, broadcast)
- @handler
+ @handler(u'Deletes an address from a device')
def delete(self, device, ip):
if not device in self.devices:
raise DeviceNotFoundError(device)
raise AddressNotFoundError(ip)
del self.devices[device].addrs[ip]
- @handler
+ @handler(u'Flushes addresses from a device')
def flush(self, device):
if not device in self.devices:
raise DeviceNotFoundError(device)
self.devices[device].addrs = dict()
- @handler
+ @handler(u'List all addresses from a device')
def list(self, device):
try:
k = self.devices[device].addrs.keys()
k = list()
return k
- @handler
+ @handler(u'Get information about addresses from a device')
def show(self, device):
try:
k = self.devices[device].addrs.values()
def as_tuple(self):
return (self.name, self.mac)
-class DeviceHandler:
+class DeviceHandler(Handler):
def __init__(self, devices):
self.devices = devices
dev_fn = path.join(template_dir, device_com)
self.device_template = Template(filename=dev_fn)
- @handler
+ @handler(u'Bring the device up')
def up(self, name):
if name in self.devices:
print self.device_template.render(dev=name, action='up')
else:
raise DeviceNotFoundError(name)
- @handler
+ @handler(u'Bring the device down')
def down(self, name):
if name in self.devices:
print self.device_template.render(dev=name, action='down')
else:
raise DeviceNotFoundError(name)
- @handler
+ @handler(u'List all devices')
def list(self):
return self.devices.keys()
- @handler
+ @handler(u'Get information about a device')
def show(self):
return self.devices.items()
-class IpHandler:
+class IpHandler(Handler):
def __init__(self, pickle_dir='.', config_dir='.'):
r"Initialize DhcpHandler object, see class documentation for details."
p = Popen('ip link list', shell=True, stdout=PIPE, close_fds=True)
devs = _get_devices(p.stdout.read())
self.devices = dict()
- for eth, mac in devs.values():
+ for eth, mac in devs:
self.devices[eth] = Device(eth, mac)
self._dump()
self.addr = AddressHandler(self.devices)
self.dev = DeviceHandler(self.devices)
self.commit()
- @handler
+ @handler(u'Commit the changes (reloading the service, if necessary).')
def commit(self):
r"commit() -> None :: Commit the changes and reload the DHCP service."
#esto seria para poner en una interfaz
self._dump()
self._write_config()
- @handler
+ @handler(u'Discard all the uncommited changes.')
def rollback(self):
r"rollback() -> None :: Discard the changes not yet commited."
self._load()
gateway=route.gateway
)
- def _get_devices(string):
- l = list()
- i = string.find('eth')
- while i != -1:
- eth = string[i:i+4]
- m = string.find('link/ether', i+4)
- mac = string[ m+11 : m+11+17]
- l.append((eth,mac))
- i = string.find('eth', m+11+17)
- return l
+def _get_devices(string):
+ l = list()
+ i = string.find('eth')
+ while i != -1:
+ eth = string[i:i+4]
+ m = string.find('link/ether', i+4)
+ mac = string[ m+11 : m+11+17]
+ l.append((eth,mac))
+ i = string.find('eth', m+11+17)
+ return l
if __name__ == '__main__':