Moved non-gui parts to their own dir.
[zeroinstall.git] / injector / writer.py
blob1ecc99fff3832e5bbdd4bcc4b94591b9a1049e84
1 import os
2 from xml.dom import minidom, XMLNS_NAMESPACE
4 import basedir
6 from model import *
8 from namespaces import config_site, config_prog, XMLNS_IFACE
10 def add_text(parent, name, text):
11 doc = parent.ownerDocument
12 element = doc.createElementNS(XMLNS_IFACE, name)
13 parent.appendChild(element)
14 element.appendChild(doc.createTextNode(text))
16 def add_impl(parent, impl):
17 doc = parent.ownerDocument
18 node = doc.createElementNS(XMLNS_IFACE, 'implementation')
19 parent.appendChild(node)
20 node.setAttribute('version', impl.get_version())
21 node.setAttribute('path', impl.path)
22 if impl.upstream_stability:
23 node.setAttribute('stability', str(impl.upstream_stability))
24 if impl.user_stability:
25 node.setAttribute('user_stability', str(impl.user_stability))
26 if impl.size:
27 node.setAttribute('size', str(impl.size))
29 for dep in impl.dependencies.values():
30 depends = doc.createElementNS(XMLNS_IFACE, 'requires')
31 depends.setAttribute('interface', dep.get_interface().uri)
32 node.appendChild(depends)
33 for bin in dep.bindings:
34 if isinstance(bin, EnvironmentBinding):
35 binding = doc.createElementNS(XMLNS_IFACE, 'environment')
36 binding.setAttribute('name', bin.name)
37 binding.setAttribute('insert', bin.insert)
38 depends.appendChild(binding)
39 else:
40 print "Warning, unknown binding type", bin
42 def save_interface(interface):
43 assert interface.uptodate
45 path = basedir.save_config_path(config_site, config_prog, 'interfaces')
46 path = os.path.join(path, escape(interface.uri))
47 #print "Save to", path
49 impl = minidom.getDOMImplementation()
50 doc = impl.createDocument(XMLNS_IFACE, 'interface', None)
52 root = doc.documentElement
53 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE)
55 add_text(root, 'name', interface.name)
56 add_text(root, 'summary', interface.summary)
57 add_text(root, 'description', interface.description)
58 if interface.stability_policy:
59 root.setAttribute('stability_policy', str(interface.stability_policy))
61 impls = interface.implementations.values()
62 impls.sort()
63 for impl in impls:
64 add_impl(root, impl)
66 doc.writexml(file(path + '.new', 'w'), addindent = " ", newl = '\n')
67 os.rename(path + '.new', path)