Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat is saveable #213

Merged
merged 21 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 80 additions & 25 deletions blueprints/blueprint_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def upload_file():

for file in files:
filename = werkzeug.utils.secure_filename(os.path.basename(file.filename))
print(f"{filename=}")
file.save(os.path.join(UPLOAD_FOLDER, filename))
return flask.make_response({"message": "File uploaded"}, 201)

Expand All @@ -99,10 +98,16 @@ def upload_file():
)
def allowed_objects():
geode_functions.validate_request(flask.request, tools_allowed_objects_json)
file_extension = os.path.splitext(flask.request.json["filename"])[1][1:]
allowed_objects = geode_functions.list_geode_objects(
file_extension, flask.request.json["key"]
)
filenames = flask.request.json["filenames"]
for index, filename in enumerate(filenames):
file_extension = os.path.splitext(filename)[1][1:]
filename_allowed_objects = geode_functions.list_geode_objects(
file_extension, flask.request.json["key"]
)
if index == 0:
allowed_objects = filename_allowed_objects
else:
allowed_objects = list(set(allowed_objects) & set(filename_allowed_objects))

return flask.make_response({"allowed_objects": allowed_objects}, 200)

Expand All @@ -119,21 +124,28 @@ def missing_files():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(flask.request, tools_missing_files_json)

missing_files = geode_functions.missing_files(
flask.request.json["input_geode_object"],
os.path.join(UPLOAD_FOLDER, flask.request.json["filename"]),
)
has_missing_files = missing_files.has_missing_files()
mandatory_files = missing_files.mandatory_files
additional_files = missing_files.additional_files

filenames = flask.request.json["filenames"]
has_missing_files = False
mandatory_files_list = []
for mandatory_file in mandatory_files:
mandatory_files_list.append(os.path.basename(mandatory_file))

additional_files_list = []
for additional_file in additional_files:
additional_files_list.append(os.path.basename(additional_file))

for index, filename in enumerate(filenames):
file_missing_files = geode_functions.missing_files(
flask.request.json["input_geode_object"],
os.path.join(UPLOAD_FOLDER, filename),
)
file_has_missing_files = file_missing_files.has_missing_files()
file_mandatory_files = file_missing_files.mandatory_files
file_additional_files = file_missing_files.additional_files

if file_has_missing_files:
has_missing_files = True

for mandatory_file in file_mandatory_files:
mandatory_files_list.append(os.path.basename(mandatory_file))

for additional_file in file_additional_files:
additional_files_list.append(os.path.basename(additional_file))

return flask.make_response(
{
Expand All @@ -145,6 +157,33 @@ def missing_files():
)


with open("blueprints/tools_geographic_coordinate_systems.json", "r") as file:
JulienChampagnol marked this conversation as resolved.
Show resolved Hide resolved
tools_geographic_coordinate_systems_json = json.load(file)


@tools_routes.route(
tools_geographic_coordinate_systems_json["route"],
methods=tools_geographic_coordinate_systems_json["methods"],
)
def geographic_coordinate_systems():
geode_functions.validate_request(
flask.request, tools_geographic_coordinate_systems_json
)
infos = geode_functions.geographic_coordinate_systems(
flask.request.json["input_geode_object"]
)
crs_list = []

for info in infos:
crs = {}
crs["name"] = info.name
crs["code"] = info.code
crs["authority"] = info.authority
crs_list.append(crs)

return flask.make_response({"crs_list": crs_list}, 200)


with open("blueprints/tools_geode_objects_and_output_extensions.json", "r") as file:
tools_geode_objects_and_output_extensions_json = json.load(file)

Expand All @@ -154,17 +193,33 @@ def missing_files():
methods=tools_geode_objects_and_output_extensions_json["methods"],
)
def geode_objects_and_output_extensions():
UPLOAD_FOLDER = flask.current_app.config["UPLOAD_FOLDER"]
geode_functions.validate_request(
flask.request, tools_geode_objects_and_output_extensions_json
)
geode_objects_output_extensions = geode_functions.geode_objects_output_extensions(
flask.request.json["input_geode_object"]
)
geode_objects_and_output_extensions = (
geode_functions.geode_objects_output_extensions(
flask.request.json["input_geode_object"]
filenames = flask.request.json["filenames"]
input_geode_object = flask.request.json["input_geode_object"]
geode_objects_and_output_extensions = {}

for index, filename in enumerate(filenames):
data = geode_functions.load(
input_geode_object, os.path.join(UPLOAD_FOLDER, filename)
)
)
file_geode_objects_and_output_extensions = (
geode_functions.geode_objects_output_extensions(input_geode_object, data)
)

if index == 0:
geode_objects_and_output_extensions = (
file_geode_objects_and_output_extensions
)
else:
for geode_object, value in file_geode_objects_and_output_extensions.items():
for output_extension, output_extension_value in value.items():
if not output_extension_value["is_saveable"]:
geode_objects_and_output_extensions[geode_object][
output_extension
]["is_saveable"] = False

return flask.make_response(
{"geode_objects_and_output_extensions": geode_objects_and_output_extensions},
Expand Down
42 changes: 2 additions & 40 deletions blueprints/tools/blueprint_crs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,6 @@ def crs_converter_versions():
)


with open(
"blueprints/tools/crs_converter_geographic_coordinate_systems.json", "r"
) as file:
crs_converter_geographic_coordinate_systems_json = json.load(file)


@crs_converter_routes.route(
crs_converter_geographic_coordinate_systems_json["route"],
methods=crs_converter_geographic_coordinate_systems_json["methods"],
)
def crs_converter_geographic_coordinate_systems():
geode_functions.validate_request(
flask.request, crs_converter_geographic_coordinate_systems_json
)
infos = geode_functions.geographic_coordinate_systems(
flask.request.json["input_geode_object"]
)
crs_list = []

for info in infos:
crs = {}
crs["name"] = info.name
crs["code"] = info.code
crs["authority"] = info.authority
crs_list.append(crs)

return flask.make_response({"crs_list": crs_list}, 200)


with open("blueprints/tools/crs_converter_convert_file.json", "r") as file:
crs_converter_convert_file_json = json.load(file)

Expand Down Expand Up @@ -92,19 +63,10 @@ async def crs_converter_convert_file():
flask.request.json["input_geode_object"], data, flask.request.json["output_crs"]
)

