This repository was archived by the owner on Mar 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhandlers.py
103 lines (80 loc) · 3.29 KB
/
handlers.py
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
import tempfile
import FreeCAD
from APIClient import fancy_handle, APICallResult
import Utils
logger = Utils.getLogger(__name__)
class HandlerException(Exception):
pass
def download_shared_model_to_memory(api, id_shared_model):
shared_model = None
def get_shared_model():
nonlocal shared_model
shared_model = api.getSharedModel(id_shared_model)
do_api_call(get_shared_model)
if not shared_model:
raise HandlerException(f"Unable to locate share link ${id_shared_model}")
if not shared_model["canDownloadDefaultModel"]:
raise HandlerException(
"This share link does not permit downloads of the original file"
)
model = shared_model["model"]
file = model["file"]
real_filename = file["custFileName"]
version_id = str(file["currentVersionId"])
versions = file["versions"]
unique_filename = [
ver["uniqueFileName"] for ver in versions if str(ver["_id"]) == version_id
][0]
return download_to_memory(api, unique_filename, real_filename)
def do_api_call(func):
api_result = fancy_handle(func)
if api_result == APICallResult.OK:
pass
elif api_result == APICallResult.DISCONNECTED:
raise HandlerException(
"You must be connected to the Internet to get a file from Ondsel Lens"
)
elif api_result == APICallResult.NOT_LOGGED_IN:
raise HandlerException("You must be logged in to get a file from Ondsel Lens")
elif api_result == APICallResult.NOT_LOGGED_IN:
raise HandlerException("You must be logged in to get a file from Ondsel Lens")
elif api_result == APICallResult.PERMISSION_ISSUE:
raise HandlerException(
"You are not allowed to download this particular file from Ondsel Lens"
)
else:
raise HandlerException(
"General error found attempting to download file. See Report View tab."
)
def download_file_version_to_memory(api, file_id, version_id, public):
unique_filename = None
real_filename = None
def get_file_detail():
nonlocal unique_filename, real_filename
file_detail, version_detail = api.get_file_version_details(
file_id, version_id, public
)
unique_filename = version_detail.uniqueFileName
real_filename = file_detail.custFileName
do_api_call(get_file_detail)
return download_to_memory(api, unique_filename, real_filename)
def download_to_memory(api, unique_filename, real_filename):
if Utils.isOpenableByFreeCAD(real_filename):
suffix = "." + Utils.get_extension(real_filename)
with tempfile.NamedTemporaryFile(prefix="sl_", suffix=suffix) as tf:
api.downloadFileFromServerUsingHandle(unique_filename, tf)
tf.flush()
if Utils.is_freecad_document(real_filename):
FreeCAD.openDocument(tf.name)
FreeCAD.ActiveDocument.Label = real_filename
else:
FreeCAD.loadFile(tf.name)
FreeCAD.ActiveDocument.FileName = ""
return real_filename
else:
raise HandlerException(f"FreeCAD cannot open {real_filename}")
def warn_downloaded_file(name_file):
logger.warn(
f"Done downloading '{name_file}'. "
"Be sure to save to disk if you want to keep the model."
)