Move classes around

This commit is contained in:
Vincent Jacques
2012-03-14 22:56:08 +01:00
parent 50ac55b25c
commit 85eef75635
38 changed files with 0 additions and 0 deletions
@@ -0,0 +1,36 @@
import itertools
class ArgumentsChecker:
def __init__( self, mandatoryParameters, optionalParameters ):
self.__mandatoryParameters = mandatoryParameters
self.__optionalParameters = optionalParameters
def check( self, args, kwds ):
data = dict( kwds )
for arg, argumentName in itertools.izip( args, itertools.chain( self.__mandatoryParameters, self.__optionalParameters ) ):
if argumentName in kwds:
raise TypeError()
else:
data[ argumentName ] = arg
for argumentName in data:
if argumentName not in itertools.chain( self.__mandatoryParameters, self.__optionalParameters ):
raise TypeError()
for argumentName in self.__mandatoryParameters:
if argumentName not in data:
raise TypeError()
return data
def documentParameters( self ):
mandatory = ", ".join( self.__mandatoryParameters )
optional = "[" + ", ".join( self.__optionalParameters ) + "]"
if len( self.__mandatoryParameters ) == 0:
if len( self.__optionalParameters ) == 0:
return ""
else:
return " " + optional + " "
else:
if len( self.__optionalParameters ) == 0:
return " " + mandatory + " "
else:
return " " + mandatory + ", " + optional + " "
@@ -0,0 +1,120 @@
from ArgumentsChecker import *
class AttributeFromCallable:
class AttributeDefinition:
def __init__( self, name, callable ):
self.__name = name
self.__callable = callable
def getValueFromRawValue( self, obj, rawValue ):
return rawValue
def updateAttributes( self, obj ):
obj._updateAttributes( { self.__name: self.__callable( obj ) } )
def isLazy( self ):
return False
def __init__( self, name, callable ):
self.__name = name
self.__callable = callable
def apply( self, cls ):
cls._addAttribute( self.__name, AttributeFromCallable.AttributeDefinition( self.__name, self.__callable ) )
def autoDocument( self ):
return ""
class MethodFromCallable:
def __init__( self, name, mandatoryParameters, optionalParameters, callable, returnTypePolicy ):
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
self.__name = name
self.__callable = callable
self.__returnTypePolicy = returnTypePolicy
def apply( self, cls ):
cls._addMethod( self.__name, self.__execute )
def __execute( self, obj, *args, **kwds ):
data = self.__argumentsChecker.check( args, kwds )
return self.__callable( obj, **data )
def autoDocument( self ):
if self.__name.startswith( "_" ):
return ""
else:
doc = "* `" + self.__name + "(" + self.__argumentsChecker.documentParameters() + ")`"
if self.__returnTypePolicy.hasMeaningfulDocumentation():
doc += ": " + self.__returnTypePolicy.documentTypeName()
doc += "\n"
return doc
class InternalAttribute:
class AttributeDefinition:
def __init__( self, typePolicy ):
self.__typePolicy = typePolicy
def getValueFromRawValue( self, obj, rawValue ):
if rawValue is None:
return None
else:
return self.__typePolicy.createLazy( obj, rawValue )
def updateAttributes( self, obj ):
attributes = obj._github._dataRequest( "GET", obj._baseUrl(), None, None )
obj._updateAttributes( attributes )
obj._markAsCompleted()
def isLazy( self ):
return True
def __init__( self, attributeName, typePolicy ):
self.__attributeName = attributeName
self.__typePolicy = typePolicy
def apply( self, cls ):
cls._addAttribute( self.__attributeName, InternalAttribute.AttributeDefinition( self.__typePolicy ) )
def autoDocument( self ):
if self.__attributeName.startswith( "_" ):
return ""
doc = "* `" + self.__attributeName + "`"
if self.__typePolicy.hasMeaningfulDocumentation():
doc += ": " + self.__typePolicy.documentTypeName()
doc += "\n"
return doc
class ExternalAttribute:
def __init__( self, attributeName, typePolicy ):
self.__attributeName = attributeName
self.__typePolicy = typePolicy
def apply( self, cls ):
cls._addMethod( "get_" + self.__attributeName, self.__execute )
def __execute( self, obj ):
return self.__typePolicy.createLazy(
obj,
obj._github._dataRequest( "GET", obj._baseUrl() + "/" + self.__attributeName, None, None )
)
def autoDocument( self ):
return "* `get_" + self.__attributeName + "()`: " + self.__typePolicy.documentTypeName() + "\n"
class SeveralAttributePolicies:
def __init__( self, attributePolicies, documentationSection = None ):
self.__attributePolicies = attributePolicies
self.__documentationSection = documentationSection
def apply( self, cls ):
for attributePolicy in self.__attributePolicies:
attributePolicy.apply( cls )
def autoDocument( self ):
doc = ""
if self.__documentationSection is not None:
doc += "\n"
doc += self.__documentationSection + "\n"
doc += "-" * len( self.__documentationSection ) + "\n"
doc += "".join( attributePolicy.autoDocument() for attributePolicy in self.__attributePolicies )
return doc
@@ -0,0 +1,224 @@
import itertools
from Basic import *
from TypePolicies import *
from ArgumentsChecker import *
class ListCapacity:
def setList( self, attributeName, singularName, typePolicy, url = None ):
self.attributeName = attributeName
self.singularName = singularName
self.safeAttributeName = attributeName.replace( "/", "_" )
self.safeSingularName = singularName.replace( "/", "_" )
self.typePolicy = typePolicy
self.__url = url
def baseUrl( self, obj ):
if self.__url is None:
return obj._baseUrl() + "/" + self.attributeName
else:
return self.__url
class ElementAddable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "add_to_" + self.safeAttributeName, self.__execute )
def __execute( self, obj, toBeAdded ):
obj._github._statusRequest(
"PUT",
self.baseUrl( obj ) + "/" + self.typePolicy.getIdentity( toBeAdded ),
None,
None
)
def autoDocument( self ):
return "* `add_to_" + self.safeAttributeName + "( " + self.singularName + " )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ElementRemovable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "remove_from_" + self.safeAttributeName, self.__execute )
def __execute( self, obj, toBeDeleted ):
obj._github._statusRequest(
"DELETE",
self.baseUrl( obj ) + "/" + self.typePolicy.getIdentity( toBeDeleted ),
None,
None
)
def autoDocument( self ):
return "* `remove_from_" + self.safeAttributeName + "( " + self.singularName + " )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
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",
self.baseUrl( obj ) + "/" + self.typePolicy.getIdentity( toBeQueried ),
None,
None
) == 204
def autoDocument( self ):
return "* `has_in_" + self.safeAttributeName + "( " + self.singularName + " )`: bool\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ListCapacityWithModifier( ListCapacity ):
def __init__( self, attributeModifiers ):
self.__attributeModifiers = attributeModifiers
def _modifyAttributes( self, obj, attributes ):
for attributeName, attributeModifier in self.__attributeModifiers.iteritems():
attributes[ attributeName ] = attributeModifier( obj )
return attributes
class ElementCreatable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
def apply( self, cls ):
cls._addMethod( "create_" + self.singularName, self.__execute )
def __execute( self, obj, *args, **kwds ):
return self.typePolicy.createLazy(
obj,
self._modifyAttributes(
obj,
obj._github._dataRequest(
"POST",
self.baseUrl( obj ),
None,
self.__argumentsChecker.check( args, kwds )
)
)
)
def autoDocument( self ):
return "* `create_" + self.singularName + "(" + self.__argumentsChecker.documentParameters() + ")`: " + self.typePolicy.documentTypeName() + "\n"
class ElementGetable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
def apply( self, cls ):
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 )
)
)
def autoDocument( self ):
return "* `get_" + self.singularName + "(" + self.__argumentsChecker.documentParameters() + ")`: " + self.typePolicy.documentTypeName() + "\n"
class SeveralElementsAddable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "add_to_" + self.safeAttributeName, self.__execute )
def __execute( self, obj, *toBeAddeds ):
obj._github._statusRequest(
"POST",
self.baseUrl( obj ),
None,
[
self.typePolicy.getIdentity( toBeAdded )
for toBeAdded in toBeAddeds
]
)
def autoDocument( self ):
return "* `add_to_" + self.safeAttributeName + "( " + self.singularName + ", ... )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class SeveralElementsRemovable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "remove_from_" + self.safeAttributeName, self.__execute )
def __execute( self, obj, *toBeDeleteds ):
obj._github._statusRequest(
"DELETE",
self.baseUrl( obj ),
None,
[
self.typePolicy.getIdentity( toBeDeleted )
for toBeDeleted in toBeDeleteds
]
)
def autoDocument( self ):
return "* `remove_from_" + self.safeAttributeName + "( " + self.singularName + ", ... )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ListGetable( ListCapacityWithModifier ):
def __init__( self, mandatoryParameters, optionalParameters, attributeModifiers = {} ):
ListCapacityWithModifier.__init__( self, attributeModifiers )
self.__argumentsChecker = ArgumentsChecker( mandatoryParameters, optionalParameters )
def apply( self, cls ):
cls._addMethod( "get_" + self.safeAttributeName, self.__execute )
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",
self.baseUrl( obj ),
params,
None
)
]
def autoDocument( self ):
return "* `get_" + self.safeAttributeName + "(" + self.__argumentsChecker.documentParameters() + ")`: list of " + self.typePolicy.documentTypeName() + "\n"
class ListSetable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "set_" + self.safeAttributeName, self.__execute )
def __execute( self, obj, *toBeSets ):
obj._github._statusRequest(
"PUT",
self.baseUrl( obj ),
None,
[
self.typePolicy.getIdentity( toBeSet )
for toBeSet in toBeSets
]
)
def autoDocument( self ):
return "* `set_" + self.safeAttributeName + "( " + self.singularName + ", ... )`\n * `" + self.singularName + "`: " + self.typePolicy.documentTypeName() + "\n"
class ListDeletable( ListCapacity ):
def apply( self, cls ):
cls._addMethod( "delete_" + self.safeAttributeName, self.__execute )
def __execute( self, obj ):
obj._github._statusRequest(
"DELETE",
self.baseUrl( obj ),
None,
None
)
def autoDocument( self ):
return "* `delete_" + self.safeAttributeName + "()`\n"
def ExternalListOfObjects( attributeName, singularName, type, *capacities, **kwds ):
for capacity in capacities:
capacity.setList( attributeName, singularName, ObjectTypePolicy( type ), **kwds )
return SeveralAttributePolicies( capacities, attributeName.capitalize().replace( "_", " " ).replace( "/", " " ) )
def ExternalListOfSimpleTypes( attributeName, singularName, type, *capacities ):
for capacity in capacities:
capacity.setList( attributeName, singularName, SimpleTypePolicy( type ) )
return SeveralAttributePolicies( capacities, attributeName.capitalize().replace( "_", " " ).replace( "/", " " ) )
@@ -0,0 +1,35 @@
class SimpleTypePolicy:
def __init__( self, type ):
self.__type = type
def createLazy( self, obj, value ):
return value
def getIdentity( self, value ):
return value
def hasMeaningfulDocumentation( self ):
return self.__type is not None
def documentTypeName( self ):
return self.__type
class ObjectTypePolicy:
def __init__( self, type ):
self.__type = type
def createLazy( self, obj, attributes ):
return self.__type( obj._github, attributes, lazy = True )
def createNonLazy( self, obj, attributes ):
return self.__type( obj._github, attributes, lazy = False )
def getIdentity( self, obj ):
assert isinstance( obj, self.__type )
return obj._identity
def hasMeaningfulDocumentation( self ):
return True
def documentTypeName( self ):
return "`" + self.__type.__name__ + "`"