From f23ed6341da0fcf7a416d04ad0ab252ed55308a5 Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sat, 12 May 2012 14:08:11 +0200 Subject: [PATCH] Create a GitTreeElement class --- .../description.000.human_readable.json | 15 ++- .../description.001.normalized.json | 60 +++++++++++- doc/ReferenceOfClasses.md | 14 ++- src/github/GitTree.py | 7 +- src/github/GitTreeElement.py | 94 +++++++++++++++++++ test/GitObjects.py | 14 ++- 6 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 src/github/GitTreeElement.py diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json b/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json index 751654cc..5525208f 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json @@ -594,11 +594,24 @@ "collections": [ ] }, + { + "name": "GitTreeElement", + "attributes": [ + { "name": "mode", "type": "string" }, + { "name": "path", "type": "string" }, + { "name": "sha", "type": "string" }, + { "name": "size", "type": "integer" }, + { "name": "type", "type": "string" }, + { "name": "url", "type": "string" } + ], + "collections": [ + ] + }, { "name": "GitTree", "attributes": [ { "name": "sha", "type": "string" }, - { "name": "tree", "type": "@todo" }, + { "name": "tree", "type": "list:GitTreeElement" }, { "name": "url", "type": "string" } ], "collections": [ diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json b/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json index 699cabbe..93bf75f2 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json @@ -3311,9 +3311,9 @@ }, { "type": { - "simple": true, - "cardinality": "scalar", - "name": "@todo" + "simple": false, + "cardinality": "list", + "name": "GitTreeElement" }, "name": "tree" }, @@ -3329,6 +3329,60 @@ "name": "GitTree", "methods": [] }, + { + "attributes": [ + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "string" + }, + "name": "mode" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "string" + }, + "name": "path" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "string" + }, + "name": "sha" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "integer" + }, + "name": "size" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "string" + }, + "name": "type" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "string" + }, + "name": "url" + } + ], + "name": "GitTreeElement", + "methods": [] + }, { "attributes": [ { diff --git a/doc/ReferenceOfClasses.md b/doc/ReferenceOfClasses.md index bf0586d7..55689517 100644 --- a/doc/ReferenceOfClasses.md +++ b/doc/ReferenceOfClasses.md @@ -452,7 +452,19 @@ Class `GitTree` Attributes ---------- * `sha`: string -* `tree` +* `tree`: list of `GitTreeElement` +* `url`: string + +Class `GitTreeElement` +====================== + +Attributes +---------- +* `mode`: string +* `path`: string +* `sha`: string +* `size`: integer +* `type`: string * `url`: string Class `Hook` diff --git a/src/github/GitTree.py b/src/github/GitTree.py index a2c648a0..29264db0 100644 --- a/src/github/GitTree.py +++ b/src/github/GitTree.py @@ -2,6 +2,7 @@ # Do not modify it manually, your work would be lost. import PaginatedList +import GitTreeElement # This allows None as a valid value for an optional parameter class DefaultValueForOptionalParametersType: @@ -60,7 +61,11 @@ class GitTree( object ): assert isinstance( attributes[ "sha" ], ( str, unicode ) ) self.__sha = attributes[ "sha" ] if "tree" in attributes and attributes[ "tree" ] is not None: - self.__tree = attributes[ "tree" ] + assert isinstance( attributes[ "tree" ], list ) and ( len( attributes[ "tree" ] ) == 0 or isinstance( attributes[ "tree" ][ 0 ], dict ) ) + self.__tree = [ + GitTreeElement.GitTreeElement( self.__requester, element, lazy = True ) + for element in attributes[ "tree" ] + ] if "url" in attributes and attributes[ "url" ] is not None: assert isinstance( attributes[ "url" ], ( str, unicode ) ) self.__url = attributes[ "url" ] diff --git a/src/github/GitTreeElement.py b/src/github/GitTreeElement.py new file mode 100644 index 00000000..cc4e647a --- /dev/null +++ b/src/github/GitTreeElement.py @@ -0,0 +1,94 @@ +# WARNING: this file is generated automaticaly. +# Do not modify it manually, your work would be lost. + +import PaginatedList +# This allows None as a valid value for an optional parameter + +class DefaultValueForOptionalParametersType: + pass +DefaultValueForOptionalParameters = DefaultValueForOptionalParametersType() + +class GitTreeElement( object ): + def __init__( self, requester, attributes, lazy ): + self.__requester = requester + self.__completed = False + self.__initAttributes() + self.__useAttributes( attributes ) + if not lazy: + self.__complete() + + @property + def mode( self ): + self.__completeIfNeeded( self.__mode ) + return self.__mode + + @property + def path( self ): + self.__completeIfNeeded( self.__path ) + return self.__path + + @property + def sha( self ): + self.__completeIfNeeded( self.__sha ) + return self.__sha + + @property + def size( self ): + self.__completeIfNeeded( self.__size ) + return self.__size + + @property + def type( self ): + self.__completeIfNeeded( self.__type ) + return self.__type + + @property + def url( self ): + self.__completeIfNeeded( self.__url ) + return self.__url + + def __initAttributes( self ): + self.__mode = None + self.__path = None + self.__sha = None + self.__size = None + self.__type = None + self.__url = None + + def __completeIfNeeded( self, testedAttribute ): + if not self.__completed and testedAttribute is None: + self.__complete() + + def __complete( self ): + status, headers, data = self.__requester.request( + "GET", + self.__url, + None, + None + ) + self.__useAttributes( data ) + self.__completed = True + + def __useAttributes( self, attributes ): + # @todo Remove this debug weakness: we shall assume that github will add new attributes + for attribute in attributes: + assert attribute in [ "mode", "path", "sha", "size", "type", "url", ] + # @todo No need to check if attribute is in attributes when attribute is mandatory + if "mode" in attributes and attributes[ "mode" ] is not None: + assert isinstance( attributes[ "mode" ], ( str, unicode ) ) + self.__mode = attributes[ "mode" ] + if "path" in attributes and attributes[ "path" ] is not None: + assert isinstance( attributes[ "path" ], ( str, unicode ) ) + self.__path = attributes[ "path" ] + if "sha" in attributes and attributes[ "sha" ] is not None: + assert isinstance( attributes[ "sha" ], ( str, unicode ) ) + self.__sha = attributes[ "sha" ] + if "size" in attributes and attributes[ "size" ] is not None: + assert isinstance( attributes[ "size" ], int ) + self.__size = attributes[ "size" ] + if "type" in attributes and attributes[ "type" ] is not None: + assert isinstance( attributes[ "type" ], ( str, unicode ) ) + self.__type = attributes[ "type" ] + if "url" in attributes and attributes[ "url" ] is not None: + assert isinstance( attributes[ "url" ], ( str, unicode ) ) + self.__url = attributes[ "url" ] diff --git a/test/GitObjects.py b/test/GitObjects.py index aba78b0e..289b9205 100644 --- a/test/GitObjects.py +++ b/test/GitObjects.py @@ -95,5 +95,17 @@ class GitTree( TestCaseWithRepo ): def testAttributes( self ): tree = self.repo.get_git_tree( "f492784d8ca837779650d1fb406a1a3587a764ad" ) self.assertEqual( tree.sha, "f492784d8ca837779650d1fb406a1a3587a764ad" ) - self.assertEqual( len( tree.tree ), 11 ) ### @todo + self.assertEqual( len( tree.tree ), 11 ) + self.assertEqual( tree.tree[ 0 ].mode, "100644" ) + self.assertEqual( tree.tree[ 0 ].path, ".gitignore" ) + self.assertEqual( tree.tree[ 0 ].sha, "8a9af1462c3f4e3358315c2d2e6ef1e7334c59dd" ) + self.assertEqual( tree.tree[ 0 ].size, 53 ) + self.assertEqual( tree.tree[ 0 ].type, "blob" ) + self.assertEqual( tree.tree[ 0 ].url, "https://api.github.com/repos/jacquev6/PyGithub/git/blobs/8a9af1462c3f4e3358315c2d2e6ef1e7334c59dd" ) + self.assertEqual( tree.tree[ 6 ].mode, "040000" ) + self.assertEqual( tree.tree[ 6 ].path, "ReplayDataForIntegrationTest" ) + self.assertEqual( tree.tree[ 6 ].sha, "60b4602b2c2070246c5df078fb7a5150b45815eb" ) + # self.assertEqual( tree.tree[ 6 ].size, None ) ### @todo This test tries to __complete the GitTreeElement. (Lazy-)Complete-ability should be specified in the json description, not deduced from the presence of a url attribute. + self.assertEqual( tree.tree[ 6 ].type, "tree" ) + self.assertEqual( tree.tree[ 6 ].url, "https://api.github.com/repos/jacquev6/PyGithub/git/trees/60b4602b2c2070246c5df078fb7a5150b45815eb" ) self.assertEqual( tree.url, "https://api.github.com/repos/jacquev6/PyGithub/git/trees/f492784d8ca837779650d1fb406a1a3587a764ad" )