filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / pylib / gfxprim / utils.py
blob4040a25ea4f8b79747fd3b592f28ac51790b415b
1 """
2 Utility methods for polishing SWIGified gfxprim.
3 """
5 def extend(cls, name=None):
6 "Decorator extending a class with a function"
7 def decf(method):
8 funname = name
9 if not funname:
10 funname = method.__name__
11 type.__setattr__(cls, funname, method)
12 return method
13 return decf
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
21 if doc:
22 method.__doc__ = doc.strip() + '\n'
23 else:
24 method.__doc__ = ""
25 if swig_doc:
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
35 if doc:
36 method.__doc__ = doc.strip() + '\n'
37 else:
38 method.__doc__ = ""
39 if swig_doc:
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"
46 def decf(method):
47 propname = name
48 if not propname:
49 propname = method.__name__
50 cls.__swig_getmethods__[propname] = method
51 return decf
54 def add_swig_setmethod(cls, name=None):
55 "Decorator to add a property set method to a SWIG-defined class"
56 def decf(method):
57 propname = name
58 if not propname:
59 propname = method.__name__
60 cls.__swig_setmethods__[propname] = method
61 return decf
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)
69 import re
70 il = [re.compile(i) for i in include]
71 el = [re.compile(i) for i in exclude]
72 for name in dir(from_):
73 try:
74 o = from_[name]
75 except TypeError:
76 o = from_.__getattribute__(name)
78 ok = True
79 if il:
80 ok = False
81 for x in il:
82 if x.match(name):
83 ok = True
84 if el:
85 for x in el:
86 if x.match(name):
87 ok = False
89 if ok:
90 newname = name if sub == None else sub(name)
91 try:
92 to[newname] = o
93 except TypeError:
94 to.__setattr__(newname, o)