fixed: compile issue
[opensg.git] / Bindings / Python / helpers / cored_node.py
blobd57f28b8db52c8d1f860dcd67f9d6ba9fb5406f2
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)
8 # any later version.
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
13 # more details.
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/>.
18 import osg2
19 import sys
21 class AllocationError(Exception):
22 pass
24 class CoredNode(object):
25 """ Mixin object for a Cored node type """
26 coreType = None
27 __slots__ = ['mNode', 'mCore']
29 def create(cls):
30 obj = cls()
31 obj.mNode = osg2.OSGBase.Node.create()
32 obj.mCore = cls.coreType.create()
33 obj.mNode.setCore(obj.mCore)
34 return obj
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)
44 def __init__(self):
45 self.mNode = None
46 self.mCore = None
48 def core(self):
49 assert self.mNode.getCore() == self.mCore
50 return self.mCore
52 def node(self):
53 return self.mNode
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):
67 self.mCore = newCore
68 self.mNode.setCore(newCore)
70 def addNodeCoreTypes(globalDict, osg2Module):
72 node_cores = []
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