2 Utility methods for polishing SWIGified gfxprim.
5 def extend(cls
, name
=None):
6 "Decorator extending a class with a function"
10 funname
= method
.__name
__
11 type.__setattr
__(cls
, funname
, method
)
16 def extend_direct(cls
, name
, call
, doc
=None, swig_doc
=True):
17 "Extend a class with a function. The first arg will be `self`."
18 def method(*args
, **kwargs
):
19 return call(*args
, **kwargs
)
20 method
.__name
__ = name
22 method
.__doc
__ = doc
.strip() + '\n'
26 method
.__doc
__ = call
.__doc
__ + '\n\n' + method
.__doc
__
27 type.__setattr
__(cls
, name
, method
)
30 def extend_submodule(module
, name
, call
, doc
=None, swig_doc
=True):
31 "Extending a submodule with a function. The first arg will be `self.ctx`."
32 def method(self
, *args
, **kwargs
):
33 return call(self
.ctx
, *args
, **kwargs
)
34 method
.__name
__ = name
36 method
.__doc
__ = doc
.strip() + '\n'
40 method
.__doc
__ = call
.__doc
__ + '\n\n' + method
.__doc
__
41 type.__setattr
__(module
, name
, method
)
44 def add_swig_getmethod(cls
, name
=None):
45 "Decorator to add a property get method to a SWIG-defined class"
49 propname
= method
.__name
__
50 cls
.__swig
_getmethods
__[propname
] = method
54 def add_swig_setmethod(cls
, name
=None):
55 "Decorator to add a property set method to a SWIG-defined class"
59 propname
= method
.__name
__
60 cls
.__swig
_setmethods
__[propname
] = method
64 def import_members(from_
, to
, include
=[], exclude
=[], sub
=None):
65 """Import members of `from_` to `to`. By default take all. If `exclude` is provided,
66 use as a filter. If `include` is provided, ONLY include those.
67 `include` and `exclude` are lists of regexes to match (include ^ and $)."""
68 assert not (include
and exclude
)
70 il
= [re
.compile(i
) for i
in include
]
71 el
= [re
.compile(i
) for i
in exclude
]
72 for name
in dir(from_
):
76 o
= from_
.__getattribute
__(name
)
90 newname
= name
if sub
== None else sub(name
)
94 to
.__setattr
__(newname
, o
)