-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathcommands.py
More file actions
164 lines (138 loc) · 5.16 KB
/
Copy pathcommands.py
File metadata and controls
164 lines (138 loc) · 5.16 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
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from collections import OrderedDict
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import gettext_lazy as _
from jsonschema import Draft4Validator
from .settings import ORGANIZATION_ENABLED_COMMANDS, USER_COMMANDS
DEFAULT_COMMANDS = OrderedDict(
(
(
'custom',
{
'label': _('Custom commands'),
'schema': {
'title': _('Custom'),
'type': 'object',
'properties': {
'command': {
'type': 'string',
'minLength': 1,
'title': _('Command'),
'pattern': '.',
},
},
'message': _('Command cannot be empty.'),
'required': ['command'],
'additionalProperties': False,
},
},
),
(
'reboot',
{
'label': _('Reboot'),
'schema': {
'title': _('Reboot'),
'type': 'null',
'additionalProperties': False,
},
},
),
(
'change_password',
{
'label': _('Change password'),
'schema': {
'title': _('Change Password'),
'type': 'object',
'required': ['password', 'confirm_password'],
'properties': {
'password': {
'$ref': '#/definitions/password_regex',
'title': _('Password'),
},
'confirm_password': {
'$ref': '#/definitions/password_regex',
'title': _('Confirm Password'),
},
},
'message': _('Your password must be atleast 6 characters long'),
'additionalProperties': False,
'definitions': {
'password_regex': {
'type': 'string',
'minLength': 6,
'maxLength': 30,
'pattern': r'[\S]',
}
},
},
},
),
)
)
COMMANDS = DEFAULT_COMMANDS.copy()
COMMAND_CHOICES = [
(command, command_config['label']) for command, command_config in COMMANDS.items()
]
def get_command_schema(command):
try:
return COMMANDS[command]['schema']
except KeyError:
raise ImproperlyConfigured(f'No such Command, {command}')
def get_command_callable(command):
try:
return COMMANDS[command]['callable']
except KeyError:
raise ImproperlyConfigured(f'No such Command, {command}')
def _validate_command(command_config):
options = command_config.keys()
assert 'label' in options
assert 'schema' in options
assert 'callable' in options
Draft4Validator(command_config['schema'])
def register_command(command_name, command_config):
"""
Registers a new command.
register_command(str,dict)
"""
if not isinstance(command_name, str):
raise ImproperlyConfigured('Command name should be type `str`.')
if not isinstance(command_config, dict):
raise ImproperlyConfigured('Command configuration should be type `dict`.')
if command_name in COMMANDS:
raise ImproperlyConfigured(f'{command_name} is an already registered Command.')
_validate_command(command_config)
COMMANDS.update({command_name: command_config})
_register_command_choice(command_name, command_config)
def unregister_command(command_name):
if not isinstance(command_name, str):
raise ImproperlyConfigured('Command name should be type `str`')
if command_name not in COMMANDS:
raise ImproperlyConfigured(f'No such Command, {command_name}')
COMMANDS.pop(command_name)
_unregister_command_choice(command_name)
def _register_command_choice(command_name, command_config):
label = command_config.get('label', command_name)
COMMAND_CHOICES.append((command_name, label))
def _unregister_command_choice(command):
for index, (key, name) in enumerate(COMMAND_CHOICES):
if key == command:
COMMAND_CHOICES.pop(index)
return
raise ImproperlyConfigured(f'No such Command choice {command}')
# Add USER_COMMANDS
for command_name, command_config in USER_COMMANDS:
register_command(command_name, command_config)
for org, commands in ORGANIZATION_ENABLED_COMMANDS.items():
if commands == '*':
ORGANIZATION_ENABLED_COMMANDS[org] = tuple(COMMANDS.keys())
ORGANIZATION_COMMAND_SCHEMA = {}
for org_id, commands in ORGANIZATION_ENABLED_COMMANDS.items():
ORGANIZATION_COMMAND_SCHEMA[org_id] = OrderedDict()
for command in commands:
ORGANIZATION_COMMAND_SCHEMA[org_id][command] = COMMANDS[command]['schema']
def get_command_choices():
"""
Returns the command choices.
"""
return COMMAND_CHOICES