-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAction.py
More file actions
54 lines (41 loc) · 1.52 KB
/
Action.py
File metadata and controls
54 lines (41 loc) · 1.52 KB
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
from constants import ACTIONS, N_ARGUMENTS, USER_MSG
# Only the actions and number of arguments are permitted.
VALID_ACTIONS = ACTIONS
VALID_ARGUMENTS = N_ARGUMENTS
class Action:
def __init__(self, arguments):
self.name = arguments[0] if len(arguments) > 0 else ""
self.arguments = arguments[0:]
self.n_args = len(arguments) - 1
self.error_message = ""
def is_valid(self) -> bool:
if not self.is_valid_action() or self.is_empty():
return False
if not self.has_valid_arguments():
return False
return True
def is_empty(self) -> bool:
self.error_message = USER_MSG["NO_ACTION"]
return True if self.name == "" else False
def get_name(self) -> str:
return self.name
def get_n_args(self) -> int:
return self.n_args
def has_valid_arguments(self) -> bool:
pos = VALID_ACTIONS.index(self.name)
expected_args = N_ARGUMENTS[pos]
if isinstance(expected_args, list):
if self.n_args in expected_args:
return True
elif expected_args == self.n_args:
return True
self.error_message = USER_MSG["N_ARGS_NO_VALID"]
return False
def is_valid_action(self) -> bool:
if self.name not in VALID_ACTIONS:
self.error_message = USER_MSG["NO_VALID_ACTION"]
return self.name in VALID_ACTIONS
def get_error_message(self) -> str:
return self.error_message
def get_args(self):
return self.arguments