]> git.llucax.com Git - software/pymin.git/blob - pymin/services/proxy/__init__.py
Update services to use SubHandler and DictSubHandler.
[software/pymin.git] / pymin / services / proxy / __init__.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
8                                 TransactionalHandler, ParametersHandler, \
9                                 DictSubHandler
10
11 import crypt
12
13 __ALL__ = ('ProxyHandler', 'Error')
14
15 class Error(HandlerError):
16     r"""
17     Error(command) -> Error instance :: Base DnsHandler exception class.
18
19     All exceptions raised by the DnsHandler inherits from this one, so you can
20     easily catch any DnsHandler exception.
21
22     message - A descriptive error message.
23     """
24     pass
25
26 class Host(Sequence):
27     def __init__(self,ip):
28         self.ip = ip
29     def as_tuple(self):
30         return (self.ip,)
31
32 # TODO convert to a SetSubHandler
33
34 class HostHandler(DictSubHandler):
35
36     handler_help = u"Manage proxy hosts"
37
38     _dict_subhandler_attr = 'hosts'
39     _dict_subhandler_class = Host
40
41 class User(Sequence):
42     def __init__(self, name, password):
43         self.name = name
44         self.password = crypt.crypt(password,'BA')
45     def as_tuple(self):
46         return (self.name, self.password)
47     def update(self, password=None):
48         if password is not None:
49             self.password = crypt.crypt(password,'BA')
50
51 class UserHandler(DictSubHandler):
52
53     handler_help = u"Manage proxy users"
54
55     _dict_subhandler_attr = 'users'
56     _dict_subhandler_class = User
57
58 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
59                    TransactionalHandler, ParametersHandler):
60
61     handler_help = u"Manage proxy service"
62
63     _initd_name = 'squid'
64
65     _persistent_attrs = ('params', 'hosts', 'users')
66
67     _restorable_defaults = dict(
68             hosts = dict(),
69             params  = dict(
70                 ip   = '192.168.0.1',
71                 port = '8080',
72             ),
73             users = dict(),
74     )
75
76     _config_writer_files = ('squid.conf','users.conf')
77     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
78
79     def __init__(self, pickle_dir='.', config_dir='.'):
80         r"Initialize DhcpHandler object, see class documentation for details."
81         self._persistent_dir = pickle_dir
82         self._config_writer_cfg_dir = config_dir
83         self._config_build_templates()
84         self._restore()
85         self.host = HostHandler(self)
86         self.user = UserHandler(self)
87
88     def _get_config_vars(self, config_file):
89         if config_file == 'squid.conf':
90             return dict(hosts=self.hosts.values(), **self.params)
91         return dict(users=self.users)
92
93
94 if __name__ == '__main__':
95
96     px = ProxyHandler()
97     px.set('ip','192.66.66.66')
98     px.set('port','666')
99     px.host.add('192.168.0.25.25')
100     px.host.add('192.168.0.25.26')
101     px.host.add('192.168.0.25.27')
102     px.host.delete('192.168.0.25.27')
103     px.user.add('lala','soronga')
104     px.user.add('culo','sarasa')
105     px.commit()