Пример #1
0
    def start(self, provider, host, conn, **kwargs):
        xslt_file = kwargs.get('xslt')
        xsd_file  = kwargs.get('xsd')
        infile    = kwargs.get('input')
        outfile   = kwargs.get('output')
        if not xslt_file.startswith('/'):
            xslt_file = os.path.join(self.xsl_dir, xslt_file)
        if xsd_file and not xsd_file.startswith('/'):
            xsd_file = os.path.join(self.xsl_dir, xsd_file)

        # Fetch the latest version of the file.
        path = provider.store.get_path(host, infile)
        if path is None:
            return # file not found
        input = provider.store.get(host, infile)

        # Transform.
        doc    = etree.parse(StringIO(input), base_url = path)
        xsl    = etree.parse(xslt_file)
        result = apply_xslt(os.path.dirname(path), xsl, doc)

        # Insert extra data into the resulting XML.
        for path, expression in self.add:
            text = eval(expression, {'host': host, 'time': time})
            _find_or_create(result.getroot(), path, text)

        # Validate.
        if xsd_file:
            xsd    = etree.parse(xsd_file)
            schema = etree.XMLSchema(xsd)
            schema.assertValid(result)

        # Write.
        if self.debug:
            print str(result)
        else:
            provider.store.store(provider, host, outfile, str(result))
Пример #2
0
    def start(self, provider, host, conn, **kwargs):
        xslt_file = kwargs.get('xslt')
        xsd_file = kwargs.get('xsd')
        infile = kwargs.get('input')
        outfile = kwargs.get('output')
        if not xslt_file.startswith('/'):
            xslt_file = os.path.join(self.xsl_dir, xslt_file)
        if xsd_file and not xsd_file.startswith('/'):
            xsd_file = os.path.join(self.xsl_dir, xsd_file)

        # Fetch the latest version of the file.
        path = provider.store.get_path(host, infile)
        if path is None:
            return  # file not found
        input = provider.store.get(host, infile)

        # Transform.
        doc = etree.parse(StringIO(input), base_url=path)
        xsl = etree.parse(xslt_file)
        result = apply_xslt(os.path.dirname(path), xsl, doc)

        # Insert extra data into the resulting XML.
        for path, expression in self.add:
            text = eval(expression, {'host': host, 'time': time})
            _find_or_create(result.getroot(), path, text)

        # Validate.
        if xsd_file:
            xsd = etree.parse(xsd_file)
            schema = etree.XMLSchema(xsd)
            schema.assertValid(result)

        # Write.
        if self.debug:
            print str(result)
        else:
            provider.store.store(provider, host, outfile, str(result))
Пример #3
0
#!/usr/bin/env python
import os
import sys
from optparse import OptionParser
from lxml import etree
from util import apply_xslt

parser = OptionParser(usage='%prog xslt_file xml_file')
options, args = parser.parse_args(sys.argv)
args.pop(0)

try:
    xslt_file = args.pop(0)
except IndexError:
    parser.error('please the name of the XSLT file')

try:
    xml_file = args.pop(0)
except IndexError:
    parser.error('please enter the name of the XML file')

doc = etree.parse(xml_file, base_url=xml_file)
xsl = etree.parse(xslt_file)

path = os.path.dirname(xml_file)
print apply_xslt(path, xsl, doc)
Пример #4
0
#!/usr/bin/env python
import os
import sys
from optparse import OptionParser
from lxml import etree
from util import apply_xslt

parser = OptionParser(usage = '%prog xslt_file xml_file')
options, args = parser.parse_args(sys.argv)
args.pop(0)

try:
    xslt_file = args.pop(0)
except IndexError:
    parser.error('please the name of the XSLT file')

try:
    xml_file = args.pop(0)
except IndexError:
    parser.error('please enter the name of the XML file')

doc = etree.parse(xml_file, base_url = xml_file)
xsl = etree.parse(xslt_file)

path = os.path.dirname(xml_file)
print apply_xslt(path, xsl, doc)