/usr/share/pyshared/cream/melange/api.py is in python-cream.melange 0.5.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | # Copyright: 2007-2013, Sebastian Billaudelle <sbillaudelle@googlemail.com>
# 2010-2013, Kristoffer Kleine <kris.kleine@yahoo.de>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os
import sys
import imp
import threading
from gi.repository import GObject as gobject
APIS = {}
def import_api_file(path, widget_id):
api_file = os.path.join(path, '__init__.py')
if os.path.isfile(api_file):
sys.path.insert(0, path)
with open(api_file) as file_handle:
imp.load_module(
'widget_api',
file_handle,
api_file,
('.py', 'r', imp.PY_SOURCE)
)
else:
APIS[widget_id] = API
def expose(func):
func._exposed = True
return func
def register(widget_id):
def wrapper(api_cls):
APIS[widget_id] = api_cls
return api_cls
return wrapper
def in_main_thread(func):
""" Decorator for functions that have to be called in the main thread. """
def wrapper(*args, **kwargs):
f = FunctionInMainThread(func)
return f(*args, **kwargs)
return wrapper
class API(object):
@expose
def log(self, msg):
self.messages.info(msg)
def get_exposed_methods(self):
methods = []
for attr in dir(self):
if hasattr(getattr(self, attr), '_exposed'):
methods.append(attr)
return methods
class Thread(threading.Thread, gobject.GObject):
"""An advanced threading class emitting a GObject signal after running."""
__gtype_name__ = 'MelangeThread'
__gsignals__ = {
'finished': (gobject.SignalFlags.RUN_LAST, None, (str, object))
}
def __init__(self, func, callback_id, args=None):
self.func = func
self.callback_id = callback_id
self.args = args if args is not None else []
threading.Thread.__init__(self)
gobject.GObject.__init__(self)
def run(self):
ret = self.func(*self.args)
gobject.timeout_add(0, self._emit, ret)
def _emit(self, ret):
self.emit('finished', self.callback_id, ret)
class FunctionInMainThread(object):
""" A wrapper for functions that have to be called in the main thread. """
def __init__(self, func):
self.func = func
self.lock = threading.Event()
self.ret = None
def __call__(self, *args, **kwargs):
self.lock.clear()
gobject.timeout_add(0, self._func_wrapper, args, kwargs)
self.lock.wait()
return self.ret
def _func_wrapper(self, args, kwargs):
try:
self.ret = self.func(*args, **kwargs)
except Exception:
import traceback
traceback.print_exc()
self.lock.set()
|