1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 """helpers for dealing with Twisted PB connections.
20 """
21
22 import re
23
24 from twisted.spread import pb
25
26 from flumotion.common import common
27 from flumotion.twisted import pb as fpb
28
29 __version__ = "$Rev: 7162 $"
30
31
33 """
34 I hold information on how to connect to a PB server somewhere. I can
35 be transferred over the wire.
36 """
37
38 - def __init__(self, host, port, use_ssl, authenticator):
39 self.host = host
40 self.port = port
41 self.use_ssl = use_ssl
42 self.authenticator = authenticator
43
45
46
47
48 if (self.authenticator
49 and getattr(self.authenticator, 'username', None)):
50 return '%s@%s:%d' % (self.authenticator.username,
51 self.host, self.port)
52 else:
53 return '%s:%d' % (self.host, self.port)
54
55 pb.setUnjellyableForClass(PBConnectionInfo, PBConnectionInfo)
56
57
58 _pat = re.compile('^(([^:@]*)(:([^:@]+))?@)?([^:@]+)(:([0-9]+))?$')
59
60
63 """
64 Parse a string representation of a PB connection into a
65 PBConnectionInfo object.
66
67 The expected format is [user[:pass]@]host[:port]. Only the host is
68 mandatory. The default values for username, password, and port will
69 be taken from the optional username, password and port arguments.
70
71 @param string: A string describing the PB connection.
72 @type string: str
73 @param username: Default username, or 'user' if not given.
74 @type username: str
75 @param password: Default password, or 'test' if not given.
76 @type password: str
77 @param port: Default port, or 7531 if not given.
78 @type port: int
79 @param use_ssl: Whether to use SSL, or True if not given. Note that
80 there is no syntax in the connection string for specifying
81 whether or not to use SSL; if you want to control this you
82 will have to provide another method.
83 @type use_ssl: bool
84
85 @rtype: L{PBConnectionInfo}
86 """
87 auth = fpb.Authenticator(username=username, password=password)
88 ret = PBConnectionInfo(None, port, use_ssl, auth)
89
90 matched = _pat.search(string)
91 if not matched:
92 raise TypeError('Invalid connection string: %s '
93 '(looking for [user[:pass]@]host[/ssl|/tcp][:port])'
94 % string)
95
96 groups = matched.groups()
97 for o, k, i, f in ((auth, 'username', 1, str),
98 (auth, 'password', 3, str),
99 (ret, 'host', 4, str),
100 (ret, 'port', 6, int)):
101 if groups[i]:
102 setattr(o, k, f(groups[i]))
103 return ret
104