From df2c351cc43aeda1253ffd09e88084d60550b327 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Wed, 22 Feb 2012 17:22:53 +0100 Subject: [PATCH] Lists with ElementGetable capacity --- github/GithubObject.UnitTest.py | 20 +++++++++++++++++++- github/GithubObject.py | 2 +- github/GithubObjects.py | 2 +- github/ObjectCapacities/List.py | 16 +++++++++++++--- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index ef7f2e92..6b9ead71 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -287,7 +287,7 @@ class GithubObjectWithModifiableListOfObjects( TestCaseWithGithubTestObject ): "GithubTestObject", BaseUrl( lambda obj: "/test" ), BasicAttributes( "a1", "a2" ), - ListOfObjects( "a3s", ContainedObject, Creatable( "a3", [ "name" ], [ "p1", "p2" ] ) ) + ListOfObjects( "a3s", ContainedObject, ElementCreatable( "a3", [ "name" ], [ "p1", "p2" ] ) ) ) def testCreate( self ): @@ -332,6 +332,24 @@ class GithubObjectWithObjectGetter( TestCaseWithGithubTestObject ): self.expectDataGet( "/test/a3s/idGet" ).andReturn( { "id": "idGet" } ) self.assertEqual( self.o.get_a3( "idGet" ).id, "idGet" ) +class GithubObjectWithElementGetableList( TestCaseWithGithubTestObject ): + ContainedObject = GithubObject( + "ContainedObject", + BaseUrl( lambda obj: "/test/a3s/" + obj.id ), + BasicAttributes( "id", "name" ) + ) + + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + BasicAttributes( "a1", "a2" ), + ListOfObjects( "a3s", ContainedObject, ElementGetable( "a3" ) ) + ) + + def testGetList( self ): + self.expectDataGet( "/test/a3s/idGet" ).andReturn( { "id": "idGet" } ) + self.assertEqual( self.o.get_a3( "idGet" ).id, "idGet" ) + def myCallable( obj, mock, arg ): return mock.call( arg ) diff --git a/github/GithubObject.py b/github/GithubObject.py index 70d276eb..d409baa1 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -2,7 +2,7 @@ import itertools import ArgumentsChecker from ObjectCapacities.Basic import AttributeFromCallable, MethodFromCallable -from ObjectCapacities.List import ListOfObjects, ListOfReferences, ListGetable, Creatable +from ObjectCapacities.List import ListOfObjects, ListOfReferences, ListGetable, ElementCreatable, ElementGetable class BadGithubObjectException( Exception ): pass diff --git a/github/GithubObjects.py b/github/GithubObjects.py index d94f4402..111260d7 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -80,7 +80,7 @@ Repository._addAttributePolicy( ComplexAttribute( "parent", Repository ) ) Repository._addAttributePolicy( ComplexAttribute( "source", Repository ) ) Repository._addAttributePolicy( ListOfReferences( "forks", Repository ) ) -__repoCreatable = Creatable( "repo", [ "name" ], [ "description", "homepage", "private", "has_issues", "has_wiki", "has_downloads", "team_id", ] ) +__repoCreatable = ElementCreatable( "repo", [ "name" ], [ "description", "homepage", "private", "has_issues", "has_wiki", "has_downloads", "team_id", ] ) AuthenticatedUser._addAttributePolicy( ListOfObjects( "repos", Repository, __repoCreatable ) ) NamedUser._addAttributePolicy( ListOfObjects( "repos", Repository ) ) Organization._addAttributePolicy( ListOfObjects( "repos", Repository, __repoCreatable ) ) diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index 29821218..9c8ce222 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -51,7 +51,7 @@ class ListOfReferences: assert isinstance( toBeQueried, self.__type ) return obj._github._statusRequest( "GET", obj._baseUrl + "/" + self.__attributeName + "/" + toBeQueried._identity, None, None ) == 204 -class Creatable: +class ElementCreatable: def __init__( self, singularName, mandatoryParameters, optionalParameters ): self.__argumentsChecker = ArgumentsChecker.ArgumentsChecker( mandatoryParameters, optionalParameters ) self.__createName = "create_" + singularName @@ -77,8 +77,18 @@ class ListGetable: for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, None, None ) ] -### @todo Merge ObjectGetter in ListOfObjects, with a SingleGettable similar to Creatable -### @todo Add a ListGetable that couls be False for non-getable lists (repo/git/commits for example) +class ElementGetable: + def __init__( self, singularName ): + self.__getName = "get_" + singularName + + def apply( self, list, cls ): + self.__type = list.type + self.__attributeName = list.attributeName + cls._addMethod( self.__getName, self.__execute ) + + def __execute( self, obj, identity ): + return self.__type( obj._github, obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName + "/" + identity, None, None ), lazy = True ) + class ListOfObjects: def __init__( self, attributeName, type, *capacities ): self.attributeName = attributeName