rules.py 4.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# Copyright (C) 2015 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

from abc import ABC, abstractmethod

Floreal Cabanettes's avatar
Floreal Cabanettes committed
20
21
from jflow.parameter import InputFile, InputFileList, InputDirectory, MultiParameter, MultiParameterList

22
23
24
25
26

class SimpleRule (ABC):
    def __init__(self, user_args, wf_instance, src_arg, all_files=None):
        self.user_args = user_args
        self.wf_instance = wf_instance
Floreal Cabanettes's avatar
Floreal Cabanettes committed
27
28
        self.parameter_name = src_arg
        self.parameter_value = user_args[src_arg]
29
        self.all_files = all_files
Floreal Cabanettes's avatar
Floreal Cabanettes committed
30
31
32
33
34
35
36
37
38
39
40
        if ">" not in src_arg:
            self.wf_parameter = getattr(wf_instance, src_arg)
        else:
            self.wf_parameter = getattr(wf_instance, src_arg[:src_arg.index(">")]).\
                get_sub_parameters_by_name()[src_arg[src_arg.index(">")+1:]][0]

        self.is_file_list = isinstance(self.wf_parameter, InputFileList)
        self.is_a_file = isinstance(self.wf_parameter, InputFile)
        self.is_file = self.is_a_file or self.is_file_list
        self.is_directory = isinstance(self.wf_parameter, InputDirectory)
        self.is_file_or_directory = self.is_file or self.is_directory
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    @abstractmethod
    def check(self):
        pass


class LinkRule (SimpleRule):
    def __init__(self, user_args, wf_instance, src_arg, targets_args, all_files=None):
        SimpleRule.__init__(self, user_args, wf_instance, src_arg, all_files)
        self.targets_args = targets_args

    @abstractmethod
    def check(self):
        pass


class ConditionalLinkRule(LinkRule):
    def __init__(self, user_args, wf_instance, src_arg, targets_args, conditions, all_files=None):
        LinkRule.__init__(self, user_args, wf_instance, src_arg, targets_args, all_files)
        self.conditions = conditions

Floreal Cabanettes's avatar
Floreal Cabanettes committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        # Check parameter is correct:
        self.check_type_argument()

        # Test condition is raised:
        self.condition_raised = None
        for condition in self.conditions:
            if (condition[0] == "!" and str(self.user_args[src_arg]) != str(condition[1:])) or (condition[0] != "!" and
               str(self.user_args[src_arg]) == str(condition)):
                self.condition_raised = condition
                break

    def check_type_argument(self):
        if isinstance(self.wf_parameter, InputFile) or isinstance(self.wf_parameter, InputFileList) or \
                isinstance(self.wf_parameter, InputDirectory) or isinstance(self.wf_parameter, MultiParameter) or \
                isinstance(self.wf_parameter, MultiParameterList):
            raise Exception("Rule " + str(self.__class__.__name__) + " (ConditionalLinkRule) is not available on files "
                            "and directories parameters or multiple parameters")

80
81
82
    @abstractmethod
    def check(self):
        pass
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


class Exclude(LinkRule):

    def check(self):
        for exclude in self.targets_args:
            if exclude in self.user_args and self.user_args[exclude] is not None:  # The target is found
                raise Exception("arguments " + self.parameter_name + " and " + exclude + " are mutually excluded")


class ExcludeIf(ConditionalLinkRule):

    def check(self):
        if self.condition_raised is not None:
            validator = Exclude(self.user_args, self.wf_instance, self.parameter_name, self.targets_args, self.all_files)
            try:
                validator.check()
            except Exception as e:
                raise Exception("Argument " + self.parameter_name + " is set to " + self.condition_raised + ": " +
                                str(e))


class ToBeRequired(LinkRule):

    def check(self):
        for require in self.targets_args:
            if require not in self.user_args or (require in self.user_args and self.user_args[require] is None):  # The target is not found
                raise Exception("the argument " + self.parameter_name + " require argument " + require +
                                " to be defined")


class ToBeRequiredIf(ConditionalLinkRule):

    def check(self):

        if self.condition_raised is not None:
            validator = ToBeRequired(self.user_args, self.wf_instance, self.parameter_name, self.targets_args,
                                     self.all_files)
            try:
                validator.check()
            except Exception as e:
                raise Exception("Argument " + self.parameter_name + " is set to " + self.condition_raised + ": " +
                                str(e))