Imported paths module from Socializer. That means unicode paths should be fully supported
This commit is contained in:
parent
a65d6a82c0
commit
9ac26bc818
160
src/paths.py
160
src/paths.py
@ -1,116 +1,70 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
from __future__ import unicode_literals
|
||||||
import inspect
|
import sys
|
||||||
import platform
|
import platform
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import glob
|
||||||
import sys
|
from platform_utils import paths as paths_
|
||||||
import string
|
|
||||||
import unicodedata
|
|
||||||
|
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
def app_data_path(app_name=None):
|
mode = "portable"
|
||||||
"""Cross-platform method for determining where to put application data."""
|
directory = None
|
||||||
"""Requires the name of the application"""
|
fsencoding = sys.getfilesystemencoding()
|
||||||
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):
|
if len(glob.glob("Uninstall.exe")) > 0: # installed copy
|
||||||
"""Creates the application's data directory, given its name."""
|
mode= "installed"
|
||||||
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():
|
def app_path():
|
||||||
"""Return the root of the application's directory"""
|
return paths_.app_path()
|
||||||
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):
|
def config_path():
|
||||||
return os.path.abspath(os.path.dirname(get_module(level)))
|
global mode, directory
|
||||||
|
if mode == "portable":
|
||||||
def documents_path():
|
if directory != None: path = os.path.join(directory, "config")
|
||||||
"""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."""
|
elif directory == None: path = os.path.join(app_path(), "config")
|
||||||
plat = platform.system()
|
elif mode == "installed":
|
||||||
if plat == 'Windows':
|
path = os.path.join(data_path(), "config")
|
||||||
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):
|
if not os.path.exists(path):
|
||||||
os.makedirs(path)
|
# log.debug("%s path does not exist, creating..." % (path,))
|
||||||
|
os.mkdir(path)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
def start_file(path):
|
def logs_path():
|
||||||
if platform.system() == 'Windows':
|
global mode, directory
|
||||||
os.startfile(path)
|
if mode == "portable":
|
||||||
else:
|
if directory != None: path = os.path.join(directory, "logs")
|
||||||
subprocess.Popen(['open', path])
|
elif directory == None: path = os.path.join(app_path(), "logs")
|
||||||
|
elif mode == "installed":
|
||||||
|
path = os.path.join(data_path(), "logs")
|
||||||
|
if not os.path.exists(path):
|
||||||
|
# log.debug("%s path does not exist, creating..." % (path,))
|
||||||
|
os.mkdir(path)
|
||||||
|
return path
|
||||||
|
|
||||||
def get_applications_path():
|
def data_path(app_name='socializer'):
|
||||||
"""Return the directory where applications are commonly installed on the system."""
|
if platform.system() == "Windows":
|
||||||
plat = platform.system()
|
data_path = os.path.join(os.getenv("AppData"), app_name)
|
||||||
if plat == 'Windows':
|
else:
|
||||||
import winpaths
|
data_path = os.path.join(os.environ['HOME'], ".%s" % app_name)
|
||||||
return winpaths.get_program_files()
|
if not os.path.exists(data_path):
|
||||||
elif plat == 'Darwin':
|
os.mkdir(data_path)
|
||||||
return '/Applications'
|
return data_path
|
||||||
|
|
||||||
|
def locale_path():
|
||||||
|
return os.path.join(app_path(), "locales")
|
||||||
|
|
||||||
|
def sound_path():
|
||||||
|
return os.path.join(app_path(), "sounds")
|
||||||
|
|
||||||
|
def com_path():
|
||||||
|
global mode, directory
|
||||||
|
if mode == "portable":
|
||||||
|
if directory != None: path = os.path.join(directory, "com_cache")
|
||||||
|
elif directory == None: path = os.path.join(app_path(), "com_cache")
|
||||||
|
elif mode == "installed":
|
||||||
|
path = os.path.join(data_path(), "com_cache")
|
||||||
|
if not os.path.exists(path):
|
||||||
|
# log.debug("%s path does not exist, creating..." % (path,))
|
||||||
|
os.mkdir(path)
|
||||||
|
return path
|
||||||
|
Loading…
Reference in New Issue
Block a user