/usr/share/pyshared/apptools/appscripting/scriptable.py is in python-apptools 4.1.0-0ubuntu1.
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 | #------------------------------------------------------------------------------
# Copyright (c) 2008, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Riverbank Computing Limited
# Description: <Enthought application scripting package component>
#------------------------------------------------------------------------------
# Enthought library imports.
from traits.api import Any, Property, Undefined
from traits.traits import trait_cast
# Local imports.
from package_globals import get_script_manager
# This is the guard that ensures that only outermost scriptable methods get
# recorded.
_outermost_call = True
def scriptable(func):
""" This is the decorator applied to functions and methods to mark them as
being scriptable.
"""
def _scripter(*args, **kwargs):
""" This is the wrapper that is returned in place of the scriptable
method.
"""
global _outermost_call
if _outermost_call:
_outermost_call = False
# See if there is an script manager set.
sm = get_script_manager()
if func.func_name == '__init__':
sm.new_object(args[0], type(args[0]), args[1:], kwargs)
try:
result = func(*args, **kwargs)
finally:
_outermost_call = True
else:
# Record the ordinary method.
try:
result = sm.record_method(func, args, kwargs)
finally:
_outermost_call = True
else:
# We aren't at the outermost call so just invoke the method.
result = func(*args, **kwargs)
return result
# Be a good citizen.
_scripter.__name__ = func.__name__
_scripter.__doc__ = func.__doc__
_scripter.__dict__.update(func.__dict__)
return _scripter
def _scriptable_get(obj, name):
""" The getter for a scriptable trait. """
global _outermost_call
saved_outermost = _outermost_call
_outermost_call = False
try:
result = getattr(obj, '_' + name, None)
if result is None:
result = obj.trait(name).default
finally:
_outermost_call = saved_outermost
if saved_outermost:
get_script_manager().record_trait_get(obj, name, result)
return result
def _scriptable_set(obj, name, value):
""" The setter for a scriptable trait. """
if _outermost_call:
get_script_manager().record_trait_set(obj, name, value)
_name = '_' + name
old_value = getattr(obj, _name, Undefined)
if old_value is not value:
setattr(obj, _name, value)
obj.trait_property_changed(name, old_value, value)
def Scriptable(trait=Any, **metadata):
""" Scriptable is a wrapper around another trait that makes it scriptable,
ie. changes to its value can be recorded. If a trait is read, but the
value isn't set to another scriptable trait or passed to a scriptable
method then the read will not be included in the recorded script. To make
sure a read is always recorded set the 'has_side_effects' argument to True.
"""
trait = trait_cast(trait)
metadata['default'] = trait.default_value()[1]
return Property(_scriptable_get, _scriptable_set, trait=trait, **metadata)
|