XDG Support
authorEd Page <eopage@byu.net>
Thu, 11 Aug 2011 00:37:50 +0000 (19:37 -0500)
committerEd Page <eopage@byu.net>
Thu, 11 Aug 2011 00:37:50 +0000 (19:37 -0500)
gonvert/constants.py
gonvert/gonvert_qt.py
gonvert/util/linux.py
gonvert/util/qwrappers.py
setup.py

index 517968a..6604780 100644 (file)
@@ -1,13 +1,8 @@
-import os
-
 __pretty_app_name__ = "Gonvert"
 __app_name__ = "gonvert"
 __version__ = "1.1.4"
 __build__ = 3
 __app_magic__ = 0xdeadbeef
 __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
 
 PROFILE_STARTUP = False
 IS_MAEMO = True
index aa3d7e5..370ea5e 100755 (executable)
@@ -19,6 +19,8 @@ QtGui = qt_compat.import_module("QtGui")
 import constants
 from util import qui_utils
 from util import misc as misc_utils
 import constants
 from util import qui_utils
 from util import misc as misc_utils
+from util import linux as linux_utils
+
 import unit_data
 
 
 import unit_data
 
 
@@ -225,8 +227,11 @@ class Gonvert(object):
                        return self._hiddenUnits[categoryName]
 
        def load_settings(self):
                        return self._hiddenUnits[categoryName]
 
        def load_settings(self):
+               settingsPath = linux_utils.get_resource_path(
+                       "config", constants.__app_name__, "settings.json"
+               )
                try:
                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")
                                settings = simplejson.load(settingsFile)
                except IOError, e:
                        _moduleLogger.info("No settings")
@@ -291,7 +296,11 @@ class Gonvert(object):
                        "useQuick": self._condensedAction.isChecked(),
                        "sortBy": sortBy,
                }
                        "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
                        simplejson.dump(settings, settingsFile)
 
        @property
@@ -398,7 +407,10 @@ class Gonvert(object):
 
        @misc_utils.log_exception(_moduleLogger)
        def _on_log(self, checked = False):
 
        @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)
                        logLines = f.xreadlines()
                        log = "".join(logLines)
                        self._clipboard.setText(log)
@@ -1547,14 +1559,25 @@ class UnitWindow(object):
 
 def run_gonvert():
        try:
 
 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
 
        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)
        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)
        rotating.setFormatter(logging.Formatter(logFormat))
        root = logging.getLogger()
        root.addHandler(rotating)
index 4e77445..21bf959 100644 (file)
@@ -28,13 +28,13 @@ def set_process_name(name):
                _moduleLogger.warning('Unable to set processName: %s" % e')
 
 
                _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
        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":
                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:
                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)
                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)
        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):
 
 
 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)
 
        if base is not None:
                finalPath = os.path.join(base, name)
index 09270cd..167f2ea 100644 (file)
@@ -11,6 +11,7 @@ QtGui = qt_compat.import_module("QtGui")
 
 import qui_utils
 import misc as misc_utils
 
 import qui_utils
 import misc as misc_utils
+import linux as linux_utils
 
 
 _moduleLogger = logging.getLogger(__name__)
 
 
 _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):
        @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)
                                logLines = f.xreadlines()
                                log = "".join(logLines)
                                self._clipboard.setText(log)
index f397d6c..743668c 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -97,6 +97,7 @@ setup(
        ],
        requires=[
                "PySide",
        ],
        requires=[
                "PySide",
+               "pyxdg",
                "simplejson",
        ],
        cmdclass={
                "simplejson",
        ],
        cmdclass={
@@ -112,7 +113,7 @@ setup(
                        "copyright": "gpl",
                        "changelog": CHANGES,
                        "buildversion": str(BUILD),
                        "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": {
                        "architecture": "any",
                },
                "sdist_diablo": {
@@ -125,7 +126,7 @@ setup(
                        "copyright": "gpl",
                        "changelog": CHANGES,
                        "buildversion": str(BUILD),
                        "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": {
                        "architecture": "any",
                },
                "sdist_fremantle": {
@@ -138,8 +139,8 @@ setup(
                        "copyright": "gpl",
                        "changelog": CHANGES,
                        "buildversion": str(BUILD),
                        "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": {
                        "architecture": "any",
                },
                "sdist_harmattan": {
@@ -154,7 +155,7 @@ setup(
                        "copyright": "gpl",
                        "changelog": CHANGES,
                        "buildversion": str(BUILD),
                        "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": {
                        "architecture": "any",
                },
                "bdist_rpm": {