]> git.llucax.com Git - software/pymin.git/blobdiff - pymin/services/proxy/__init__.py
Add logging to pymin.services.proxy module.
[software/pymin.git] / pymin / services / proxy / __init__.py
index 2740cc8c8ab0f8d546cf6b8986d19bb37d825b17..1e24cd44656e2ed66cb16268ddd1bd6291c59371 100644 (file)
 # vim: set encoding=utf-8 et sw=4 sts=4 :
 
 from os import path
+import logging ; log = logging.getLogger('pymin.services.proxy')
 
 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')
-
-class Error(HandlerError):
-    r"""
-    Error(command) -> Error instance :: Base DnsHandler exception class.
-
-    All exceptions raised by the DnsHandler inherits from this one, so you can
-    easily catch any DnsHandler exception.
-
-    message - A descriptive error message.
-    """
-    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
-
+__ALL__ = ('ProxyHandler',)
 
 class Host(Sequence):
-
     def __init__(self,ip):
         self.ip = ip
-
     def as_tuple(self):
-        return (self.ip)
-
-class HostHandler(Handler):
-
-    handler_help = u"Manage proxy hosts"
+        return (self.ip,)
 
-    def __init__(self, hosts):
-        self.hosts = hosts
+# TODO convert to a SetSubHandler
 
-    @handler(u'Adds a host')
-    def add(self, ip):
-        if ip in self.hosts:
-            raise HostAlreadyExistsError(ip)
-        self.hosts[ip] = Host(ip)
+class HostHandler(DictSubHandler):
 
-    @handler(u'Deletes a host')
-    def delete(self, ip):
-        if not ip in self.hosts:
-            raise HostNotFoundError(ip)
-        del self.hosts[ip]
-
-    @handler(u'Shows all hosts')
-    def list(self):
-        return self.hosts.keys()
-
-    @handler(u'Get information about all hosts')
-    def show(self):
-        return self.hosts.items()
+    handler_help = u"Manage proxy hosts"
 
+    _cont_subhandler_attr = 'hosts'
+    _cont_subhandler_class = Host
 
-class UserHandler(Handler):
+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')
 
-    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]
+    _cont_subhandler_attr = 'users'
+    _cont_subhandler_class = User
 
 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
                    TransactionalHandler, ParametersHandler):
@@ -132,12 +68,13 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
 
     def __init__(self, pickle_dir='.', config_dir='.'):
         r"Initialize DhcpHandler object, see class documentation for details."
+        log.debug(u'ProxyHandler(%r, %r)', pickle_dir, config_dir)
         self._persistent_dir = pickle_dir
         self._config_writer_cfg_dir = config_dir
         self._config_build_templates()
-        self._restore()
-        self.host = HostHandler(self.hosts)
-        self.user = UserHandler(self.users)
+        InitdHandler.__init__(self)
+        self.host = HostHandler(self)
+        self.user = UserHandler(self)
 
     def _get_config_vars(self, config_file):
         if config_file == 'squid.conf':
@@ -147,6 +84,12 @@ class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
 
 if __name__ == '__main__':
 
+    logging.basicConfig(
+        level   = logging.DEBUG,
+        format  = '%(asctime)s %(levelname)-8s %(message)s',
+        datefmt = '%H:%M:%S',
+    )
+
     px = ProxyHandler()
     px.set('ip','192.66.66.66')
     px.set('port','666')
@@ -156,4 +99,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()