From: Ed Page Date: Wed, 29 Dec 2010 13:41:37 +0000 (-0600) Subject: Pulling in latest skeleton code X-Git-Url: http://git.maemo.org/git/?p=theonering;a=commitdiff_plain;h=4beea848e2ea4297c8e223637f38bc3e529dec0d Pulling in latest skeleton code --- diff --git a/src/constants.py b/src/constants.py index 3732d08..54b814f 100644 --- a/src/constants.py +++ b/src/constants.py @@ -5,7 +5,7 @@ __app_name__ = "telepathy-theonering" __version__ = "0.8.23" __build__ = 0 __app_magic__ = 0xdeadbeef -_data_path_ = os.path.join(os.path.expanduser("~"), ".telepathy-theonering") +_data_path_ = os.path.join(os.path.expanduser("~"), ".%s" % __app_name__) _user_settings_ = "%s/settings.ini" % _data_path_ _user_logpath_ = "%s/theonering.log" % _data_path_ _telepathy_protocol_name_ = "gv" diff --git a/src/util/go_utils.py b/src/util/go_utils.py index 10b14e8..97d671c 100644 --- a/src/util/go_utils.py +++ b/src/util/go_utils.py @@ -95,20 +95,24 @@ class Async(object): class Timeout(object): - def __init__(self, func): + def __init__(self, func, once = True): self.__func = func self.__timeoutId = None + self.__once = once def start(self, **kwds): assert self.__timeoutId is None + callback = self._on_once if self.__once else self.__func + assert len(kwds) == 1 timeoutInSeconds = kwds["seconds"] assert 0 <= timeoutInSeconds + if timeoutInSeconds == 0: - self.__timeoutId = gobject.idle_add(self._on_once) + self.__timeoutId = gobject.idle_add(callback) else: - self.__timeoutId = timeout_add_seconds(timeoutInSeconds, self._on_once) + self.__timeoutId = timeout_add_seconds(timeoutInSeconds, callback) def is_running(self): return self.__timeoutId is not None @@ -153,6 +157,10 @@ class AsyncPool(object): pass # eat up queue to cut down dumb work self.__workQueue.put(_QUEUE_EMPTY) + def clear_tasks(self): + for _ in algorithms.itr_available(self.__workQueue): + pass # eat up queue to cut down dumb work + def add_task(self, func, args, kwds, on_success, on_error): task = func, args, kwds, on_success, on_error self.__workQueue.put(task) @@ -244,6 +252,24 @@ class AsyncLinearExecution(object): ) +class AutoSignal(object): + + def __init__(self, toplevel): + self.__disconnectPool = [] + toplevel.connect("destroy", self.__on_destroy) + + def connect_auto(self, widget, *args): + id = widget.connect(*args) + self.__disconnectPool.append((widget, id)) + + @misc.log_exception(_moduleLogger) + def __on_destroy(self, widget): + _moduleLogger.info("Destroy: %r (%s to clean up)" % (self, len(self.__disconnectPool))) + for widget, id in self.__disconnectPool: + widget.disconnect(id) + del self.__disconnectPool[:] + + def throttled(minDelay, queue): """ Throttle the calls to a function by queueing all the calls that happen diff --git a/src/util/linux.py b/src/util/linux.py index 4837f2a..4e77445 100644 --- a/src/util/linux.py +++ b/src/util/linux.py @@ -1,13 +1,79 @@ #!/usr/bin/env python +import os import logging +try: + from xdg import BaseDirectory as _BaseDirectory + BaseDirectory = _BaseDirectory +except ImportError: + BaseDirectory = None + + +_moduleLogger = logging.getLogger(__name__) + + +_libc = None + def set_process_name(name): try: # change process name for killall - import ctypes - libc = ctypes.CDLL('libc.so.6') - libc.prctl(15, name, 0, 0, 0) + global _libc + if _libc is None: + import ctypes + _libc = ctypes.CDLL('libc.so.6') + _libc.prctl(15, name, 0, 0, 0) except Exception, e: - logging.warning('Unable to set processName: %s" % e') + _moduleLogger.warning('Unable to set processName: %s" % e') + + +def get_new_resource(resourceType, resource, name): + 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) + elif resourceType == "config": + base = BaseDirectory.xdg_config_home + elif resourceType == "cache": + base = BaseDirectory.xdg_cache_home + else: + raise RuntimeError("Unknown type: "+resourceType) + else: + base = os.path.join(os.path.expanduser("~"), ".%s" % resource) + + filePath = os.path.join(base, resource, name) + dirPath = os.path.dirname(filePath) + if not os.path.exists(dirPath): + # Looking before I leap to not mask errors + os.makedirs(dirPath) + + return filePath + + +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 + + if base is not None: + finalPath = os.path.join(base, name) + if os.path.exists(finalPath): + return finalPath + + altBase = os.path.join(os.path.expanduser("~"), ".%s" % resource) + finalPath = os.path.join(altBase, name) + if os.path.exists(finalPath): + return finalPath + else: + raise RuntimeError("Resource not found: %r" % ((resourceType, resource, name), ))