Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look in imagefile and pixmap paths as created by pip #1285

Merged
merged 5 commits into from
Sep 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 66 additions & 14 deletions printrun/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,59 @@ def setup_logging(out, filepath = None, reset_handlers = False):
logger.addHandler(logging_handler)

def iconfile(filename):
'''
Get the full path to filename by checking in standard icon locations
("pixmaps" directories) or use the frozen executable if applicable
(See the lookup_file function's documentation for behavior).
'''
if hasattr(sys, "frozen") and sys.frozen == "windows_exe":
return sys.executable
else:
return pixmapfile(filename)

def imagefile(filename):
shared_pronterface_images_dir = os.path.join(DATADIR, 'pronterface/images')
candidate = os.path.join(shared_pronterface_images_dir, filename)
if os.path.exists(candidate):
return candidate
local_candidate = os.path.join(os.path.dirname(sys.argv[0]),
"images", filename)
if os.path.exists(local_candidate):
return local_candidate
frozen_candidate=os.path.join(getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))),"images",filename)
if os.path.exists(frozen_candidate):
return frozen_candidate
else:
return os.path.join("images", filename)
'''
Get the full path to filename by checking standard image locations,
those being possible locations of the pronterface "images" directory
(See the lookup_file function's documentation for behavior).
'''
my_local_share = os.path.join(
os.path.dirname(os.path.dirname(sys.argv[0])),
"share",
"pronterface"
) # Used by pip install
image_dirs = [
os.path.join(DATADIR, 'pronterface', 'images'),
os.path.join(os.path.dirname(sys.argv[0]), "images"),
os.path.join(my_local_share, "images"),
os.path.join(
getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))),
"images"
), # Check manually since lookup_file checks in frozen but not /images
]
path = lookup_file(filename, image_dirs)
if path == filename:
# The file wasn't found in any known location, so use a relative
# path.
path = os.path.join("images", filename)
return path

def lookup_file(filename, prefixes):
'''
Get the full path to filename by checking one or more prefixes,
or in the frozen data if applicable. If a result from this
(or from callers such as imagefile) is used for the wx.Image
constructor and filename isn't found, the C++ part of wx
will raise an exception (wx._core.wxAssertionError): "invalid
image".

Sequential arguments:
filename -- a filename without the path.
prefixes -- a list of paths.

Returns:
The full path if found, or filename if not found.
'''
local_candidate = os.path.join(os.path.dirname(sys.argv[0]), filename)
if os.path.exists(local_candidate):
return local_candidate
Expand All @@ -118,14 +150,34 @@ def lookup_file(filename, prefixes):
return filename

def pixmapfile(filename):
'''
Get the full path to filename by checking in standard icon
("pixmaps") directories (See the lookup_file function's
documentation for behavior).
'''
shared_pixmaps_dir = os.path.join(DATADIR, 'pixmaps')
return lookup_file(filename, [shared_pixmaps_dir])
local_pixmaps_dir = os.path.join(
os.path.dirname(os.path.dirname(sys.argv[0])),
"share",
"pixmaps"
) # Used by pip install
pixmaps_dirs = [shared_pixmaps_dir, local_pixmaps_dir]
return lookup_file(filename, pixmaps_dirs)

def sharedfile(filename):
'''
Get the full path to filename by checking in the shared
directory (See the lookup_file function's documentation for behavior).
'''
shared_pronterface_dir = os.path.join(DATADIR, 'pronterface')
return lookup_file(filename, [shared_pronterface_dir])

def configfile(filename):
'''
Get the full path to filename by checking in the
standard configuration directory (See the lookup_file
function's documentation for behavior).
'''
return lookup_file(filename, [os.path.expanduser("~/.printrun/"), ])
rockstorm101 marked this conversation as resolved.
Show resolved Hide resolved

def decode_utf8(s):
Expand Down