From e1a0747fe614afc5d04a938db6ee4bfd136798cb Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 12 Feb 2012 14:25:48 +0100 Subject: [PATCH] Handle simple scalar attributes in constructor --- github/GithubObject.UnitTest.py | 23 +++++++++++++++--- github/GithubObject.py | 43 ++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/github/GithubObject.UnitTest.py b/github/GithubObject.UnitTest.py index 0f9e83c7..178ee4a5 100644 --- a/github/GithubObject.UnitTest.py +++ b/github/GithubObject.UnitTest.py @@ -1,9 +1,16 @@ import unittest import MockMockMock -from GithubObject import GithubObject, SimpleScalarAttributes +from GithubObject import BadGithubObjectException, GithubObject, SimpleScalarAttributes -class TestCase( unittest.TestCase ): +class GithubObjectTestCase( unittest.TestCase ): + def testDuplicatedAttribute( self ): + with self.assertRaises( BadGithubObjectException ): + GithubObject( "", SimpleScalarAttributes( "a", "a" ) ) + with self.assertRaises( BadGithubObjectException ): + GithubObject( "", SimpleScalarAttributes( "a" ), SimpleScalarAttributes( "a" ) ) + +class TestCaseWithGithubTestObject( unittest.TestCase ): def setUp( self ): unittest.TestCase.setUp( self ) self.g = MockMockMock.Mock( "github" ) @@ -13,7 +20,7 @@ class TestCase( unittest.TestCase ): self.g.tearDown() unittest.TestCase.tearDown( self ) -class GithubObjectWithOnlySimpleAttributes( TestCase ): +class GithubObjectWithOnlySimpleAttributes( TestCaseWithGithubTestObject ): GithubTestObject = GithubObject( "GithubTestObject", SimpleScalarAttributes( "a1", "a2", "a3", "a4" ) @@ -22,4 +29,14 @@ class GithubObjectWithOnlySimpleAttributes( TestCase ): def testConstruction( self ): pass # Everything is done in setUp/tearDown + def testCompletion( self ): + # A GithubObject: + # - knows the attributes given to its constructor + self.assertEqual( self.o.a1, 1 ) + self.assertEqual( self.o.a2, 2 ) + + def testUnknownAttribute( self ): + # A GithubObject: + # - does not have silly attributes + self.assertRaises( AttributeError, lambda: self.o.foobar ) unittest.main() diff --git a/github/GithubObject.py b/github/GithubObject.py index 629518fb..8ff2067e 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -1,10 +1,45 @@ +class BadGithubObjectException( Exception ): + pass + class SimpleScalarAttributes: - def __init__( self, *attributes ): - self.__attributes = attributes + class AttributeDefinition: + def getValueFromRawValue( self, rawValue ): + return rawValue + + def __init__( self, *attributeNames ): + self.__attributeNames = attributeNames + + def getAttributeDefinitions( self ): + return [ + ( attributeName, SimpleScalarAttributes.AttributeDefinition() ) + for attributeName in self.__attributeNames + ] + +def GithubObject( className, *attributePolicies ): + attributeDefinitions = dict() + for attributePolicy in attributePolicies: + for attributeName, attributeDefinition in attributePolicy.getAttributeDefinitions(): + if attributeName in attributeDefinitions: + raise BadGithubObjectException( "Same attribute defined by two policies" ) + else: + attributeDefinitions[ attributeName ] = attributeDefinition -def GithubObject( *attributes ): class GithubObject: def __init__( self, github, attributes, lazy ): - pass + self.__attributes = dict() + self.__updateAttributes( attributes ) + + def __getattr__( self, attributeName ): + if attributeName in attributeDefinitions: + # if attributeName not in self.__attributes: + # self.__fetchAttribute( attributeName ) + return self.__attributes[ attributeName ] + else: + raise AttributeError() + + def __updateAttributes( self, attributes ): + for attributeName, attributeValue in attributes.iteritems(): + attributeDefinition = attributeDefinitions[ attributeName ] + self.__attributes[ attributeName ] = attributeDefinition.getValueFromRawValue( attributeValue ) return GithubObject