Large-scale API cleanup
[zeroinstall.git] / zeroinstall / injector / writer.py
blob0617a97033ed8e761ae24b46efd2266f2c4ea5a3
1 """
2 Save per-interface and per-feed configuration information.
3 """
5 # Copyright (C) 2009, Thomas Leonard
6 # See the README file for details, or visit http://0install.net.
8 from zeroinstall import _
9 import os
10 from xml.dom import minidom, XMLNS_NAMESPACE
12 from zeroinstall.support import basedir
14 from zeroinstall.injector import model
15 from zeroinstall.injector.namespaces import config_site, config_prog, XMLNS_IFACE
17 def _atomic_save(doc, parent, uri):
18 import tempfile
19 tmp_fd, tmp_name = tempfile.mkstemp(dir = parent)
20 try:
21 tmp_file = os.fdopen(tmp_fd, 'w')
22 doc.writexml(tmp_file, addindent = " ", newl = '\n')
23 tmp_file.close()
24 path = os.path.join(parent, model._pretty_escape(uri))
25 os.rename(tmp_name, path)
26 except:
27 os.unlink(tmp_name)
28 raise
30 def save_feed(feed):
31 """Save information about a feed. Currently, this is the last_checked time and any user-set stability ratings.
32 @since: 0.49"""
33 feeds = basedir.save_config_path(config_site, config_prog, 'feeds')
35 impl = minidom.getDOMImplementation()
36 doc = impl.createDocument(XMLNS_IFACE, 'feed-preferences', None)
37 root = doc.documentElement
38 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE)
40 if feed.last_checked:
41 root.setAttribute('last-checked', str(feed.last_checked))
43 impls = feed.implementations.values()
44 impls.sort()
45 for impl in impls:
46 if impl.user_stability:
47 node = doc.createElementNS(XMLNS_IFACE, 'implementation')
48 root.appendChild(node)
49 node.setAttribute('user-stability', str(impl.user_stability))
50 node.setAttribute('id', impl.id)
52 _atomic_save(doc, feeds, feed.url)
54 def save_interface(interface):
55 user_overrides = basedir.save_config_path(config_site, config_prog, 'interfaces')
57 impl = minidom.getDOMImplementation()
58 doc = impl.createDocument(XMLNS_IFACE, 'interface-preferences', None)
60 root = doc.documentElement
61 root.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', XMLNS_IFACE)
62 root.setAttribute('uri', interface.uri)
64 if interface.stability_policy:
65 root.setAttribute('stability-policy', str(interface.stability_policy))
67 for feed in interface.extra_feeds:
68 if feed.user_override:
69 elem = doc.createElementNS(XMLNS_IFACE, 'feed')
70 root.appendChild(elem)
71 elem.setAttribute('src', feed.uri)
72 if feed.arch:
73 elem.setAttribute('arch', feed.arch)
75 _atomic_save(doc, user_overrides, interface.uri)