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, parent):
76 @handler(u'Adds a host')
78 if ip in self.parent.hosts:
79 raise HostAlreadyExistsError(ip)
80 self.parent.hosts[ip] = Host(ip)
82 @handler(u'Deletes a host')
84 if not ip in self.parent.hosts:
85 raise HostNotFoundError(ip)
86 del self.parent.hosts[ip]
88 @handler(u'Shows all hosts')
90 return self.parent.hosts.keys()
92 @handler(u'Get information about all hosts')
94 return self.parent.hosts.items()
97 class UserHandler(Handler):
99 def __init__(self, parent):
102 @handler('Adds a user')
103 def add(self, user, password):
104 if user in self.parent.users:
105 raise UserAlreadyExistsError(user)
106 self.parent.users[user] = crypt.crypt(password,'BA')
108 @handler('Deletes a user')
109 def delete(self, user):
110 if not user in self.parent.users:
111 raise UserNotFound(user)
112 del self.parent.users[user]
114 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
115 TransactionalHandler, ParametersHandler):
117 handler_help = u"Manage proxy service"
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)
142 self.user = UserHandler(self)
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')