1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
8 TransactionalHandler, ParametersHandler
10 __ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
13 class Error(HandlerError):
15 Error(command) -> Error instance :: Base DnsHandler exception class.
17 All exceptions raised by the DnsHandler inherits from this one, so you can
18 easily catch any DnsHandler exception.
20 message - A descriptive error message.
23 def __init__(self, message):
24 r"Initialize the Error object. See class documentation for more info."
25 self.message = message
30 class HostError(Error, KeyError):
32 HostError(hostname) -> HostError instance
34 This is the base exception for all host related errors.
37 def __init__(self, hostname):
38 r"Initialize the object. See class documentation for more info."
39 self.message = 'Host error: "%s"' % hostname
41 class HostAlreadyExistsError(HostError):
43 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
45 This exception is raised when trying to add a hostname that already exists.
48 def __init__(self, hostname):
49 r"Initialize the object. See class documentation for more info."
50 self.message = 'Host already exists: "%s"' % hostname
52 class HostNotFoundError(HostError):
54 HostNotFoundError(hostname) -> HostNotFoundError instance
56 This exception is raised when trying to operate on a hostname that doesn't
60 def __init__(self, hostname):
61 r"Initialize the object. See class documentation for more info."
62 self.message = 'Host not found: "%s"' % hostname
67 def __init__(self,ip):
73 class HostHandler(Handler):
75 def __init__(self, hosts):
78 @handler(u'Adds a host')
81 raise HostAlreadyExistsError(ip)
82 self.hosts[ip] = Host(ip)
84 @handler(u'Deletes a host')
86 if not ip in self.hosts:
87 raise HostNotFoundError(ip)
90 @handler(u'Shows all hosts')
92 return self.hosts.keys()
94 @handler(u'Get information about all hosts')
96 return self.hosts.items()
99 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
100 TransactionalHandler, ParametersHandler):
102 _initd_name = 'squid'
104 _persistent_attrs = ('params', 'hosts')
106 _restorable_defaults = dict(
114 _config_writer_files = 'squid.conf'
115 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
117 def __init__(self, pickle_dir='.', config_dir='.'):
118 r"Initialize DhcpHandler object, see class documentation for details."
119 self._persistent_dir = pickle_dir
120 self._config_writer_cfg_dir = config_dir
121 self._config_build_templates()
123 self.host = HostHandler(self.hosts)
125 def _get_config_vars(self, config_file):
126 return dict(hosts=self.hosts.values(), **self.params)
129 if __name__ == '__main__':
132 px.set('ip','192.66.66.66')
134 px.host.add('192.168.0.25.25')
135 px.host.add('192.168.0.25.26')
136 px.host.add('192.168.0.25.27')
137 px.host.delete('192.168.0.25.27')