From df8525e3391df267a0579c27b675da18d6a1b122 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sat, 25 Feb 2012 10:03:29 +0000 Subject: [PATCH] SeveralElementsAddable and SeveralElementsRemovable --- github/GithubObject.UnitTest.py | 48 +++++++++++++++++++------ github/GithubObject.py | 10 +++--- github/GithubObjects.py | 2 +- github/ObjectCapacities/List.py | 26 ++++++++++---- github/ObjectCapacities/TypePolicies.py | 2 +- 5 files changed, 63 insertions(+), 25 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index da290eb8..53b5ddca 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -25,23 +25,23 @@ class TestCaseWithGithubTestObject( unittest.TestCase ): def expectDataGet( self, url, arguments = None ): return self.g.expect._dataRequest( "GET", url, arguments, None ) - def expectStatusPut( self, url, data = None ): - return self.g.expect._statusRequest( "PUT", url, None, data ) - - def expectStatusGet( self, url ): - return self.g.expect._statusRequest( "GET", url, None, None ) + def expectDataPost( self, url, data ): + return self.g.expect._dataRequest( "POST", url, None, data ) def expectDataPatch( self, url, data ): return self.g.expect._dataRequest( "PATCH", url, None, data ) - def expectDataPost( self, url, data ): - return self.g.expect._dataRequest( "POST", url, None, data ) + def expectStatusGet( self, url ): + return self.g.expect._statusRequest( "GET", url, None, None ) def expectStatusPost( self, url, data ): return self.g.expect._statusRequest( "POST", url, None, data ) - def expectStatusDelete( self, url ): - return self.g.expect._statusRequest( "DELETE", url, None, None ) + def expectStatusPut( self, url, data = None ): + return self.g.expect._statusRequest( "PUT", url, None, data ) + + def expectStatusDelete( self, url, data = None ): + return self.g.expect._statusRequest( "DELETE", url, None, data ) class GithubObjectWithOnlyInternalSimpleAttributes( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( @@ -329,7 +329,7 @@ class GithubObjectWithElementCreatableExternalListOfObjects( TestCaseWithGithubT with self.assertRaises( TypeError ): self.o.create_a3( foobar = 42 ) -class GithubObjectWithListAddableExternalListOfObjects( TestCaseWithGithubTestObject ): +class GithubObjectWithSeveralElementsAddableExternalListOfObjects( TestCaseWithGithubTestObject ): ContainedObject = GithubObject( "ContainedObject", BaseUrl( lambda obj: "/test/a3s/" + obj.id ), @@ -341,7 +341,7 @@ class GithubObjectWithListAddableExternalListOfObjects( TestCaseWithGithubTestOb "GithubTestObject", BaseUrl( lambda obj: "/test" ), InternalSimpleAttributes( "a1", "a2" ), - ExternalListOfObjects( "a3s", ContainedObject, ListAddable() ) + ExternalListOfObjects( "a3s", ContainedObject, SeveralElementsAddable() ) ) def testAddToList( self ): @@ -403,6 +403,32 @@ class GithubObjectWithElementGetableExternalListOfObjects( TestCaseWithGithubTes self.expectDataGet( "/test/a3s/idGet" ).andReturn( { "id": "idGet" } ) self.assertEqual( self.o.get_a3( "idGet" ).id, "idGet" ) +class GithubObjectWithMultiCapacityExternalListOfSimpleTypes( TestCaseWithGithubTestObject ): + GithubTestObject = GithubObject( + "GithubTestObject", + BaseUrl( lambda obj: "/test" ), + InternalSimpleAttributes( "a1", "a2" ), + ExternalListOfSimpleTypes( "a3s", + ListGetable( [], [] ), + SeveralElementsAddable(), + SeveralElementsRemovable(), + ) + ) + + def testGetList( self ): + self.expectDataGet( "/test/a3s", {} ).andReturn( [ "a", "b", "c" ] ) + a3s = self.o.get_a3s() + self.assertEqual( len( a3s ), 3 ) + self.assertEqual( a3s[ 0 ], "a" ) + + def testAddToList( self ): + self.expectStatusPost( "/test/a3s", [ "a", "b", "c" ] ).andReturn( 204 ) + a3s = self.o.add_to_a3s( "a", "b", "c" ) + + def testDeleteFromList( self ): + self.expectStatusDelete( "/test/a3s", [ "a", "b", "c" ] ).andReturn( 204 ) + a3s = self.o.remove_from_a3s( "a", "b", "c" ) + def myCallable( obj, mock, arg ): return mock.call( arg ) diff --git a/github/GithubObject.py b/github/GithubObject.py index bafaa2ea..77d55049 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -1,9 +1,9 @@ import itertools -import ObjectCapacities.ArgumentsChecker as ArgumentsChecker -from ObjectCapacities.Basic import AttributeFromCallable, MethodFromCallable, InternalAttribute, SeveralAttributePolicies -from ObjectCapacities.List import ExternalListOfObjects, ListGetable, ElementCreatable, ElementGetable, ElementAddable, ElementRemovable, ElementHasable, ListAddable, ListSetable, ListDeletable -from ObjectCapacities.TypePolicies import SimpleTypePolicy, ObjectTypePolicy +from ObjectCapacities.ArgumentsChecker import ArgumentsChecker +from ObjectCapacities.Basic import * +from ObjectCapacities.List import * +from ObjectCapacities.TypePolicies import * class BadGithubObjectException( Exception ): pass @@ -26,7 +26,7 @@ def Identity( identity ): class Editable( MethodFromCallable ): def __init__( self, mandatoryParameters, optionalParameters ): MethodFromCallable.__init__( self, "edit", self.__execute ) - self.__argumentsChecker = ArgumentsChecker.ArgumentsChecker( mandatoryParameters, optionalParameters ) + self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) def __execute( self, obj, *args, **kwds ): data = self.__argumentsChecker.check( args, kwds ) diff --git a/github/GithubObjects.py b/github/GithubObjects.py index 3696e35c..1c490018 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -169,7 +169,7 @@ Issue = GithubObject( Editable( [], [ "title", "body", "assignee", "state", "milestone", "labels" ] ), ExternalListOfObjects( "labels", Label, ListGetable( [], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), - ListAddable(), + SeveralElementsAddable(), ListSetable(), ListDeletable(), ElementRemovable(), diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index 6aab2319..82940485 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -55,6 +55,20 @@ class ElementGetable( ListCapacity ): def __execute( self, obj, *args, **kwds ): return self.typePolicy.createNonLazy( obj, self.__attributes( obj, *args, **kwds ) ) +class SeveralElementsAddable( ListCapacity ): + def apply( self, cls ): + cls._addMethod( "add_to_" + self.safeAttributeName, self.__execute ) + + def __execute( self, obj, *toBeAddeds ): + obj._github._statusRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, [ self.typePolicy.getIdentity( toBeAdded ) for toBeAdded in toBeAddeds ] ) + +class SeveralElementsRemovable( ListCapacity ): + def apply( self, cls ): + cls._addMethod( "remove_from_" + self.safeAttributeName, self.__execute ) + + def __execute( self, obj, *toBeDeleteds ): + obj._github._statusRequest( "DELETE", obj._baseUrl + "/" + self.attributeName, None, [ self.typePolicy.getIdentity( toBeDeleted ) for toBeDeleted in toBeDeleteds ] ) + class ListGetable( ListCapacity ): def __init__( self, mandatoryParameters, optionalParameters, modifyAttributes = lambda obj, attributes: attributes ): self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) @@ -70,13 +84,6 @@ class ListGetable( ListCapacity ): for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.attributeName, params, None ) ] -class ListAddable( ListCapacity ): - def apply( self, cls ): - cls._addMethod( "add_to_" + self.safeAttributeName, self.__execute ) - - def __execute( self, obj, *toBeAddeds ): - obj._github._statusRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, [ self.typePolicy.getIdentity( toBeAdded ) for toBeAdded in toBeAddeds ] ) - class ListSetable( ListCapacity ): def apply( self, cls ): cls._addMethod( "set_" + self.safeAttributeName, self.__execute ) @@ -95,3 +102,8 @@ def ExternalListOfObjects( attributeName, type, *capacities ): for capacity in capacities: capacity.setList( attributeName, ObjectTypePolicy( type ) ) return SeveralAttributePolicies( capacities ) + +def ExternalListOfSimpleTypes( attributeName, *capacities ): + for capacity in capacities: + capacity.setList( attributeName, SimpleTypePolicy() ) + return SeveralAttributePolicies( capacities ) diff --git a/github/ObjectCapacities/TypePolicies.py b/github/ObjectCapacities/TypePolicies.py index dfef675c..62624f58 100644 --- a/github/ObjectCapacities/TypePolicies.py +++ b/github/ObjectCapacities/TypePolicies.py @@ -2,7 +2,7 @@ class SimpleTypePolicy: def createLazy( self, obj, value ): return value - def createNonLazy( self, obj, value ): + def getIdentity( self, value ): return value class ObjectTypePolicy: