Package flumotion :: Package worker :: Module config
[hide private]

Source Code for Module flumotion.worker.config

  1  # -*- Mode: Python; test-case-name:flumotion.test.test_workerconfig -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """ 
 23  Parsing of configuration files. 
 24  """ 
 25   
 26  import os 
 27  from xml.dom import minidom, Node 
 28  from xml.parsers import expat 
 29   
 30  from flumotion.common import log, common 
 31   
 32  __version__ = "$Rev: 6125 $" 
 33   
 34   
35 -class ConfigError(Exception):
36 pass
37
38 -class ConfigEntryManager:
39 "I represent a <manager> entry in a worker config file"
40 - def __init__(self, host, port, transport):
41 self.host = host 42 self.port = port 43 self.transport = transport
44
45 -class ConfigEntryAuthentication:
46 "I represent a <authentication> entry in a worker config file"
47 - def __init__(self, username, password):
48 self.username = username 49 self.password = password
50
51 -class WorkerConfigXML(log.Loggable):
52 logCategory = 'config' 53
54 - def __init__(self, filename, string=None):
55 self.name = None 56 self.manager = None 57 self.authentication = None 58 self.feederports = None 59 self.fludebug = None 60 self.randomFeederports = False 61 62 try: 63 if filename != None: 64 self.debug('Loading configuration file `%s\'' % filename) 65 self.doc = minidom.parse(filename) 66 else: 67 self.doc = minidom.parseString(string) 68 except expat.ExpatError, e: 69 raise ConfigError("XML parser error: %s" % e) 70 71 if filename != None: 72 self.path = os.path.split(filename)[0] 73 else: 74 self.path = None 75 76 self.parse()
77 78 # FIXME: privatize, called from __init__
79 - def parse(self):
80 # <worker name="default"> 81 # <manager> 82 # <authentication> 83 # ... 84 # </worker> 85 86 root = self.doc.documentElement 87 88 if not root.nodeName == 'worker': 89 raise ConfigError("unexpected root node': %s" % root.nodeName) 90 91 if root.hasAttribute('name'): 92 self.name = str(root.getAttribute('name')) 93 94 for node in root.childNodes: 95 if (node.nodeType == Node.TEXT_NODE or 96 node.nodeType == Node.COMMENT_NODE): 97 continue 98 if node.nodeName == 'manager': 99 self.manager = self.parseManager(node) 100 elif node.nodeName == 'authentication': 101 self.authentication = self.parseAuthentication(node) 102 elif node.nodeName == 'feederports': 103 self.feederports, self.randomFeederports = \ 104 self.parseFeederports(node) 105 elif node.nodeName == 'debug': 106 self.fludebug = str(node.firstChild.nodeValue) 107 else: 108 raise ConfigError("unexpected node under '%s': %s" % (root.nodeName, node.nodeName))
109
110 - def parseManager(self, node):
111 # <manager> 112 # <host>...</host> 113 # <port>...</port> 114 # <transport>...</transport> 115 # </manager> 116 117 host = None 118 port = None 119 transport = None 120 for child in node.childNodes: 121 if (child.nodeType == Node.TEXT_NODE or 122 child.nodeType == Node.COMMENT_NODE): 123 continue 124 125 if child.nodeName == "host": 126 if child.firstChild: 127 host = str(child.firstChild.nodeValue) 128 else: 129 host = 'localhost' 130 elif child.nodeName == "port": 131 try: 132 port = int(child.firstChild.nodeValue) 133 except ValueError: 134 raise ConfigError("<port> value must be an integer") 135 elif child.nodeName == "transport": 136 transport = str(child.firstChild.nodeValue) 137 if not transport in ('tcp', 'ssl'): 138 raise ConfigError("<transport> must be ssl or tcp") 139 140 else: 141 raise ConfigError("unexpected '%s' node: %s" % (node.nodeName, child.nodeName)) 142 143 return ConfigEntryManager(host, port, transport)
144
145 - def parseAuthentication(self, node):
146 # <authentication> 147 # <username>...</username> 148 # <password>...</password> 149 # </authentication> 150 151 username = None 152 password = None 153 for child in node.childNodes: 154 if (child.nodeType == Node.TEXT_NODE or 155 child.nodeType == Node.COMMENT_NODE): 156 continue 157 158 if child.nodeName == "username": 159 username = str(child.firstChild.nodeValue) 160 elif child.nodeName == "password": 161 password = str(child.firstChild.nodeValue) 162 else: 163 raise ConfigError("unexpected '%s' node: %s" % (node.nodeName, child.nodeName)) 164 165 return ConfigEntryAuthentication(username, password)
166
167 - def parseFeederports(self, node):
168 """ 169 Returns a list of feeder ports to use (possibly empty), 170 and whether or not to use random feeder ports. 171 172 @rtype: (list, bool) 173 """ 174 # returns a list of allowed port numbers 175 # port := int 176 # port-range := port "-" port 177 # port-term := port | port-range 178 # port-list := "" | port-term | port-term "," port-list 179 # <feederports>port-list</feederports> 180 random = False 181 if node.hasAttribute('random'): 182 random = common.strToBool(node.getAttribute('random')) 183 ports = [] 184 if not node.firstChild: 185 return (ports, random) 186 terms = str(node.firstChild.nodeValue).split(',') 187 for term in terms: 188 if '-' in term: 189 (lower, upper) = [int(x) for x in term.split('-')] 190 for port in range(lower, upper+1): 191 if port not in ports: 192 ports.append(port) 193 else: 194 port = int(term) 195 if port not in ports: 196 ports.append(port) 197 return (ports, random)
198