Package flumotion :: Package admin :: Package assistant :: Module configurationwriter
[hide private]

Source Code for Module flumotion.admin.assistant.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,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 17   
 18  import operator 
 19  from cStringIO import StringIO 
 20  from xml.sax.saxutils import quoteattr 
 21   
 22  from flumotion.common.xmlwriter import cmpComponentType, XMLWriter 
 23  from flumotion.configure import configure 
 24   
 25  __version__ = "$Rev: 6246 $" 
 26   
 27   
28 -class ConfigurationWriter(XMLWriter):
29 """I am responsible for writing the state of a flow created by the 30 configuration assistant into XML. 31 I will try my best write pretty XML which can be editable by humans at a 32 later point. 33 """ 34
35 - def __init__(self, flowName, flowComponents, atmosphereComponents):
36 """ 37 @param flowName: name of the flow 38 @param flowComponents: components to be included in the flow 39 @param atmosphereComponents: components to be included 40 in the atmosphere 41 """ 42 super(ConfigurationWriter, self).__init__() 43 self._flowName = flowName 44 self._flowComponents = flowComponents 45 self._atmosphereComponents = atmosphereComponents 46 self._writePlanet()
47
48 - def _writePlanet(self):
49 self.pushTag('planet') 50 self._writeAtmosphere(self._atmosphereComponents) 51 self._writeFlow(self._flowName, self._flowComponents) 52 self.popTag()
53
54 - def _writeAtmosphere(self, components):
55 if not components: 56 return 57 self.pushTag('atmosphere') 58 self._writeComponents(components) 59 self.popTag()
60
61 - def _writeFlow(self, flowName, components):
62 if not components: 63 return 64 self.pushTag('flow', [('name', flowName)]) 65 self._writeComponents(components) 66 self.popTag()
67
68 - def _writeComponents(self, components):
69 components = sorted(components, 70 cmp=cmpComponentType, 71 key=operator.attrgetter('componentType')) 72 for component in components: 73 self._writeComponent(component)
74
75 - def _writeComponent(self, component):
76 # Do not write components which already exists in the flow, 77 # This is used to create configuration snippets sent to the 78 # asssistant which links to existing components 79 if component.exists: 80 return 81 82 # FIXME: when the assistant can be split among projects, "project" 83 # and "version" should be taken from the relevant project 84 attrs = [('name', component.name), 85 ('type', component.componentType), 86 ('project', configure.PACKAGE), 87 ('worker', component.worker), 88 ('version', configure.version)] 89 self.pushTag('component', attrs) 90 self._writeEaters(component.getEaters()) 91 self._writeProperties(component.getProperties()) 92 self._writeComponentPlugs(component.plugs) 93 self.popTag()
94
95 - def _writeEaters(self, eaters):
96 eaters = list(eaters) 97 if not eaters: 98 return 99 self.pushTag('eater', [('name', "default")]) 100 for sourceName in eaters: 101 self.writeTag('feed', data=sourceName) 102 self.popTag()
103
104 - def _writeProperties(self, properties):
105 if not properties: 106 return 107 self.writeLine() 108 propertyNames = properties.keys() 109 propertyNames.sort() 110 111 def serialize(propVal): 112 if isinstance(propVal, tuple): 113 return ["%d/%d" % propVal] 114 elif isinstance(propVal, list): 115 return propVal 116 else: 117 return [propVal]
118 for name, value in properties.items(): 119 attrs = [('name', name)] 120 for value in serialize(value): 121 if isinstance(value, dict): 122 self.pushTag('compound-property', attrs) 123 self._writeProperties(value) 124 self.popTag() 125 else: 126 self.writeTag('property', attrs, value)
127
128 - def _writeComponentPlugs(self, plugs):
129 if not plugs: 130 return 131 self.writeLine() 132 self.pushTag('plugs') 133 for plug in plugs: 134 self._writeComponentPlug(plug) 135 self.popTag()
136
137 - def _writeComponentPlug(self, plug):
138 self.pushTag('plug', [('type', plug.plugType)]) 139 self._writeProperties(plug.getProperties()) 140 self.popTag()
141