mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 19:01:15 +00:00
Refactor
This commit is contained in:
@@ -232,11 +232,11 @@ class GithubObjectWithListGetableList( TestCaseWithGithubTestObject ):
|
||||
"GithubTestObject",
|
||||
BaseUrl( lambda obj: "/test" ),
|
||||
BasicAttributes( "a1", "a2" ),
|
||||
ListOfObjects( "a3s", ContainedObject, ListGetable() )
|
||||
ListOfObjects( "a3s", ContainedObject, ListGetable( [], [] ) )
|
||||
)
|
||||
|
||||
def testGetList( self ):
|
||||
self.expectDataGet( "/test/a3s" ).andReturn( [ { "id": "id1" }, { "id": "id2" }, { "id": "id3" } ] )
|
||||
self.expectDataGet( "/test/a3s", {} ).andReturn( [ { "id": "id1" }, { "id": "id2" }, { "id": "id3" } ] )
|
||||
a3s = self.o.get_a3s()
|
||||
self.assertEqual( len( a3s ), 3 )
|
||||
self.assertEqual( a3s[ 0 ].id, "id1" )
|
||||
|
||||
@@ -82,9 +82,10 @@ Repository._addAttributePolicy( ListOfReferences( "forks", Repository ) )
|
||||
|
||||
__repoElementCreatable = ElementCreatable( "repo", [ "name" ], [ "description", "homepage", "private", "has_issues", "has_wiki", "has_downloads", "team_id", ] )
|
||||
__repoElementGetable = ElementGetable( "repo", lambda obj, name: { "owner": { "login": obj.login }, "name": name } )
|
||||
AuthenticatedUser._addAttributePolicy( ListOfObjects( "repos", Repository, ListGetable(), __repoElementGetable, __repoElementCreatable ) )
|
||||
NamedUser._addAttributePolicy( ListOfObjects( "repos", Repository, ListGetable(), __repoElementGetable ) )
|
||||
Organization._addAttributePolicy( ListOfObjects( "repos", Repository, ListGetable(), __repoElementGetable, __repoElementCreatable ) )
|
||||
__repoListGetable = ListGetable( [], [] )
|
||||
AuthenticatedUser._addAttributePolicy( ListOfObjects( "repos", Repository, __repoListGetable, __repoElementGetable, __repoElementCreatable ) )
|
||||
NamedUser._addAttributePolicy( ListOfObjects( "repos", Repository, __repoListGetable, __repoElementGetable ) )
|
||||
Organization._addAttributePolicy( ListOfObjects( "repos", Repository, __repoListGetable, __repoElementGetable, __repoElementCreatable ) )
|
||||
|
||||
AuthenticatedUser._addAttributePolicy( ListOfReferences( "watched", Repository, addable = True, removable = True, hasable = True ) )
|
||||
NamedUser._addAttributePolicy( ListOfReferences( "watched", Repository ) )
|
||||
|
||||
@@ -7,8 +7,6 @@ class ArgumentsChecker:
|
||||
|
||||
def check( self, args, kwds ):
|
||||
data = dict( kwds )
|
||||
if len( args ) + len( kwds ) == 0:
|
||||
raise TypeError()
|
||||
for arg, argumentName in itertools.izip( args, itertools.chain( self.__mandatoryParameters, self.__optionalParameters ) ):
|
||||
if argumentName in kwds:
|
||||
raise TypeError()
|
||||
|
||||
@@ -4,6 +4,17 @@ import ArgumentsChecker
|
||||
|
||||
class ListOfReferences:
|
||||
def __init__( self, attributeName, type, addable = False, removable = False, hasable = False, getParameters = [] ):
|
||||
self.__capacities = list()
|
||||
self.attributeName = attributeName
|
||||
self.type = type
|
||||
self.__capacities.append( ListGetable( [], getParameters ) )
|
||||
# if addable:
|
||||
# self.__capacities.append( ElementAddable() )
|
||||
# if removable:
|
||||
# self.__capacities.append( ElementRemoveable() )
|
||||
# if hasable:
|
||||
# self.__capacities.append( ElementHasable() )
|
||||
|
||||
self.__attributeName = attributeName
|
||||
self.__type = type
|
||||
self.__getName = "get_" + attributeName
|
||||
@@ -22,7 +33,9 @@ class ListOfReferences:
|
||||
self.__hasName = None
|
||||
|
||||
def apply( self, cls ):
|
||||
cls._addMethod( self.__getName, self.__executeGet )
|
||||
for capacity in self.__capacities:
|
||||
capacity.apply( self, cls )
|
||||
|
||||
if self.__addName is not None:
|
||||
cls._addMethod( self.__addName, self.__executeAdd )
|
||||
if self.__removeName is not None:
|
||||
@@ -30,15 +43,6 @@ class ListOfReferences:
|
||||
if self.__hasName is not None:
|
||||
cls._addMethod( self.__hasName, self.__executeHas )
|
||||
|
||||
def __executeGet( self, obj, *args, **kwds ):
|
||||
### @todo ArgumentsChecker?
|
||||
for arg, argumentName in itertools.izip( args, self.__getParameters ):
|
||||
kwds[ argumentName ] = arg
|
||||
return [
|
||||
self.__type( obj._github, attributes, lazy = True )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, kwds, None )
|
||||
]
|
||||
|
||||
def __executeAdd( self, obj, toBeAdded ):
|
||||
assert isinstance( toBeAdded, self.__type )
|
||||
obj._github._statusRequest( "PUT", obj._baseUrl + "/" + self.__attributeName + "/" + toBeAdded._identity, None, None )
|
||||
@@ -66,15 +70,19 @@ class ElementCreatable:
|
||||
return self.__type( obj._github, obj._github._dataRequest( "POST", obj._baseUrl + "/" + self.__attributeName, None, data ), lazy = True )
|
||||
|
||||
class ListGetable:
|
||||
def __init__( self, mandatoryParameters, optionalParameters ):
|
||||
self.__argumentsChecker = ArgumentsChecker.ArgumentsChecker( mandatoryParameters, optionalParameters )
|
||||
|
||||
def apply( self, list, cls ):
|
||||
self.__type = list.type
|
||||
self.__attributeName = list.attributeName
|
||||
cls._addMethod( "get_" + list.attributeName, self.__execute )
|
||||
|
||||
def __execute( self, obj ):
|
||||
def __execute( self, obj, *args, **kwds ):
|
||||
params = self.__argumentsChecker.check( args, kwds )
|
||||
return [
|
||||
self.__type( obj._github, attributes, lazy = True )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, None, None )
|
||||
for attributes in obj._github._dataRequest( "GET", obj._baseUrl + "/" + self.__attributeName, params, None )
|
||||
]
|
||||
|
||||
class ElementGetable:
|
||||
|
||||
Reference in New Issue
Block a user