parser.py 3.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/

__authors__ = ["H.Payno"]
__license__ = "MIT"
__date__ = "19/08/2019"

from collections import namedtuple
31
32
33
34
import importlib
import logging

_logger = logging.getLogger(__name__)
35
36
37
38


class Parser(object):
    @staticmethod
39
    def scheme_load(file_, load_handlers=True):
40
41
        """

42
43
44
        :param file_: file containing the scheme definition
        :param bool load_handlers: try to load the handlers of each node. Used
                                   to make sure the process won't fail
45
46
47
48
        :return: :class:`Scheme`
        """
        raise NotImplementedError()

49
50
    @staticmethod
    def get_aliases():
51
52
53
54
55
56
        """

        :return: the list of aliases for a processing_pt (module.class or
                 module.function name)
        :rtype: dict
        """
57
58
        aliases = {}
        try:
59
            import ppfaddon
60
61
62
63
        except ImportError:
            return aliases
        else:
            import pkgutil
64
            for importer, modname, ispkg in pkgutil.iter_modules(ppfaddon.__path__):
65
                try:
66
                    mod_name = '.'.join((ppfaddon.__name__, modname, 'aliases'))
67
68
69
70
71
72
73
74
75
76
                    module = importlib.import_module(mod_name)
                except ImportError:
                    _logger.warning(modname + ' does not fit the add-on design, skip it')
                else:
                    if hasattr(module, 'aliases'):
                        new_aliases = getattr(module, 'aliases')
                        if not isinstance(new_aliases, dict):
                            raise TypeError('aliases should be an instance of dict')
                        else:
                            aliases.update(new_aliases)
77
        return aliases
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    __main__ = scheme_load


_scheme = namedtuple(
    "_scheme",
    ["title", "version", "description", "nodes", "links", "annotations"])

_node = namedtuple(
    "_node",
    ["id", "title", "name", "position", "project_name", "qualified_name",
     "version", "data"])

_data = namedtuple(
    "_data",
    ["format", "data"])

_link = namedtuple(
    "_link",
    ["id", "source_node_id", "sink_node_id", "source_channel", "sink_channel",
     "enabled"])

_annotation = namedtuple(
    "_annotation",
    ["id", "type", "params"])

_text_params = namedtuple(
    "_text_params",
    ["geometry", "text", "font"])

_arrow_params = namedtuple(
    "_arrow_params",
    ["geometry", "color"])

_nxNode = namedtuple(
    "_node",
    ["id", "class_", "properties", "data", "qualified_name"])

_nxNodeProperty = namedtuple(
    "_property",
    ["name", "class_", "value"])

_nxLink = namedtuple(
    "_nxLink",
    ["port", "relation"])

_nxRelation = namedtuple(
    "_relation",
    ["id", "class_", 'properties'])