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.
26 class HostError(Error, KeyError):
28 HostError(hostname) -> HostError instance
30 This is the base exception for all host related errors.
33 def __init__(self, hostname):
34 r"Initialize the object. See class documentation for more info."
35 self.message = u'Host error: "%s"' % hostname
37 class HostAlreadyExistsError(HostError):
39 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
41 This exception is raised when trying to add a hostname that already exists.
44 def __init__(self, hostname):
45 r"Initialize the object. See class documentation for more info."
46 self.message = u'Host already exists: "%s"' % hostname
48 class HostNotFoundError(HostError):
50 HostNotFoundError(hostname) -> HostNotFoundError instance
52 This exception is raised when trying to operate on a hostname that doesn't
56 def __init__(self, hostname):
57 r"Initialize the object. See class documentation for more info."
58 self.message = u'Host not found: "%s"' % hostname
63 def __init__(self,ip):
69 class HostHandler(Handler):
71 handler_help = u"Manage proxy hosts"
73 def __init__(self, hosts):
76 @handler(u'Adds a host')
79 raise HostAlreadyExistsError(ip)
80 self.hosts[ip] = Host(ip)
82 @handler(u'Deletes a host')
84 if not ip in self.hosts:
85 raise HostNotFoundError(ip)
88 @handler(u'Shows all hosts')
90 return self.hosts.keys()
92 @handler(u'Get information about all hosts')
94 return self.hosts.items()
97 class UserHandler(Handler):
99 def __init__(self, users):
102 def add(self, user, password):
103 if user in self.users:
104 raise UserAlreadyExistsError(user)
105 self.users[user] = crypt.crypt(password,'BA')
107 def delete(self, user):
108 if not user in self.users:
109 raise UserNotFound(user)
112 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
113 TransactionalHandler, ParametersHandler):
115 handler_help = u"Manage proxy service"
117 _initd_name = 'squid'
119 _persistent_attrs = ('params', 'hosts', 'users')
121 _restorable_defaults = dict(
130 _config_writer_files = ('squid.conf','users.conf')
131 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
133 def __init__(self, pickle_dir='.', config_dir='.'):
134 r"Initialize DhcpHandler object, see class documentation for details."
135 self._persistent_dir = pickle_dir
136 self._config_writer_cfg_dir = config_dir
137 self._config_build_templates()
139 self.host = HostHandler(self.hosts)
140 self.user = UserHandler(self.users)
142 def _get_config_vars(self, config_file):
143 if config_file == 'squid.conf':
144 return dict(hosts=self.hosts.values(), **self.params)
145 return dict(users=self.users)
148 if __name__ == '__main__':
151 px.set('ip','192.66.66.66')
153 px.host.add('192.168.0.25.25')
154 px.host.add('192.168.0.25.26')
155 px.host.add('192.168.0.25.27')
156 px.host.delete('192.168.0.25.27')
157 px.user.add('lala','soronga')
158 px.user.add('culo','sarasa')