edouard@1940: #!/usr/bin/env python
edouard@1940: # -*- coding: utf-8 -*-
edouard@1940: # This file is part of Beremiz.
edouard@1940: # See COPYING file for copyrights details.
edouard@1940: 
kinsamanka@3750: 
edouard@1940: import os
edouard@1940: from lxml import etree
edouard@1940: import util.paths as paths
kinsamanka@3751: from . structures import StdBlckLibs
Edouard@2627: from XSLTransform import XSLTransform
edouard@1940: 
edouard@1940: ScriptDirectory = paths.AbsDir(__file__)
edouard@1940: 
Edouard@2627: class XSLTModelQuery(XSLTransform):
edouard@1940:     """ a class to handle XSLT queries on project and libs """
Edouard@1953:     def __init__(self, controller, xsltpath, ext=None):
Edouard@2627: 
edouard@1940:         # arbitrary set debug to false, updated later
edouard@1940:         self.debug = False
edouard@1940: 
edouard@1940:         # merge xslt extensions for library access to query specific ones
edouard@1940:         xsltext = [
Edouard@1953:             ("GetProject", lambda *_ignored:
Edouard@1953:              [controller.GetProject(self.debug)]),
Edouard@1953:             ("GetStdLibs", lambda *_ignored:
kinsamanka@3750:              [lib for lib in list(StdBlckLibs.values())]),
Edouard@1953:             ("GetExtensions", lambda *_ignored:
Edouard@1953:              [ctn["types"] for ctn in controller.ConfNodeTypes])
Edouard@1953:         ]
edouard@1940: 
Edouard@1953:         if ext is not None:
Edouard@1953:             xsltext.extend(ext)
Edouard@1953: 
Edouard@2627:         XSLTransform.__init__(self,
Edouard@2627:                            os.path.join(ScriptDirectory, xsltpath),
Edouard@2627:                            xsltext)
edouard@1940:     def _process_xslt(self, root, debug, **kwargs):
edouard@1940:         self.debug = debug
Edouard@2627:         return self.transform(root, **kwargs)
edouard@1944: 
edouard@1944: # -------------------------------------------------------------------------------
edouard@1944: #           Helpers functions for translating list of arguments
edouard@1944: #                       from xslt to valid arguments
edouard@1944: # -------------------------------------------------------------------------------
edouard@1944: 
edouard@1944: 
edouard@1944: def _StringValue(x):
edouard@1944:     return x
edouard@1944: 
edouard@1944: 
edouard@1944: def _BoolValue(x):
edouard@1944:     return x in ["true", "0"]
edouard@1944: 
edouard@1944: 
edouard@1944: def _translate_args(translations, args):
edouard@1944:     return [translate(arg[0]) if len(arg) > 0 else None
edouard@1944:             for translate, arg in
edouard@1944:             zip(translations, args)]