Skip to content

Commit

Permalink
Merge pull request OpenShot#2986 from ferdnyc/info-paths
Browse files Browse the repository at this point in the history
A bunch of filepath-related fixes
  • Loading branch information
ferdnyc authored Nov 19, 2019
2 parents b0fe642 + 836ba54 commit 02d58d9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/classes/exporters/final_cut_pro.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def export_xml():
track_count += 1

try:
file = open(file_path.encode('UTF-8'), "wb") # wb needed for windows support
file = open(os.fsencode(file_path), "wb") # wb needed for windows support
file.write(bytes(xmldoc.toxml(), 'UTF-8'))
file.close()
except IOError as inst:
Expand Down
44 changes: 27 additions & 17 deletions src/classes/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,45 @@
COMPANY_NAME = "OpenShot Studios, LLC"
COPYRIGHT = "Copyright (c) 2008-2018 %s" % COMPANY_NAME
CWD = os.getcwd()

# Application paths
PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # Primary openshot folder
RESOURCES_PATH = os.path.join(PATH, "resources")
PROFILES_PATH = os.path.join(PATH, "profiles")
IMAGES_PATH = os.path.join(PATH, "images")
EXPORT_PRESETS_PATH = os.path.join(PATH, "presets")

# User paths
HOME_PATH = os.path.join(os.path.expanduser("~"))
USER_PATH = os.path.join(HOME_PATH, ".openshot_qt")
BACKUP_PATH = os.path.join(USER_PATH)
RECOVERY_PATH = os.path.join(USER_PATH, "recovery")
BLENDER_PATH = os.path.join(USER_PATH, "blender")
RESOURCES_PATH = os.path.join(PATH, "resources")
THUMBNAIL_PATH = os.path.join(USER_PATH, "thumbnail")
CACHE_PATH = os.path.join(USER_PATH, "cache")
PREVIEW_CACHE_PATH = os.path.join(USER_PATH, "preview-cache")
BLENDER_PATH = os.path.join(USER_PATH, "blender")
ASSETS_PATH = os.path.join(USER_PATH, "assets")
TITLE_PATH = os.path.join(USER_PATH, "title")
PROFILES_PATH = os.path.join(PATH, "profiles")
IMAGES_PATH = os.path.join(PATH, "images")
TRANSITIONS_PATH = os.path.join(USER_PATH, "transitions")
EXPORT_PRESETS_PATH = os.path.join(PATH, "presets")
EXPORT_TESTS = os.path.join(USER_PATH, "tests")
PREVIEW_CACHE_PATH = os.path.join(USER_PATH, "preview-cache")
USER_PROFILES_PATH = os.path.join(USER_PATH, "profiles")
USER_PRESETS_PATH = os.path.join(USER_PATH, "presets")

# User files
BACKUP_FILE = os.path.join(BACKUP_PATH, "backup.osp")
USER_DEFAULT_PROJECT = os.path.join(USER_PATH, "default.project")

# Create PATHS if they do not exist (this is where temp files are stored... such as cached thumbnails)
for folder in [USER_PATH, THUMBNAIL_PATH, CACHE_PATH, BLENDER_PATH, TITLE_PATH, PROFILES_PATH, IMAGES_PATH,
TRANSITIONS_PATH, EXPORT_TESTS, BACKUP_PATH, USER_PROFILES_PATH, USER_PRESETS_PATH, PREVIEW_CACHE_PATH,
RECOVERY_PATH]:
if not os.path.exists(folder.encode("UTF-8")):
# Create user paths if they do not exist
# (this is where temp files are stored... such as cached thumbnails)
for folder in [USER_PATH, BACKUP_PATH, RECOVERY_PATH, THUMBNAIL_PATH, CACHE_PATH,
BLENDER_PATH, ASSETS_PATH, TITLE_PATH, TRANSITIONS_PATH,
PREVIEW_CACHE_PATH, USER_PROFILES_PATH, USER_PRESETS_PATH]:
if not os.path.exists(os.fsencode(folder)):
os.makedirs(folder, exist_ok=True)

# names of all contributors, using "u" for unicode encoding
JT = {"name": u"Jonathan Thomas", "email": "jonathan@openshot.org", "website":"http://openshot.org/developers/jonathan"}
# Maintainer details, for packaging
JT = {"name": "Jonathan Thomas",
"email": "jonathan@openshot.org",
"website": "http://openshot.org/developers/jonathan"}

# Blender minimum version required (a string value)
BLENDER_MIN_VERSION = "2.80"
Expand All @@ -79,15 +89,15 @@

try:
from language import openshot_lang
language_path=":/locale/"
language_path = ":/locale/"
except ImportError:
language_path=os.path.join(PATH, 'language')
language_path = os.path.join(PATH, 'language')
print("Compiled translation resources missing!")
print("Loading translations from: {}".format(language_path))

