diff --git a/IntegrationTest.py b/IntegrationTest.py index c709c311..6cd9374d 100644 --- a/IntegrationTest.py +++ b/IntegrationTest.py @@ -92,6 +92,7 @@ class IntegrationTest: self.playScenario() def prepareRecord( self ): + self.avoidError500FromGithub = lambda: time.sleep( 1 ) try: import GithubCredentials self.g = Github( GithubCredentials.login, GithubCredentials.password ) @@ -104,6 +105,7 @@ class IntegrationTest: exit( 1 ) def prepareReplay( self ): + self.avoidError500FromGithub = lambda: 0 try: file = open( self.__fileName ) httplib.HTTPSConnection = lambda *args, **kwds: ReplayingHttpsConnection( file ) @@ -143,7 +145,7 @@ class IntegrationTest: def doSomeWritesToRepository( self ): u = self.g.get_user() r = u.create_repo( name = "TestPyGithub", description = "Created by PyGithub", has_wiki = False ) - time.sleep( 1 ) # Avoid error 500 from github :p + self.avoidError500FromGithub() # Git objects b1 = r.create_git_blob( "This blob was created by PyGithub", encoding = "latin1" ) @@ -187,7 +189,7 @@ class IntegrationTest: o = self.g.get_organization( "BeaverSoftware" ) rf = o.create_fork( r ) - time.sleep( 1 ) # Avoid error 500 from github :p + self.avoidError500FromGithub() b3 = rf.create_git_blob( "This blob was ter created by PyGithub", encoding = "latin1" ) t3 = rf.create_git_tree( [ { "path": "foo.bar", "mode": "100644", "type": "blob", "sha": b3.sha } ] ) c3 = rf.create_git_commit( "This commit was ter created by PyGithub", t3.sha, [ c2.sha ] ) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 2741f342..64c9f419 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -326,6 +326,7 @@ class GithubObjectWithElementCreatableExternalListOfObjects( TestCaseWithGithubT self.assertEqual( self.o.create_a3( "nameCreate", 1, 2 ).id, "idCreate" ) def testCreateWithSillyArgument( self ): + self.g.expect._dataRequest.andReturn( None ) with self.assertRaises( TypeError ): self.o.create_a3( foobar = 42 ) @@ -396,7 +397,7 @@ class GithubObjectWithElementGetableExternalListOfObjects( TestCaseWithGithubTes "GithubTestObject", BaseUrl( lambda obj: "/test" ), InternalSimpleAttributes( "a1", "a2" ), - ExternalListOfObjects( "a3s", "a3", ContainedObject, ElementGetable( lambda obj, id: { "id": id } ) ) + ExternalListOfObjects( "a3s", "a3", ContainedObject, ElementGetable( [ "id" ], [] ) ) ) def testGetList( self ): diff --git a/github/GithubObjects.py b/github/GithubObjects.py index bb7f798b..1b768f4a 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -133,6 +133,8 @@ Label = GithubObject( Deletable(), ) +__modifyAttributesForObjectsReferingReferedRepo = { "_repo": lambda obj: obj._repo } + Milestone = GithubObject( "Milestone", BaseUrl( lambda obj: obj._repo._baseUrl + "/milestones/" + str( obj.number ) ), @@ -144,7 +146,7 @@ Milestone = GithubObject( InternalObjectAttribute( "creator", NamedUser ), Editable( [ "title" ], [ "state", "description", "due_on" ] ), Deletable(), - ExternalListOfObjects( "labels", "label", Label, ListGetable( [], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ) ), + ExternalListOfObjects( "labels", "label", Label, ListGetable( [], [], __modifyAttributesForObjectsReferingReferedRepo ) ), ) IssueComment = GithubObject( @@ -173,16 +175,16 @@ Issue = GithubObject( InternalObjectAttribute( "milestone", Milestone ), Editable( [], [ "title", "body", "assignee", "state", "milestone", "labels" ] ), ExternalListOfObjects( "labels", "label", Label, - ListGetable( [], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), + ListGetable( [], [], __modifyAttributesForObjectsReferingReferedRepo ), SeveralElementsAddable(), ListSetable(), ListDeletable(), ElementRemovable(), ), ExternalListOfObjects( "comments", "comment", IssueComment, - ListGetable( [], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), - ElementGetable( lambda repo, id: { "_repo": repo, "id": id } ), - ElementCreatable( [ "body" ], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), + ListGetable( [], [], __modifyAttributesForObjectsReferingReferedRepo ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingReferedRepo ), + ElementCreatable( [ "body" ], [], __modifyAttributesForObjectsReferingReferedRepo ), ), ) @@ -249,7 +251,7 @@ Branch = GithubObject( InternalObjectAttribute( "commit", Commit ) ) -__modifyAttributesForObjectsReferingRepo = lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj }.iteritems() ) ) +__modifyAttributesForObjectsReferingRepo = { "_repo": lambda repo: repo } Repository = GithubObject( "Repository", BaseUrl( lambda obj: "/repos/" + obj.owner.login + "/" + obj.name ), @@ -270,53 +272,53 @@ Repository = GithubObject( Editable( [ "name" ], [ "description", "homepage", "public", "has_issues", "has_wiki", "has_downloads" ] ), ExternalListOfObjects( "git/refs", "git_ref", GitRef, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, ref: { "_repo": repo, "ref": ref } ), + ElementGetable( [ "ref" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "ref", "sha" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/commits", "git_commit", GitCommit, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "message", "tree", "parents" ], [ "author", "commiter" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/trees", "git_tree", GitTree, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "tree" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/blobs", "git_blob", GitBlob, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "content", "encoding" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/tags", "git_tag", GitTag, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "tag", "message", "object", "type" ], [ "tagger" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "labels", "label", Label, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, name: { "_repo": repo, "name": name } ), + ElementGetable( [ "name" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "name", "color" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "milestones", "milestone", Milestone, ListGetable( [], [ "state", "sort", "direction" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, number: { "_repo": repo, "number": number } ), + ElementGetable( [ "number" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "title" ], [ "state", "description", "due_on" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "issues", "issue", Issue, ListGetable( [], [ "milestone", "state", "assignee", "mentioned", "labels", "sort", "direction", "since" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, number: { "_repo": repo, "number": number } ), + ElementGetable( [ "number" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "title" ], [ "body", "assignee", "milestone", "labels", ], __modifyAttributesForObjectsReferingRepo ) ), ExternalSimpleAttribute( "languages" ), ExternalListOfObjects( "downloads", "download", Download, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, id : { "_repo": repo, "number": number } ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "name", "size" ], [ "description", "content_type" ], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "comments", "comment", CommitComment, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, id : { "_repo": repo, "id": id } ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "commits", "commit", Commit, ListGetable( [], [ "sha", "path" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, sha : { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "tags", "tag", Tag, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), @@ -330,7 +332,7 @@ Repository._addAttributePolicy( InternalObjectAttribute( "source", Repository ) Repository._addAttributePolicy( ExternalListOfObjects( "forks", "fork", Repository, ListGetable( [], [] ) ) ) __repoElementCreatable = ElementCreatable( [ "name" ], [ "description", "homepage", "private", "has_issues", "has_wiki", "has_downloads", "team_id", ] ) -__repoElementGetable = ElementGetable( lambda obj, name: { "owner": { "login": obj.login }, "name": name } ) +__repoElementGetable = ElementGetable( [ "name" ], [], { "owner" : lambda user: user } ) __repoListGetable = ListGetable( [], [] ) AuthenticatedUser._addAttributePolicy( ExternalListOfObjects( "repos", "repo", Repository, __repoListGetable, __repoElementGetable, __repoElementCreatable ) ) NamedUser._addAttributePolicy( ExternalListOfObjects( "repos", "repo", Repository, __repoListGetable, __repoElementGetable ) ) diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index abe0d8ad..eef9745a 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -17,62 +17,120 @@ class ElementAddable( ListCapacity ): cls._addMethod( "add_to_" + self.safeAttributeName, self.__execute ) def __execute( self, obj, toBeAdded ): - obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeAdded ), None, None ) + obj._github._statusRequest( + "PUT", + obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeAdded ), + None, + None + ) class ElementRemovable( ListCapacity ): def apply( self, cls ): cls._addMethod( "remove_from_" + self.safeAttributeName, self.__execute ) def __execute( self, obj, toBeDeleted ): - obj._github._statusRequest( "DELETE", obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeDeleted ), None, None ) + obj._github._statusRequest( + "DELETE", + obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeDeleted ), + None, + None + ) class ElementHasable( ListCapacity ): def apply( self, cls ): cls._addMethod( "has_in_" + self.safeAttributeName, self.__execute ) def __execute( self, obj, toBeQueried ): - return obj._github._statusRequest( "GET", obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeQueried ), None, None ) == 204 + return obj._github._statusRequest( + "GET", + obj._baseUrl + "/" + self.attributeName + "/" + self.typePolicy.getIdentity( toBeQueried ), + None, + None + ) == 204 class ElementCreatable( ListCapacity ): - def __init__( self, mandatoryParameters, optionalParameters, modifyAttributes = lambda obj, attributes: attributes ): + def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ): self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) - self.__modifyAttributes = modifyAttributes + self.__attributeModifiers = attributeModifiers def apply( self, cls ): cls._addMethod( "create_" + self.singularName, self.__execute ) def __execute( self, obj, *args, **kwds ): - data = self.__argumentsChecker.check( args, kwds ) - return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) ) ) + return self.typePolicy.createLazy( + obj, + self.__modifyAttributes( + obj, + obj._github._dataRequest( + "POST", + obj._baseUrl + "/" + self.attributeName, + None, + self.__argumentsChecker.check( args, kwds ) + ) + ) + ) + + def __modifyAttributes( self, obj, attributes ): + for attributeName, attributeModifier in self.__attributeModifiers.iteritems(): + attributes[ attributeName ] = attributeModifier( obj ) + return attributes class ElementGetable( ListCapacity ): - def __init__( self, attributes ): - self.__attributes = attributes + def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ): + self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) + self.__attributeModifiers = attributeModifiers def apply( self, cls ): cls._addMethod( "get_" + self.singularName, self.__execute ) def __execute( self, obj, *args, **kwds ): - return self.typePolicy.createNonLazy( obj, self.__attributes( obj, *args, **kwds ) ) + return self.typePolicy.createNonLazy( + obj, + self.__modifyAttributes( + obj, + self.__argumentsChecker.check( args, kwds ) + ) + ) + + def __modifyAttributes( self, obj, attributes ): + for attributeName, attributeModifier in self.__attributeModifiers.iteritems(): + attributes[ attributeName ] = attributeModifier( obj ) + return attributes 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 ] ) + 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 ] ) + 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 ): + def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ): self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) - self.__modifyAttributes = modifyAttributes + self.__attributeModifiers = attributeModifiers def apply( self, cls ): cls._addMethod( "get_" + self.safeAttributeName, self.__execute ) @@ -80,23 +138,49 @@ class ListGetable( ListCapacity ): def __execute( self, obj, *args, **kwds ): params = self.__argumentsChecker.check( args, kwds ) return [ - self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, attributes ) ) - for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.attributeName, params, None ) + self.typePolicy.createLazy( + obj, + self.__modifyAttributes( obj, attributes ) + ) + for attributes in obj._github._dataRequest( + "GET", + obj._baseUrl + "/" + self.attributeName, + params, + None + ) ] + def __modifyAttributes( self, obj, attributes ): + for attributeName, attributeModifier in self.__attributeModifiers.iteritems(): + attributes[ attributeName ] = attributeModifier( obj ) + return attributes + class ListSetable( ListCapacity ): def apply( self, cls ): cls._addMethod( "set_" + self.safeAttributeName, self.__execute ) def __execute( self, obj, *toBeSets ): - obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.attributeName, None, [ self.typePolicy.getIdentity( toBeSet ) for toBeSet in toBeSets ] ) + obj._github._statusRequest( + "PUT", + obj._baseUrl + "/" + self.attributeName, + None, + [ + self.typePolicy.getIdentity( toBeSet ) + for toBeSet in toBeSets + ] + ) class ListDeletable( ListCapacity ): def apply( self, cls ): cls._addMethod( "delete_" + self.safeAttributeName, self.__execute ) def __execute( self, obj ): - obj._github._statusRequest( "DELETE", obj._baseUrl + "/" + self.attributeName, None, None ) + obj._github._statusRequest( + "DELETE", + obj._baseUrl + "/" + self.attributeName, + None, + None + ) def ExternalListOfObjects( attributeName, singularName, type, *capacities ): for capacity in capacities: diff --git a/github/ObjectCapacities/TypePolicies.py b/github/ObjectCapacities/TypePolicies.py index 62624f58..bd0bc125 100644 --- a/github/ObjectCapacities/TypePolicies.py +++ b/github/ObjectCapacities/TypePolicies.py @@ -10,7 +10,10 @@ class ObjectTypePolicy: self.__type = type def createLazy( self, obj, attributes ): - return self.__type( obj._github, attributes, lazy = True ) + if isinstance( attributes, self.__type ): + return attributes + else: + return self.__type( obj._github, attributes, lazy = True ) def createNonLazy( self, obj, attributes ): return self.__type( obj._github, attributes, lazy = False )