X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/296d853cc95fd5bef262248cfe21b507abd26a4f..6e8f2cd18b71c41f6d4c32ae698f5147cf0507da:/pymin/services/proxy/__init__.py diff --git a/pymin/services/proxy/__init__.py b/pymin/services/proxy/__init__.py index b3b1172..2aa521d 100644 --- a/pymin/services/proxy/__init__.py +++ b/pymin/services/proxy/__init__.py @@ -7,6 +7,8 @@ from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ TransactionalHandler, ParametersHandler +import crypt + __ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError', 'HostNotFoundError') @@ -19,13 +21,7 @@ class Error(HandlerError): message - A descriptive error message. """ - - def __init__(self, message): - r"Initialize the Error object. See class documentation for more info." - self.message = message - - def __str__(self): - return self.message + pass class HostError(Error, KeyError): r""" @@ -36,7 +32,7 @@ class HostError(Error, KeyError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host error: "%s"' % hostname + self.message = u'Host error: "%s"' % hostname class HostAlreadyExistsError(HostError): r""" @@ -47,7 +43,7 @@ class HostAlreadyExistsError(HostError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host already exists: "%s"' % hostname + self.message = u'Host already exists: "%s"' % hostname class HostNotFoundError(HostError): r""" @@ -59,7 +55,7 @@ class HostNotFoundError(HostError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host not found: "%s"' % hostname + self.message = u'Host not found: "%s"' % hostname class Host(Sequence): @@ -72,36 +68,57 @@ class Host(Sequence): class HostHandler(Handler): - def __init__(self, hosts): - self.hosts = hosts + handler_help = u"Manage proxy hosts" + + def __init__(self, parent): + self.parent = parent @handler(u'Adds a host') def add(self, ip): - if ip in self.hosts: + if ip in self.parent.hosts: raise HostAlreadyExistsError(ip) - self.hosts[ip] = Host(ip) + self.parent.hosts[ip] = Host(ip) @handler(u'Deletes a host') def delete(self, ip): - if not ip in self.hosts: + if not ip in self.parent.hosts: raise HostNotFoundError(ip) - del self.hosts[ip] + del self.parent.hosts[ip] @handler(u'Shows all hosts') def list(self): - return self.hosts.keys() + return self.parent.hosts.keys() @handler(u'Get information about all hosts') def show(self): - return self.hosts.items() - + return self.parent.hosts.items() + + +class UserHandler(Handler): + + def __init__(self, parent): + self.parent = parent + + @handler('Adds a user') + def add(self, user, password): + if user in self.parent.users: + raise UserAlreadyExistsError(user) + self.parent.users[user] = crypt.crypt(password,'BA') + + @handler('Deletes a user') + def delete(self, user): + if not user in self.parent.users: + raise UserNotFound(user) + del self.parent.users[user] class ProxyHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, ParametersHandler): + handler_help = u"Manage proxy service" + _initd_name = 'squid' - _persistent_attrs = ('params', 'hosts') + _persistent_attrs = ('params', 'hosts', 'users') _restorable_defaults = dict( hosts = dict(), @@ -109,9 +126,10 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler, ip = '192.168.0.1', port = '8080', ), + users = dict(), ) - _config_writer_files = 'squid.conf' + _config_writer_files = ('squid.conf','users.conf') _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates') def __init__(self, pickle_dir='.', config_dir='.'): @@ -120,10 +138,13 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler, self._config_writer_cfg_dir = config_dir self._config_build_templates() self._restore() - self.host = HostHandler(self.hosts) + self.host = HostHandler(self) + self.user = UserHandler(self) def _get_config_vars(self, config_file): - return dict(hosts=self.hosts.values(), **self.params) + if config_file == 'squid.conf': + return dict(hosts=self.hosts.values(), **self.params) + return dict(users=self.users) if __name__ == '__main__': @@ -135,4 +156,6 @@ if __name__ == '__main__': px.host.add('192.168.0.25.26') px.host.add('192.168.0.25.27') px.host.delete('192.168.0.25.27') - px.commit() \ No newline at end of file + px.user.add('lala','soronga') + px.user.add('culo','sarasa') + px.commit()