Skip to content

Commit 01cdd72

Browse files
committed
Update json_data.py
json_data: Logs, relative path fix - Lots of log.debug() messages added - project.current_filepath doesn't have a value for new projects, at the point we're saving, so use the 'new_project_assets' context path instead.
1 parent 5cf818a commit 01cdd72

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/classes/json_data.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ def convert_paths_to_absolute(self, file_path, data):
253253
# Optimized regex replacement
254254
data = re.sub(path_regex, self.replace_string_to_absolute, data)
255255

256-
except Exception as ex:
257-
log.error("Error while converting relative paths to absolute paths: %s" % str(ex))
256+
except Exception:
257+
log.error("Error while converting relative paths to absolute paths", exc_info=1)
258258

259259
return data
260260

@@ -266,14 +266,16 @@ def replace_string_to_relative(self, match):
266266

267267
# Determine if thumbnail path is found
268268
if info.THUMBNAIL_PATH in folder_path:
269-
# Convert path to relative thumbnail path
269+
log.debug("Generating relative thumbnail path to %s in %s",
270+
file_path, folder_path)
270271
new_path = os.path.join("thumbnail", file_path).replace("\\", "/")
271272
new_path = json.dumps(new_path, ensure_ascii=False)
272273
return '"%s": %s' % (key, new_path)
273274

274275
# Determine if @transitions path is found
275276
elif os.path.join(info.PATH, "transitions") in folder_path:
276-
# Yes, this is an OpenShot transition
277+
log.debug("Generating relative @transitions path for %s in %s",
278+
file_path, folder_path)
277279
folder_path, category_path = os.path.split(folder_path)
278280

279281
# Convert path to @transitions/ path
@@ -283,15 +285,15 @@ def replace_string_to_relative(self, match):
283285

284286
# Determine if @emojis path is found
285287
elif os.path.join(info.PATH, "emojis") in folder_path:
286-
# Yes, this is an OpenShot emoji
287-
# Convert path to @emojis/ path
288+
log.debug("Generating relative @emojis path for %s in %s",
289+
file_path, folder_path)
288290
new_path = os.path.join("@emojis", file_path).replace("\\", "/")
289291
new_path = json.dumps(new_path, ensure_ascii=False)
290292
return '"%s": %s' % (key, new_path)
291293

292294
# Determine if @assets path is found
293295
elif path_context["new_project_assets"] in folder_path:
294-
# Yes, this is an OpenShot transitions
296+
log.debug("Replacing path to %s in %s", file_path, folder_path)
295297
folder_path = folder_path.replace(path_context["new_project_assets"], "@assets")
296298

297299
# Convert path to @assets/ path
@@ -305,16 +307,19 @@ def replace_string_to_relative(self, match):
305307
orig_abs_path = os.path.abspath(path)
306308

307309
# Determine windows drives that the project and file are on
308-
project_win_drive = os.path.splitdrive(get_app().project.current_filepath)[0]
310+
project_win_drive = os.path.splitdrive(path_context.get("new_project_folder", ""))[0]
309311
file_win_drive = os.path.splitdrive(path)[0]
310312
if file_win_drive != project_win_drive:
313+
log.debug("Drive mismatch, not making path relative: %s", orig_abs_path)
311314
# If the file is on different drive. Don't abbreviate the path.
312-
return orig_abs_path
315+
clean_path = orig_abs_path.replace("\\", "/")
316+
clean_path = json.dumps(clean_path, ensure_ascii=False)
317+
return f"{key}: {clean_path}"
313318

314319
# Remove file from abs path
315320
orig_abs_folder = os.path.dirname(orig_abs_path)
316321

317-
# Calculate new relateive path
322+
log.debug("Generating new relative path for %s", orig_abs_path)
318323
new_rel_path_folder = os.path.relpath(orig_abs_folder, path_context.get("new_project_folder", ""))
319324
new_rel_path = os.path.join(new_rel_path_folder, file_path).replace("\\", "/")
320325
new_rel_path = json.dumps(new_rel_path, ensure_ascii=False)
@@ -335,8 +340,8 @@ def convert_paths_to_relative(self, file_path, previous_path, data):
335340
# Optimized regex replacement
336341
data = re.sub(path_regex, self.replace_string_to_relative, data)
337342

338-
except Exception as ex:
339-
log.error("Error while converting absolute paths to relative paths: %s" % str(ex))
343+
except Exception:
344+
log.error("Error while converting absolute paths to relative paths", exc_info=1)
340345

341346
return data
342347

0 commit comments

Comments
 (0)