From f5e9a55ed13d4976657c29109938dc97ca0b8f3c Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 10:24:37 +0000 Subject: [PATCH 1/6] Explicit arguments in ElementGetable --- github/GithubObject.UnitTest.py | 2 +- github/GithubObjects.py | 26 ++++++++++++------------- github/ObjectCapacities/List.py | 10 +++++++--- github/ObjectCapacities/TypePolicies.py | 5 ++++- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 2741f342..83dee1ce 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -396,7 +396,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..1950963e 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -181,7 +181,7 @@ Issue = GithubObject( ), 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 } ), + ElementGetable( [ "id" ], [], "_repo" ), ElementCreatable( [ "body" ], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), ), ) @@ -270,53 +270,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" ], [], "_repo" ), ElementCreatable( [ "ref", "sha" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/commits", "git_commit", GitCommit, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], "_repo" ), ElementCreatable( [ "message", "tree", "parents" ], [ "author", "commiter" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/trees", "git_tree", GitTree, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], "_repo" ), ElementCreatable( [ "tree" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/blobs", "git_blob", GitBlob, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], "_repo" ), ElementCreatable( [ "content", "encoding" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/tags", "git_tag", GitTag, - ElementGetable( lambda repo, sha: { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], "_repo" ), ElementCreatable( [ "tag", "message", "object", "type" ], [ "tagger" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "labels", "label", Label, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, name: { "_repo": repo, "name": name } ), + ElementGetable( [ "name" ], [], "_repo" ), ElementCreatable( [ "name", "color" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "milestones", "milestone", Milestone, ListGetable( [], [ "state", "sort", "direction" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, number: { "_repo": repo, "number": number } ), + ElementGetable( [ "number" ], [], "_repo" ), 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" ], [], "_repo" ), 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" ], [], "_repo" ), ElementCreatable( [ "name", "size" ], [ "description", "content_type" ], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "comments", "comment", CommitComment, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, id : { "_repo": repo, "id": id } ), + ElementGetable( [ "id" ], [], "_repo" ), ), ExternalListOfObjects( "commits", "commit", Commit, ListGetable( [], [ "sha", "path" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( lambda repo, sha : { "_repo": repo, "sha": sha } ), + ElementGetable( [ "sha" ], [], "_repo" ), ), ExternalListOfObjects( "tags", "tag", Tag, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), @@ -330,7 +330,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" ) __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..e875b0a3 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -46,14 +46,18 @@ class ElementCreatable( ListCapacity ): return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) ) ) class ElementGetable( ListCapacity ): - def __init__( self, attributes ): - self.__attributes = attributes + def __init__( self, mandatoryParameters, optionalParameters, objReferenceName = None ): + self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) + self.__objReferenceName = objReferenceName 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 ) ) + attributes = self.__argumentsChecker.check( args, kwds ) + if self.__objReferenceName is not None: + attributes[ self.__objReferenceName ] = obj + return self.typePolicy.createNonLazy( obj, attributes ) class SeveralElementsAddable( ListCapacity ): def apply( self, cls ): 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 ) From b9a0ba9ade2f790af541beb08af6233a42438ddd Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 10:29:59 +0000 Subject: [PATCH 2/6] On the way to remove modifyAttributes from ElementCreatable and ListGetable --- github/GithubObjects.py | 10 ++++++---- github/ObjectCapacities/List.py | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/github/GithubObjects.py b/github/GithubObjects.py index 1950963e..838d588f 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -133,6 +133,8 @@ Label = GithubObject( Deletable(), ) +__modifyAttributesForObjectsReferingReferedRepo = lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) + 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() ) ) ), + ListGetable( [], [], __modifyAttributesForObjectsReferingReferedRepo ), ElementGetable( [ "id" ], [], "_repo" ), - ElementCreatable( [ "body" ], [], lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) ), + ElementCreatable( [ "body" ], [], __modifyAttributesForObjectsReferingReferedRepo ), ), ) diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index e875b0a3..fda63cea 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -43,7 +43,8 @@ class ElementCreatable( ListCapacity ): 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 ) ) ) + attributes = obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) + return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, attributes ) ) class ElementGetable( ListCapacity ): def __init__( self, mandatoryParameters, optionalParameters, objReferenceName = None ): From 8ed008de51ce9ab85bcd073239e126b88d183af5 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 10:37:57 +0000 Subject: [PATCH 3/6] Replace modifyAttributes by attributeModifiers --- github/GithubObjects.py | 4 ++-- github/ObjectCapacities/List.py | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/github/GithubObjects.py b/github/GithubObjects.py index 838d588f..5e7ad31e 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -133,7 +133,7 @@ Label = GithubObject( Deletable(), ) -__modifyAttributesForObjectsReferingReferedRepo = lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj._repo }.iteritems() ) ) +__modifyAttributesForObjectsReferingReferedRepo = { "_repo": lambda obj: obj._repo } Milestone = GithubObject( "Milestone", @@ -251,7 +251,7 @@ Branch = GithubObject( InternalObjectAttribute( "commit", Commit ) ) -__modifyAttributesForObjectsReferingRepo = lambda obj, attributes: dict( itertools.chain( attributes.iteritems(), { "_repo": obj }.iteritems() ) ) +__modifyAttributesForObjectsReferingRepo = { "_repo": lambda obj: obj } Repository = GithubObject( "Repository", BaseUrl( lambda obj: "/repos/" + obj.owner.login + "/" + obj.name ), diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index fda63cea..9329470e 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -34,9 +34,9 @@ class ElementHasable( ListCapacity ): 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 ) @@ -46,6 +46,11 @@ class ElementCreatable( ListCapacity ): attributes = obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, attributes ) ) + def __modifyAttributes( self, obj, attributes ): + for attributeName, attributeModifier in self.__attributeModifiers.iteritems(): + attributes[ attributeName ] = attributeModifier( obj ) + return attributes + class ElementGetable( ListCapacity ): def __init__( self, mandatoryParameters, optionalParameters, objReferenceName = None ): self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) @@ -75,9 +80,9 @@ class SeveralElementsRemovable( ListCapacity ): 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 ) @@ -89,6 +94,11 @@ class ListGetable( ListCapacity ): 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 ) From f8b9580b40d24d3236031b7924e8ef13e9088e5f Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 10:46:46 +0000 Subject: [PATCH 4/6] Apply attributeModifiers to ElementGetable too --- github/GithubObjects.py | 28 ++++++++++++++-------------- github/ObjectCapacities/List.py | 17 +++++++++-------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/github/GithubObjects.py b/github/GithubObjects.py index 5e7ad31e..1b768f4a 100644 --- a/github/GithubObjects.py +++ b/github/GithubObjects.py @@ -183,7 +183,7 @@ Issue = GithubObject( ), ExternalListOfObjects( "comments", "comment", IssueComment, ListGetable( [], [], __modifyAttributesForObjectsReferingReferedRepo ), - ElementGetable( [ "id" ], [], "_repo" ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingReferedRepo ), ElementCreatable( [ "body" ], [], __modifyAttributesForObjectsReferingReferedRepo ), ), ) @@ -251,7 +251,7 @@ Branch = GithubObject( InternalObjectAttribute( "commit", Commit ) ) -__modifyAttributesForObjectsReferingRepo = { "_repo": lambda obj: obj } +__modifyAttributesForObjectsReferingRepo = { "_repo": lambda repo: repo } Repository = GithubObject( "Repository", BaseUrl( lambda obj: "/repos/" + obj.owner.login + "/" + obj.name ), @@ -272,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( [ "ref" ], [], "_repo" ), + ElementGetable( [ "ref" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "ref", "sha" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/commits", "git_commit", GitCommit, - ElementGetable( [ "sha" ], [], "_repo" ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "message", "tree", "parents" ], [ "author", "commiter" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/trees", "git_tree", GitTree, - ElementGetable( [ "sha" ], [], "_repo" ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "tree" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/blobs", "git_blob", GitBlob, - ElementGetable( [ "sha" ], [], "_repo" ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "content", "encoding" ], [], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "git/tags", "git_tag", GitTag, - ElementGetable( [ "sha" ], [], "_repo" ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "tag", "message", "object", "type" ], [ "tagger" ], __modifyAttributesForObjectsReferingRepo ) ), ExternalListOfObjects( "labels", "label", Label, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( [ "name" ], [], "_repo" ), + ElementGetable( [ "name" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "name", "color" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "milestones", "milestone", Milestone, ListGetable( [], [ "state", "sort", "direction" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( [ "number" ], [], "_repo" ), + 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( [ "number" ], [], "_repo" ), + ElementGetable( [ "number" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "title" ], [ "body", "assignee", "milestone", "labels", ], __modifyAttributesForObjectsReferingRepo ) ), ExternalSimpleAttribute( "languages" ), ExternalListOfObjects( "downloads", "download", Download, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( [ "id" ], [], "_repo" ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingRepo ), ElementCreatable( [ "name", "size" ], [ "description", "content_type" ], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "comments", "comment", CommitComment, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( [ "id" ], [], "_repo" ), + ElementGetable( [ "id" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "commits", "commit", Commit, ListGetable( [], [ "sha", "path" ], __modifyAttributesForObjectsReferingRepo ), - ElementGetable( [ "sha" ], [], "_repo" ), + ElementGetable( [ "sha" ], [], __modifyAttributesForObjectsReferingRepo ), ), ExternalListOfObjects( "tags", "tag", Tag, ListGetable( [], [], __modifyAttributesForObjectsReferingRepo ), @@ -332,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( [ "name" ], [], "owner" ) +__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 9329470e..4483d329 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -43,8 +43,7 @@ class ElementCreatable( ListCapacity ): def __execute( self, obj, *args, **kwds ): data = self.__argumentsChecker.check( args, kwds ) - attributes = obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) - return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, attributes ) ) + return self.typePolicy.createLazy( obj, self.__modifyAttributes( obj, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.attributeName, None, data ) ) ) def __modifyAttributes( self, obj, attributes ): for attributeName, attributeModifier in self.__attributeModifiers.iteritems(): @@ -52,18 +51,20 @@ class ElementCreatable( ListCapacity ): return attributes class ElementGetable( ListCapacity ): - def __init__( self, mandatoryParameters, optionalParameters, objReferenceName = None ): + def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ): self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters ) - self.__objReferenceName = objReferenceName + self.__attributeModifiers = attributeModifiers def apply( self, cls ): cls._addMethod( "get_" + self.singularName, self.__execute ) def __execute( self, obj, *args, **kwds ): - attributes = self.__argumentsChecker.check( args, kwds ) - if self.__objReferenceName is not None: - attributes[ self.__objReferenceName ] = obj - return self.typePolicy.createNonLazy( obj, attributes ) + 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 ): From b5c1d0a9327b3cbcf47e5f9cc48e1e50e23881aa Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 11:17:01 +0000 Subject: [PATCH 5/6] Re-layout code --- github/GithubObject.UnitTest.py | 1 + github/ObjectCapacities/List.py | 92 ++++++++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 83dee1ce..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 ) diff --git a/github/ObjectCapacities/List.py b/github/ObjectCapacities/List.py index 4483d329..eef9745a 100644 --- a/github/ObjectCapacities/List.py +++ b/github/ObjectCapacities/List.py @@ -17,21 +17,36 @@ 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, attributeModifiers = {} ): @@ -42,8 +57,18 @@ class ElementCreatable( ListCapacity ): 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(): @@ -59,7 +84,13 @@ class ElementGetable( ListCapacity ): cls._addMethod( "get_" + self.singularName, self.__execute ) def __execute( self, obj, *args, **kwds ): - return self.typePolicy.createNonLazy( obj, self.__modifyAttributes( obj, self.__argumentsChecker.check( 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(): @@ -71,14 +102,30 @@ class SeveralElementsAddable( ListCapacity ): 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, attributeModifiers = {} ): @@ -91,8 +138,16 @@ 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 ): @@ -105,14 +160,27 @@ class ListSetable( ListCapacity ): 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: From 4cc7a998afd5392e16f533cc2a2e1537345d70e3 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 26 Feb 2012 11:17:50 +0000 Subject: [PATCH 6/6] Improve speed of replayed integration test --- IntegrationTest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 ] )