plcopen/XSLTModelQuery.py
changeset 1940 8dc4ebc97777
child 1943 9dc0e38552b2
equal deleted inserted replaced
1939:db478d17bc3a 1940:8dc4ebc97777
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 # This file is part of Beremiz.
       
     4 # See COPYING file for copyrights details.
       
     5 
       
     6 from __future__ import absolute_import
       
     7 import os
       
     8 from lxml import etree
       
     9 import util.paths as paths
       
    10 from plcopen.structures import StdBlckLibs
       
    11 
       
    12 ScriptDirectory = paths.AbsDir(__file__)
       
    13 
       
    14 class XSLTModelQuery(object):
       
    15     """ a class to handle XSLT queries on project and libs """
       
    16     def __init__(self, controller, xsltpath, ext = []):
       
    17         # arbitrary set debug to false, updated later
       
    18         self.debug = False
       
    19 
       
    20         # merge xslt extensions for library access to query specific ones
       
    21         xsltext = [
       
    22             ("GetProject", lambda *_ignored: 
       
    23                 controller.GetProject(self.debug)),
       
    24             ("GetStdLibs", lambda *_ignored: 
       
    25                 [lib for lib in StdBlckLibs.values()]),
       
    26             ("GetExtensions", lambda *_ignored: 
       
    27                 [ctn["types"] for ctn in controller.ConfNodeTypes])
       
    28         ] + ext
       
    29 
       
    30         # parse and compile. "beremiz" arbitrary namespace for extensions 
       
    31         self.xslt = etree.XSLT(
       
    32             etree.parse(
       
    33                 os.path.join(ScriptDirectory, xsltpath),
       
    34                 etree.XMLParser()),
       
    35             extensions={ ("beremiz", name):call for name, call in xsltext})
       
    36 
       
    37     def _process_xslt(self, root, debug, **kwargs):
       
    38         self.debug = debug
       
    39         return self.xslt(root,**{k:etree.XSLT.strparam(v) for k,v in kwargs.iteritems()})