mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-07-17 21:56:07 -04:00
Putting all the code from the current master branch of TWBlue
This commit is contained in:
0
src/platform_utils/__init__.py
Normal file
0
src/platform_utils/__init__.py
Normal file
0
src/platform_utils/autostart/__init__.py
Normal file
0
src/platform_utils/autostart/__init__.py
Normal file
41
src/platform_utils/autostart/windows.py
Normal file
41
src/platform_utils/autostart/windows.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import _winreg
|
||||
import os
|
||||
import sys
|
||||
from platform_utils import paths
|
||||
|
||||
RUN_REGKEY = ur"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
|
||||
|
||||
def is_installed(app_subkey):
|
||||
"""Checks if the currently running copy is installed or portable variant. Requires the name of the application subkey found under the uninstall section in Windows registry."""
|
||||
|
||||
try:
|
||||
key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s" % app_subkey)
|
||||
inst_dir = _winreg.QueryValueEx(key,"InstallLocation")[0]
|
||||
except WindowsError:
|
||||
return False
|
||||
_winreg.CloseKey(key)
|
||||
try:
|
||||
return os.stat(inst_dir) == os.stat(paths.app_path())
|
||||
except WindowsError:
|
||||
return False
|
||||
|
||||
def getAutoStart(app_name):
|
||||
"""Queries if the automatic startup should be set for the application or not, depending on it's current state."""
|
||||
|
||||
try:
|
||||
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, RUN_REGKEY)
|
||||
val = _winreg.QueryValueEx(key, unicode(app_name))[0]
|
||||
return os.stat(val) == os.stat(sys.argv[0])
|
||||
except (WindowsError, OSError):
|
||||
return False
|
||||
|
||||
def setAutoStart(app_name, enable=True):
|
||||
"""Configures automatic startup for the application, if the enable argument is set to True. If set to False, deletes the application AutoStart value."""
|
||||
|
||||
if getAutoStart(app_name) == enable:
|
||||
return
|
||||
key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, RUN_REGKEY, 0, _winreg.KEY_WRITE)
|
||||
if enable:
|
||||
_winreg.SetValueEx(key, unicode(app_name), None, _winreg.REG_SZ, sys.argv[0])
|
||||
else:
|
||||
_winreg.DeleteValue(key, unicode(app_name))
|
16
src/platform_utils/blackhole.py
Normal file
16
src/platform_utils/blackhole.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# Replacement for py2exe distributed module
|
||||
# Avoids the use of the standard py2exe console.
|
||||
# Just import this file and it should go away
|
||||
|
||||
import sys
|
||||
if hasattr(sys,"frozen"): # true only if we are running as a py2exe app
|
||||
class Blackhole(object):
|
||||
def write(self,text):
|
||||
pass
|
||||
def flush(self):
|
||||
pass
|
||||
sys.stdout = Blackhole()
|
||||
sys.stderr = Blackhole()
|
||||
del Blackhole
|
||||
del sys
|
||||
|
51
src/platform_utils/libloader.py
Normal file
51
src/platform_utils/libloader.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import ctypes
|
||||
import collections
|
||||
import platform
|
||||
import os
|
||||
|
||||
TYPES = {
|
||||
'Linux': {
|
||||
'loader': ctypes.CDLL,
|
||||
'functype': ctypes.CFUNCTYPE,
|
||||
'prefix': 'lib',
|
||||
'extension': '.so'
|
||||
},
|
||||
'Darwin': {
|
||||
'loader': ctypes.CDLL,
|
||||
'functype': ctypes.CFUNCTYPE,
|
||||
'prefix': 'lib',
|
||||
'extension': '.dylib'
|
||||
},
|
||||
}
|
||||
if platform.system() == 'Windows':
|
||||
TYPES['Windows'] = {
|
||||
'loader': ctypes.WinDLL,
|
||||
'functype': ctypes.WINFUNCTYPE,
|
||||
'prefix': "",
|
||||
'extension': '.dll'
|
||||
}
|
||||
|
||||
class LibraryLoadError(Exception): pass
|
||||
|
||||
def load_library(library, x86_path='.', x64_path='.', *args, **kwargs):
|
||||
lib = find_library_path(library, x86_path=x86_path, x64_path=x64_path)
|
||||
loaded = _do_load(lib, *args, **kwargs)
|
||||
if loaded is not None:
|
||||
return loaded
|
||||
raise LibraryLoadError('unable to load %r. Provided library path: %r' % (library, path))
|
||||
|
||||
def _do_load(file, *args, **kwargs):
|
||||
loader = TYPES[platform.system()]['loader']
|
||||
return loader(file, *args, **kwargs)
|
||||
|
||||
def find_library_path(libname, x86_path='.', x64_path='.'):
|
||||
libname = '%s%s' % (TYPES[platform.system()]['prefix'], libname)
|
||||
if platform.machine() == 'x86_64':
|
||||
path = os.path.join(x64_path, libname)
|
||||
else:
|
||||
path = os.path.join(x86_path, libname)
|
||||
ext = TYPES[platform.system()]['extension']
|
||||
return '%s%s' % (path, ext)
|
||||
|
||||
def get_functype():
|
||||
return TYPES[platform.system()]['functype']
|
114
src/platform_utils/paths.py
Normal file
114
src/platform_utils/paths.py
Normal file
@@ -0,0 +1,114 @@
|
||||
import inspect
|
||||
import platform
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import string
|
||||
import unicodedata
|
||||
|
||||
|
||||
def app_data_path(app_name=None):
|
||||
"""Cross-platform method for determining where to put application data."""
|
||||
"""Requires the name of the application"""
|
||||
plat = platform.system()
|
||||
if plat == 'Windows':
|
||||
import winpaths
|
||||
path = winpaths.get_appdata()
|
||||
elif plat == 'Darwin':
|
||||
path = os.path.join(os.path.expanduser('~'), 'Library', 'Application Support')
|
||||
elif plat == 'Linux':
|
||||
path = os.path.expanduser('~')
|
||||
app_name = '.%s' % app_name.replace(' ', '_')
|
||||
return os.path.join(path, app_name)
|
||||
|
||||
def prepare_app_data_path(app_name):
|
||||
"""Creates the application's data directory, given its name."""
|
||||
dir = app_data_path(app_name)
|
||||
return ensure_path(dir)
|
||||
|
||||
def embedded_data_path():
|
||||
if platform.system() == 'Darwin' and is_frozen():
|
||||
return os.path.abspath(os.path.join(executable_directory(), '..', 'Resources'))
|
||||
return app_path()
|
||||
|
||||
def is_frozen():
|
||||
"""Return a bool indicating if application is compressed"""
|
||||
import imp
|
||||
return hasattr(sys, 'frozen') or imp.is_frozen("__main__")
|
||||
|
||||
def get_executable():
|
||||
"""Returns the full executable path/name if frozen, or the full path/name of the main module if not."""
|
||||
if is_frozen():
|
||||
if platform.system() != 'Darwin':
|
||||
return sys.executable
|
||||
#On darwin, sys.executable points to python. We want the full path to the exe we ran.
|
||||
exedir = os.path.abspath(os.path.dirname(sys.executable))
|
||||
items = os.listdir(exedir)
|
||||
items.remove('python')
|
||||
return os.path.join(exedir, items[0])
|
||||
#Not frozen
|
||||
try:
|
||||
import __main__
|
||||
return os.path.abspath(__main__.__file__)
|
||||
except AttributeError:
|
||||
return sys.argv[0]
|
||||
|
||||
def get_module(level=2):
|
||||
"""Hacky method for deriving the caller of this function's module."""
|
||||
return inspect.getmodule(inspect.stack()[level][0]).__file__
|
||||
|
||||
def executable_directory():
|
||||
"""Always determine the directory of the executable, even when run with py2exe or otherwise frozen"""
|
||||
executable = get_executable()
|
||||
path = os.path.abspath(os.path.dirname(executable))
|
||||
return path
|
||||
|
||||
def app_path():
|
||||
"""Return the root of the application's directory"""
|
||||
path = executable_directory()
|
||||
if is_frozen() and platform.system() == 'Darwin':
|
||||
path = os.path.abspath(os.path.join(path, '..', '..'))
|
||||
return path
|
||||
|
||||
def module_path(level=2):
|
||||
return os.path.abspath(os.path.dirname(get_module(level)))
|
||||
|
||||
def documents_path():
|
||||
"""On windows, returns the path to My Documents. On OSX, returns the user's Documents folder. For anything else, returns the user's home directory."""
|
||||
plat = platform.system()
|
||||
if plat == 'Windows':
|
||||
import winpaths
|
||||
path = winpaths.get_my_documents()
|
||||
elif plat == 'Darwin':
|
||||
path = os.path.join(os.path.expanduser('~'), 'Documents')
|
||||
else:
|
||||
path = os.path.expanduser('~')
|
||||
return path
|
||||
|
||||
def safe_filename(filename):
|
||||
"""Given a filename, returns a safe version with no characters that would not work on different platforms."""
|
||||
SAFE_FILE_CHARS = "'-_.()[]{}!@#$%^&+=`~ "
|
||||
filename = unicode(filename)
|
||||
new_filename = ''.join(c for c in filename if c in SAFE_FILE_CHARS or c.isalnum())
|
||||
#Windows doesn't like directory names ending in space, macs consider filenames beginning with a dot as hidden, and windows removes dots at the ends of filenames.
|
||||
return new_filename.strip(' .')
|
||||
|
||||
def ensure_path(path):
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
return path
|
||||
|
||||
def start_file(path):
|
||||
if platform.system() == 'Windows':
|
||||
os.startfile(path)
|
||||
else:
|
||||
subprocess.Popen(['open', path])
|
||||
|
||||
def get_applications_path():
|
||||
"""Return the directory where applications are commonly installed on the system."""
|
||||
plat = platform.system()
|
||||
if plat == 'Windows':
|
||||
import winpaths
|
||||
return winpaths.get_program_files()
|
||||
elif plat == 'Darwin':
|
||||
return '/Applications'
|
27
src/platform_utils/process.py
Normal file
27
src/platform_utils/process.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import platform
|
||||
import ctypes
|
||||
import os
|
||||
import signal
|
||||
|
||||
|
||||
def kill_windows_process(pid):
|
||||
PROCESS_TERMINATE = 1
|
||||
SYNCHRONIZE=1048576
|
||||
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, False, pid)
|
||||
ctypes.windll.kernel32.TerminateProcess(handle, -1)
|
||||
ctypes.windll.kernel32.WaitForSingleObject(handle, 1000)
|
||||
ctypes.windll.kernel32.CloseHandle(handle)
|
||||
|
||||
def kill_unix_process(pid):
|
||||
try:
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def kill_process(pid):
|
||||
if pid < 0:
|
||||
return
|
||||
if platform.system() == 'Windows':
|
||||
kill_windows_process(pid)
|
||||
else:
|
||||
kill_unix_process(pid)
|
0
src/platform_utils/shell_integration/__init__.py
Normal file
0
src/platform_utils/shell_integration/__init__.py
Normal file
10
src/platform_utils/shell_integration/windows.py
Normal file
10
src/platform_utils/shell_integration/windows.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import _winreg
|
||||
|
||||
SHELL_REGKEY = ur"Directory\shell"
|
||||
|
||||
def context_menu_integrate(item_key_name, item_display_text, item_command):
|
||||
app_menu_key = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, SHELL_REGKEY, 0, _winreg.KEY_WRITE)
|
||||
menu_item_key = _winreg.CreateKey(app_menu_key, item_key_name)
|
||||
_winreg.SetValueEx(menu_item_key, None, None, _winreg.REG_SZ, item_display_text)
|
||||
item_command_key = _winreg.CreateKey(menu_item_key, 'command')
|
||||
_winreg.SetValueEx(item_command_key, None, None, _winreg.REG_SZ, item_command)
|
9
src/platform_utils/web_browser.py
Normal file
9
src/platform_utils/web_browser.py
Normal file
@@ -0,0 +1,9 @@
|
||||
import platform
|
||||
import webbrowser
|
||||
|
||||
def open(url):
|
||||
if platform.system() == 'Windows':
|
||||
browser = webbrowser.get('windows-default')
|
||||
else:
|
||||
browser = webbrowser
|
||||
browser.open_new_tab(url)
|
Reference in New Issue
Block a user