1 # PyOpenSG is (C) Copyright 2005-2009 by Allen Bierbaum
3 # This file is part of PyOpenSG.
5 # PyOpenSG is free software; you can redistribute it and/or modify it under
6 # the terms of the GNU Lesser General Public License as published by the Free
7 # Software Foundation; either version 2 of the License, or (at your option)
10 # PyOpenSG is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 class AllocationError(Exception):
24 class CoredNode(object):
25 """ Mixin object for a Cored node type """
27 __slots__
= ['mNode', 'mCore']
31 obj
.mNode
= osg2
.OSGBase
.Node
.create()
32 obj
.mCore
= cls
.coreType
.create()
33 obj
.mNode
.setCore(obj
.mCore
)
35 create
= classmethod(create
)
37 def __new__(cls
, *args
, **kwargs
):
38 # A cored not can only be created from within the cored_node module and from code named 'create'.
39 if __name__
!= sys
._getframe
(1).f_globals
['__name__'] or \
40 'create' != sys
._getframe
(1).f_code
.co_name
:
41 raise AllocationError("Can't construct a '%s' directly, use %s.create()"%(cls
.__name
__, cls
.__name
__))
42 return object.__new
__(cls
, *args
, **kwargs
)
49 assert self
.mNode
.getCore() == self
.mCore
55 def __getattr__(self
, name
):
56 return getattr(self
.mCore
, name
)
58 # Use code to find the calling frame.
59 def __setattr__(self
, name
, value
):
60 # Figure out the module that we were called from. If it is not
61 # the current module, then throw an exception.
62 if __name__
!= sys
._getframe
(1).f_globals
['__name__']:
63 raise AttributeError("Can't set attributes on a %s."%self
.__class
__.__name
__)
64 object.__setattr
__(self
, name
, value
)
66 def setCore(self
, newCore
):
68 self
.mNode
.setCore(newCore
)
70 def addNodeCoreTypes(globalDict
, osg2Module
):
74 node_cores
.extend([(n
,c
) for (n
,c
) in osg2Module
.__dict
__.iteritems() if isinstance(c
,type) \
75 and (osg2
.OSGBase
.NodeCore
in c
.__mro
__) and not c
.__name
__.endswith("Base")])
77 for (name
,ctype
) in node_cores
:
78 cored_node_classname
= "%sNode"%name
79 cored_node_class
= type(cored_node_classname
, (CoredNode
,), {'coreType':ctype
,})
81 globalDict
[cored_node_classname
] = cored_node_class