X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/d678eca2d9738772ed0a0b82db1792dac6730393..4eb6c6308a028e4d4d0673c8f2090e74294d6e36:/pymin/services/proxy/__init__.py?ds=inline diff --git a/pymin/services/proxy/__init__.py b/pymin/services/proxy/__init__.py index 5e8460e..0577938 100644 --- a/pymin/services/proxy/__init__.py +++ b/pymin/services/proxy/__init__.py @@ -5,12 +5,12 @@ from os import path from pymin.seqtools import Sequence from pymin.dispatcher import Handler, handler, HandlerError from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler + TransactionalHandler, ParametersHandler, \ + DictSubHandler import crypt -__ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError', - 'HostNotFoundError') +__ALL__ = ('ProxyHandler', 'Error') class Error(HandlerError): r""" @@ -21,101 +21,45 @@ 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 - -class HostError(Error, KeyError): - r""" - HostError(hostname) -> HostError instance - - This is the base exception for all host related errors. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host error: "%s"' % hostname - -class HostAlreadyExistsError(HostError): - r""" - HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance - - This exception is raised when trying to add a hostname that already exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host already exists: "%s"' % hostname - -class HostNotFoundError(HostError): - r""" - HostNotFoundError(hostname) -> HostNotFoundError instance - - This exception is raised when trying to operate on a hostname that doesn't - exists. - """ - - def __init__(self, hostname): - r"Initialize the object. See class documentation for more info." - self.message = 'Host not found: "%s"' % hostname - + pass class Host(Sequence): - def __init__(self,ip): self.ip = ip - def as_tuple(self): - return (self.ip) - -class HostHandler(Handler): - - def __init__(self, hosts): - self.hosts = hosts + return (self.ip,) - @handler(u'Adds a host') - def add(self, ip): - if ip in self.hosts: - raise HostAlreadyExistsError(ip) - self.hosts[ip] = Host(ip) +# TODO convert to a SetSubHandler - @handler(u'Deletes a host') - def delete(self, ip): - if not ip in self.hosts: - raise HostNotFoundError(ip) - del self.hosts[ip] +class HostHandler(DictSubHandler): - @handler(u'Shows all hosts') - def list(self): - return self.hosts.keys() + handler_help = u"Manage proxy hosts" - @handler(u'Get information about all hosts') - def show(self): - return self.hosts.items() + _dict_subhandler_attr = 'hosts' + _dict_subhandler_class = Host +class User(Sequence): + def __init__(self, name, password): + self.name = name + self.password = crypt.crypt(password,'BA') + def as_tuple(self): + return (self.name, self.password) + def update(self, password=None): + if password is not None: + self.password = crypt.crypt(password,'BA') -class UserHandler(Handler): - - def __init__(self, users): - self.users = users +class UserHandler(DictSubHandler): - def add(self, user, password): - if user in self.users: - raise UserAlreadyExistsError(user) - self.users[user] = crypt.crypt(password,'BA') + handler_help = u"Manage proxy users" - def delete(self, user): - if not user in self.users: - raise UserNotFound(user) - del self.users[user] + _dict_subhandler_attr = 'users' + _dict_subhandler_class = User class ProxyHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, ParametersHandler): + handler_help = u"Manage proxy service" + _initd_name = 'squid' _persistent_attrs = ('params', 'hosts', 'users') @@ -138,8 +82,8 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler, self._config_writer_cfg_dir = config_dir self._config_build_templates() self._restore() - self.host = HostHandler(self.hosts) - self.user = UserHandler(self.users) + self.host = HostHandler(self) + self.user = UserHandler(self) def _get_config_vars(self, config_file): if config_file == 'squid.conf': @@ -158,4 +102,4 @@ if __name__ == '__main__': px.host.delete('192.168.0.25.27') px.user.add('lala','soronga') px.user.add('culo','sarasa') - px.commit() \ No newline at end of file + px.commit()