From 5aceaa19f25b7de9e50922fe4144b0c2bfe246bf Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 27 May 2012 13:06:50 +0100 Subject: [PATCH] Reduce differences between generated code and correct code --- .../description.000.human_readable.json | 26 +++- .../description.001.normalized.json | 127 ++++++++++++++++-- .../JsonDescriptionOfGithubApiV3/normalize.py | 39 +++++- codegen/templates/ReferenceOfClasses.md | 2 +- doc/ReferenceOfClasses.md | 5 + src/github/Label.py | 2 - src/github/PullRequest.py | 6 +- src/github/Repository.py | 2 +- 8 files changed, 189 insertions(+), 20 deletions(-) diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json b/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json index c6c0aa97..43a2ef1b 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.000.human_readable.json @@ -758,6 +758,9 @@ { "name": "color", "type": "@todo" }, { "name": "name", "type": "@todo" }, { "name": "url", "type": "@todo" } + ], + "identity": [ + { "type": "attribute", "value": [ "name" ] } ] }, { @@ -1172,7 +1175,8 @@ { "type": "attribute", "value": [ "url" ] }, { "type": "constant", "value": "/merge" } ], - "information": "status" + "information": "status", + "postParameters": true } } ] @@ -1404,11 +1408,18 @@ ] }, "getElement": { - "parameter": { "name": "ref", "type": "@todo" } + "parameter": { "name": "ref", "type": "@todo" }, + "request": { + "url": [ + { "type": "attribute", "value": [ "url" ] }, + { "type": "constant", "value": "/git/" }, + { "type": "argument", "value": [ "ref" ] } + ] + } }, "url": [ { "type": "attribute", "value": [ "url" ] }, - { "type": "constant", "value": "/git" } + { "type": "constant", "value": "/git/refs" } ] }, { @@ -1586,6 +1597,15 @@ }, "getElement": { "parameter": { "name": "number", "type": "@todo" } + }, + "createElement": { + "optionalParameters": [ + { "name": "title", "type": "@todo" }, + { "name": "body", "type": "@todo" }, + { "name": "issue", "type": "@todo" }, + { "name": "base", "type": "@todo" }, + { "name": "head", "type": "@todo" } + ] } }, { diff --git a/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json b/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json index eb3771b9..c7fe59fa 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json +++ b/codegen/JsonDescriptionOfGithubApiV3/description.001.normalized.json @@ -4037,6 +4037,22 @@ "labels" ], "mandatoryParameters": [], + "request": { + "url": [ + { + "type": "attribute", + "value": [ + "url" + ] + }, + { + "type": "constant", + "value": "/labels" + } + ], + "information": "status", + "verb": "DELETE" + }, "isMutation": false, "optionalParameters": [], "type": { @@ -4143,6 +4159,23 @@ }, "name": "label" }, + "request": { + "url": [ + { + "type": "attribute", + "value": [ + "url" + ] + }, + { + "type": "constant", + "value": "/labels" + } + ], + "information": "status", + "verb": "PUT", + "postParameters": true + }, "isMutation": false, "optionalParameters": [], "type": { @@ -4363,6 +4396,14 @@ ], "isCompletable": false, "name": "Label", + "identity": [ + { + "type": "attribute", + "value": [ + "name" + ] + } + ], "methods": [ { "group": "modification", @@ -6913,7 +6954,8 @@ } ], "information": "status", - "verb": "PUT" + "verb": "PUT", + "postParameters": true }, "isMutation": false, "optionalParameters": [ @@ -8415,7 +8457,7 @@ }, { "type": "constant", - "value": "/git" + "value": "/git/refs" } ], "information": "status", @@ -8456,11 +8498,7 @@ }, { "type": "constant", - "value": "/git" - }, - { - "type": "constant", - "value": "/" + "value": "/git/" }, { "type": "argument", @@ -8497,7 +8535,7 @@ }, { "type": "constant", - "value": "/git" + "value": "/git/refs" } ], "information": "data", @@ -9624,6 +9662,79 @@ "name": "Milestone" } }, + { + "group": "pulls", + "name": [ + "create", + "pull" + ], + "mandatoryParameters": [], + "request": { + "url": [ + { + "type": "attribute", + "value": [ + "url" + ] + }, + { + "type": "constant", + "value": "/pulls" + } + ], + "information": "status", + "verb": "POST", + "postParameters": true + }, + "isMutation": false, + "optionalParameters": [ + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "@todo" + }, + "name": "title" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "@todo" + }, + "name": "body" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "@todo" + }, + "name": "issue" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "@todo" + }, + "name": "base" + }, + { + "type": { + "simple": true, + "cardinality": "scalar", + "name": "@todo" + }, + "name": "head" + } + ], + "type": { + "simple": false, + "cardinality": "scalar", + "name": "PullRequest" + } + }, { "group": "pulls", "name": [ diff --git a/codegen/JsonDescriptionOfGithubApiV3/normalize.py b/codegen/JsonDescriptionOfGithubApiV3/normalize.py index 214d7ab4..9a253e03 100644 --- a/codegen/JsonDescriptionOfGithubApiV3/normalize.py +++ b/codegen/JsonDescriptionOfGithubApiV3/normalize.py @@ -7,6 +7,19 @@ import itertools ### @todo Mandatory/optional attributes ### @todo Remove '_identity' from the normalized json description +def mergeDict( base, *additions ): + r = dict( base ) + for addition in additions: + for k in addition: + if k in r: + if isinstance( r[ k ], dict ): + r[ k ] = mergeDict( r[ k ], addition[ k ] ) + # else: + # we ignore the addition: the first dict which specifies a value wins + else: + r[ k ] = addition[ k ] + return r + def checkKeys( d, mandatoryKeys, optionalKeys = [] ): assert set( d.keys() ) >= set( mandatoryKeys ), d.keys() assert set( d.keys() ) <= set( mandatoryKeys ) | set( optionalKeys ) | set( [ "@todo" ] ), d.keys() @@ -73,8 +86,7 @@ class Function: # POST parameters from input def __init__( self, desc, *additionalDescs ): - for additionalDesc in additionalDescs: - desc.update( additionalDesc ) + desc = mergeDict( desc, *additionalDescs ) checkKeys( desc, [ "name", "type", "group" ], [ "url", "isMutation", "mandatoryParameters", "optionalParameters", "variadicParameter", "parameter", "request" ] ) # @todo Move request to mandatoryKeys self.name = desc[ "name" ] @@ -169,7 +181,16 @@ class Collection: ) ) if "deleteList" in desc: assert desc[ "deleteList" ] is True - self.methods.append( Function( { "name": [ "delete" ] + name, "type": "void", "group": desc[ "name" ] } ) ) + self.methods.append( Function( + { "name": [ "delete" ] + name, "type": "void", "group": desc[ "name" ] }, + { + "request": { + "verb": "DELETE", + "url": self.__url, + "information": "status", + } + } + ) ) if "getElement" in desc: if "url" in desc[ "getElement" ]: urlForGetElement = desc[ "getElement" ][ "url" ] @@ -247,7 +268,17 @@ class Collection: ) ) if "setList" in desc: assert desc[ "setList" ] is True - self.methods.append( Function( { "name": [ "set" ] + name, "type": "void", "group": desc[ "name" ], "variadicParameter": { "name": desc[ "singularName" ], "type": desc[ "type" ] } } ) ) + self.methods.append( Function( + { "name": [ "set" ] + name, "type": "void", "group": desc[ "name" ], "variadicParameter": { "name": desc[ "singularName" ], "type": desc[ "type" ] } }, + { + "request": { + "verb": "PUT", + "url": self.__url, + "information": "status", + "postParameters": True, + } + } + ) ) class Class: def __init__( self, desc ): diff --git a/codegen/templates/ReferenceOfClasses.md b/codegen/templates/ReferenceOfClasses.md index 8331d694..fb6550ef 100644 --- a/codegen/templates/ReferenceOfClasses.md +++ b/codegen/templates/ReferenceOfClasses.md @@ -3,7 +3,7 @@ You obtain instances through calls to `get_` and `create_` methods. Class `Github` ============== -* Constructed from user's login and password +* Constructed from user's login and password or OAuth token * `get_user()`: `AuthenticatedUser` * `get_user( login )`: `NamedUser` * `get_organization( login )`: `Organization` diff --git a/doc/ReferenceOfClasses.md b/doc/ReferenceOfClasses.md index 87faca25..9186bdd4 100644 --- a/doc/ReferenceOfClasses.md +++ b/doc/ReferenceOfClasses.md @@ -1166,6 +1166,11 @@ Modification Pulls ----- * `create_pull( < title, body, base, head > or < issue, base, head > )`: `PullRequest` + * `title` + * `body` + * `issue` + * `base` + * `head` * `get_pull( number )`: `PullRequest` * `number` * `get_pulls( [state] )`: list of `PullRequest` diff --git a/src/github/Label.py b/src/github/Label.py index 64acc1c1..a8ae7877 100644 --- a/src/github/Label.py +++ b/src/github/Label.py @@ -45,7 +45,6 @@ class Label( object ): ) self.__useAttributes( data ) - # @todo Remove '_identity' from the normalized json description @property def _identity( self ): return urllib.quote( self.name ) @@ -56,7 +55,6 @@ class Label( object ): self.__url = None def __useAttributes( self, attributes ): - # @todo No need to check if attribute is in attributes when attribute is mandatory if "color" in attributes and attributes[ "color" ] is not None: # pragma no branch self.__color = attributes[ "color" ] if "name" in attributes and attributes[ "name" ] is not None: # pragma no branch diff --git a/src/github/PullRequest.py b/src/github/PullRequest.py index b434f56c..04a002bb 100644 --- a/src/github/PullRequest.py +++ b/src/github/PullRequest.py @@ -240,11 +240,15 @@ class PullRequest( object ): return status == 204 def merge( self, commit_message = DefaultValueForOptionalParameters ): + post_parameters = { + } + if commit_message is not DefaultValueForOptionalParameters: + post_parameters[ "commit_message" ] = commit_message status, headers, data = self.__requester.request( "PUT", str( self.url ) + "/merge", None, - {} + post_parameters ) def __initAttributes( self ): diff --git a/src/github/Repository.py b/src/github/Repository.py index 794e87c3..84df4662 100644 --- a/src/github/Repository.py +++ b/src/github/Repository.py @@ -584,7 +584,7 @@ class Repository( object ): def get_git_ref( self, ref ): status, headers, data = self.__requester.request( "GET", - str( self.url ) + "/git" + "/" + str( ref ), + str( self.url ) + "/git/" + str( ref ), None, None )