8
8
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
9
9
# not expressly granted therein are reserved by Shotgun Software Inc.
10
10
11
+ import filecmp
11
12
import logging
13
+ import json
12
14
import pprint
13
15
import os
16
+ import shutil
14
17
import socketio
15
18
16
- from ... api import alias_api
19
+ from tk_framework_alias_utils import environment_utils
17
20
18
- from ..api_request import AliasApiRequestWrapper
19
21
from ... import alias_bridge
22
+ from ...api import alias_api
23
+ from ..api_request import AliasApiRequestWrapper
24
+ from ..server_json import AliasServerJSON
20
25
from ...utils .invoker import execute_in_main_thread
21
26
from ...utils .exceptions import (
22
27
AliasApiRequestException ,
@@ -172,7 +177,7 @@ def on_get_alias_api(self, sid):
172
177
"""
173
178
Get the Alias API module.
174
179
175
- The module will be JSON-serialized before returning to the client.
180
+ The module will be JSON-serialized and returned to the client.
176
181
177
182
:param sid: The client session id that made the request.
178
183
:type sid: str
@@ -186,6 +191,70 @@ def on_get_alias_api(self, sid):
186
191
187
192
return alias_api
188
193
194
+ def on_load_alias_api (self , sid ):
195
+ """
196
+ Load the Alias API and JSON-serialize the module to file on disk.
197
+
198
+ This is an alternative method to getting the Alias API module
199
+ and returning the module directly to the client. In this method, the
200
+ module is JSON-serialized, saved locally to disk, and the file path
201
+ to the JSON-serialized module is returned to the client. The client
202
+ can then load the JSON-serialized module from the file.
203
+
204
+ This is a more efficient way for the client to obtain the Alias API
205
+ module because the module data is not sent over the network, which will
206
+ only become slower as the Alias API module grows (e.g. new functionality
207
+ is added).
208
+
209
+ :param sid: The client session id that made the request.
210
+ :type sid: str
211
+ :return: The file path to the JSON-serialized Alias API module.
212
+ :rtype: str
213
+ """
214
+
215
+ if self .client_sid is None or sid != self .client_sid :
216
+ return
217
+
218
+ # Get the directory and path to the Alias API cached file. The cache
219
+ # file name will have format:
220
+ # {alias_api_module_name}{alias_version}_{python_version}.json
221
+ api_info = self .on_get_alias_api_info (sid )
222
+ api_filename , api_ext = os .path .splitext (
223
+ os .path .basename (api_info ["file_path" ])
224
+ )
225
+ cache_filepath = environment_utils .get_alias_api_cache_file_path (
226
+ api_filename , api_info ["alias_version" ], api_info ["python_version" ]
227
+ )
228
+ cache_dir = os .path .dirname (cache_filepath )
229
+
230
+ # Get the path to the Alias API module (.pyd) that was used to create
231
+ # the cache. The cache module file name will have format:
232
+ # {alias_api_module_name}{alias_version}_{python_version}.pyd
233
+ base_cache_module_filename , _ = os .path .splitext (
234
+ os .path .basename (cache_filepath )
235
+ )
236
+ cache_module_filename = f"{ base_cache_module_filename } .{ api_ext } "
237
+ cache_module_filepath = os .path .join (cache_dir , cache_module_filename )
238
+
239
+ # Check if the cache is up-to-date. If not, create a new cache
240
+ if (
241
+ not os .path .exists (cache_filepath )
242
+ or not os .path .exists (cache_module_filepath )
243
+ or not filecmp .cmp (api_info ["file_path" ], cache_module_filepath )
244
+ ):
245
+ # Ensure the cache directory exists
246
+ if not os .path .exists (cache_dir ):
247
+ os .makedirs (cache_dir )
248
+ # Create the Alias API cache
249
+ with open (cache_filepath , "w" ) as fp :
250
+ json .dump (alias_api , fp = fp , cls = AliasServerJSON .encoder_class ())
251
+ # Copy the module to the cache folder in order to determine next time if the
252
+ # cache requies an update
253
+ shutil .copyfile (api_info ["file_path" ], cache_module_filepath )
254
+
255
+ # Return the path to the Alias API cache file
256
+ return cache_filepath
257
+
189
258
def on_get_alias_api_info (self , sid ):
190
259
"""
191
260
Get the Alias API module info.
0 commit comments