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
|
# Sketch - A Python-based interactive drawing program
# Copyright (C) 1998, 1999, 2000, 2002 by Bernhard Herzog
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
from types import ListType
from Sketch import _
from Sketch.const import SCRIPT_UNDO, SCRIPT_GET, SCRIPT_OBJECT, \
SCRIPT_OBJECTLIST, SelectSet
from Sketch.warn import warn, USER
class UndoMethodWrapper:
def __init__(self, method, document):
self.method = method
self.document = document
def __call__(self, *args, **kw):
self.document.AddUndo(apply(self.method, args, kw))
class ObjectMethodWrapper:
def __init__(self, method, document):
self.method = method
self.document = document
def __call__(self, *args, **kw):
return ObjectWrapper(apply(self.method, args, kw), self.document)
class ObjectListMethodWrapper:
def __init__(self, method, document):
self.method = method
self.document = document
def __call__(self, *args, **kw):
objects = apply(self.method, args, kw)
return map(ObjectWrapper, objects, (self.document,) * len(objects))
# special methods (__*__) which can be called safely
safe_special_methods = {
"__eq__": 1,
"__lt__": 1,
"__gt__": 1,
"__cmp__": 1,
"__repr__": 1,
"__str__": 1,
"__coerce__": 1,
}
class Wrapper:
def __init__(self, object, document):
self._object = object
self._document = document
def __getattr__(self, attr):
try:
access = self._object.script_access[attr]
except KeyError:
if not safe_special_methods.get(attr):
warn(USER,'Cant access attribute %s of %s in safe user script',
attr, self._object)
raise AttributeError, attr
if access == SCRIPT_UNDO:
return UndoMethodWrapper(getattr(self._object, attr),
self._document)
elif access == SCRIPT_GET:
return getattr(self._object, attr)
elif access == SCRIPT_OBJECT:
return ObjectMethodWrapper(getattr(self._object, attr),
self._document)
elif access == SCRIPT_OBJECTLIST:
return ObjectListMethodWrapper(getattr(self._object, attr),
self._document)
else:
raise AttributeError, attr
def __cmp__(self, other):
return cmp(self._object, strip_wrapper(other))
def strip_wrapper(wrapped):
if isinstance(wrapped, Wrapper):
return wrapped._object
else:
return object
class DocumentWrapper(Wrapper):
def __init__(self, document):
Wrapper.__init__(self, document, document)
def SelectObject(self, objects, mode = SelectSet):
if type(objects) == ListType:
objects = map(strip_wrapper, objects)
else:
objects = strip_wrapper(objects)
self._document.SelectObject(objects, mode)
def DeselectObject(self, object):
self._document.DeselectObject(strip_wrapper(object))
def Insert(self, object, undo_text = _("Create Object")):
self._document(strip_wrapper(object), undo_text)
#
ObjectWrapper = Wrapper
|