# Compile language list from :/locale resource
langdir = QDir(language_path)
langs = langdir.entryList(['OpenShot.*.qm'], QDir.NoDotAndDotDot|QDir.Files,
langs = langdir.entryList(['OpenShot.*.qm'], QDir.NoDotAndDotDot | QDir.Files,
sort=QDir.Name)
for trpath in langs:
SUPPORTED_LANGUAGES.append(trpath.split('.')[1])
Expand Down
4 changes: 2 additions & 2 deletions src/classes/json_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def replace_string_to_absolute(self, match):
path = match.groups(0)[1]

# Find absolute path of file (if needed)
utf_path = json.loads('"%s"' % path, encoding="utf-8") # parse bytestring into unicode string
utf_path = os.fsencode(path) # parse bytestring into unicode string
if "@transitions" in utf_path:
new_path = path.replace("@transitions", os.path.join(info.PATH, "transitions"))
new_path = json.dumps(new_path) # Escape backslashes
Expand Down Expand Up @@ -200,7 +200,7 @@ def replace_string_to_relative(self, match):
"""Replace matched string for converting paths to relative paths"""
key = match.groups(0)[0]
path = match.groups(0)[1]
utf_path = json.loads('"%s"' % path, encoding="utf-8") # parse bytestring into unicode string
utf_path = os.fsencode(path) # parse bytestring into unicode string
folder_path, file_path = os.path.split(os.path.abspath(utf_path))

# Determine if thumbnail path is found
Expand Down
4 changes: 2 additions & 2 deletions src/classes/project_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def read_legacy_project_file(self, file_path):
# Keep track of files that failed to load
failed_files = []

with open(file_path.encode('UTF-8'), 'rb') as f:
with open(os.fsencode(file_path), 'rb') as f:
try:
# Unpickle legacy openshot project file
v1_data = pickle.load(f, fix_imports=True, encoding="UTF-8")
Expand Down Expand Up @@ -888,7 +888,7 @@ def move_temp_paths_to_project_folder(self, file_path, previous_path=None):

def add_to_recent_files(self, file_path):
""" Add this project to the recent files list """
if not file_path or "backup.osp" in file_path:
if not file_path or file_path is info.BACKUP_FILE:
# Ignore backup recovery project
return

Expand Down
2 changes: 1 addition & 1 deletion src/classes/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def load(self):
file_path = os.path.join(info.USER_PATH, self.settings_filename)

# Load user settings (if found)
if os.path.exists(file_path.encode('UTF-8')):
if os.path.exists(os.fsencode(file_path)):

# Will raise exception to caller on failure to read
try:
Expand Down
30 changes: 13 additions & 17 deletions src/windows/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@ def closeEvent(self, event):
def recover_backup(self):
"""Recover the backup file (if any)"""
log.info("recover_backup")
# Check for backup.osp file
recovery_path = os.path.join(info.BACKUP_PATH, "backup.osp")

# Load recovery project
if os.path.exists(recovery_path):
log.info("Recovering backup file: %s" % recovery_path)
self.open_project(recovery_path, clear_thumbnails=False)
# Check for backup.osp file
if os.path.exists(info.BACKUP_FILE):
# Load recovery project
log.info("Recovering backup file: %s" % info.BACKUP_FILE)
self.open_project(info.BACKUP_FILE, clear_thumbnails=False)

# Clear the file_path (which is set by saving the project)
project = get_app().project
Expand Down Expand Up @@ -266,7 +265,7 @@ def create_lock_file(self):
last_log_line = ""

# Throw exception (with last libopenshot line... if found)
log.error("Unhandled crash detected... will attempt to recover backup project: %s" % info.BACKUP_PATH)
log.error("Unhandled crash detected... will attempt to recover backup project: %s" % info.BACKUP_FILE)
track_metric_error("unhandled-crash%s" % last_log_line, True)

# Remove file
Expand Down Expand Up @@ -568,11 +567,10 @@ def clear_all_thumbnails(self):
os.mkdir(openshot_title_path)

# Clear any backups
backup_path = os.path.join(info.BACKUP_PATH, "backup.osp")
if os.path.exists(backup_path):
log.info("Clear backup: %s" % backup_path)
if os.path.exists(info.BACKUP_FILE):
log.info("Clear backup: %s" % info.BACKUP_FILE)
# Remove backup file
os.unlink(backup_path)
os.unlink(info.BACKUP_FILE)

except:
log.info("Failed to clear thumbnails: %s" % info.THUMBNAIL_PATH)
Expand Down Expand Up @@ -657,16 +655,14 @@ def auto_save_project(self):
self.save_project(file_path)

# Remove backup.osp (if any)
recovery_path = os.path.join(info.BACKUP_PATH, "backup.osp")
if os.path.exists(recovery_path):
if os.path.exists(info.BACKUP_FILE):
# Delete backup.osp since we just saved the actual project
os.unlink(recovery_path)
os.unlink(info.BACKUP_FILE)

else:
# No saved project found
recovery_path = os.path.join(info.BACKUP_PATH, "backup.osp")
log.info("Creating backup of project file: %s" % recovery_path)
get_app().project.save(recovery_path, move_temp_files=False, make_paths_relative=False)
log.info("Creating backup of project file: %s" % info.BACKUP_FILE)
get_app().project.save(info.BACKUP_FILE, move_temp_files=False, make_paths_relative=False)

# Clear the file_path (which is set by saving the project)
get_app().project.current_filepath = None
Expand Down
2 changes: 1 addition & 1 deletion src/windows/title_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def writeToFile(self, xmldoc):
if not self.filename.endswith("svg"):
self.filename = self.filename + ".svg"
try:
file = open(self.filename.encode('UTF-8'), "wb") # wb needed for windows support
file = open(os.fsencode(self.filename), "wb") # wb needed for windows support
file.write(bytes(xmldoc.toxml(), 'UTF-8'))
file.close()
except IOError as inst:
Expand Down

0 comments on commit 02d58d9

Please sign in to comment.