1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter \
8 ,TransactionalHandler, DictSubHandler, call
10 __ALL__ = ('PppHandler')
12 class Error(HandlerError):
14 Error(command) -> Error instance :: Base ConnectionHandler exception class.
16 All exceptions raised by the ConnectionHandler inherits from this one, so you can
17 easily catch any ConnectionHandler exception.
19 message - A descriptive error message.
23 class ConnectionError(Error, KeyError):
25 ConnectionError(hostname) -> ConnectionError instance
27 This is the base exception for all connection related errors.
30 def __init__(self, connection):
31 r"Initialize the object. See class documentation for more info."
32 self.message = u'Connection error: "%s"' % connection
34 class ConnectionNotFoundError(ConnectionError):
35 def __init__(self, connection):
36 r"Initialize the object. See class documentation for more info."
37 self.message = u'Connection not found error: "%s"' % connection
39 class Connection(Sequence):
41 def __init__(self, name, dev, username=None, password=None):
44 self.username = username
45 self.password = password
48 return (self.name, self.dev, self.username, self.password)
50 def update(self, dev=None, username=None, password=None):
53 if username is not None:
54 self.username = username
55 if password is not None:
56 self.password = password
59 class ConnectionHandler(DictSubHandler):
61 handler_help = u"Manages connections for the ppp service"
63 _dict_subhandler_attr = 'conns'
64 _dict_subhandler_class = Connection
66 class PppHandler(Restorable, ConfigWriter, TransactionalHandler):
68 handler_help = u"Manage ppp service"
70 _persistent_attrs = ('conns')
72 _restorable_defaults = dict(
76 _config_writer_files = ('options.X','pap-secrets','nameX')
77 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
79 def __init__(self, pickle_dir='.', config_dir='.'):
80 r"Initialize Ppphandler 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()
85 self.conn = ConnectionHandler(self)
87 @handler('Starts the service')
88 def start(self, name):
89 if name in self.conns:
93 raise ConnectionNotFoundError(name)
95 @handler('Stops the service')
97 if name in self.conns:
101 raise ConnectionNotFoundError(name)
103 @handler('Reloads the service')
105 for conn in self.conns.values():
107 self.start(conn.name)
109 def _write_config(self):
110 r"_write_config() -> None :: Generate all the configuration files."
111 vars = dict(conns=self.conns)
112 self._write_single_config('pap-secrets','pap-secrets',vars)
113 for conn in self.conns.values():
114 vars = dict(conn=conn)
115 self._write_single_config('nameX',conn.name, vars)
116 self._write_single_config('options.X','options.' + conn.name, vars)
119 if __name__ == '__main__':
121 p.conn.add('test2','tty2','luca','luca')