1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from seqtools import Sequence
6 from dispatcher import Handler, handler, HandlerError
7 from services.util import Restorable, ConfigWriter
8 from services.util import InitdHandler, TransactionalHandler, ParametersHandler
10 __ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
11 'HostNotFoundError', 'ParameterError', 'ParameterNotFoundError')
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
64 class ParameterError(Error, KeyError):
66 ParameterError(paramname) -> ParameterError instance
68 This is the base exception for all DhcpHandler parameters related errors.
71 def __init__(self, paramname):
72 r"Initialize the object. See class documentation for more info."
73 self.message = 'Parameter error: "%s"' % paramname
75 class ParameterNotFoundError(ParameterError):
77 ParameterNotFoundError(hostname) -> ParameterNotFoundError instance
79 This exception is raised when trying to operate on a parameter that doesn't
83 def __init__(self, paramname):
84 r"Initialize the object. See class documentation for more info."
85 self.message = 'Parameter not found: "%s"' % paramname
89 def __init__(self,ip):
95 class HostHandler(Handler):
97 def __init__(self, hosts):
100 @handler(u'Adds a host')
103 raise HostAlreadyExistsError(ip)
104 self.hosts[ip] = Host(ip)
106 @handler(u'Deletes a host')
107 def delete(self, ip):
108 if not ip in self.hosts:
109 raise HostNotFoundError(ip)
112 @handler(u'Shows all hosts')
114 return self.hosts.keys()
116 @handler(u'Get information about all hosts')
118 return self.hosts.items()
121 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
122 TransactionalHandler, ParametersHandler):
124 _initd_name = 'squid'
126 _persistent_attrs = ('params', 'hosts')
128 _restorable_defaults = dict(
136 _config_writer_files = 'squid.conf'
137 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
139 def __init__(self, pickle_dir='.', config_dir='.'):
140 r"Initialize DhcpHandler object, see class documentation for details."
141 self._persistent_dir = pickle_dir
142 self._config_writer_cfg_dir = config_dir
143 self._config_build_templates()
145 self.host = HostHandler(self.hosts)
147 def _get_config_vars(self, config_file):
148 return dict(hosts=self.hosts.values(), **self.params)
151 if __name__ == '__main__':
154 px.set('ip','192.66.66.66')
156 px.host.add('192.168.0.25.25')
157 px.host.add('192.168.0.25.26')
158 px.host.add('192.168.0.25.27')
159 px.host.delete('192.168.0.25.27')