From 28d4b14d856515ddc057f7a93726c473e89ccfbc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Aug 2011 19:32:16 -0500 Subject: [PATCH] First attempt at XDG support --- ejpi/constants.py | 5 ----- ejpi/ejpi_qt.py | 29 ++++++++++++++++++++++++----- ejpi/util/linux.py | 36 +++++++++++++++++++++--------------- ejpi/util/qwrappers.py | 6 +++++- setup.py | 11 ++++++----- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/ejpi/constants.py b/ejpi/constants.py index 2d40783..a76fb16 100644 --- a/ejpi/constants.py +++ b/ejpi/constants.py @@ -1,12 +1,7 @@ -import os - __pretty_app_name__ = "e**(j pi) + 1 = 0" __app_name__ = "ejpi" __version__ = "1.0.6" __build__ = 12 __app_magic__ = 0xdeadbeef -_data_path_ = os.path.join(os.path.expanduser("~"), ".%s" % __app_name__) -_user_settings_ = "%s/settings.ini" % _data_path_ -_user_logpath_ = "%s/%s.log" % (_data_path_, __app_name__) IS_MAEMO = True diff --git a/ejpi/ejpi_qt.py b/ejpi/ejpi_qt.py index 082f260..00dea95 100755 --- a/ejpi/ejpi_qt.py +++ b/ejpi/ejpi_qt.py @@ -15,6 +15,7 @@ QtGui = qt_compat.import_module("QtGui") import constants from util import misc as misc_utils +from util import linux as linux_utils from util import qui_utils from util import qwrappers @@ -37,8 +38,11 @@ class Calculator(qwrappers.ApplicationWrapper): qwrappers.ApplicationWrapper.__init__(self, app, constants) def load_settings(self): + settingsPath = linux_utils.get_resource_path( + "config", constants.__app_name__, "settings.json" + ) try: - with open(constants._user_settings_, "r") as settingsFile: + with open(settingsPath) as settingsFile: settings = simplejson.load(settingsFile) except IOError, e: _moduleLogger.info("No settings") @@ -56,7 +60,11 @@ class Calculator(qwrappers.ApplicationWrapper): "isFullScreen": self._fullscreenAction.isChecked(), "isPortrait": self._orientationAction.isChecked(), } - with open(constants._user_settings_, "w") as settingsFile: + + settingsPath = linux_utils.get_resource_path( + "config", constants.__app_name__, "settings.json" + ) + with open(settingsPath, "w") as settingsFile: simplejson.dump(settings, settingsFile) @property @@ -129,7 +137,7 @@ class MainWindow(qwrappers.WindowWrapper): os.path.join(os.path.dirname(__file__), "plugins/"), ] - _user_history = "%s/history.stack" % constants._data_path_ + _user_history = linux_utils.get_resource_path("config", constants.__app_name__, "history.stack") def __init__(self, parent, app): qwrappers.WindowWrapper.__init__(self, parent, app) @@ -352,14 +360,25 @@ class MainWindow(qwrappers.WindowWrapper): def run(): try: - os.makedirs(constants._data_path_) + os.makedirs(linux_utils.get_resource_path("config", constants.__app_name__)) + except OSError, e: + if e.errno != 17: + raise + try: + os.makedirs(linux_utils.get_resource_path("cache", constants.__app_name__)) + except OSError, e: + if e.errno != 17: + raise + try: + os.makedirs(linux_utils.get_resource_path("data", constants.__app_name__)) except OSError, e: if e.errno != 17: raise + logPath = linux_utils.get_resource_path("cache", constants.__app_name__, "%s.log" % constants.__app_name__) logFormat = '(%(relativeCreated)5d) %(levelname)-5s %(threadName)s.%(name)s.%(funcName)s: %(message)s' logging.basicConfig(level=logging.DEBUG, format=logFormat) - rotating = logging.handlers.RotatingFileHandler(constants._user_logpath_, maxBytes=512*1024, backupCount=1) + rotating = logging.handlers.RotatingFileHandler(logPath, maxBytes=512*1024, backupCount=1) rotating.setFormatter(logging.Formatter(logFormat)) root = logging.getLogger() root.addHandler(rotating) diff --git a/ejpi/util/linux.py b/ejpi/util/linux.py index 4e77445..21bf959 100644 --- a/ejpi/util/linux.py +++ b/ejpi/util/linux.py @@ -28,13 +28,13 @@ def set_process_name(name): _moduleLogger.warning('Unable to set processName: %s" % e') -def get_new_resource(resourceType, resource, name): +def _get_xdg_path(resourceType): if BaseDirectory is not None: if resourceType == "data": base = BaseDirectory.xdg_data_home if base == "/usr/share/mime": # Ugly hack because somehow Maemo 4.1 seems to be set to this - base = os.path.join(os.path.expanduser("~"), ".%s" % resource) + base = None elif resourceType == "config": base = BaseDirectory.xdg_config_home elif resourceType == "cache": @@ -42,10 +42,26 @@ def get_new_resource(resourceType, resource, name): else: raise RuntimeError("Unknown type: "+resourceType) else: + base = None + + return base + + +def get_resource_path(resourceType, resource, name = None): + base = _get_xdg_path(resourceType) + if base is not None: + dirPath = os.path.join(base, resource) + else: base = os.path.join(os.path.expanduser("~"), ".%s" % resource) + dirPath = base + if name is not None: + dirPath = os.path.join(dirPath, name) + return dirPath + - filePath = os.path.join(base, resource, name) - dirPath = os.path.dirname(filePath) +def get_new_resource(resourceType, resource, name): + dirPath = get_resource_path(resourceType, resource) + filePath = os.path.join(dirPath, name) if not os.path.exists(dirPath): # Looking before I leap to not mask errors os.makedirs(dirPath) @@ -54,17 +70,7 @@ def get_new_resource(resourceType, resource, name): def get_existing_resource(resourceType, resource, name): - if BaseDirectory is not None: - if resourceType == "data": - base = BaseDirectory.xdg_data_home - elif resourceType == "config": - base = BaseDirectory.xdg_config_home - elif resourceType == "cache": - base = BaseDirectory.xdg_cache_home - else: - raise RuntimeError("Unknown type: "+resourceType) - else: - base = None + base = _get_xdg_path(resourceType) if base is not None: finalPath = os.path.join(base, name) diff --git a/ejpi/util/qwrappers.py b/ejpi/util/qwrappers.py index 09270cd..167f2ea 100644 --- a/ejpi/util/qwrappers.py +++ b/ejpi/util/qwrappers.py @@ -11,6 +11,7 @@ QtGui = qt_compat.import_module("QtGui") import qui_utils import misc as misc_utils +import linux as linux_utils _moduleLogger = logging.getLogger(__name__) @@ -169,7 +170,10 @@ class ApplicationWrapper(object): @misc_utils.log_exception(_moduleLogger) def _on_log(self, checked = False): with qui_utils.notify_error(self._errorLog): - with open(self._constants._user_logpath_, "r") as f: + logPath = linux_utils.get_resource_path( + "cache", self._constants.__app_name__, "%s.log" % self._constants.__app_name__ + ) + with open(logPath, "r") as f: logLines = f.xreadlines() log = "".join(logLines) self._clipboard.setText(log) diff --git a/setup.py b/setup.py index 434a207..aaa1fcb 100755 --- a/setup.py +++ b/setup.py @@ -98,6 +98,7 @@ setup( ], requires=[ "PySide", + "pyxdg", ], cmdclass={ 'sdist_ubuntu': sdist_maemo, @@ -112,7 +113,7 @@ setup( "copyright": "lgpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python, python-pyside.qtcore, python-pyside.qtgui", + "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-xdg", "architecture": "any", }, "sdist_diablo": { @@ -125,7 +126,7 @@ setup( "copyright": "lgpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui", + "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python-xdg", "architecture": "any", }, "sdist_fremantle": { @@ -138,8 +139,8 @@ setup( "copyright": "lgpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python2.5-qt4-maemo5", - #"depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-pyside.qtmaemo5", + "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python2.5-qt4-maemo5, python-xdg", + #"depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-pyside.qtmaemo5, python-xdg", "architecture": "any", }, "sdist_harmattan": { @@ -154,7 +155,7 @@ setup( "copyright": "lgpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python, python-pyside.qtcore, python-pyside.qtgui", + "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-xdg", "architecture": "any", }, "bdist_rpm": { -- 1.7.9.5