From 6ed73e1b58458818979bedbb9bbe41711c7fa1df Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 13 Aug 2011 21:22:45 -0500 Subject: [PATCH] Adding XDG support --- dialcentral/alarm_handler.py | 15 ++++++----- dialcentral/alarm_notify.py | 18 +++++++++---- dialcentral/constants.py | 7 ----- dialcentral/dialcentral_qt.py | 45 +++++++++++++++++++------------- dialcentral/examples/log_notifier.py | 16 +++++------- dialcentral/examples/sound_notifier.py | 20 +++++++------- dialcentral/util/qwrappers.py | 6 ++++- 7 files changed, 72 insertions(+), 55 deletions(-) diff --git a/dialcentral/alarm_handler.py b/dialcentral/alarm_handler.py index a79f992..bf14f82 100644 --- a/dialcentral/alarm_handler.py +++ b/dialcentral/alarm_handler.py @@ -1,14 +1,18 @@ #!/usr/bin/env python +from __future__ import with_statement + import os import time import datetime import ConfigParser import logging +import dbus + import util.qt_compat as qt_compat QtCore = qt_compat.QtCore -import dbus +from util import linux as linux_utils _FREMANTLE_ALARM = "Fremantle" @@ -432,9 +436,11 @@ def main(): parser.add_option("-r", "--recurrence", action="store", type="int", dest="recurrence", help="How often the alarm occurs", default=5) (commandOptions, commandArgs) = parser.parse_args() + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") + alarmHandler = AlarmHandler() config = ConfigParser.SafeConfigParser() - config.read(constants._user_settings_) + config.read(settingsPath) alarmHandler.load_settings(config, "alarm") if commandOptions.display: @@ -449,11 +455,8 @@ def main(): alarmHandler.apply_settings(isEnabled, recurrence) alarmHandler.save_settings(config, "alarm") - configFile = open(constants._user_settings_, "wb") - try: + with open(settingsPath, "wb") as configFile: config.write(configFile) - finally: - configFile.close() if __name__ == "__main__": diff --git a/dialcentral/alarm_notify.py b/dialcentral/alarm_notify.py index bc6240e..548fd47 100755 --- a/dialcentral/alarm_notify.py +++ b/dialcentral/alarm_notify.py @@ -8,9 +8,13 @@ import logging import logging.handlers import constants +from util import linux as linux_utils from backends.gvoice import gvoice +CACHE_PATH = linux_utils.get_resource_path("cache", constants.__app_name__) + + def get_missed(backend): missedPage = backend._browser.download(backend._XML_MISSED_URL) missedJson = backend._grab_json(missedPage) @@ -53,8 +57,8 @@ def is_type_changed(backend, type, get_material): jsonMaterial = get_material(backend) unreadCount = jsonMaterial["unreadCounts"][type] - previousSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.old.json" % type) - currentSnapshotPath = os.path.join(constants._data_path_, "snapshot_%s.json" % type) + previousSnapshotPath = os.path.join(CACHE_PATH, "snapshot_%s.old.json" % type) + currentSnapshotPath = os.path.join(CACHE_PATH, "snapshot_%s.json" % type) try: os.remove(previousSnapshotPath) @@ -87,7 +91,7 @@ def is_type_changed(backend, type, get_material): def create_backend(config): - gvCookiePath = os.path.join(constants._data_path_, "gv_cookies.txt") + gvCookiePath = os.path.join(CACHE_PATH, "gv_cookies.txt") backend = gvoice.GVoiceBackend(gvCookiePath) loggedIn = False @@ -150,8 +154,10 @@ def is_changed(config, backend): def notify_on_change(): + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") + config = ConfigParser.SafeConfigParser() - config.read(constants._user_settings_) + config.read(settingsPath) backend = create_backend(config) notifyUser = is_changed(config, backend) @@ -165,9 +171,11 @@ def notify_on_change(): if __name__ == "__main__": + notifierPath = os.path.join(CACHE_PATH, "notifier.log") + 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._notifier_logpath_, maxBytes=512*1024, backupCount=1) + rotating = logging.handlers.RotatingFileHandler(notifierPath, maxBytes=512*1024, backupCount=1) rotating.setFormatter(logging.Formatter(logFormat)) root = logging.getLogger() root.addHandler(rotating) diff --git a/dialcentral/constants.py b/dialcentral/constants.py index 56ebddc..ba7fadf 100644 --- a/dialcentral/constants.py +++ b/dialcentral/constants.py @@ -1,14 +1,7 @@ -import os - __pretty_app_name__ = "DialCentral" __app_name__ = "dialcentral" __version__ = "1.3.6" __build__ = 0 __app_magic__ = 0xdeadbeef -_data_path_ = os.path.join(os.path.expanduser("~"), ".%s" % __app_name__) -_user_settings_ = "%s/settings.ini" % _data_path_ -_custom_notifier_settings_ = "%s/notifier.ini" % _data_path_ -_user_logpath_ = "%s/%s.log" % (_data_path_, __app_name__) -_notifier_logpath_ = "%s/notifier.log" % _data_path_ IS_MAEMO = True diff --git a/dialcentral/dialcentral_qt.py b/dialcentral/dialcentral_qt.py index 3dcf46f..f1bde9b 100755 --- a/dialcentral/dialcentral_qt.py +++ b/dialcentral/dialcentral_qt.py @@ -19,6 +19,7 @@ import alarm_handler from util import qtpie from util import qwrappers from util import qui_utils +from util import linux as linux_utils from util import misc as misc_utils import session @@ -48,8 +49,9 @@ class Dialcentral(qwrappers.ApplicationWrapper): def load_settings(self): try: + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") config = ConfigParser.SafeConfigParser() - config.read(constants._user_settings_) + config.read(settingsPath) except IOError, e: _moduleLogger.info("No settings") return @@ -70,7 +72,8 @@ class Dialcentral(qwrappers.ApplicationWrapper): self._mainWindow.save_settings(config) - with open(constants._user_settings_, "wb") as configFile: + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") + with open(settingsPath, "wb") as configFile: config.write(configFile) def get_icon(self, name): @@ -103,7 +106,7 @@ class Dialcentral(qwrappers.ApplicationWrapper): @property def fsContactsPath(self): - return os.path.join(constants._data_path_, "contacts") + return linux_utils.get_resource_path("data", constants.__app_name__, "contacts") @property def streamHandler(self): @@ -258,7 +261,8 @@ class MainWindow(qwrappers.WindowWrapper): self._window.resized.connect(self._on_window_resized) self._errorLog = self._app.errorLog - self._session = session.Session(self._errorLog, constants._data_path_) + cachePath = linux_utils.get_resource_path("cache", constants.__app_name__) + self._session = session.Session(self._errorLog, cachePath) self._session.error.connect(self._on_session_error) self._session.loggedIn.connect(self._on_login) self._session.loggedOut.connect(self._on_logout) @@ -420,15 +424,13 @@ class MainWindow(qwrappers.WindowWrapper): orientation = config.get(constants.__pretty_app_name__, "orientation") except ConfigParser.NoOptionError, e: _moduleLogger.info( - "Settings file %s is missing option %s" % ( - constants._user_settings_, + "Settings file is missing option %s" % ( e.option, ), ) except ConfigParser.NoSectionError, e: _moduleLogger.info( - "Settings file %s is missing section %s" % ( - constants._user_settings_, + "Settings file is missing section %s" % ( e.section, ), ) @@ -443,15 +445,13 @@ class MainWindow(qwrappers.WindowWrapper): self._updateVoicemailOnMissedCall = config.getboolean("2 - Account Info", "updateVoicemailOnMissedCall") except ConfigParser.NoOptionError, e: _moduleLogger.info( - "Settings file %s is missing option %s" % ( - constants._user_settings_, + "Settings file is missing option %s" % ( e.option, ), ) except ConfigParser.NoSectionError, e: _moduleLogger.info( - "Settings file %s is missing section %s" % ( - constants._user_settings_, + "Settings file is missing section %s" % ( e.section, ), ) @@ -476,16 +476,14 @@ class MainWindow(qwrappers.WindowWrapper): settingValue = config.get(sectionName, settingName) except ConfigParser.NoOptionError, e: _moduleLogger.info( - "Settings file %s is missing section %s" % ( - constants._user_settings_, + "Settings file is missing section %s" % ( e.section, ), ) return except ConfigParser.NoSectionError, e: _moduleLogger.info( - "Settings file %s is missing section %s" % ( - constants._user_settings_, + "Settings file is missing section %s" % ( e.section, ), ) @@ -761,14 +759,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/dialcentral/examples/log_notifier.py b/dialcentral/examples/log_notifier.py index 541ac18..ab0a1cc 100644 --- a/dialcentral/examples/log_notifier.py +++ b/dialcentral/examples/log_notifier.py @@ -2,24 +2,22 @@ from __future__ import with_statement -import sys import datetime import ConfigParser - -sys.path.insert(0,"/usr/lib/dialcentral/") - - -import constants -import alarm_notify +from dialcentral import constants +from dialcentral.util import linux as linux_utils +from dialcentral import alarm_notify def notify_on_change(): - with open(constants._notifier_logpath_, "a") as file: + notifierLogPath = linux_utils.get_resource_path("cache", constants.__app_name__, "notifier.log") + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") + with open(notifierLogPath, "a") as file: file.write("Notification: %r\n" % (datetime.datetime.now(), )) config = ConfigParser.SafeConfigParser() - config.read(constants._user_settings_) + config.read(settingsPath) backend = alarm_notify.create_backend(config) notifyUser = alarm_notify.is_changed(config, backend) diff --git a/dialcentral/examples/sound_notifier.py b/dialcentral/examples/sound_notifier.py index c31e413..aab9ddd 100644 --- a/dialcentral/examples/sound_notifier.py +++ b/dialcentral/examples/sound_notifier.py @@ -1,26 +1,26 @@ #!/usr/bin/env python import os -import sys import ConfigParser import logging -sys.path.insert(0,"/usr/lib/dialcentral/") - - -import constants -import alarm_notify +from dialcentral import constants +from dialcentral.util import linux as linux_utils +from dialcentral import alarm_notify def notify_on_change(): + settingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "settings.ini") + notifierSettingsPath = linux_utils.get_resource_path("config", constants.__app_name__, "notifier.ini") + config = ConfigParser.SafeConfigParser() - config.read(constants._user_settings_) + config.read(settingsPath) backend = alarm_notify.create_backend(config) notifyUser = alarm_notify.is_changed(config, backend) config = ConfigParser.SafeConfigParser() - config.read(constants._custom_notifier_settings_) + config.read(notifierSettingsPath) soundFile = config.get("Sound Notifier", "soundfile") soundFile = "/usr/lib/gv-notifier/alert.mp3" @@ -36,7 +36,9 @@ def notify_on_change(): if __name__ == "__main__": - logging.basicConfig(level=logging.WARNING, filename=constants._notifier_logpath_) + notifierLogPath = linux_utils.get_resource_path("cache", constants.__app_name__, "notifier.log") + + logging.basicConfig(level=logging.WARNING, filename=notifierLogPath) logging.info("Sound Notifier %s-%s" % (constants.__version__, constants.__build__)) logging.info("OS: %s" % (os.uname()[0], )) logging.info("Kernel: %s (%s) for %s" % os.uname()[2:]) diff --git a/dialcentral/util/qwrappers.py b/dialcentral/util/qwrappers.py index 09270cd..167f2ea 100644 --- a/dialcentral/util/qwrappers.py +++ b/dialcentral/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) -- 1.7.9.5