Commit bdc45c69 authored by Jobin Rezai's avatar Jobin Rezai

Update OS specific path checks

Check which OS the script is running in early on.
Also, check for relative paths in Windows for Glade files and the config file.
In Linux, use an absolute path.

This is because cx_Freeze in Windows doesn't like modules[__name__].__file__ to get the script's path.
So in Windows, we just use relative paths, and in Linux, we use modules[__name__].__file__ to get the script's full path.
Snaps like to use absolute paths it seems.
parent 53a0fd5b
......@@ -126,6 +126,9 @@ class GladeWindowType(Enum):
ABOUT_WINDOW = auto()
PROGRESS_WINDOW = auto()
class CurrentOS(Enum):
RUNNING_LINUX = auto()
RUNNING_WINDOWS = auto()
def get_glade_file_path(glade_window_type):
......@@ -140,8 +143,16 @@ def get_glade_file_path(glade_window_type):
# Get the full path of where the current module is
# Example: /snap/openresizer/x1/bin/openresizer.py
module_full_path = modules[__name__].__file__
# The full path only seems to matter in Linux Snaps.
# Don't attempt to run the line below in Windows,
# because it'll throw an exception: module '__main__' has no attribute '__file__'
if DefaultSettings.PLATFORM == CurrentOS.RUNNING_LINUX:
module_full_path = modules[__name__].__file__
elif DefaultSettings.PLATFORM == CurrentOS.RUNNING_WINDOWS:
# We only use this variable in Linux, not Windows, but we still need to initialize it.
module_full_path = ""
# Get the path only, of where the current module is
# Example: /snap/openresizer/x1/bin
......@@ -187,6 +198,9 @@ class DefaultSettings:
DEFAULT_WINDOW_WIDTH = 630
DEFAULT_WINDOW_HEIGHT = 514
# Keeps record of which OS the script is running in
PLATFORM = None
class ProcessorUsage(Enum):
SINGLE_CPU = auto()
MULTI_CPU = auto()
......@@ -480,11 +494,10 @@ class ProgressWindow:
def on_link_gotosavefolder_activate_link(self, widget):
current_os = osname()
try:
if current_os == "Linux":
if DefaultSettings.PLATFORM == CurrentOS.RUNNING_LINUX:
subprocess.Popen(["xdg-open", self.destination_path])
## Convert the path to a uri that Gio can use
......@@ -493,7 +506,7 @@ class ProgressWindow:
## Open folder
#Gio.app_info_launch_default_for_uri(uri, None)
elif current_os == "Windows":
elif DefaultSettings.PLATFORM == CurrentOS.RUNNING_WINDOWS:
subprocess.Popen(["explorer", self.destination_path])
except BaseException as e:
......@@ -1013,15 +1026,21 @@ has been reinstalled and this error no longer appears.""",
Check the Local App Data folder, and if the config file is not there, check for the config in the same
directory of the script.
"""
config_source_full_path = path.join(path.dirname(modules[__name__].__file__), "config")
# with cx_Freeze, modules[__name__].__file__ will return an error: module '__main__' has no attribute '__file__'
# So if we're running in Windows, use relative paths.
if DefaultSettings.PLATFORM == CurrentOS.RUNNING_LINUX:
# In Linux, we need the full path (Snap's seem to prefer it, rather than relative paths)
# We don't use this variable for Windows, it's used just in Linux.
config_source_full_path = path.join(path.dirname(modules[__name__].__file__), "config")
# The config file name by itself
config_file_name = "config"
# Get the name of the OS that this script is running in.
current_os = osname()
if current_os == "Linux":
if DefaultSettings.PLATFORM == CurrentOS.RUNNING_LINUX:
# In Linux, check to see if the script is running in a Snap. If so, the config
# file needs to be copied to $SNAP_USER_DATA, and read/write from there, if it's not already
# copied there.
......@@ -1070,7 +1089,7 @@ has been reinstalled and this error no longer appears.""",
# If we're in Windows, check the local app data path
elif current_os == "Windows":
elif DefaultSettings.PLATFORM == CurrentOS.RUNNING_WINDOWS:
# Get the local app data path
if "LOCALAPPDATA" in environ:
......@@ -1302,9 +1321,6 @@ has been reinstalled and this error no longer appears.""",
def on_drag_and_drop(self, treeview_widget, drag_context, x, y, selection_data, info, timestamp):
# Get the name of the OS that the script is running in
current_os = osname()
# The selection data is normally returned as a byte, so we use .decode() to convert it to a string
data:str = selection_data.get_data().decode()
......@@ -1324,7 +1340,7 @@ has been reinstalled and this error no longer appears.""",
# Strip the file:// part
for full_path in path_list:
if current_os == "Windows" and full_path.startswith("file:///"):
if DefaultSettings.PLATFORM == CurrentOS.RUNNING_WINDOWS and full_path.startswith("file:///"):
# Windows
# Get the path without file:// and remove any extra \r characters
......@@ -1334,7 +1350,7 @@ has been reinstalled and this error no longer appears.""",
# Example: C:/folder/file.jpg changes to C:\folder\file.jpg
full_path = str(Path(full_path)) #full_path.replace(r"/", r"\")
elif current_os == "Linux" and full_path.startswith("file://"):
elif DefaultSettings.PLATFORM == CurrentOS.RUNNING_LINUX and full_path.startswith("file://"):
# Linux
# Get the path without file:// and remove any extra \r characters
......@@ -2136,7 +2152,17 @@ has been reinstalled and this error no longer appears.""",
if __name__ == "__main__":
# Required for cx_Freeze
freeze_support()
# Get the OS that we're on
current_os = osname()
if current_os == "Linux":
DefaultSettings.PLATFORM = CurrentOS.RUNNING_LINUX
elif current_os == "Windows":
DefaultSettings.PLATFORM = CurrentOS.RUNNING_WINDOWS
# Create the main window
window = MainWindow()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment