Package flumotion :: Package common :: Module connection
[hide private]

Source Code for Module flumotion.common.connection

  1  # Flumotion - a streaming media server 
  2  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  3  # All rights reserved. 
  4   
  5  # This file may be distributed and/or modified under the terms of 
  6  # the GNU General Public License version 2 as published by 
  7  # the Free Software Foundation. 
  8  # This file is distributed without any warranty; without even the implied 
  9  # warranty of merchantability or fitness for a particular purpose. 
 10  # See "LICENSE.GPL" in the source distribution for more information. 
 11   
 12  # Licensees having purchased or holding a valid Flumotion Advanced 
 13  # Streaming Server license may use this file in accordance with the 
 14  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 15  # See "LICENSE.Flumotion" in the source distribution for more information. 
 16   
 17  # Headers in this file shall remain intact. 
 18   
 19   
 20  """ 
 21  Abstractions for dealing with PB connections. 
 22  """ 
 23   
 24  import re 
 25   
 26  from twisted.spread import pb 
 27   
 28  from flumotion.common import common 
 29  from flumotion.twisted import pb as fpb 
 30   
31 -class PBConnectionInfo(pb.Copyable, pb.RemoteCopy):
32 """ 33 I hold information on how to connect to a PB server somewhere. I can 34 be transferred over the wire. 35 """ 36 __implements__ = common.mergeImplements(pb.Copyable, pb.RemoteCopy) 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
44 - def __str__(self):
45 # have to use getattr in the case that the authenticator was 46 # transferred over the wire, because the remote reference is not 47 # an authenticator 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 _pat = re.compile('^(([^:@]*)(:([^:@]+))?@)?([^:@]+)(:([0-9]+))?$')
58 -def parsePBConnectionInfo(string, username='user', password='test', 59 port=7531, use_ssl=True):
60 """ 61 Parse a string representation of a PB connection into a 62 PBConnectionInfo object. 63 64 The expected format is [user[:pass]@]host[:port]. Only the host is 65 mandatory. The default values for username, password, and port will 66 be taken from the optional username, password and port arguments. 67 68 @param string: A string describing the PB connection. 69 @type string: str 70 @param username: Default username, or 'user' if not given. 71 @type username: str 72 @param password: Default password, or 'test' if not given. 73 @type password: str 74 @param port: Default port, or 7531 if not given. 75 @type port: int 76 @param use_ssl: Whether to use SSL, or True if not given. Note that 77 there is no syntax in the connection string for specifying 78 whether or not to use SSL; if you want to control this you 79 will have to provide another method. 80 @type use_ssl: bool 81 82 @rtype: L{PBConnectionInfo} 83 """ 84 auth = fpb.Authenticator(username=username, password=password) 85 ret = PBConnectionInfo(None, port, use_ssl, auth) 86 87 matched = _pat.search(string) 88 if not matched: 89 raise TypeError('Invalid connection string: %s ' 90 '(looking for [user[:pass]@]host[/ssl|/tcp][:port])' 91 % string) 92 93 groups = matched.groups() 94 for o, k, i, f in ((auth, 'username', 1, str), 95 (auth, 'password', 3, str), 96 (ret, 'host', 4, str), 97 (ret, 'port', 6, int)): 98 if groups[i]: 99 setattr(o, k, f(groups[i])) 100 return ret
101