2013-10-12 17:54:13 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2013-10-12
|
2015-01-03 21:30:57 +00:00
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2014-10-12 14:39:21 +00:00
|
|
|
#
|
2015-01-03 21:33:16 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
2014-10-12 14:39:21 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2013-10-12 17:54:13 +00:00
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
import os.path as op
|
2013-11-16 17:06:16 +00:00
|
|
|
import logging
|
2013-11-10 16:05:03 +00:00
|
|
|
|
2013-10-12 17:54:13 +00:00
|
|
|
class SpecialFolder:
|
|
|
|
AppData = 1
|
|
|
|
Cache = 2
|
|
|
|
|
|
|
|
def open_url(url):
|
|
|
|
"""Open ``url`` with the default browser.
|
|
|
|
"""
|
|
|
|
_open_url(url)
|
|
|
|
|
|
|
|
def open_path(path):
|
|
|
|
"""Open ``path`` with its associated application.
|
|
|
|
"""
|
|
|
|
_open_path(str(path))
|
|
|
|
|
|
|
|
def reveal_path(path):
|
|
|
|
"""Open the folder containing ``path`` with the default file browser.
|
|
|
|
"""
|
|
|
|
_reveal_path(str(path))
|
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
def special_folder_path(special_folder, appname=None):
|
2013-10-12 17:54:13 +00:00
|
|
|
"""Returns the path of ``special_folder``.
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
``special_folder`` is a SpecialFolder.* const. The result is the special folder for the current
|
|
|
|
application. The running process' application info is used to determine relevant information.
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
You can override the application name with ``appname``. This argument is ingored under Qt.
|
2013-10-12 17:54:13 +00:00
|
|
|
"""
|
2013-11-10 16:05:03 +00:00
|
|
|
return _special_folder_path(special_folder, appname)
|
2013-10-12 17:54:13 +00:00
|
|
|
|
|
|
|
try:
|
2013-11-16 17:06:16 +00:00
|
|
|
# Normally, we would simply do "from cocoa import proxy", but due to a bug in pytest (currently
|
|
|
|
# at v2.4.2), our test suite is broken when we do that. This below is a workaround until that
|
|
|
|
# bug is fixed.
|
|
|
|
import cocoa
|
|
|
|
if not hasattr(cocoa, 'proxy'):
|
|
|
|
raise ImportError()
|
|
|
|
proxy = cocoa.proxy
|
2013-10-12 17:54:13 +00:00
|
|
|
_open_url = proxy.openURL_
|
|
|
|
_open_path = proxy.openPath_
|
|
|
|
_reveal_path = proxy.revealPath_
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
def _special_folder_path(special_folder, appname=None):
|
2013-10-12 17:54:13 +00:00
|
|
|
if special_folder == SpecialFolder.Cache:
|
2013-11-10 16:05:03 +00:00
|
|
|
base = proxy.getCachePath()
|
2013-10-12 17:54:13 +00:00
|
|
|
else:
|
2013-11-10 16:05:03 +00:00
|
|
|
base = proxy.getAppdataPath()
|
|
|
|
if not appname:
|
|
|
|
appname = proxy.bundleInfo_('CFBundleName')
|
|
|
|
return op.join(base, appname)
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-10-12 17:54:13 +00:00
|
|
|
except ImportError:
|
|
|
|
try:
|
2013-10-20 19:15:09 +00:00
|
|
|
from PyQt5.QtCore import QUrl, QStandardPaths
|
|
|
|
from PyQt5.QtGui import QDesktopServices
|
2014-03-29 21:42:23 +00:00
|
|
|
def _open_url(url):
|
|
|
|
QDesktopServices.openUrl(QUrl(url))
|
|
|
|
|
2013-10-12 17:54:13 +00:00
|
|
|
def _open_path(path):
|
|
|
|
url = QUrl.fromLocalFile(str(path))
|
|
|
|
QDesktopServices.openUrl(url)
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-10-12 17:54:13 +00:00
|
|
|
def _reveal_path(path):
|
|
|
|
_open_path(op.dirname(str(path)))
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-10 16:05:03 +00:00
|
|
|
def _special_folder_path(special_folder, appname=None):
|
2013-10-12 17:54:13 +00:00
|
|
|
if special_folder == SpecialFolder.Cache:
|
2013-10-20 19:15:09 +00:00
|
|
|
qtfolder = QStandardPaths.CacheLocation
|
2013-10-12 17:54:13 +00:00
|
|
|
else:
|
2013-10-20 19:15:09 +00:00
|
|
|
qtfolder = QStandardPaths.DataLocation
|
|
|
|
return QStandardPaths.standardLocations(qtfolder)[0]
|
2013-10-12 17:54:13 +00:00
|
|
|
except ImportError:
|
2013-11-16 17:06:16 +00:00
|
|
|
# We're either running tests, and these functions don't matter much or we're in a really
|
|
|
|
# weird situation. Let's just have dummy fallbacks.
|
|
|
|
logging.warning("Can't setup desktop functions!")
|
|
|
|
def _open_path(path):
|
|
|
|
pass
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-16 17:06:16 +00:00
|
|
|
def _reveal_path(path):
|
|
|
|
pass
|
2014-10-12 14:39:21 +00:00
|
|
|
|
2013-11-16 17:06:16 +00:00
|
|
|
def _special_folder_path(special_folder, appname=None):
|
|
|
|
return '/tmp'
|