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.
24 class HostError(Error, KeyError):
26 HostError(hostname) -> HostError instance
28 This is the base exception for all host related errors.
31 def __init__(self, hostname):
32 r"Initialize the object. See class documentation for more info."
33 self.message = u'Host error: "%s"' % hostname
35 class HostAlreadyExistsError(HostError):
37 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
39 This exception is raised when trying to add a hostname that already exists.
42 def __init__(self, hostname):
43 r"Initialize the object. See class documentation for more info."
44 self.message = u'Host already exists: "%s"' % hostname
46 class HostNotFoundError(HostError):
48 HostNotFoundError(hostname) -> HostNotFoundError instance
50 This exception is raised when trying to operate on a hostname that doesn't
54 def __init__(self, hostname):
55 r"Initialize the object. See class documentation for more info."
56 self.message = u'Host not found: "%s"' % hostname
61 def __init__(self,ip):
67 class HostHandler(Handler):
69 handler_help = u"Manage proxy hosts"
71 def __init__(self, hosts):
74 @handler(u'Adds a host')
77 raise HostAlreadyExistsError(ip)
78 self.hosts[ip] = Host(ip)
80 @handler(u'Deletes a host')
82 if not ip in self.hosts:
83 raise HostNotFoundError(ip)
86 @handler(u'Shows all hosts')
88 return self.hosts.keys()
90 @handler(u'Get information about all hosts')
92 return self.hosts.items()
95 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
96 TransactionalHandler, ParametersHandler):
98 handler_help = u"Manage proxy service"
100 _initd_name = 'squid'
102 _persistent_attrs = ('params', 'hosts')
104 _restorable_defaults = dict(
112 _config_writer_files = 'squid.conf'
113 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
115 def __init__(self, pickle_dir='.', config_dir='.'):
116 r"Initialize DhcpHandler object, see class documentation for details."
117 self._persistent_dir = pickle_dir
118 self._config_writer_cfg_dir = config_dir
119 self._config_build_templates()
121 self.host = HostHandler(self.hosts)
123 def _get_config_vars(self, config_file):
124 return dict(hosts=self.hosts.values(), **self.params)
127 if __name__ == '__main__':
130 px.set('ip','192.66.66.66')
132 px.host.add('192.168.0.25.25')
133 px.host.add('192.168.0.25.26')
134 px.host.add('192.168.0.25.27')
135 px.host.delete('192.168.0.25.27')