From 5801ede89c5499c264a3204e034f87f0f92d496b Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 10 Aug 2011 19:37:50 -0500 Subject: [PATCH] XDG Support --- gonvert/constants.py | 5 ----- gonvert/gonvert_qt.py | 33 ++++++++++++++++++++++++++++----- gonvert/util/linux.py | 36 +++++++++++++++++++++--------------- gonvert/util/qwrappers.py | 6 +++++- setup.py | 11 ++++++----- 5 files changed, 60 insertions(+), 31 deletions(-) diff --git a/gonvert/constants.py b/gonvert/constants.py index 517968a..6604780 100644 --- a/gonvert/constants.py +++ b/gonvert/constants.py @@ -1,13 +1,8 @@ -import os - __pretty_app_name__ = "Gonvert" __app_name__ = "gonvert" __version__ = "1.1.4" __build__ = 3 __app_magic__ = 0xdeadbeef -_data_path_ = os.path.join(os.path.expanduser("~"), ".%s" % __app_name__) -_user_settings_ = "%s/settings.json" % _data_path_ -_user_logpath_ = "%s/%s.log" % (_data_path_, __app_name__) PROFILE_STARTUP = False IS_MAEMO = True diff --git a/gonvert/gonvert_qt.py b/gonvert/gonvert_qt.py index aa3d7e5..370ea5e 100755 --- a/gonvert/gonvert_qt.py +++ b/gonvert/gonvert_qt.py @@ -19,6 +19,8 @@ QtGui = qt_compat.import_module("QtGui") import constants from util import qui_utils from util import misc as misc_utils +from util import linux as linux_utils + import unit_data @@ -225,8 +227,11 @@ class Gonvert(object): return self._hiddenUnits[categoryName] 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, "r") as settingsFile: settings = simplejson.load(settingsFile) except IOError, e: _moduleLogger.info("No settings") @@ -291,7 +296,11 @@ class Gonvert(object): "useQuick": self._condensedAction.isChecked(), "sortBy": sortBy, } - 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 @@ -398,7 +407,10 @@ class Gonvert(object): @misc_utils.log_exception(_moduleLogger) def _on_log(self, checked = False): - with open(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) @@ -1547,14 +1559,25 @@ class UnitWindow(object): def run_gonvert(): 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/gonvert/util/linux.py b/gonvert/util/linux.py index 4e77445..21bf959 100644 --- a/gonvert/util/linux.py +++ b/gonvert/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/gonvert/util/qwrappers.py b/gonvert/util/qwrappers.py index 09270cd..167f2ea 100644 --- a/gonvert/util/qwrappers.py +++ b/gonvert/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 f397d6c..743668c 100755 --- a/setup.py +++ b/setup.py @@ -97,6 +97,7 @@ setup( ], requires=[ "PySide", + "pyxdg", "simplejson", ], cmdclass={ @@ -112,7 +113,7 @@ setup( "copyright": "gpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-simplejson", + "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-xdg, python-simplejson", "architecture": "any", }, "sdist_diablo": { @@ -125,7 +126,7 @@ setup( "copyright": "gpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python-simplejson", + "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python-xdg, python-simplejson", "architecture": "any", }, "sdist_fremantle": { @@ -138,8 +139,8 @@ setup( "copyright": "gpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python2.5-qt4-maemo5, python-simplejson", - #"depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-pyside.qtmaemo5, python-simplejson", + "depends": "python2.5, python2.5-qt4-core, python2.5-qt4-gui, python2.5-qt4-maemo5, python-xdg, python-simplejson", + #"depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-pyside.qtmaemo5, python-xdg, python-simplejson", "architecture": "any", }, "sdist_harmattan": { @@ -154,7 +155,7 @@ setup( "copyright": "gpl", "changelog": CHANGES, "buildversion": str(BUILD), - "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-simplejson", + "depends": "python, python-pyside.qtcore, python-pyside.qtgui, python-xdg, python-simplejson", "architecture": "any", }, "bdist_rpm": { -- 1.7.9.5