mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 12:11:33 -06:00
Create a utils file for low level helpers like path utilities
This commit is contained in:
64
Utils.py
Normal file
64
Utils.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
def is_bundled():
|
||||
return getattr(sys, 'frozen', False)
|
||||
|
||||
def local_path(path):
|
||||
if local_path.cached_path is not None:
|
||||
return os.path.join(local_path.cached_path, path)
|
||||
|
||||
if is_bundled():
|
||||
# we are running in a bundle
|
||||
local_path.cached_path = sys._MEIPASS
|
||||
else:
|
||||
# we are running in a normal Python environment
|
||||
local_path.cached_path = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
return os.path.join(local_path.cached_path, path)
|
||||
|
||||
local_path.cached_path = None
|
||||
|
||||
def output_path(path):
|
||||
if output_path.cached_path is not None:
|
||||
return os.path.join(output_path.cached_path, path)
|
||||
|
||||
if not is_bundled():
|
||||
output_path.cached_path = '.'
|
||||
return os.path.join(output_path.cached_path, path)
|
||||
else:
|
||||
# has been packaged, so cannot use CWD for output.
|
||||
if sys.platform == 'win32':
|
||||
#windows
|
||||
import ctypes.wintypes
|
||||
CSIDL_PERSONAL = 5 # My Documents
|
||||
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
|
||||
|
||||
buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
|
||||
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None, SHGFP_TYPE_CURRENT, buf)
|
||||
|
||||
documents = buf.value
|
||||
|
||||
elif sys.platform == 'darwin':
|
||||
from AppKit import NSSearchPathForDirectoriesInDomains
|
||||
# http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
|
||||
NSDocumentDirectory = 9
|
||||
NSUserDomainMask = 1
|
||||
# True for expanding the tilde into a fully qualified path
|
||||
documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, True)[0]
|
||||
else:
|
||||
raise NotImplementedError('Not supported yet')
|
||||
|
||||
output_path.cached_path = os.path.join(documents, 'ALttPEntranceRandomizer')
|
||||
if not os.path.exists(output_path.cached_path):
|
||||
os.mkdir(output_path.cached_path)
|
||||
return os.path.join(output_path.cached_path, path)
|
||||
|
||||
output_path.cached_path = None
|
||||
|
||||
def open_file(filename):
|
||||
if sys.platform == 'win32':
|
||||
os.startfile(filename)
|
||||
else:
|
||||
open_Command = 'open' if sys.platform == 'darwin' else 'xdg-open'
|
||||
subprocess.call([open_command, filename])
|
Reference in New Issue
Block a user