geode_functions.save(
saved_files = geode_functions.save(
flask.request.json["output_geode_object"],
data,
os.path.abspath(UPLOAD_FOLDER),
new_file_name,
)
response = flask.send_from_directory(
directory=UPLOAD_FOLDER,
path=new_file_name,
as_attachment=True,
mimetype="application/octet-binary",
)
response.headers["new-file-name"] = new_file_name
response.headers["Access-Control-Expose-Headers"] = "new-file-name"

return response
return geode_functions.send_file(UPLOAD_FOLDER, saved_files, new_file_name)
42 changes: 2 additions & 40 deletions blueprints/tools/blueprint_file_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,48 +54,10 @@ async def file_converter_convert_file():
strict_file_name = os.path.splitext(secure_filename)[0]
new_file_name = strict_file_name + "." + flask.request.json["output_extension"]

sub_folder = f"{UPLOAD_FOLDER}/{strict_file_name}/"
if os.path.exists(sub_folder):
shutil.rmtree(sub_folder)

geode_functions.save(
saved_files = geode_functions.save(
flask.request.json["output_geode_object"],
data,
os.path.abspath(UPLOAD_FOLDER),
new_file_name,
)
mimetype = "application/octet-binary"

list_exceptions = ["triangle", "vtm"]
if flask.request.json["output_extension"] in list_exceptions:
if flask.request.json["output_extension"] == "triangle":
os.mkdir(sub_folder)
os.chdir(sub_folder)
generated_files = f"{UPLOAD_FOLDER}/{strict_file_name}"
shutil.move(generated_files + ".ele", sub_folder)
shutil.move(generated_files + ".neigh", sub_folder)
shutil.move(generated_files + ".node", sub_folder)
os.chdir("..")
elif flask.request.json["output_extension"] == "vtm":
generated_files = f"{UPLOAD_FOLDER}/{strict_file_name}"
shutil.move(generated_files + ".vtm", sub_folder)
new_file_name = strict_file_name + ".zip"
mimetype = "application/zip"
with zipfile.ZipFile(f"{UPLOAD_FOLDER}/{new_file_name}", "w") as zipObj:
for folder_name, sub_folders, file_names in os.walk(sub_folder):
for flask.request.json["filename"] in file_names:
file_path = os.path.join(
folder_name, flask.request.json["filename"]
)
zipObj.write(file_path, os.path.basename(file_path))

response = flask.send_from_directory(
directory=UPLOAD_FOLDER,
path=new_file_name,
as_attachment=True,
mimetype=mimetype,
)
response.headers["new-file-name"] = new_file_name
response.headers["Access-Control-Expose-Headers"] = "new-file-name"

return response
return geode_functions.send_file(UPLOAD_FOLDER, saved_files, new_file_name)
11 changes: 8 additions & 3 deletions blueprints/tools_allowed_objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
"methods": ["POST"],
"type": "object",
"properties": {
"filename": {
"type": "string"
"filenames": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"key": {
"type": ["string", "null"]
}
},
"required": ["filename", "key"],
"required": ["filenames", "key"],
"additionalProperties": false
}
10 changes: 9 additions & 1 deletion blueprints/tools_geode_objects_and_output_extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
"properties": {
"input_geode_object": {
"type": "string"
},
"filenames": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
}
},
"required": ["input_geode_object"],
"required": ["input_geode_object", "filenames"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$id": "/tools/crs_converter/geographic_coordinate_systems",
"$id": "/tools/geographic_coordinate_systems",
"route": "/geographic_coordinate_systems",
"methods": ["POST"],
"type": "object",
Expand Down
11 changes: 8 additions & 3 deletions blueprints/tools_missing_files.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
"input_geode_object": {
"type": "string"
},
"filename": {
"type": "string"
"filenames": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
}
},
"required": ["input_geode_object", "filename"],
"required": ["input_geode_object", "filenames"],
"additionalProperties": false
}
Loading
Loading