diff options
author | Jacob Gustafson <7557867+poikilos@users.noreply.github.com> | 2022-09-11 22:26:02 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-11 22:26:02 +0300 |
commit | e60a93fd6c0216d57f5dc0a78118e89e9fda2d9a (patch) | |
tree | 5caa0f8f7ae90388cd51cce1d4f06a31b6dd60e8 | |
parent | 10718fa31c277e35d93b67fc7075aa68cc79da3c (diff) |
* Look in imagefile and pixmap paths as created by pip
(Fix #1248). Also, use lookup_file in the imagefile function, and add related documentation (PEP8 documentation plus returns).
-rw-r--r-- | printrun/utils.py | 80 |
1 files changed, 66 insertions, 14 deletions
diff --git a/printrun/utils.py b/printrun/utils.py index 9bed2f9..d3eda2d 100644 --- a/printrun/utils.py +++ b/printrun/utils.py @@ -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 @@ -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/"), ]) def decode_utf8(s): |