diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2011-07-27 09:07:33 -0400 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2011-07-27 09:07:33 -0400 |
commit | c0a7ace1b35c61848a7d755cdb14bd61d13932ed (patch) | |
tree | 48486a2016b7328cbc481358405bae6fcd04cc35 | |
parent | 61766a5d46c15bb2b41eb07d822d04fb96ee8055 (diff) | |
download | bcfg2-c0a7ace1b35c61848a7d755cdb14bd61d13932ed.tar.gz bcfg2-c0a7ace1b35c61848a7d755cdb14bd61d13932ed.tar.bz2 bcfg2-c0a7ace1b35c61848a7d755cdb14bd61d13932ed.zip |
added write() method to Properties to write back persistent changes
-rw-r--r-- | doc/server/plugins/connectors/properties.txt | 17 | ||||
-rw-r--r-- | src/lib/Server/Plugins/Properties.py | 47 |
2 files changed, 59 insertions, 5 deletions
diff --git a/doc/server/plugins/connectors/properties.txt b/doc/server/plugins/connectors/properties.txt index 3329f48bd..7695e902c 100644 --- a/doc/server/plugins/connectors/properties.txt +++ b/doc/server/plugins/connectors/properties.txt @@ -53,8 +53,23 @@ more details on how Group and Client tags are parsed.) For instance:: if el.tag == "Server"] %} +If you need to make persistent changes to properties data, you can use +the ``write`` method of the ``PropertyFile`` class:: + + {% python + import lxml.etree + from genshi.template import TemplateError + lxml.etree.SubElement(metadata.Properties['foo.xml'], + "Client", + name=metadata.hostname) + if not metadata.Properties['foo.xml'].write(): + raise TemplateError("Failed to write changes back to foo.xml") + +The ``write`` method checks the data in the object against its schema +before writing it; see `Data Structures`_ for details. + As we formulate more common use cases, we will add them to the -!PropertyFile class as methods. This will simplify templates. +``PropertyFile`` class as methods. This will simplify templates. You can also access the XML data that comprises a property file directly in one of several ways: diff --git a/src/lib/Server/Plugins/Properties.py b/src/lib/Server/Plugins/Properties.py index 54c5def57..58f7215c9 100644 --- a/src/lib/Server/Plugins/Properties.py +++ b/src/lib/Server/Plugins/Properties.py @@ -1,12 +1,51 @@ +import os +import sys import copy +import logging import lxml.etree - import Bcfg2.Server.Plugin +logger = logging.getLogger('Bcfg2.Plugins.Properties') class PropertyFile(Bcfg2.Server.Plugin.StructFile): """Class for properties files.""" - pass + def write(self): + """ Write the data in this data structure back to the property + file """ + if self.validate_data(): + try: + open(self.name, + "wb").write(lxml.etree.tostring(self.xdata, + pretty_print=True)) + return True + except IOError: + err = sys.exc_info()[1] + logger.error("Failed to write %s: %s" % (self.name, err)) + return False + else: + return False + + def validate_data(self): + """ ensure that the data in this object validates against the + XML schema for this property file (if a schema exists) """ + schemafile = self.name.replace(".xml", ".xsd") + if os.path.exists(schemafile): + try: + schema = lxml.etree.XMLSchema(file=schemafile) + except: + logger.error("Failed to process schema for %s" % self.name) + return False + else: + # no schema exists + return True + + if not schema.validate(self.xdata): + logger.error("Data for %s fails to validate; run bcfg2-lint for " + "more details" % self.name) + return False + else: + return True + class PropDirectoryBacked(Bcfg2.Server.Plugin.DirectoryBacked): __child__ = PropertyFile @@ -28,8 +67,8 @@ class Properties(Bcfg2.Server.Plugin.Plugin, self.store = PropDirectoryBacked(self.data, core.fam) except OSError: e = sys.exc_info()[1] - Bcfg2.Server.Plugin.logger.error("Error while creating Properties " - "store: %s %s" % (e.strerror, e.filename)) + self.logger.error("Error while creating Properties store: %s %s" % + (e.strerror, e.filename)) raise Bcfg2.Server.Plugin.PluginInitError def get_additional_data(self, _): |