in modular_api/commands_generator.py [0:0]
def _get_api_route_flag_and_secured_params(self, subgroup):
group_content = inspect.getsource(self._module)
lines = group_content.split('\n')
command_definitions = {}
for index, line in enumerate(lines):
if line.startswith('#'):
continue
if '@{}.command'.format(self._group_name) in line:
# find from @{group}.command to enclosing """ of docstring
command_lines = [line]
comment_close_sum_counter = 0
counter = 1
# definition of command function
command_def_line_passed = False
while comment_close_sum_counter != 2:
current_line = lines[index + counter]
if 'def ' in current_line:
command_def_line_passed = True
if command_def_line_passed and '):' in current_line and \
'\"\"\"' not in lines[index + counter + 1]:
comment_close_sum_counter = 2 # to break the loop
if '\"\"\"' in current_line:
comment_close_sum_counter += 1
counter += 1
continue
command_lines.append(current_line.strip())
counter += 1
# prepare for analysis
prepared_lines = []
for i, line in enumerate(command_lines):
if not str(line).startswith('@') and not \
str(line).startswith('def'):
prepared_lines[-1] = prepared_lines[-1] + line
else:
prepared_lines.append(line)
# analyze lines
name = None
is_command_hidden = False
route_config = {}
secure_parameters = []
files_parameters = {}
security_parameters_found = False
secured_parameters_string = ''
flag_parameters = []
for line in prepared_lines:
if f'@{self._group_name}.command' in line:
name = line.split('\'')[1]
if '@api_route' in line:
route_config = self._get_route_configuration_from_line(
line=line, default=route_config)
if '@click.option' in line:
response = _get_param_def_from_line(line)
param_name = response.get('name')
if response.get('is_flag'):
flag_parameters.append(param_name)
convert_content_to_file = response.get('convert_content_to_file')
temp_file_extension = response.get('temp_file_extension')
if any([convert_content_to_file, temp_file_extension]):
files_parameters.update(
{
param_name: {
'convert_content_to_file': convert_content_to_file,
'temp_file_extension': temp_file_extension
}
}
)
if '@shadow_wrapper' in line:
is_command_hidden = True
if 'secured_params=' in line \
and not security_parameters_found:
if ']' in line:
security_parameters_found = True
secured_parameters_string += line
secure_parameters = self._get_parameters_to_be_secured(
secured_parameters_string)
secured_parameters_string += line
default_rt = self._get_default_route_config(
group_full_name=self._group_name,
command_name=name,
subgroup=subgroup
)
route_config = self._merge_route_configs(primary=route_config,
secondary=default_rt)
command_definitions.update({
name: {
'route': route_config,
'secure_parameters': secure_parameters,
'files_parameters': files_parameters,
'is_command_hidden': is_command_hidden,
'flag_parameters': flag_parameters
}
})
return command_definitions