File: parameters.py

package info (click to toggle)
themole 0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 900 kB
  • sloc: python: 9,957; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,853 bytes parent folder | download | duplicates (3)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Developed by: Nasel(http://www.nasel.com.ar)
#
# Authors:
# Matías Fontanini
# Santiago Alessandri
# Gastón Traberg

class Parameter:
    def __init__(self, exec_function=None, no_args_str='Expected arguments', invalid_args_function=None):
        self.children = {}
        self.generator = None
        self.exec_function = exec_function
        if invalid_args_function is None:
            invalid_args_function = Parameter.default_inv_args
        self.no_args_str = no_args_str
        self.invalid_args_function = invalid_args_function

    def set_param_generator(self, generator):
        self.generator = generator

    def add_parameter(self, name, param):
        self.children[name] = param

    def parameter_list(self, mole, params):
        if self.generator is not None:
            my_params = self.generator(mole, params)
        else:
            my_params = self.children
        if len(params) > 0:
            if params[0] in my_params:
                return my_params[params[0]].parameter_list(mole, params[1:])
            else:
                return []
        else:
            return my_params.keys()
        
    @classmethod
    def default_inv_args(cls, arg):
        print("Invalid argument '{0}'".format(arg))

    def execute(self, mole, params):
        if len(params) > 0:
            if self.generator is not None:
                my_params = self.generator(mole, params)
            else:
                my_params = self.children
            
            if params[0] in my_params:
                return my_params[params[0]].execute(mole, params[1:])
            else:
                if self.generator is None and len(self.children) > 0:
                    self.invalid_args_function(params[0])
                else:
                    return self._exec(mole, params)
        else:
            return self._exec(mole, params)

    def _exec(self, mole, params):
        if self.exec_function is None:
            output_manager.error(self.no_args_str).line_break()
            return False
        else:
            self.exec_function(mole, params)
            return True