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
12 __ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
15 class Error(HandlerError):
17 Error(command) -> Error instance :: Base DnsHandler exception class.
19 All exceptions raised by the DnsHandler inherits from this one, so you can
20 easily catch any DnsHandler exception.
22 message - A descriptive error message.
25 def __init__(self, message):
26 r"Initialize the Error object. See class documentation for more info."
27 self.message = message
32 class HostError(Error, KeyError):
34 HostError(hostname) -> HostError instance
36 This is the base exception for all host related errors.
39 def __init__(self, hostname):
40 r"Initialize the object. See class documentation for more info."
41 self.message = 'Host error: "%s"' % hostname
43 class HostAlreadyExistsError(HostError):
45 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
47 This exception is raised when trying to add a hostname that already exists.
50 def __init__(self, hostname):
51 r"Initialize the object. See class documentation for more info."
52 self.message = 'Host already exists: "%s"' % hostname
54 class HostNotFoundError(HostError):
56 HostNotFoundError(hostname) -> HostNotFoundError instance
58 This exception is raised when trying to operate on a hostname that doesn't
62 def __init__(self, hostname):
63 r"Initialize the object. See class documentation for more info."
64 self.message = 'Host not found: "%s"' % hostname
69 def __init__(self,ip):
75 class HostHandler(Handler):
77 def __init__(self, hosts):
80 @handler(u'Adds a host')
83 raise HostAlreadyExistsError(ip)
84 self.hosts[ip] = Host(ip)
86 @handler(u'Deletes a host')
88 if not ip in self.hosts:
89 raise HostNotFoundError(ip)
92 @handler(u'Shows all hosts')
94 return self.hosts.keys()
96 @handler(u'Get information about all hosts')
98 return self.hosts.items()
101 class UserHandler(Handler):
103 def __init__(self, users):
106 def add(self, user, password):
107 if user in self.users:
108 raise UserAlreadyExistsError(user)
109 self.users[user] = crypt.crypt(password,'BA')
111 def delete(self, user):
112 if not user in self.users:
113 raise UserNotFound(user)
116 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
117 TransactionalHandler, ParametersHandler):
119 _initd_name = 'squid'
121 _persistent_attrs = ('params', 'hosts', 'users')
123 _restorable_defaults = dict(
132 _config_writer_files = ('squid.conf','users.conf')
133 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
135 def __init__(self, pickle_dir='.', config_dir='.'):
136 r"Initialize DhcpHandler object, see class documentation for details."
137 self._persistent_dir = pickle_dir
138 self._config_writer_cfg_dir = config_dir
139 self._config_build_templates()
141 self.host = HostHandler(self.hosts)
142 self.user = UserHandler(self.users)
144 def _get_config_vars(self, config_file):
145 if config_file == 'squid.conf':
146 return dict(hosts=self.hosts.values(), **self.params)
147 return dict(users=self.users)
150 if __name__ == '__main__':
153 px.set('ip','192.66.66.66')
155 px.host.add('192.168.0.25.25')
156 px.host.add('192.168.0.25.26')
157 px.host.add('192.168.0.25.27')
158 px.host.delete('192.168.0.25.27')
159 px.user.add('lala','soronga')
160 px.user.add('culo','sarasa')