]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/proxy/__init__.py
Update services to use SubHandler and DictSubHandler.
[software/pymin.git] / pymin / services / proxy / __init__.py
index 2aa521db5583335d5fddf5d4b52c1817e96ea96a..0577938271deced0545b6786caf08fa8f6545fe4 100644 (file)
@@ -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, \
 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
 
 
 import crypt
 
-__ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
-            'HostNotFoundError')
+__ALL__ = ('ProxyHandler', 'Error')
 
 class Error(HandlerError):
     r"""
 
 class Error(HandlerError):
     r"""
@@ -23,93 +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.
-    """
+class Host(Sequence):
+    def __init__(self,ip):
+        self.ip = ip
+    def as_tuple(self):
+        return (self.ip,)
 
 
-    def __init__(self, hostname):
-        r"Initialize the object. See class documentation for more info."
-        self.message = u'Host not found: "%s"' % hostname
+# TODO convert to a SetSubHandler
 
 
+class HostHandler(DictSubHandler):
 
 
-class Host(Sequence):
+    handler_help = u"Manage proxy hosts"
 
 
-    def __init__(self,ip):
-        self.ip = ip
+    _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):
     def as_tuple(self):
-        return (self.ip)
+        return (self.name, self.password)
+    def update(self, password=None):
+        if password is not None:
+            self.password = crypt.crypt(password,'BA')
 
 
-class HostHandler(Handler):
+class UserHandler(DictSubHandler):
 
 
-    handler_help = u"Manage proxy hosts"
+    handler_help = u"Manage proxy users"
 
 
-    def __init__(self, parent):
-        self.parent = parent
-
-    @handler(u'Adds a host')
-    def add(self, ip):
-        if ip in self.parent.hosts:
-            raise HostAlreadyExistsError(ip)
-        self.parent.hosts[ip] = Host(ip)
-
-    @handler(u'Deletes a host')
-    def delete(self, ip):
-        if not ip in self.parent.hosts:
-            raise HostNotFoundError(ip)
-        del self.parent.hosts[ip]
-
-    @handler(u'Shows all hosts')
-    def list(self):
-        return self.parent.hosts.keys()
-
-    @handler(u'Get information about all hosts')
-    def show(self):
-        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]
+    _dict_subhandler_attr = 'users'
+    _dict_subhandler_class = User
 
 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                    TransactionalHandler, ParametersHandler):
 
 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                    TransactionalHandler, ParametersHandler):