1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import logging ; log = logging.getLogger('pymin.services.proxy')
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import Handler, handler, HandlerError
8 from pymin.service.util import Restorable, ConfigWriter, InitdHandler, \
9 TransactionalHandler, ParametersHandler, \
15 __all__ = ('ProxyHandler', 'get_service')
18 def get_service(config):
19 return ProxyHandler(config.proxy.pickle_dir, config.proxy.config_dir)
23 def __init__(self,ip):
28 # TODO convert to a SetSubHandler
30 class HostHandler(DictSubHandler):
32 handler_help = u"Manage proxy hosts"
34 _cont_subhandler_attr = 'hosts'
35 _cont_subhandler_class = Host
38 def __init__(self, name, password):
40 self.password = crypt.crypt(password,'BA')
42 return (self.name, self.password)
43 def update(self, password=None):
44 if password is not None:
45 self.password = crypt.crypt(password,'BA')
47 class UserHandler(DictSubHandler):
49 handler_help = u"Manage proxy users"
51 _cont_subhandler_attr = 'users'
52 _cont_subhandler_class = User
54 class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
55 TransactionalHandler, ParametersHandler):
57 handler_help = u"Manage proxy service"
61 _persistent_attrs = ('params', 'hosts', 'users')
63 _restorable_defaults = dict(
72 _config_writer_files = ('squid.conf','users.conf')
73 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
75 def __init__(self, pickle_dir='.', config_dir='.'):
76 r"Initialize DhcpHandler object, see class documentation for details."
77 log.debug(u'ProxyHandler(%r, %r)', pickle_dir, config_dir)
78 self._persistent_dir = pickle_dir
79 self._config_writer_cfg_dir = config_dir
80 self._config_build_templates()
81 InitdHandler.__init__(self)
82 self.host = HostHandler(self)
83 self.user = UserHandler(self)
85 def _get_config_vars(self, config_file):
86 if config_file == 'squid.conf':
87 return dict(hosts=self.hosts.values(), **self.params)
88 return dict(users=self.users)
91 if __name__ == '__main__':
94 level = logging.DEBUG,
95 format = '%(asctime)s %(levelname)-8s %(message)s',
100 px.set('ip','192.66.66.66')
102 px.host.add('192.168.0.25.25')
103 px.host.add('192.168.0.25.26')
104 px.host.add('192.168.0.25.27')
105 px.host.delete('192.168.0.25.27')
106 px.user.add('lala','soronga')
107 px.user.add('culo','sarasa')