Package flumotion :: Package wizard :: Module configurationwriter
[hide private]

Source Code for Module flumotion.wizard.configurationwriter

  1  # -*- Mode: Python; test-case-name: flumotion.test.test_wizard -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008 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  from cStringIO import StringIO 
 23  from xml.sax.saxutils import quoteattr 
 24   
 25  from flumotion.common.xmlwriter import cmpComponentType, XMLWriter 
 26  from flumotion.configure import configure 
 27   
 28  __version__ = "$Rev: 6246 $" 
 29   
 30   
31 -class ConfigurationWriter(XMLWriter):
32 """I am responsible for writing the state of a flow created by the wizard 33 into XML. 34 I will try my best write pretty XML which can be editable by humans at a 35 later point. 36 """ 37
38 - def __init__(self, flowName, flowComponents, atmosphereComponents):
39 """ 40 @param flowName: name of the flow 41 @param flowComponents: components to be included in the flow 42 @param atmosphereComponents: components to be included in the atmosphere 43 """ 44 super(ConfigurationWriter, self).__init__() 45 self._flowName = flowName 46 self._flowComponents = flowComponents 47 self._atmosphereComponents = atmosphereComponents 48 self._writePlanet()
49
50 - def _writePlanet(self):
51 self.pushTag('planet') 52 self._writeAtmosphere(self._atmosphereComponents) 53 self._writeFlow(self._flowName, self._flowComponents) 54 self.popTag()
55
56 - def _writeAtmosphere(self, components):
57 if not components: 58 return 59 self.pushTag('atmosphere') 60 self._writeComponents(components) 61 self.popTag()
62
63 - def _writeFlow(self, flowName, components):
64 if not components: 65 return 66 self.pushTag('flow', [('name', flowName)]) 67 self._writeComponents(components) 68 self.popTag()
69
70 - def _writeComponents(self, components):
71 # FIXME: When we can depend on Python 2.4, use 72 # sorted(flow.get('components'), 73 # cmp=cmpComponentType, 74 # key=operator.attrgetter('componentType')) 75 # 76 def componentSort(a, b): 77 return cmpComponentType(a.componentType, 78 b.componentType)
79 components = list(components) 80 components.sort(cmp=componentSort) 81 for component in components: 82 self._writeComponent(component)
83
84 - def _writeComponent(self, component):
85 # Do not write components which already exists in the flow, 86 # This is used to create configuration snippets sent to the 87 # wizard which links to existing components 88 if component.exists: 89 return 90 91 # FIXME: when the wizard can be split among projects, "project" 92 # and "version" should be taken from the relevant project 93 attrs = [('name', component.name), 94 ('type', component.componentType), 95 ('project', configure.PACKAGE), 96 ('worker', component.worker), 97 ('version', configure.version)] 98 self.pushTag('component', attrs) 99 self._writeEaters(component.getEaters()) 100 self._writeProperties(component.getProperties()) 101 self._writeComponentPlugs(component.plugs) 102 self.popTag()
103
104 - def _writeEaters(self, eaters):
105 eaters = list(eaters) 106 if not eaters: 107 return 108 self.pushTag('eater', [('name', "default")]) 109 for sourceName in eaters: 110 self.writeTag('feed', data=sourceName) 111 self.popTag()
112
113 - def _writeProperties(self, properties):
114 if not properties: 115 return 116 self.writeLine() 117 propertyNames = properties.keys() 118 propertyNames.sort() 119 for name in propertyNames: 120 value = properties[name] 121 # Fractions, perhaps we should do type introspection here? 122 if isinstance(value, tuple): 123 assert len(value) == 2 124 value = '%d/%d' % value 125 self.writeTag('property', [('name', name)], value)
126
127 - def _writeComponentPlugs(self, plugs):
128 if not plugs: 129 return 130 self.writeLine() 131 self.pushTag('plugs') 132 for plug in plugs: 133 self._writeComponentPlug(plug) 134 self.popTag()
135
136 - def _writeComponentPlug(self, plug):
137 self.pushTag('plug', [('type', plug.plugType)]) 138 self._writeProperties(plug.getProperties()) 139 self.popTag()
140