From 2fd8a7c9f4efe7862c3a255343669a34e096c6f7 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 12 Feb 2012 18:57:29 +0100 Subject: [PATCH] Implement list of objects as attributes --- RoadMap.md | 2 +- github/GithubObject.UnitTest.py | 26 ++++++++++++++++++++++++++ github/GithubObject.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/RoadMap.md b/RoadMap.md index 17b58cf1..5cc78e3f 100644 --- a/RoadMap.md +++ b/RoadMap.md @@ -8,7 +8,7 @@ Documentation - api and how it is wrapped - rationale: - lazyness for objects returned by API, not for objects requested by user - - naming: get_xxx() to avoid clashes with attribute xxx (User.followers for example) + - naming: get_xxx() to avoid clashes with attribute xxx (User.followers for example), and to explicit api calls - lazy completion, but no caching - explicit edit instead of writeable attributes - data model (cf Design.md) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index c96aa903..60844543 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -156,4 +156,30 @@ class DeletableGithubObject( TestCaseWithGithubTestObject ): self.expectDelete( "/test" ) self.o.delete() +class GithubObjectWithExtendedListAttribute( TestCaseWithGithubTestObject ): + ContainedObject = GithubObject( + "ContainedObject", + BaseUrl( lambda obj: "/test/a3s/" + obj.id ), + SimpleScalarAttributes( "id", "name" ) + ) + + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + SimpleScalarAttributes( "a1", "a2" ), + Deletable(), + ExtendedListAttribute( + pluralName = "a3s", + type = ContainedObject + ) + ) + + def testGetList( self ): + self.expectGet( "/test/a3s" ).andReturn( [ { "id": "id1" }, { "id": "id2" }, { "id": "id3" } ] ) + a3s = self.o.get_a3s() + self.assertEqual( len( a3s ), 3 ) + self.assertEqual( a3s[ 0 ].id, "id1" ) + self.expectGet( "/test/a3s/id1" ).andReturn( { "name": "name1" } ) + self.assertEqual( a3s[ 0 ].name, "name1" ) + unittest.main() diff --git a/github/GithubObject.py b/github/GithubObject.py index f7ccd728..f8ee4d67 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -28,6 +28,37 @@ class SimpleScalarAttributes: for attributeName in self.__attributeNames ] +class ExtendedListAttribute: + class Getter: + def __init__( self, obj, pluralName, type ): + self.__obj = obj + self.__pluralName = pluralName + self.__type = type + + def __call__( self ): + return [ + self.__type( self.__obj._github, attributes, lazy = True ) + for attributes in self.__obj._github._rawRequest( "GET", self.__obj._baseUrl + "/" + self.__pluralName ) + ] + + class AttributeDefinition: + def __init__( self, pluralName, type ): + self.__pluralName = pluralName + self.__type = type + + def getValueFromRawValue( self, obj, rawValue ): + return rawValue + + def updateAttributes( self, obj ): + obj._updateAttributes( { "get_" + self.__pluralName: ExtendedListAttribute.Getter( obj, self.__pluralName, self.__type ) } ) + + def __init__( self, pluralName, type ): + self.__pluralName = pluralName + self.__type = type + + def getAttributeDefinitions( self ): + yield "get_" + self.__pluralName, ExtendedListAttribute.AttributeDefinition( self.__pluralName, self.__type ) + class Editable: class Editor: def __init__( self, obj, mandatoryParameters, optionalParameters ):