]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/proxy/__init__.py
Bugfix: catch LookupErrors instead of IndexError in ContainerSubHandler.
[software/pymin.git] / pymin / services / proxy / __init__.py
index 399eba1bf341b5c9eb37bcad32d9738ff572dd48..85bafd8c2d44cecd0299b49ce9ae240a51ca5e7b 100644 (file)
@@ -5,10 +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, \
 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
 
 
-__ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
-            'HostNotFoundError')
+import crypt
+
+__ALL__ = ('ProxyHandler', 'Error')
 
 class Error(HandlerError):
     r"""
 
 class Error(HandlerError):
     r"""
@@ -21,76 +23,37 @@ class Error(HandlerError):
     """
     pass
 
     """
     pass
 
-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 = u'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 = u'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 = u'Host not found: "%s"' % hostname
-
-
 class Host(Sequence):
 class Host(Sequence):
-
     def __init__(self,ip):
         self.ip = ip
     def __init__(self,ip):
         self.ip = ip
-
     def as_tuple(self):
     def as_tuple(self):
-        return (self.ip)
+        return (self.ip,)
 
 
-class HostHandler(Handler):
+# TODO convert to a SetSubHandler
 
 
-    handler_help = u"Manage proxy hosts"
+class HostHandler(DictSubHandler):
 
 
-    def __init__(self, hosts):
-        self.hosts = hosts
+    handler_help = u"Manage proxy hosts"
 
 
-    @handler(u'Adds a host')
-    def add(self, ip):
-        if ip in self.hosts:
-            raise HostAlreadyExistsError(ip)
-        self.hosts[ip] = Host(ip)
+    _cont_subhandler_attr = 'hosts'
+    _cont_subhandler_class = Host
 
 
-    @handler(u'Deletes a host')
-    def delete(self, ip):
-        if not ip in self.hosts:
-            raise HostNotFoundError(ip)
-        del self.hosts[ip]
+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')
 
 
-    @handler(u'Shows all hosts')
-    def list(self):
-        return self.hosts.keys()
+class UserHandler(DictSubHandler):
 
 
-    @handler(u'Get information about all hosts')
-    def show(self):
-        return self.hosts.items()
+    handler_help = u"Manage proxy users"
 
 
+    _cont_subhandler_attr = 'users'
+    _cont_subhandler_class = User
 
 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                    TransactionalHandler, ParametersHandler):
 
 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                    TransactionalHandler, ParametersHandler):
@@ -99,7 +62,7 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
 
     _initd_name = 'squid'
 
 
     _initd_name = 'squid'
 
-    _persistent_attrs = ('params', 'hosts')
+    _persistent_attrs = ('params', 'hosts', 'users')
 
     _restorable_defaults = dict(
             hosts = dict(),
 
     _restorable_defaults = dict(
             hosts = dict(),
@@ -107,9 +70,10 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                 ip   = '192.168.0.1',
                 port = '8080',
             ),
                 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='.'):
     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
 
     def __init__(self, pickle_dir='.', config_dir='.'):
@@ -118,10 +82,13 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
         self._config_writer_cfg_dir = config_dir
         self._config_build_templates()
         self._restore()
         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):
 
     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__':
 
 
 if __name__ == '__main__':
@@ -133,4 +100,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.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()