diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 7aafd136..1a70bfa7 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -156,6 +156,27 @@ class DeletableGithubObject( TestCaseWithGithubTestObject ): self.expectDelete( "/test" ) self.o.delete() +class GithubObjectWithExtendedScalarAttribute( TestCaseWithGithubTestObject ): + ContainedObject = GithubObject( + "ContainedObject", + BaseUrl( lambda obj: "/test/a3s/" + obj.id ), + SimpleScalarAttributes( "id", "name", "desc" ) + ) + + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + SimpleScalarAttributes( "a1", "a2" ), + ExtendedScalarAttribute( "a3", ContainedObject ) + ) + + def testCompletion( self ): + self.expectGet( "/test" ).andReturn( { "a3": { "id": "id1", "name": "name1" } } ) + self.assertEqual( self.o.a3.id, "id1" ) + self.assertEqual( self.o.a3.name, "name1" ) + self.expectGet( "/test/a3s/id1" ).andReturn( { "desc": "desc1" } ) + self.assertEqual( self.o.a3.desc, "desc1" ) + class GithubObjectWithExtendedListAttribute( TestCaseWithGithubTestObject ): ContainedObject = GithubObject( "ContainedObject", diff --git a/github/GithubObject.py b/github/GithubObject.py index 61ec19a5..209d91b0 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -59,6 +59,30 @@ class ExtendedListAttribute: def getAttributeDefinitions( self ): yield "get_" + self.__pluralName, ExtendedListAttribute.AttributeDefinition( self.__pluralName, self.__type ) +class ExtendedScalarAttribute: + class AttributeDefinition: + def __init__( self, attributeName, type ): + self.__attributeName = attributeName + self.__type = type + + def getValueFromRawValue( self, obj, rawValue ): + return self.__type( obj._github, rawValue, lazy = True ) + + def updateAttributes( self, obj ): + attributes = obj._github._rawRequest( "GET", obj._baseUrl ) + # for attributeName in self.__attributeNames: + # if attributeName not in attributes: + # attributes[ attributeName ] = None + obj._updateAttributes( attributes ) + + def __init__( self, attributeName, type ): + self.__attributeName = attributeName + self.__type = type + + def getAttributeDefinitions( self ): + print ">>>", self.__attributeName + yield self.__attributeName, ExtendedScalarAttribute.AttributeDefinition( self.__attributeName, self.__type ) + class Editable: class Editor: def __init__( self, obj, mandatoryParameters, optionalParameters ):