From 8caf582b32a69a4393be98c18bf704cdb06d0038 Mon Sep 17 00:00:00 2001 From: Timothy Choi Date: Mon, 16 Sep 2024 13:55:53 +0200 Subject: [PATCH] refactor(distributed): Un-nest generateGlobalObject's applyFieldValues function (#1667) --- .../src/distributed/ConnectionRepository.py | 61 ++++++++++--------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/direct/src/distributed/ConnectionRepository.py b/direct/src/distributed/ConnectionRepository.py index 62c2d5da46..a29c61cafa 100644 --- a/direct/src/distributed/ConnectionRepository.py +++ b/direct/src/distributed/ConnectionRepository.py @@ -174,35 +174,6 @@ class ConnectionRepository( return retVal def generateGlobalObject(self, doId, dcname, values=None): - def applyFieldValues(distObj, dclass, values): - for i in range(dclass.getNumInheritedFields()): - field = dclass.getInheritedField(i) - if field.asMolecularField() is None: - value = values.get(field.getName(), None) - if value is None and field.isRequired(): - # Gee, this could be better. What would really be - # nicer is to get value from field.getDefaultValue - # or similar, but that returns a binary string, not - # a python tuple, like the following does. If you - # want to change something better, please go ahead. - packer = DCPacker() - packer.beginPack(field) - packer.packDefaultValue() - packer.endPack() - - unpacker = DCPacker() - unpacker.setUnpackData(packer.getString()) - unpacker.beginUnpack(field) - value = unpacker.unpackObject() - unpacker.endUnpack() - if value is not None: - function = getattr(distObj, field.getName()) - if function is not None: - function(*value) - else: - self.notify.error("\n\n\nNot able to find %s.%s"%( - distObj.__class__.__name__, field.getName())) - # Look up the dclass dclass = self.dclassesByName.get(dcname+self.dcSuffix) if dclass is None: @@ -229,7 +200,37 @@ class ConnectionRepository( distObj.generateInit() # Only called when constructed distObj.generate() if values is not None: - applyFieldValues(distObj, dclass, values) + for i in range(dclass.getNumInheritedFields()): + field = dclass.getInheritedField(i) + if field.asMolecularField() is None: + value = values.get(field.getName(), None) + if value is None and field.isRequired(): + # Gee, this could be better. What would really be + # nicer is to get value from field.getDefaultValue + # or similar, but that returns a binary string, not + # a python tuple, like the following does. If you + # want to change something better, please go ahead. + packer = DCPacker() + packer.beginPack(field) + packer.packDefaultValue() + packer.endPack() + + unpacker = DCPacker() + unpacker.setUnpackData(packer.getString()) + unpacker.beginUnpack(field) + value = unpacker.unpackObject() + unpacker.endUnpack() + if value is not None: + function = getattr(distObj, field.getName()) + if function is not None: + function(*value) + else: + self.notify.error( + "\n\n\nNot able to find %s.%s" % ( + distObj.__class__.__name__, + field.getName() + ) + ) distObj.announceGenerate() distObj.parentId = 0 distObj.zoneId = 0