Add more files and lines when scaffolding (#1222)
* auto-generatr urls.py * add mocks to moto/__init__.py and moto/backends.py * add uri to urls.py * change output of scaffold.py
This commit is contained in:
parent
215aeded5d
commit
ea26466e6d
@ -107,6 +107,62 @@ def render_template(tmpl_dir, tmpl_filename, context, service, alt_filename=None
|
|||||||
f.write(rendered)
|
f.write(rendered)
|
||||||
|
|
||||||
|
|
||||||
|
def append_mock_to_init_py(service):
|
||||||
|
path = os.path.join(os.path.dirname(__file__), '..', 'moto', '__init__.py')
|
||||||
|
with open(path) as f:
|
||||||
|
lines = [_.replace('\n', '') for _ in f.readlines()]
|
||||||
|
|
||||||
|
if any(_ for _ in lines if re.match('^from.*mock_{}.*$'.format(service), _)):
|
||||||
|
return
|
||||||
|
filtered_lines = [_ for _ in lines if re.match('^from.*mock.*$', _)]
|
||||||
|
last_import_line_index = lines.index(filtered_lines[-1])
|
||||||
|
|
||||||
|
new_line = 'from .{} import mock_{} # flake8: noqa'.format(service, service)
|
||||||
|
lines.insert(last_import_line_index + 1, new_line)
|
||||||
|
|
||||||
|
body = '\n'.join(lines) + '\n'
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(body)
|
||||||
|
|
||||||
|
|
||||||
|
def append_mock_import_to_backends_py(service):
|
||||||
|
path = os.path.join(os.path.dirname(__file__), '..', 'moto', 'backends.py')
|
||||||
|
with open(path) as f:
|
||||||
|
lines = [_.replace('\n', '') for _ in f.readlines()]
|
||||||
|
|
||||||
|
if any(_ for _ in lines if re.match('^from moto\.{}.*{}_backends.*$'.format(service, service), _)):
|
||||||
|
return
|
||||||
|
filtered_lines = [_ for _ in lines if re.match('^from.*backends.*$', _)]
|
||||||
|
last_import_line_index = lines.index(filtered_lines[-1])
|
||||||
|
|
||||||
|
new_line = 'from moto.{} import {}_backends'.format(service, service)
|
||||||
|
lines.insert(last_import_line_index + 1, new_line)
|
||||||
|
|
||||||
|
body = '\n'.join(lines) + '\n'
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(body)
|
||||||
|
|
||||||
|
def append_mock_dict_to_backends_py(service):
|
||||||
|
path = os.path.join(os.path.dirname(__file__), '..', 'moto', 'backends.py')
|
||||||
|
with open(path) as f:
|
||||||
|
lines = [_.replace('\n', '') for _ in f.readlines()]
|
||||||
|
|
||||||
|
# 'xray': xray_backends
|
||||||
|
if any(_ for _ in lines if re.match(".*'{}': {}_backends.*".format(service, service), _)):
|
||||||
|
return
|
||||||
|
filtered_lines = [_ for _ in lines if re.match(".*'.*':.*_backends.*", _)]
|
||||||
|
last_elem_line_index = lines.index(filtered_lines[-1])
|
||||||
|
|
||||||
|
new_line = " '{}': {}_backends,".format(service, service)
|
||||||
|
prev_line = lines[last_elem_line_index]
|
||||||
|
if not prev_line.endswith('{') and not prev_line.endswith(','):
|
||||||
|
lines[last_elem_line_index] += ','
|
||||||
|
lines.insert(last_elem_line_index + 1, new_line)
|
||||||
|
|
||||||
|
body = '\n'.join(lines) + '\n'
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(body)
|
||||||
|
|
||||||
def initialize_service(service, operation, api_protocol):
|
def initialize_service(service, operation, api_protocol):
|
||||||
"""create lib and test dirs if not exist
|
"""create lib and test dirs if not exist
|
||||||
"""
|
"""
|
||||||
@ -115,11 +171,14 @@ def initialize_service(service, operation, api_protocol):
|
|||||||
|
|
||||||
print_progress('Initializing service', service, 'green')
|
print_progress('Initializing service', service, 'green')
|
||||||
|
|
||||||
service_class = boto3.client(service).__class__.__name__
|
client = boto3.client(service)
|
||||||
|
service_class = client.__class__.__name__
|
||||||
|
endpoint_prefix = client._service_model.endpoint_prefix
|
||||||
|
|
||||||
tmpl_context = {
|
tmpl_context = {
|
||||||
'service': service,
|
'service': service,
|
||||||
'service_class': service_class
|
'service_class': service_class,
|
||||||
|
'endpoint_prefix': endpoint_prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
# initialize service directory
|
# initialize service directory
|
||||||
@ -148,6 +207,11 @@ def initialize_service(service, operation, api_protocol):
|
|||||||
tmpl_dir, tmpl_filename, tmpl_context, service, alt_filename
|
tmpl_dir, tmpl_filename, tmpl_context, service, alt_filename
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# append mock to init files
|
||||||
|
append_mock_to_init_py(service)
|
||||||
|
append_mock_import_to_backends_py(service)
|
||||||
|
append_mock_dict_to_backends_py(service)
|
||||||
|
|
||||||
def to_upper_camel_case(s):
|
def to_upper_camel_case(s):
|
||||||
return ''.join([_.title() for _ in s.split('_')])
|
return ''.join([_.title() for _ in s.split('_')])
|
||||||
|
|
||||||
@ -324,6 +388,41 @@ def insert_code_to_class(path, base_class, new_code):
|
|||||||
f.write(body)
|
f.write(body)
|
||||||
|
|
||||||
|
|
||||||
|
def insert_url(service, operation):
|
||||||
|
client = boto3.client(service)
|
||||||
|
service_class = client.__class__.__name__
|
||||||
|
aws_operation_name = to_upper_camel_case(operation)
|
||||||
|
uri = client._service_model.operation_model(aws_operation_name).http['requestUri']
|
||||||
|
|
||||||
|
path = os.path.join(os.path.dirname(__file__), '..', 'moto', service, 'urls.py')
|
||||||
|
with open(path) as f:
|
||||||
|
lines = [_.replace('\n', '') for _ in f.readlines()]
|
||||||
|
|
||||||
|
if any(_ for _ in lines if re.match(uri, _)):
|
||||||
|
return
|
||||||
|
|
||||||
|
url_paths_found = False
|
||||||
|
last_elem_line_index = -1
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
if line.startswith('url_paths'):
|
||||||
|
url_paths_found = True
|
||||||
|
if url_paths_found and line.startswith('}'):
|
||||||
|
last_elem_line_index = i - 1
|
||||||
|
|
||||||
|
prev_line = lines[last_elem_line_index]
|
||||||
|
if not prev_line.endswith('{') and not prev_line.endswith(','):
|
||||||
|
lines[last_elem_line_index] += ','
|
||||||
|
|
||||||
|
new_line = " '{0}%s$': %sResponse.dispatch," % (
|
||||||
|
uri, service_class
|
||||||
|
)
|
||||||
|
lines.insert(last_elem_line_index + 1, new_line)
|
||||||
|
|
||||||
|
body = '\n'.join(lines) + '\n'
|
||||||
|
with open(path, 'w') as f:
|
||||||
|
f.write(body)
|
||||||
|
|
||||||
|
|
||||||
def insert_query_codes(service, operation):
|
def insert_query_codes(service, operation):
|
||||||
func_in_responses = get_function_in_responses(service, operation, 'query')
|
func_in_responses = get_function_in_responses(service, operation, 'query')
|
||||||
func_in_models = get_function_in_models(service, operation)
|
func_in_models = get_function_in_models(service, operation)
|
||||||
@ -346,6 +445,9 @@ def insert_query_codes(service, operation):
|
|||||||
print_progress('inserting code', models_path, 'green')
|
print_progress('inserting code', models_path, 'green')
|
||||||
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
||||||
|
|
||||||
|
# edit urls.py
|
||||||
|
insert_url(service, operation)
|
||||||
|
|
||||||
def insert_json_codes(service, operation):
|
def insert_json_codes(service, operation):
|
||||||
func_in_responses = get_function_in_responses(service, operation, 'json')
|
func_in_responses = get_function_in_responses(service, operation, 'json')
|
||||||
func_in_models = get_function_in_models(service, operation)
|
func_in_models = get_function_in_models(service, operation)
|
||||||
@ -360,6 +462,9 @@ def insert_json_codes(service, operation):
|
|||||||
print_progress('inserting code', models_path, 'green')
|
print_progress('inserting code', models_path, 'green')
|
||||||
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
||||||
|
|
||||||
|
# edit urls.py
|
||||||
|
insert_url(service, operation)
|
||||||
|
|
||||||
def insert_restjson_codes(service, operation):
|
def insert_restjson_codes(service, operation):
|
||||||
func_in_models = get_function_in_models(service, operation)
|
func_in_models = get_function_in_models(service, operation)
|
||||||
|
|
||||||
@ -369,6 +474,9 @@ def insert_restjson_codes(service, operation):
|
|||||||
print_progress('inserting code', models_path, 'green')
|
print_progress('inserting code', models_path, 'green')
|
||||||
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
insert_code_to_class(models_path, BaseBackend, func_in_models)
|
||||||
|
|
||||||
|
# edit urls.py
|
||||||
|
insert_url(service, operation)
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
def main():
|
def main():
|
||||||
service, operation = select_service_and_operation()
|
service, operation = select_service_and_operation()
|
||||||
@ -383,7 +491,7 @@ def main():
|
|||||||
else:
|
else:
|
||||||
print_progress('skip inserting code', 'api protocol "{}" is not supported'.format(api_protocol), 'yellow')
|
print_progress('skip inserting code', 'api protocol "{}" is not supported'.format(api_protocol), 'yellow')
|
||||||
|
|
||||||
click.echo('You will still need to make "{0}/urls.py", add the backend into "backends.py" and add the mock into "__init__.py"'.format(service))
|
click.echo('You will still need to add the mock into "__init__.py"'.format(service))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
9
scripts/template/lib/urls.py.j2
Normal file
9
scripts/template/lib/urls.py.j2
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
from .responses import {{ service_class }}Response
|
||||||
|
|
||||||
|
url_bases = [
|
||||||
|
"https?://{{ endpoint_prefix }}.(.+).amazonaws.com",
|
||||||
|
]
|
||||||
|
|
||||||
|
url_paths = {
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user