/usr/lib/python2.7/dist-packages/zim/actions.py is in zim 0.68~rc1-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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | # -*- coding: utf-8 -*-
# Copyright 2013-2015 Jaap Karssenberg <jaap.karssenberg@gmail.com>
'''Action interface classes.
Objects can have "actions", which are basically attributes of the
class L{Action} or L{ToggleAction}. These objects are callable as bound
methods. So actions are kind of special methods that define some
interface parameters, like what icon and label to use in the menu.
Use the L{action} and L{toggle_action} decorators to create actions.
There is no direct relation with the C{gtk.Action} and C{gtk.ToggleAction}
classes, but it can cooperate with these classes and use them as proxies.
'''
import inspect
import weakref
import logging
import re
import zim.errors
logger = logging.getLogger('zim')
# We want to switch between <Control> for linux and windows and
# <Command> for OS X. The gtk solution is to use the abstract <Primary>
# modifier key. Unfortunately, this is not supported in gtk before
# version gtk_version 2.24.7. Therefore we try to detect whether this
# abstract key is supported or not, and if not, we fall back to <Control>.
#
# Secondary use of the PRIMARY_MODIFIER constant is that it can be
# shown in user menus.
_accelerator_preparse_re = re.compile('(?i)<Primary>')
def gtk_accelerator_preparse(code, force=False):
'''Pre-parse the accelerator code to change <Primary> into
<Control> or <Command> if <Primary> is not supported.
@param code: accelerator code
@param force: if C{True} <Primary> is replaced even if not needed
@returns: same or modified accelerator code
'''
if not code:
return code # tolerate None ...
m = _accelerator_preparse_re.search(code)
if m:
import gtk
x, mod = gtk.accelerator_parse('<Primary>')
if not mod:
# <Primary> is not supported - anyway to detect OS X?
return _accelerator_preparse_re.sub('<Control>', code)
elif force:
if mod == gtk.gdk.META_MASK:
return _accelerator_preparse_re.sub('<Command>', code)
else:
return _accelerator_preparse_re.sub('<Control>', code)
else:
return code
else:
return code
try:
import gtk
PRIMARY_MODIFIER_STRING = gtk_accelerator_preparse('<primary>', force=True)
PRIMARY_MODIFIER_MASK = gtk.gdk.META_MASK if PRIMARY_MODIFIER_STRING == '<Command>' else gtk.gdk.CONTROL_MASK
except ImportError:
PRIMARY_MODIFIER_STRING = None
PRIMARY_MODIFIER_MASK = None
# FIXME - temporary helper method - remove it again when all users are refactored
def gtk_accelerator_preparse_list(actions):
myactions = []
for action in actions:
if len(action) > 3:
a = list(action)
a[3] = gtk_accelerator_preparse(a[3])
action = tuple(a)
myactions.append(action)
return myactions
class ActionMethod(object):
pass
def action(label, stock=None, accelerator='', tooltip='', readonly=True, alt_accelerator=None):
'''Decorator to turn a method into an L{Action} object
Methods decorated with this decorator can have keyword arguments
but no positional arguments.
@param label: the label used e.g for the menu item
@param stock: stock item to define the icon
@param accelerator: accelerator key description
@param tooltip: tooltip text, if C{None} will default to C{label}
@param readonly: if C{True} this action should also be available
for readonly notebooks
@param alt_accelerator: alternative accelerator key binding
'''
# TODO see where "readonly" should go
def _action(function):
return Action(function.__name__, function, label, stock, accelerator, tooltip, readonly, alt_accelerator)
return _action
class Action(ActionMethod):
'''Action, used by the L{action} decorator'''
def __init__(self, name, func, label, stock=None, accelerator='', tooltip='', readonly=True, alt_accelerator=None):
assert not (stock and '<' in stock), 'Looks like stock contains accelerator: %s %s' % (name, stock)
assert self._assert_args(func), '%s() has incompatible argspec' % func.__name__
if not tooltip:
tooltip = label.replace('_', '')
self.name = name
self.readonly = readonly
self.func = func
self._attr = (self.name, label, tooltip, stock)
self._alt_attr = (self.name + '_alt1', label, tooltip, stock)
self._accel = gtk_accelerator_preparse(accelerator)
self._alt_accel = gtk_accelerator_preparse(alt_accelerator)
def _assert_args(self, func):
args, varargs, keywords, defaults = inspect.getargspec(func)
if defaults:
return len(defaults) == len(args) - 1 # -1 for "self"
else:
return len(args) == 1 # self
def __get__(self, instance, klass):
if instance is None:
return self # class access
# instance acces, return bound method
def func(*args, **kwargs):
self.func(instance, *args, **kwargs)
return func
def connect_actionable(self, instance, actionable):
'''Connect a C{gtk.Action} or C{gtk.Button} to this action.
@param instance: the object instance that owns this action
@param actionable: proxy object, needs to have methods
C{set_active(is_active)} and C{get_active()} and a signal
'C{activate}'.
'''
actionable.connect('activate', self.do_activate, instance)
def do_activate(self, actionable, instance):
'''Callback for activate signal of connected objects'''
logger.debug('Action: %s', self.name)
try:
self.__get__(instance, instance.__class__)()
except:
zim.errors.exception_handler(
'Exception during action: %s' % self.name)
def toggle_action(label, stock=None, accelerator='', tooltip='', readonly=True, init=False):
'''Decorator to turn a method into an L{ToggleAction} object
The decorated method should be defined as:
C{my_toggle_method(self, active)}. The 'C{active}' parameter is a
boolean that reflects the new state of the toggle.
Users can also call the method without setting the C{active}
parameter. In this case the wrapper determines how to toggle the
state and calls the inner function with the new state.
@param label: the label used e.g for the menu item
@param stock: stock item to define the icon
@param accelerator: accelerator key description
@param tooltip: tooltip text, if C{None} will default to C{label}
@param readonly: if C{True} this action should also be available
for readonly notebooks
@param init: initial state of the toggle
'''
# TODO see where "readonly" should go
def _toggle_action(function):
return ToggleAction(function.__name__, function, label, stock, accelerator, tooltip, readonly, init)
return _toggle_action
class ToggleAction(Action):
'''Toggle action, used by the L{toggle_action} decorator'''
def __init__(self, name, func, label, stock=None, accelerator='', tooltip='', readonly=True, init=False):
# The ToggleAction instance lives in the client class object;
# using weakkeydict to store instance attributes per
# client object
Action.__init__(self, name, func, label, stock, accelerator, tooltip, readonly)
self._init = init
self._state = weakref.WeakKeyDictionary()
self._proxies = weakref.WeakKeyDictionary()
def _assert_args(self, func):
args, varargs, keywords, defaults = inspect.getargspec(func)
return len(args) == 2 # (self, active)
def __get__(self, instance, klass):
if instance is None:
return self # class access
if not instance in self._state:
self._state[instance] = self._init
# instance acces, return bound method
def func(active=None):
if active is None:
active = not self._state[instance]
elif active == self._state[instance]:
return # nothing to do
self.func(instance, active)
# Update state and notify actionables
self._state[instance] = active
for actionable in self._proxies.get(instance, []):
actionable.set_active(active)
return func
def connect_actionable(self, instance, actionable):
'''Connect a C{gtk.ToggleAction} or C{gtk.ToggleButton} to this action.
@param instance: the object instance that owns this action
@param actionable: proxy object, needs to have methods
C{set_active(is_active)} and C{get_active()} and a signal
'C{toggled}'.
'''
actionable.set_active(self._state.get(instance, self._init))
actionable.connect('toggled', self.do_activate, instance)
if not instance in self._proxies:
self._proxies[instance] = []
self._proxies[instance].append(actionable)
def do_activate(self, actionable, instance):
'''Callback for activate signal of connected objects'''
active = actionable.get_active()
if active != self._state.get(instance, self._init):
logger.debug('Action: %s(%s)', self.name, active)
try:
self.__get__(instance, instance.__class__)()
except Exception as error:
zim.errors.exception_handler(
'Exception during toggle action: %s(%s)' % (self.name, active))
def radio_action(*radio_options):
def _action(function):
return RadioAction(function.__name__, function, radio_options)
return _action
def radio_option(key, label, stock=None, accelerator='', tooltip=''):
return (key, stock, label, accelerator, tooltip)
# switching stock & label to match actiongroup.add_radio_actions()
def gtk_radioaction_set_current(g_radio_action, key):
# gtk.radioaction.set_current is gtk >= 2.10
for a in g_radio_action.get_group():
if a.get_name().endswith('_' + key):
a.activate()
break
class RadioAction(ActionMethod):
def __init__(self, name, func, radio_options):
# The RadioAction instance lives in the client class object;
# using weakkeydict to store instance attributes per
# client object
self.name = name
self.func = func
self.keys = [opt[0] for opt in radio_options]
self._entries = tuple(
(name + '_' + opt[0],) + opt[1:] + (i,)
for i, opt in enumerate(radio_options)
)
self._state = weakref.WeakKeyDictionary()
self._proxies = weakref.WeakKeyDictionary()
def _assert_args(self, func):
args, varargs, keywords, defaults = inspect.getargspec(func)
return len(args) == 2 # (self, key)
def __get__(self, instance, klass):
if instance is None:
return self # class access
# instance acces, return bound method
def func(key):
if not key in self.keys:
raise ValueError('Invalid key: %s' % key)
self.func(instance, key)
# Update state and notify actionables
self._state[instance] = key
for actionable in self._proxies.get(instance, []):
gtk_radioaction_set_current(actionable, key)
return func
def do_changed(self, gaction, current, instance):
'''Callback for activate signal of connected objects'''
try:
name = current.get_name()
assert name.startswith(self.name + '_')
key = name[len(self.name) + 1:]
if instance in self._state and key == self._state[instance]:
pass
else:
logger.debug('Action: %s(%s)', self.name, key)
self.__get__(instance, instance.__class__)(key)
except:
zim.errors.exception_handler(
'Exception during action: %s(%s)' % (self.name, key))
def get_gtk_actiongroup(obj):
'''Return a C{gtk.ActionGroup} for an object using L{Action}
objects as attributes.
Defines the attribute C{obj.actiongroup} if it does not yet exist.
This method can only be used when gtk is available
'''
import gtk
if hasattr(obj, 'actiongroup') \
and obj.actiongroup is not None:
return obj.actiongroup
obj.actiongroup = gtk.ActionGroup(obj.__class__.__name__)
for name, action in inspect.getmembers(obj.__class__, lambda m: isinstance(m, ActionMethod)):
if isinstance(action, RadioAction):
obj.actiongroup.add_radio_actions(action._entries)
gaction = obj.actiongroup.get_action(action._entries[0][0])
gaction.connect('changed', action.do_changed, obj)
if not obj in action._proxies:
action._proxies[obj] = []
action._proxies[obj].append(gaction)
if obj in action._state:
key = action._state[obj]
gtk_radioaction_set_current(gaction, key)
else:
_gtk_add_action_with_accel(obj, obj.actiongroup, action, action._attr, action._accel)
if action._alt_accel:
_gtk_add_action_with_accel(obj, obj.actiongroup, action, action._alt_attr, action._alt_accel)
return obj.actiongroup
def _gtk_add_action_with_accel(obj, actiongroup, action, attr, accel):
import gtk
if isinstance(action, ToggleAction):
gaction = gtk.ToggleAction(*attr)
else:
gaction = gtk.Action(*attr)
gaction.zim_readonly = action.readonly # HACK
action.connect_actionable(obj, gaction)
actiongroup.add_action_with_accel(gaction, accel)
|