code
stringlengths
1
1.72M
language
stringclasses
1 value
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import imp import inspect import new import re import sys import types import unittest from protorpc import test_util from protorpc import descriptor from protorpc import messages class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = messages class ValidationErrorTest(test_util.TestCase): def testStr_NoFieldName(self): """Test string version of ValidationError when no name provided.""" self.assertEquals('Validation error', str(messages.ValidationError('Validation error'))) def testStr_FieldName(self): """Test string version of ValidationError when no name provided.""" validation_error = messages.ValidationError('Validation error') validation_error.field_name = 'a_field' self.assertEquals('Field a_field: Validation error', str(validation_error)) class EnumTest(test_util.TestCase): def setUp(self): """Set up tests.""" # Redefine Color class in case so that changes to it (an error) in one test # does not affect other tests. global Color class Color(messages.Enum): RED = 20 ORANGE = 2 YELLOW = 40 GREEN = 4 BLUE = 50 INDIGO = 5 VIOLET = 80 def testNames(self): """Test that names iterates over enum names.""" self.assertEquals( set(['BLUE', 'GREEN', 'INDIGO', 'ORANGE', 'RED', 'VIOLET', 'YELLOW']), set(Color.names())) def testNumbers(self): """Tests that numbers iterates of enum numbers.""" self.assertEquals(set([2, 4, 5, 20, 40, 50, 80]), set(Color.numbers())) def testIterate(self): """Test that __iter__ iterates over all enum values.""" self.assertEquals(set(Color), set([Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.INDIGO, Color.VIOLET])) def testNaturalOrder(self): """Test that natural order enumeration is in numeric order.""" self.assertEquals([Color.ORANGE, Color.GREEN, Color.INDIGO, Color.RED, Color.YELLOW, Color.BLUE, Color.VIOLET], sorted(Color)) def testByName(self): """Test look-up by name.""" self.assertEquals(Color.RED, Color.lookup_by_name('RED')) self.assertRaises(KeyError, Color.lookup_by_name, 20) self.assertRaises(KeyError, Color.lookup_by_name, Color.RED) def testByNumber(self): """Test look-up by number.""" self.assertRaises(KeyError, Color.lookup_by_number, 'RED') self.assertEquals(Color.RED, Color.lookup_by_number(20)) self.assertRaises(KeyError, Color.lookup_by_number, Color.RED) def testConstructor(self): """Test that constructor look-up by name or number.""" self.assertEquals(Color.RED, Color('RED')) self.assertEquals(Color.RED, Color(u'RED')) self.assertEquals(Color.RED, Color(20)) self.assertEquals(Color.RED, Color(20L)) self.assertEquals(Color.RED, Color(Color.RED)) self.assertRaises(TypeError, Color, 'Not exists') self.assertRaises(TypeError, Color, 'Red') self.assertRaises(TypeError, Color, 100) self.assertRaises(TypeError, Color, 10.0) def testLen(self): """Test that len function works to count enums.""" self.assertEquals(7, len(Color)) def testNoSubclasses(self): """Test that it is not possible to sub-class enum classes.""" def declare_subclass(): class MoreColor(Color): pass self.assertRaises(messages.EnumDefinitionError, declare_subclass) def testClassNotMutable(self): """Test that enum classes themselves are not mutable.""" self.assertRaises(AttributeError, setattr, Color, 'something_new', 10) def testInstancesMutable(self): """Test that enum instances are not mutable.""" self.assertRaises(TypeError, setattr, Color.RED, 'something_new', 10) def testDefEnum(self): """Test def_enum works by building enum class from dict.""" WeekDay = messages.Enum.def_enum({'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 6, 'Saturday': 7, 'Sunday': 8}, 'WeekDay') self.assertEquals('Wednesday', WeekDay(3).name) self.assertEquals(6, WeekDay('Friday').number) self.assertEquals(WeekDay.Sunday, WeekDay('Sunday')) def testNonInt(self): """Test that non-integer values rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': '1'}, 'BadEnum') def testNegativeInt(self): """Test that negative numbers rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': -1}, 'BadEnum') def testLowerBound(self): """Test that zero is accepted by enum def.""" class NotImportant(messages.Enum): """Testing for value zero""" VALUE = 0 self.assertEquals(0, int(NotImportant.VALUE)) def testTooLargeInt(self): """Test that numbers too large are rejected.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': (2 ** 29)}, 'BadEnum') def testRepeatedInt(self): """Test duplicated numbers are forbidden.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Ok': 1, 'Repeated': 1}, 'BadEnum') def testStr(self): """Test converting to string.""" self.assertEquals('RED', str(Color.RED)) self.assertEquals('ORANGE', str(Color.ORANGE)) def testInt(self): """Test converting to int.""" self.assertEquals(20, int(Color.RED)) self.assertEquals(2, int(Color.ORANGE)) def testRepr(self): """Test enum representation.""" self.assertEquals('Color(RED, 20)', repr(Color.RED)) self.assertEquals('Color(YELLOW, 40)', repr(Color.YELLOW)) def testDocstring(self): """Test that docstring is supported ok.""" class NotImportant(messages.Enum): """I have a docstring.""" VALUE1 = 1 self.assertEquals('I have a docstring.', NotImportant.__doc__) def testDeleteEnumValue(self): """Test that enum values cannot be deleted.""" self.assertRaises(TypeError, delattr, Color, 'RED') def testEnumName(self): """Test enum name.""" self.assertEquals('messages_test.Color', Color.definition_name()) def testDefinitionName_OverrideModule(self): """Test enum module is overriden by module package name.""" global package try: package = 'my.package' self.assertEquals('my.package.Color', Color.definition_name()) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for enum.""" class Enum1(messages.Enum): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('Enum1', Enum1.definition_name()) self.assertEquals(unicode, type(Enum1.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested Enum names.""" class MyMessage(messages.Message): class NestedEnum(messages.Enum): pass class NestedMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals('messages_test.MyMessage.NestedEnum', MyMessage.NestedEnum.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedEnum', MyMessage.NestedMessage.NestedEnum.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterEnum(messages.Enum): pass self.assertEquals(None, OuterEnum.message_definition()) class OuterMessage(messages.Message): class InnerEnum(messages.Enum): pass self.assertEquals(OuterMessage, OuterMessage.InnerEnum.message_definition()) def testComparison(self): """Test comparing various enums to different types.""" class Enum1(messages.Enum): VAL1 = 1 VAL2 = 2 class Enum2(messages.Enum): VAL1 = 1 self.assertEquals(Enum1.VAL1, Enum1.VAL1) self.assertNotEquals(Enum1.VAL1, Enum1.VAL2) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertNotEquals(Enum1.VAL1, 'VAL1') self.assertNotEquals(Enum1.VAL1, 1) self.assertNotEquals(Enum1.VAL1, 2) self.assertNotEquals(Enum1.VAL1, None) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertTrue(Enum1.VAL1 < Enum1.VAL2) self.assertTrue(Enum1.VAL2 > Enum1.VAL1) self.assertNotEquals(1, Enum2.VAL1) class FieldListTest(test_util.TestCase): def setUp(self): self.integer_field = messages.IntegerField(1, repeated=True) def testConstructor(self): self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, [1, 2, 3])) self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, (1, 2, 3))) self.assertEquals([], messages.FieldList(self.integer_field, [])) def testNone(self): self.assertRaises(TypeError, messages.FieldList, self.integer_field, None) def testDoNotAutoConvertString(self): string_field = messages.StringField(1, repeated=True) self.assertRaises(messages.ValidationError, messages.FieldList, string_field, 'abc') def testConstructorCopies(self): a_list = [1, 3, 6] field_list = messages.FieldList(self.integer_field, a_list) self.assertFalse(a_list is field_list) self.assertFalse(field_list is messages.FieldList(self.integer_field, field_list)) def testNonRepeatedField(self): self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'FieldList may only accept repeated fields', messages.FieldList, messages.IntegerField(1), []) def testConstructor_InvalidValues(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 1 (type <type 'str'>)"), messages.FieldList, self.integer_field, ["1", "2", "3"]) def testConstructor_Scalars(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: 3", messages.FieldList, self.integer_field, 3) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <listiterator object", messages.FieldList, self.integer_field, iter([1, 2, 3])) def testSetSlice(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) field_list[1:3] = [10, 20] self.assertEquals([1, 10, 20, 4, 5], field_list) def testSetSlice_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) def setslice(): field_list[1:3] = ['10', '20'] self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setslice) def testSetItem(self): field_list = messages.FieldList(self.integer_field, [2]) field_list[0] = 10 self.assertEquals([10], field_list) def testSetItem_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def setitem(): field_list[0] = '10' self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setitem) def testAppend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.append(10) self.assertEquals([2, 10], field_list) def testAppend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def append(): field_list.append('10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), append) def testExtend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.extend([10]) self.assertEquals([2, 10], field_list) def testExtend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def extend(): field_list.extend(['10']) self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), extend) def testInsert(self): field_list = messages.FieldList(self.integer_field, [2, 3]) field_list.insert(1, 10) self.assertEquals([2, 10, 3], field_list) def testInsert_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2, 3]) def insert(): field_list.insert(1, '10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), insert) class FieldTest(test_util.TestCase): def ActionOnAllFieldClasses(self, action): """Test all field classes except Message and Enum. Message and Enum require separate tests. Args: action: Callable that takes the field class as a parameter. """ for field_class in (messages.IntegerField, messages.FloatField, messages.BooleanField, messages.BytesField, messages.StringField, ): action(field_class) def testNumberAttribute(self): """Test setting the number attribute.""" def action(field_class): # Check range. self.assertRaises(messages.InvalidNumberError, field_class, 0) self.assertRaises(messages.InvalidNumberError, field_class, -1) self.assertRaises(messages.InvalidNumberError, field_class, messages.MAX_FIELD_NUMBER + 1) # Check reserved. self.assertRaises(messages.InvalidNumberError, field_class, messages.FIRST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, messages.LAST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, '1') # This one should work. field_class(number=1) self.ActionOnAllFieldClasses(action) def testRequiredAndRepeated(self): """Test setting the required and repeated fields.""" def action(field_class): field_class(1, required=True) field_class(1, repeated=True) self.assertRaises(messages.FieldDefinitionError, field_class, 1, required=True, repeated=True) self.ActionOnAllFieldClasses(action) def testInvalidVariant(self): """Test field with invalid variants.""" def action(field_class): self.assertRaises(messages.InvalidVariantError, field_class, 1, variant=messages.Variant.ENUM) self.ActionOnAllFieldClasses(action) def testDefaultVariant(self): """Test that default variant is used when not set.""" def action(field_class): field = field_class(1) self.assertEquals(field_class.DEFAULT_VARIANT, field.variant) self.ActionOnAllFieldClasses(action) def testAlternateVariant(self): """Test that default variant is used when not set.""" field = messages.IntegerField(1, variant=messages.Variant.UINT32) self.assertEquals(messages.Variant.UINT32, field.variant) def testDefaultFields_Single(self): """Test default field is correct type.""" defaults = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): field_class(1, default=defaults[field_class]) self.ActionOnAllFieldClasses(action) # Run defaults test again checking for str/unicode compatiblity. defaults[messages.StringField] = 'abc' self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidSingle(self): """Test default field is correct type.""" def action(field_class): self.assertRaises(messages.InvalidDefaultError, field_class, 1, default=object()) self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidRepeated(self): """Test default field does not accept defaults.""" self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'Repeated fields may not have defaults', messages.StringField, 1, repeated=True, default=[1, 2, 3]) def testDefaultFields_None(self): """Test none is always acceptable.""" def action(field_class): field_class(1, default=None) field_class(1, required=True, default=None) field_class(1, repeated=True, default=None) self.ActionOnAllFieldClasses(action) def testDefaultFields_Enum(self): """Test the default for enum fields.""" class Symbol(messages.Enum): ALPHA = 1 BETA = 2 GAMMA = 3 field = messages.EnumField(Symbol, 1, default=Symbol.ALPHA) self.assertEquals(Symbol.ALPHA, field.default) def testDefaultFields_EnumStringDelayedResolution(self): """Test that enum fields resolve default strings.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default='OPTIONAL') self.assertEquals(descriptor.FieldDescriptor.Label.OPTIONAL, field.default) def testDefaultFields_EnumIntDelayedResolution(self): """Test that enum fields resolve default integers.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=2) self.assertEquals(descriptor.FieldDescriptor.Label.REQUIRED, field.default) def testDefaultFields_EnumOkIfTypeKnown(self): """Test that enum fields accept valid default values when type is known.""" field = messages.EnumField(descriptor.FieldDescriptor.Label, 1, default='REPEATED') self.assertEquals(descriptor.FieldDescriptor.Label.REPEATED, field.default) def testDefaultFields_EnumForceCheckIfTypeKnown(self): """Test that enum fields validate default values if type is known.""" self.assertRaisesWithRegexpMatch(TypeError, 'No such value for NOT_A_LABEL in ' 'Enum Label', messages.EnumField, descriptor.FieldDescriptor.Label, 1, default='NOT_A_LABEL') def testDefaultFields_EnumInvalidDelayedResolution(self): """Test that enum fields raise errors upon delayed resolution error.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=200) self.assertRaisesWithRegexpMatch(TypeError, 'No such value for 200 in Enum Label', getattr, field, 'default') def testValidate_Valid(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate(values[field_class]) # Required. field = field_class(1, required=True) field.validate(values[field_class]) # Repeated. field = field_class(1, repeated=True) field.validate([]) field.validate(()) field.validate([values[field_class]]) field.validate((values[field_class],)) # Right value, but not repeated. self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.ActionOnAllFieldClasses(action) def testValidate_Invalid(self): """Test validation of valid values.""" values = {messages.IntegerField: "10", messages.FloatField: 1, messages.BooleanField: 0, messages.BytesField: 10.20, messages.StringField: 42, } def action(field_class): # Optional. field = field_class(1) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Required. field = field_class(1, required=True) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(messages.ValidationError, field.validate, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate, (values[field_class],)) self.ActionOnAllFieldClasses(action) def testValidate_None(self): """Test that None is valid for non-required fields.""" def action(field_class): # Optional. field = field_class(1) field.validate(None) # Required. field = field_class(1, required=True) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Required field is missing', field.validate, None) # Repeated. field = field_class(1, repeated=True) field.validate(None) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Repeated values may not be None', field.validate, [None]) self.assertRaises(messages.ValidationError, field.validate, (None,)) self.ActionOnAllFieldClasses(action) def testValidateElement(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate_element(values[field_class]) # Required. field = field_class(1, required=True) field.validate_element(values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(message.VAlidationError, field.validate_element, []) self.assertRaises(message.VAlidationError, field.validate_element, ()) field.validate_element(values[field_class]) field.validate_element(values[field_class]) # Right value, but repeated. self.assertRaises(messages.ValidationError, field.validate_element, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate_element, (values[field_class],)) def testReadOnly(self): """Test that objects are all read-only.""" def action(field_class): field = field_class(10) self.assertRaises(AttributeError, setattr, field, 'number', 20) self.assertRaises(AttributeError, setattr, field, 'anything_else', 'whatever') self.ActionOnAllFieldClasses(action) def testMessageField(self): """Test the construction of message fields.""" self.assertRaises(messages.FieldDefinitionError, messages.MessageField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.MessageField, messages.Message, 10) class MyMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) self.assertEquals(MyMessage, field.type) def testMessageField_ForwardReference(self): """Test the construction of forward reference message fields.""" global MyMessage global ForwardMessage try: class MyMessage(messages.Message): self_reference = messages.MessageField('MyMessage', 1) forward = messages.MessageField('ForwardMessage', 2) nested = messages.MessageField('ForwardMessage.NestedMessage', 3) inner = messages.MessageField('Inner', 4) class Inner(messages.Message): sibling = messages.MessageField('Sibling', 1) class Sibling(messages.Message): pass class ForwardMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals(MyMessage, MyMessage.field_by_name('self_reference').type) self.assertEquals(ForwardMessage, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedMessage, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) self.assertEquals(MyMessage.Sibling, MyMessage.Inner.field_by_name('sibling').type) finally: try: del MyMessage del ForwardMessage except: pass def testMessageField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AnEnum try: class AnEnum(messages.Enum): pass class AnotherMessage(messages.Message): a_field = messages.MessageField('AnEnum', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AnEnum def testMessageFieldValidate(self): """Test validation on message field.""" class MyMessage(messages.Message): pass class AnotherMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) field.validate(MyMessage()) self.assertRaises(messages.ValidationError, field.validate, AnotherMessage()) def testIntegerField_AllowLong(self): """Test that the integer field allows for longs.""" messages.IntegerField(10, default=long(10)) def testMessageFieldValidate_Initialized(self): """Test validation on message field.""" class MyMessage(messages.Message): field1 = messages.IntegerField(1, required=True) field = messages.MessageField(MyMessage, 10) # Will validate messages where is_initialized() is False. message = MyMessage() field.validate(message) message.field1 = 20 field.validate(message) def testEnumField(self): """Test the construction of enum fields.""" self.assertRaises(messages.FieldDefinitionError, messages.EnumField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.EnumField, messages.Enum, 10) class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 field = messages.EnumField(Color, 10) self.assertEquals(Color, field.type) class Another(messages.Enum): VALUE = 1 self.assertRaises(messages.InvalidDefaultError, messages.EnumField, Color, 10, default=Another.VALUE) def testEnumField_ForwardReference(self): """Test the construction of forward reference enum fields.""" global MyMessage global ForwardEnum global ForwardMessage try: class MyMessage(messages.Message): forward = messages.EnumField('ForwardEnum', 1) nested = messages.EnumField('ForwardMessage.NestedEnum', 2) inner = messages.EnumField('Inner', 3) class Inner(messages.Enum): pass class ForwardEnum(messages.Enum): pass class ForwardMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals(ForwardEnum, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedEnum, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) finally: try: del MyMessage del ForwardEnum del ForwardMessage except: pass def testEnumField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AMessage try: class AMessage(messages.Message): pass class AnotherMessage(messages.Message): a_field = messages.EnumField('AMessage', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AMessage def testMessageDefinition(self): """Test that message definition is set on fields.""" class MyMessage(messages.Message): my_field = messages.StringField(1) self.assertEquals(MyMessage, MyMessage.field_by_name('my_field').message_definition()) def testNoneAssignment(self): """Test that assigning None does not change comparison.""" class MyMessage(messages.Message): my_field = messages.StringField(1) m1 = MyMessage() m2 = MyMessage() m2.my_field = None self.assertEquals(m1, m2) def testNonAsciiStr(self): """Test validation fails for non-ascii StringField values.""" class Thing(messages.Message): string_field = messages.StringField(2) thing = Thing() self.assertRaisesWithRegexpMatch( messages.ValidationError, 'Field string_field: Encountered non-ASCII string', setattr, thing, 'string_field', test_util.BINARY) class MessageTest(test_util.TestCase): """Tests for message class.""" def CreateMessageClass(self): """Creates a simple message class with 3 fields. Fields are defined in alphabetical order but with conflicting numeric order. """ class ComplexMessage(messages.Message): a3 = messages.IntegerField(3) b1 = messages.StringField(1) c2 = messages.StringField(2) return ComplexMessage def testSameNumbers(self): """Test that cannot assign two fields with same numbers.""" def action(): class BadMessage(messages.Message): f1 = messages.IntegerField(1) f2 = messages.IntegerField(1) self.assertRaises(messages.DuplicateNumberError, action) def testStrictAssignment(self): """Tests that cannot assign to unknown or non-reserved attributes.""" class SimpleMessage(messages.Message): field = messages.IntegerField(1) simple_message = SimpleMessage() self.assertRaises(AttributeError, setattr, simple_message, 'does_not_exist', 10) def testListAssignmentDoesNotCopy(self): class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message = SimpleMessage() original = message.repeated message.repeated = [] self.assertFalse(original is message.repeated) def testValidate_Optional(self): """Tests validation of optional fields.""" class SimpleMessage(messages.Message): non_required = messages.IntegerField(1) simple_message = SimpleMessage() simple_message.check_initialized() simple_message.non_required = 10 simple_message.check_initialized() def testValidate_Required(self): """Tests validation of required fields.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertRaises(messages.ValidationError, simple_message.check_initialized) simple_message.required = 10 simple_message.check_initialized() def testValidate_Repeated(self): """Tests validation of repeated fields.""" class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) simple_message = SimpleMessage() # Check valid values. for valid_value in [], [10], [10, 20], (), (10,), (10, 20): simple_message.repeated = valid_value simple_message.check_initialized() # Check cleared. simple_message.repeated = [] simple_message.check_initialized() # Check invalid values. for invalid_value in 10, ['10', '20'], [None], (None,): self.assertRaises(messages.ValidationError, setattr, simple_message, 'repeated', invalid_value) def testIsInitialized(self): """Tests is_initialized.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertFalse(simple_message.is_initialized()) simple_message.required = 10 self.assertTrue(simple_message.is_initialized()) def testNestedMethodsNotAllowed(self): """Test that method definitions on Message classes are not allowed.""" def action(): class WithMethods(messages.Message): def not_allowed(self): pass self.assertRaises(messages.MessageDefinitionError, action) def testNestedAttributesNotAllowed(self): """Test that attribute assignment on Message classes are not allowed.""" def int_attribute(): class WithMethods(messages.Message): not_allowed = 1 def string_attribute(): class WithMethods(messages.Message): not_allowed = 'not allowed' def enum_attribute(): class WithMethods(messages.Message): not_allowed = Color.RED for action in (int_attribute, string_attribute, enum_attribute): self.assertRaises(messages.MessageDefinitionError, action) def testNameIsSetOnFields(self): """Make sure name is set on fields after Message class init.""" class HasNamedFields(messages.Message): field = messages.StringField(1) self.assertEquals('field', HasNamedFields.field_by_number(1).name) def testSubclassingMessageDisallowed(self): """Not permitted to create sub-classes of message classes.""" class SuperClass(messages.Message): pass def action(): class SubClass(SuperClass): pass self.assertRaises(messages.MessageDefinitionError, action) def testAllFields(self): """Test all_fields method.""" ComplexMessage = self.CreateMessageClass() fields = list(ComplexMessage.all_fields()) # Order does not matter, so sort now. fields = sorted(fields, lambda f1, f2: cmp(f1.name, f2.name)) self.assertEquals(3, len(fields)) self.assertEquals('a3', fields[0].name) self.assertEquals('b1', fields[1].name) self.assertEquals('c2', fields[2].name) def testFieldByName(self): """Test getting field by name.""" ComplexMessage = self.CreateMessageClass() self.assertEquals(3, ComplexMessage.field_by_name('a3').number) self.assertEquals(1, ComplexMessage.field_by_name('b1').number) self.assertEquals(2, ComplexMessage.field_by_name('c2').number) self.assertRaises(KeyError, ComplexMessage.field_by_name, 'unknown') def testFieldByNumber(self): """Test getting field by number.""" ComplexMessage = self.CreateMessageClass() self.assertEquals('a3', ComplexMessage.field_by_number(3).name) self.assertEquals('b1', ComplexMessage.field_by_number(1).name) self.assertEquals('c2', ComplexMessage.field_by_number(2).name) self.assertRaises(KeyError, ComplexMessage.field_by_number, 4) def testGetAssignedValue(self): """Test getting the assigned value of a field.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertEquals(None, message.get_assigned_value('a_value')) message.a_value = u'a string' self.assertEquals(u'a string', message.get_assigned_value('a_value')) message.a_value = u'a default' self.assertEquals(u'a default', message.get_assigned_value('a_value')) self.assertRaisesWithRegexpMatch( AttributeError, 'Message SomeMessage has no field no_such_field', message.get_assigned_value, 'no_such_field') def testReset(self): """Test resetting a field value.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertRaises(AttributeError, message.reset, 'unknown') self.assertEquals(u'a default', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) message.a_value = u'a new value' self.assertEquals(u'a new value', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) def testAllowNestedEnums(self): """Test allowing nested enums in a message definition.""" class Trade(messages.Message): class Duration(messages.Enum): GTC = 1 DAY = 2 class Currency(messages.Enum): USD = 1 GBP = 2 INR = 3 # Sorted by name order seems to be the only feasible option. self.assertEquals(['Currency', 'Duration'], Trade.__enums__) # Message definition will now be set on Enumerated objects. self.assertEquals(Trade, Trade.Duration.message_definition()) def testAllowNestedMessages(self): """Test allowing nested messages in a message definition.""" class Trade(messages.Message): class Lot(messages.Message): pass class Agent(messages.Message): pass # Sorted by name order seems to be the only feasible option. self.assertEquals(['Agent', 'Lot'], Trade.__messages__) self.assertEquals(Trade, Trade.Agent.message_definition()) self.assertEquals(Trade, Trade.Lot.message_definition()) # But not Message itself. def action(): class Trade(messages.Message): NiceTry = messages.Message self.assertRaises(messages.MessageDefinitionError, action) def testDisallowClassAssignments(self): """Test setting class attributes may not happen.""" class MyMessage(messages.Message): pass self.assertRaises(AttributeError, setattr, MyMessage, 'x', 'do not assign') def testEquality(self): """Test message class equality.""" # Comparison against enums must work. class MyEnum(messages.Enum): val1 = 1 val2 = 2 # Comparisons against nested messages must work. class AnotherMessage(messages.Message): string = messages.StringField(1) class MyMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.EnumField(MyEnum, 2) field3 = messages.MessageField(AnotherMessage, 3) message1 = MyMessage() self.assertNotEquals('hi', message1) self.assertNotEquals(AnotherMessage(), message1) self.assertEquals(message1, message1) message2 = MyMessage() self.assertEquals(message1, message2) message1.field1 = 10 self.assertNotEquals(message1, message2) message2.field1 = 20 self.assertNotEquals(message1, message2) message2.field1 = 10 self.assertEquals(message1, message2) message1.field2 = MyEnum.val1 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val2 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val1 self.assertEquals(message1, message2) message1.field3 = AnotherMessage() message1.field3.string = 'value1' self.assertNotEquals(message1, message2) message2.field3 = AnotherMessage() message2.field3.string = 'value2' self.assertNotEquals(message1, message2) message2.field3.string = 'value1' self.assertEquals(message1, message2) def testRepr(self): """Test represtation of Message object.""" class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) unassigned = messages.StringField(3) unassigned_with_default = messages.StringField(4, default=u'a default') my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' self.assertEquals("<MyMessage\n integer_value: 42\n" " string_value: u'A string'>", repr(my_message)) def testValidation(self): """Test validation of message values.""" # Test optional. class SubMessage(messages.Message): pass class Message(messages.Message): val = messages.MessageField(SubMessage, 1) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test required. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, required=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test repeated. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, repeated=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <SubMessage>", setattr, message, 'val', SubMessage()) message.val = [SubMessage()] message_field.validate(message) def testDefinitionName(self): """Test message name.""" class MyMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) def testDefinitionName_OverrideModule(self): """Test message module is overriden by module package name.""" class MyMessage(messages.Message): pass global package package = 'my.package' try: self.assertEquals('my.package.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for message.""" class MyMessage(messages.Message): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested message names.""" class MyMessage(messages.Message): class NestedMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage.NestedMessage', MyMessage.NestedMessage.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedMessage', MyMessage.NestedMessage.NestedMessage.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterMessage(messages.Message): class InnerMessage(messages.Message): pass self.assertEquals(None, OuterMessage.message_definition()) self.assertEquals(OuterMessage, OuterMessage.InnerMessage.message_definition()) def testConstructorKwargs(self): """Test kwargs via constructor.""" class SomeMessage(messages.Message): name = messages.StringField(1) number = messages.IntegerField(2) expected = SomeMessage() expected.name = 'my name' expected.number = 200 self.assertEquals(expected, SomeMessage(name='my name', number=200)) def testConstructorNotAField(self): """Test kwargs via constructor with wrong names.""" class SomeMessage(messages.Message): pass self.assertRaisesWithRegexpMatch( AttributeError, 'May not assign arbitrary value does_not_exist to message SomeMessage', SomeMessage, does_not_exist=10) def testGetUnsetRepeatedValue(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) instance = SomeMessage() self.assertEquals([], instance.repeated) self.assertTrue(isinstance(instance.repeated, messages.FieldList)) def testCompareAutoInitializedRepeatedFields(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message1 = SomeMessage(repeated=[]) message2 = SomeMessage() self.assertEquals(message1, message2) class FindDefinitionTest(test_util.TestCase): """Test finding definitions relative to various definitions and modules.""" def setUp(self): """Set up module-space. Starts off empty.""" self.modules = {} def DefineModule(self, name): """Define a module and its parents in module space. Modules that are already defined in self.modules are not re-created. Args: name: Fully qualified name of modules to create. Returns: Deepest nested module. For example: DefineModule('a.b.c') # Returns c. """ name_path = name.split('.') full_path = [] for node in name_path: full_path.append(node) full_name = '.'.join(full_path) self.modules.setdefault(full_name, new.module(full_name)) return self.modules[name] def DefineMessage(self, module, name, children={}, add_to_module=True): """Define a new Message class in the context of a module. Used for easily describing complex Message hierarchy. Message is defined including all child definitions. Args: module: Fully qualified name of module to place Message class in. name: Name of Message to define within module. children: Define any level of nesting of children definitions. To define a message, map the name to another dictionary. The dictionary can itself contain additional definitions, and so on. To map to an Enum, define the Enum class separately and map it by name. add_to_module: If True, new Message class is added to module. If False, new Message is not added. """ # Make sure module exists. module_instance = self.DefineModule(module) # Recursively define all child messages. for attribute, value in children.items(): if isinstance(value, dict): children[attribute] = self.DefineMessage( module, attribute, value, False) # Override default __module__ variable. children['__module__'] = module # Instantiate and possibly add to module. message_class = new.classobj(name, (messages.Message,), dict(children)) if add_to_module: setattr(module_instance, name, message_class) return message_class def Importer(self, module, globals='', locals='', fromlist=None): """Importer function. Acts like __import__. Only loads modules from self.modules. Does not try to load real modules defined elsewhere. Does not try to handle relative imports. Args: module: Fully qualified name of module to load from self.modules. """ if fromlist is None: module = module.split('.')[0] try: return self.modules[module] except KeyError: raise ImportError() def testNoSuchModule(self): """Test searching for definitions that do no exist.""" self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'does.not.exist', importer=self.Importer) def testRefersToModule(self): """Test that referring to a module does not return that module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module', importer=self.Importer) def testNoDefinition(self): """Test not finding a definition in an existing module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.MyMessage', importer=self.Importer) def testNotADefinition(self): """Test trying to fetch something that is not a definition.""" module = self.DefineModule('i.am.a.module') setattr(module, 'A', 'a string') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.A', importer=self.Importer) def testGlobalFind(self): """Test finding definitions from fully qualified module names.""" A = self.DefineMessage('a.b.c', 'A', {}) self.assertEquals(A, messages.find_definition('a.b.c.A', importer=self.Importer)) B = self.DefineMessage('a.b.c', 'B', {'C':{}}) self.assertEquals(B.C, messages.find_definition('a.b.c.B.C', importer=self.Importer)) def testRelativeToModule(self): """Test finding definitions relative to modules.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.b') c = self.DefineModule('a.b.c') # Define messages. A = self.DefineMessage('a', 'A') B = self.DefineMessage('a.b', 'B') C = self.DefineMessage('a.b.c', 'C') D = self.DefineMessage('a.b.d', 'D') # Find A, B, C and D relative to a. self.assertEquals(A, messages.find_definition( 'A', a, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.B', a, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.c.C', a, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.d.D', a, importer=self.Importer)) # Find A, B, C and D relative to b. self.assertEquals(A, messages.find_definition( 'A', b, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', b, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'c.C', b, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', b, importer=self.Importer)) # Find A, B, C and D relative to c. Module d is the same case as c. self.assertEquals(A, messages.find_definition( 'A', c, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', c, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', c, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', c, importer=self.Importer)) def testRelativeToMessages(self): """Test finding definitions relative to Message definitions.""" A = self.DefineMessage('a.b', 'A', {'B': {'C': {}, 'D': {}}}) B = A.B C = A.B.C D = A.B.D # Find relative to A. self.assertEquals(A, messages.find_definition( 'A', A, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', A, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'B.C', A, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'B.D', A, importer=self.Importer)) # Find relative to B. self.assertEquals(A, messages.find_definition( 'A', B, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', B, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', B, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', B, importer=self.Importer)) # Find relative to C. self.assertEquals(A, messages.find_definition( 'A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', C, importer=self.Importer)) # Find relative to C searching from c. self.assertEquals(A, messages.find_definition( 'b.A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.A.B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.A.B.C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.A.B.D', C, importer=self.Importer)) def testAbsoluteReference(self): """Test finding absolute definition names.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.a') # Define messages. aA = self.DefineMessage('a', 'A') aaA = self.DefineMessage('a.a', 'A') # Always find a.A. self.assertEquals(aA, messages.find_definition('.a.A', None, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', a, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aA, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aaA, importer=self.Importer)) def testFindEnum(self): """Test that Enums are found.""" class Color(messages.Enum): pass A = self.DefineMessage('a', 'A', {'Color': Color}) self.assertEquals( Color, messages.find_definition('Color', A, importer=self.Importer)) def testFalseScope(self): """Test that Message definitions nested in strange objects are hidden.""" global X class X(object): class A(messages.Message): pass self.assertRaises(TypeError, messages.find_definition, 'A', X) self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'X.A', sys.modules[__name__]) def testSearchAttributeFirst(self): """Make sure not faked out by module, but continues searching.""" A = self.DefineMessage('a', 'A') module_A = self.DefineModule('a.A') self.assertEquals(A, messages.find_definition( 'a.A', None, importer=self.Importer)) class FindDefinitionUnicodeTests(test_util.TestCase): def testUnicodeString(self): """Test using unicode names.""" self.assertEquals('ServiceMapping', messages.find_definition( u'protorpc.registry.ServiceMapping', None).__name__) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate_proto_test.""" import os import shutil import cStringIO import sys import tempfile import unittest from protorpc import descriptor from protorpc import generate_proto from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate_proto class FormatProtoFileTest(test_util.TestCase): def setUp(self): self.file_descriptor = descriptor.FileDescriptor() self.output = cStringIO.StringIO() @property def result(self): return self.output.getvalue() def MakeMessage(self, name='MyMessage', fields=[]): message = descriptor.MessageDescriptor() message.name = name message.fields = fields messages_list = getattr(self.file_descriptor, 'fields', []) messages_list.append(message) self.file_descriptor.message_types = messages_list def testBlankPackage(self): self.file_descriptor.package = None generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('', self.result) def testEmptyPackage(self): self.file_descriptor.package = 'my_package' generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('package my_package;\n', self.result) def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '10' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1 [default=10];\n' '}\n', self.result) def testRepeatedFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '[10, 20]' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' repeated int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefaultString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = 'hello' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default='hello'];\n" '}\n', self.result) def testSingleFieldWithDefaultEmptyString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = '' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default=''];\n" '}\n', self.result) def testSingleFieldWithDefaultMessage(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'MyNestedMessage' field.default_value = 'not valid' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional MyNestedMessage message_field = 1;\n" '}\n', self.result) def testSingleFieldWithDefaultEnum(self): field = descriptor.FieldDescriptor() field.name = 'enum_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.MyEnum' field.default_value = '17' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional my_package.MyEnum enum_field = 1 " "[default=17];\n" '}\n', self.result) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protojson.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import base64 import sys import unittest from protorpc import messages from protorpc import protojson from protorpc import test_util from django.utils import simplejson class MyMessage(messages.Message): """Test message containing various types.""" class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 class Nested(messages.Message): nested_value = messages.StringField(1) a_string = messages.StringField(2) an_integer = messages.IntegerField(3) a_float = messages.FloatField(4) a_boolean = messages.BooleanField(5) an_enum = messages.EnumField(Color, 6) a_nested = messages.MessageField(Nested, 7) a_repeated = messages.IntegerField(8, repeated=True) class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protojson # TODO(rafek): Convert this test to the compliance test in test_util. class ProtojsonTest(test_util.TestCase, test_util.ProtoConformanceTestBase): """Test JSON encoding and decoding.""" PROTOLIB = protojson def CompareEncoded(self, expected_encoded, actual_encoded): """JSON encoding will be laundered to remove string differences.""" self.assertEquals(simplejson.loads(expected_encoded), simplejson.loads(actual_encoded)) encoded_empty_message = '{}' encoded_partial = """{ "double_value": 1.23, "int64_value": -100000000000, "int32_value": 1020, "string_value": "a string", "enum_value": "VAL2" } """ encoded_full = """{ "double_value": 1.23, "float_value": -2.5, "int64_value": -100000000000, "uint64_value": 102020202020, "int32_value": 1020, "bool_value": true, "string_value": "a string\u044f", "bytes_value": "YSBieXRlc//+", "enum_value": "VAL2" } """ encoded_repeated = """{ "double_value": [1.23, 2.3], "float_value": [-2.5, 0.5], "int64_value": [-100000000000, 20], "uint64_value": [102020202020, 10], "int32_value": [1020, 718], "bool_value": [true, false], "string_value": ["a string\u044f", "another string"], "bytes_value": ["YSBieXRlc//+", "YW5vdGhlciBieXRlcw=="], "enum_value": ["VAL2", "VAL1"] } """ encoded_nested = """{ "nested": { "a_value": "a string" } } """ encoded_repeated_nested = """{ "repeated_nested": [{"a_value": "a string"}, {"a_value": "another string"}] } """ unexpected_tag_message = '{"unknown": "value"}' encoded_default_assigned = '{"a_value": "a default"}' encoded_nested_empty = '{"nested": {}}' encoded_repeated_nested_empty = '{"repeated_nested": [{}, {}]}' encoded_extend_message = '{"int64_value": [400, 50, 6000]}' encoded_string_types = '{"string_value": "Latin"}' def testConvertIntegerToFloat(self): """Test that integers passed in to float fields are converted. This is necessary because JSON outputs integers for numbers with 0 decimals. """ message = protojson.decode_message(MyMessage, '{"a_float": 10}') self.assertTrue(isinstance(message.a_float, float)) self.assertEquals(10.0, message.a_float) def testWrongTypeAssignment(self): """Test when wrong type is assigned to a field.""" self.assertRaises(messages.ValidationError, protojson.decode_message, MyMessage, '{"a_string": 10}') def testNumericEnumeration(self): """Test that numbers work for enum values.""" message = protojson.decode_message(MyMessage, '{"an_enum": 2}') expected_message = MyMessage() expected_message.an_enum = MyMessage.Color.GREEN self.assertEquals(expected_message, message) def testNullValues(self): """Test that null values overwrite existing values.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, ('{"an_integer": null,' ' "a_nested": null' '}'))) def testEmptyList(self): """Test that empty lists are ignored.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, '{"a_repeated": []}')) def testNotJSON(self): """Test error when string is not valid JSON.""" self.assertRaises(ValueError, protojson.decode_message, MyMessage, '{this is not json}') def testDoNotEncodeStrangeObjects(self): """Test trying to encode a strange object. The main purpose of this test is to complete coverage. It ensures that the default behavior of the JSON encoder is preserved when someone tries to serialized an unexpected type. """ class BogusObject(object): def check_initialized(self): pass self.assertRaises(TypeError, protojson.encode_message, BogusObject()) def testMergeEmptyString(self): """Test merging the empty or space only string.""" message = protojson.decode_message(test_util.OptionalMessage, '') self.assertEquals(test_util.OptionalMessage(), message) message = protojson.decode_message(test_util.OptionalMessage, ' ') self.assertEquals(test_util.OptionalMessage(), message) class InvalidJsonModule(object): pass class ValidJsonModule(object): class JSONEncoder(object): pass class TestJsonDependencyLoading(test_util.TestCase): """Test loading various implementations of json.""" def setUp(self): """Save original import function.""" self.django_simplejson = sys.modules.pop('django.utils.simplejson', None) self.simplejson = sys.modules.pop('simplejson', None) self.json = sys.modules.pop('json', None) self.original_import = __builtins__.__import__ def block_all_jsons(name, *args, **kwargs): if 'json' in name: if name in sys.modules: module = sys.modules[name] module.name = name return module raise ImportError('Unable to find %s' % name) else: return self.original_import(name, *args, **kwargs) __builtins__.__import__ = block_all_jsons def tearDown(self): """Restore original import functions and any loaded modules.""" __builtins__.__import__ = self.original_import def reset_module(name, module): if module: sys.modules[name] = module else: sys.modules.pop(name, None) reset_module('django.utils.simplejson', self.django_simplejson) reset_module('simplejson', self.simplejson) reset_module('json', self.json) reload(protojson) def testLoadProtojsonWithValidJsonModule(self): """Test loading protojson module with a valid json dependency.""" sys.modules['json'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('json', protojson.json.name) def testLoadProtojsonWithSimplejsonModule(self): """Test loading protojson module with simplejson dependency.""" sys.modules['simplejson'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModule(self): """Loading protojson module with an invalid json defaults to simplejson.""" sys.modules['json'] = InvalidJsonModule sys.modules['simplejson'] = ValidJsonModule # Ignore bad module and default back to simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModuleAndNoSimplejson(self): """Loading protojson module with invalid json and no simplejson.""" sys.modules['json'] = InvalidJsonModule # Bad module without simplejson back raises errors. self.assertRaisesWithRegexpMatch( ImportError, 'json library "json" is not compatible with ProtoRPC', reload, protojson) def testLoadProtojsonWithNoJsonModules(self): """Loading protojson module with invalid json and no simplejson.""" # No json modules raise the first exception. self.assertRaisesWithRegexpMatch( ImportError, 'Unable to find json', reload, protojson) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import sys import unittest from protorpc import generate from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate class IndentWriterTest(test_util.TestCase): def setUp(self): self.out = cStringIO.StringIO() self.indent_writer = generate.IndentWriter(self.out) def testWriteLine(self): self.indent_writer.write_line('This is a line') self.indent_writer.write_line('This is another line') self.assertEquals('This is a line\n' 'This is another line\n', self.out.getvalue()) def testLeftShift(self): self.run_count = 0 def mock_write_line(line): self.run_count += 1 self.assertEquals('same as calling write_line', line) self.indent_writer.write_line = mock_write_line self.indent_writer << 'same as calling write_line' self.assertEquals(1, self.run_count) def testIndentation(self): self.indent_writer << 'indent 0' self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.indent_writer.end_indent() self.indent_writer << 'end 2' self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testAltIndentation(self): self.indent_writer = generate.IndentWriter(self.out, indent_space=3) self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 2' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertEquals(0, self.indent_writer.indent_level) self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals(0, self.indent_writer.indent_level) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testIndent(self): self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) def indent1(): self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) def indent2(): self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) test_util.do_with(self.indent_writer.indent(), indent2) self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer << 'end 2' test_util.do_with(self.indent_writer.indent(), indent1) self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer << 'end 1' self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """JSON support for message types. Public classes: MessageJSONEncoder: JSON encoder for message objects. Public functions: encode_message: Encodes a message in to a JSON string. decode_message: Merge from a JSON string in to a message. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import base64 import logging from protorpc import messages __all__ = [ 'CONTENT_TYPE', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/json' def _load_json_module(): """Try to load a valid json module. There are more than one json modules that might be installed. They are mostly compatible with one another but some versions may be different. This function attempts to load various json modules in a preferred order. It does a basic check to guess if a loaded version of json is compatible. Returns: Comptable json module. Raises: ImportError if there are no json modules or the loaded json module is not compatible with ProtoRPC. """ first_import_error = None for module_name in ['json', 'simplejson', 'django.utils.simplejson']: try: module = __import__(module_name, {}, {}, 'json') if not hasattr(module, 'JSONEncoder'): message = ('json library "%s" is not compatible with ProtoRPC' % module_name) logging.warning(message) raise ImportError(message) else: return module except ImportError, err: if not first_import_error: first_import_error = err logging.error('Must use valid json library (Python 2.6 json, simplejson or ' 'django.utils.simplejson)') raise first_import_error json = _load_json_module() class _MessageJSONEncoder(json.JSONEncoder): """Message JSON encoder class. Extension of JSONEncoder that can build JSON from a message object. """ def default(self, value): """Return dictionary instance from a message object. Args: value: Value to get dictionary for. If not encodable, will call superclasses default method. """ if isinstance(value, messages.Enum): return str(value) if isinstance(value, messages.Message): result = {} for field in value.all_fields(): item = value.get_assigned_value(field.name) if item not in (None, [], ()): if isinstance(field, messages.BytesField): if field.repeated: item = [base64.b64encode(i) for i in item] else: item = base64.b64encode(item) result[field.name] = item return result else: return super(_MessageJSONEncoder, self).default(value) def encode_message(message): """Encode Message instance to JSON string. Args: Message instance to encode in to JSON string. Returns: String encoding of Message instance in protocol JSON format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() return json.dumps(message, cls=_MessageJSONEncoder) def decode_message(message_type, encoded_message): """Merge JSON structure to Message instance. Args: message_type: Message to decode data to. encoded_message: JSON encoded version of message. Returns: Decoded instance of message_type. Raises: ValueError: If encoded_message is not valid JSON. messages.ValidationError if merged message is not initialized. """ if not encoded_message.strip(): return message_type() dictionary = json.loads(encoded_message) def decode_dictionary(message_type, dictionary): """Merge dictionary in to message. Args: message: Message to merge dictionary in to. dictionary: Dictionary to extract information from. Dictionary is as parsed from JSON. Nested objects will also be dictionaries. """ message = message_type() for key, value in dictionary.iteritems(): if value is None: message.reset(key) continue try: field = message.field_by_name(key) except KeyError: # TODO(rafek): Support saving unknown values. continue # Normalize values in to a list. if isinstance(value, list): if not value: continue else: value = [value] valid_value = [] for item in value: if isinstance(field, messages.EnumField): item = field.type(item) elif isinstance(field, messages.BytesField): item = base64.b64decode(item) elif isinstance(field, messages.MessageField): item = decode_dictionary(field.type, item) elif (isinstance(field, messages.FloatField) and isinstance(item, (int, long))): item = float(item) valid_value.append(item) if field.repeated: existing_value = getattr(message, field.name) setattr(message, field.name, valid_value) else: setattr(message, field.name, valid_value[-1]) return message message = decode_dictionary(message_type, dictionary) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Main module for ProtoRPC package.""" __author__ = 'rafek@google.com (Rafe Kaplan)'
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.descriptor.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import unittest from protorpc import descriptor from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util RUSSIA = u'\u0420\u043e\u0441\u0441\u0438\u044f' class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = descriptor class DescribeEnumValueTest(test_util.TestCase): def testDescribe(self): class MyEnum(messages.Enum): MY_NAME = 10 expected = descriptor.EnumValueDescriptor() expected.name = 'MY_NAME' expected.number = 10 described = descriptor.describe_enum_value(MyEnum.MY_NAME) described.check_initialized() self.assertEquals(expected, described) class DescribeEnumTest(test_util.TestCase): def testEmptyEnum(self): class EmptyEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'EmptyEnum' described = descriptor.describe_enum(EmptyEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MyScope(messages.Message): class NestedEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'NestedEnum' described = descriptor.describe_enum(MyScope.NestedEnum) described.check_initialized() self.assertEquals(expected, described) def testEnumWithItems(self): class EnumWithItems(messages.Enum): A = 3 B = 1 C = 2 expected = descriptor.EnumDescriptor() expected.name = 'EnumWithItems' a = descriptor.EnumValueDescriptor() a.name = 'A' a.number = 3 b = descriptor.EnumValueDescriptor() b.name = 'B' b.number = 1 c = descriptor.EnumValueDescriptor() c.name = 'C' c.number = 2 expected.values = [b, c, a] described = descriptor.describe_enum(EnumWithItems) described.check_initialized() self.assertEquals(expected, described) class DescribeFieldTest(test_util.TestCase): def testLabel(self): for repeated, required, expected_label in ( (True, False, descriptor.FieldDescriptor.Label.REPEATED), (False, True, descriptor.FieldDescriptor.Label.REQUIRED), (False, False, descriptor.FieldDescriptor.Label.OPTIONAL)): field = messages.IntegerField(10, required=required, repeated=repeated) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = expected_label expected.variant = descriptor.FieldDescriptor.Variant.INT64 described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault(self): for field_class, default, expected_default in ( (messages.IntegerField, 200, '200'), (messages.FloatField, 1.5, '1.5'), (messages.FloatField, 1e6, '1000000.0'), (messages.BooleanField, True, 'true'), (messages.BooleanField, False, 'false'), (messages.BytesField, 'ab\xF1', 'ab\\xf1'), (messages.StringField, RUSSIA, RUSSIA), ): field = field_class(10, default=default) field.name = u'a_field' expected = descriptor.FieldDescriptor() expected.name = u'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = field_class.DEFAULT_VARIANT expected.default_value = expected_default described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault_EnumField(self): class MyEnum(messages.Enum): VAL = 1 field = messages.EnumField(MyEnum, 10, default=MyEnum.VAL) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.EnumField.DEFAULT_VARIANT expected.type_name = 'descriptor_test.MyEnum' expected.default_value = '1' described = descriptor.describe_field(field) self.assertEquals(expected, described) def testMessageField(self): field = messages.MessageField(descriptor.FieldDescriptor, 10) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.MessageField.DEFAULT_VARIANT expected.type_name = ('protorpc.descriptor.FieldDescriptor') described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) class DescribeMessageTest(test_util.TestCase): def testEmptyDefinition(self): class MyMessage(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MyMessage' described = descriptor.describe_message(MyMessage) described.check_initialized() self.assertEquals(expected, described) def testDefinitionWithFields(self): class MessageWithFields(messages.Message): field1 = messages.IntegerField(10) field2 = messages.StringField(30) field3 = messages.IntegerField(20) expected = descriptor.MessageDescriptor() expected.name = 'MessageWithFields' expected.fields = [ descriptor.describe_field(MessageWithFields.field_by_name('field1')), descriptor.describe_field(MessageWithFields.field_by_name('field3')), descriptor.describe_field(MessageWithFields.field_by_name('field2')), ] described = descriptor.describe_message(MessageWithFields) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MessageWithEnum(messages.Message): class Mood(messages.Enum): GOOD = 1 BAD = 2 UGLY = 3 class Music(messages.Enum): CLASSIC = 1 JAZZ = 2 BLUES = 3 expected = descriptor.MessageDescriptor() expected.name = 'MessageWithEnum' expected.enum_types = [descriptor.describe_enum(MessageWithEnum.Mood), descriptor.describe_enum(MessageWithEnum.Music)] described = descriptor.describe_message(MessageWithEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedMessage(self): class MessageWithMessage(messages.Message): class Nesty(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MessageWithMessage' expected.message_types = [ descriptor.describe_message(MessageWithMessage.Nesty)] described = descriptor.describe_message(MessageWithMessage) described.check_initialized() self.assertEquals(expected, described) class DescribeMethodTest(test_util.TestCase): """Test describing remote methods.""" def testDescribe(self): class Request(messages.Message): pass class Response(messages.Message): pass @remote.method(Request, Response) def remote_method(request): pass expected = descriptor.MethodDescriptor() expected.name = 'remote_method' expected.request_type = 'descriptor_test.Request' expected.response_type = 'descriptor_test.Response' described = descriptor.describe_method(remote_method) described.check_initialized() self.assertEquals(expected, described) class DescribeServiceTest(test_util.TestCase): """Test describing service classes.""" def testDescribe(self): class Request1(messages.Message): pass class Response1(messages.Message): pass class Request2(messages.Message): pass class Response2(messages.Message): pass class MyService(remote.Service): @remote.method(Request1, Response1) def method1(self, request): pass @remote.method(Request2, Response2) def method2(self, request): pass expected = descriptor.ServiceDescriptor() expected.name = 'MyService' expected.methods = [] expected.methods.append(descriptor.describe_method(MyService.method1)) expected.methods.append(descriptor.describe_method(MyService.method2)) described = descriptor.describe_service(MyService) described.check_initialized() self.assertEquals(expected, described) class DescribeFileTest(test_util.TestCase): """Test describing modules.""" def LoadModule(self, module_name, source): result = {'__name__': module_name, 'messages': messages, 'remote': remote, } exec source in result module = new.module(module_name) for name, value in result.iteritems(): setattr(module, name, value) return module def testEmptyModule(self): """Test describing an empty file.""" module = new.module('my.package.name') expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testNoPackageName(self): """Test describing a module with no module name.""" module = new.module('') expected = descriptor.FileDescriptor() described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testPackageName(self): """Test using the 'package' module attribute.""" module = new.module('my.module.name') module.package = 'my.package.name' expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testMessages(self): """Test that messages are described.""" module = self.LoadModule('my.package', 'class Message1(messages.Message): pass\n' 'class Message2(messages.Message): pass\n') message1 = descriptor.MessageDescriptor() message1.name = 'Message1' message2 = descriptor.MessageDescriptor() message2.name = 'Message2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.message_types = [message1, message2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testEnums(self): """Test that enums are described.""" module = self.LoadModule('my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testServices(self): """Test that services are described.""" module = self.LoadModule('my.package', 'class Service1(remote.Service): pass\n' 'class Service2(remote.Service): pass\n') service1 = descriptor.ServiceDescriptor() service1.name = 'Service1' service2 = descriptor.ServiceDescriptor() service2.name = 'Service2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.service_types = [service1, service2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) class DescribeFileSetTest(test_util.TestCase): """Test describing multiple modules.""" def testNoModules(self): """Test what happens when no modules provided.""" described = descriptor.describe_file_set([]) described.check_initialized() # The described FileSet.files will be None. self.assertEquals(descriptor.FileSet(), described) def testWithModules(self): """Test what happens when no modules provided.""" modules = [new.module('package1'), new.module('package1')] file1 = descriptor.FileDescriptor() file1.package = 'package1' file2 = descriptor.FileDescriptor() file2.package = 'package2' expected = descriptor.FileSet() expected.files = [file1, file1] described = descriptor.describe_file_set(modules) described.check_initialized() self.assertEquals(expected, described) class DescribeTest(test_util.TestCase): def testModule(self): self.assertEquals(descriptor.describe_file(test_util), descriptor.describe(test_util)) def testMethod(self): class Param(messages.Message): pass class Service(remote.Service): @remote.method(Param, Param) def fn(self): return Param() self.assertEquals(descriptor.describe_method(Service.fn), descriptor.describe(Service.fn)) def testField(self): self.assertEquals( descriptor.describe_field(test_util.NestedMessage.a_value), descriptor.describe(test_util.NestedMessage.a_value)) def testEnumValue(self): self.assertEquals( descriptor.describe_enum_value( test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.describe(test_util.OptionalMessage.SimpleEnum.VAL1)) def testMessage(self): self.assertEquals(descriptor.describe_message(test_util.NestedMessage), descriptor.describe(test_util.NestedMessage)) def testEnum(self): self.assertEquals( descriptor.describe_enum(test_util.OptionalMessage.SimpleEnum), descriptor.describe(test_util.OptionalMessage.SimpleEnum)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testUndescribable(self): class NonService(object): def fn(self): pass for value in (NonService, NonService.fn, 1, 'string', 1.2, None): self.assertEquals(None, descriptor.describe(value)) class ModuleFinderTest(test_util.TestCase): def testFindModule(self): self.assertEquals(descriptor.describe_file(registry), descriptor.import_descriptor_loader('protorpc.registry')) def testFindMessage(self): self.assertEquals( descriptor.describe_message(descriptor.FileSet), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet')) def testFindField(self): self.assertEquals( descriptor.describe_field(descriptor.FileSet.files), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet.files')) def testFindEnumValue(self): self.assertEquals( descriptor.describe_enum_value(test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.import_descriptor_loader( 'protorpc.test_util.OptionalMessage.SimpleEnum.VAL1')) def testFindMethod(self): self.assertEquals( descriptor.describe_method(registry.RegistryService.services), descriptor.import_descriptor_loader( 'protorpc.registry.RegistryService.services')) def testFindService(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('protorpc.registry.RegistryService')) def testFindWithAbsoluteName(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('.protorpc.registry.RegistryService')) def testFindWrongThings(self): for name in ('a', 'protorpc.registry.RegistryService.__init__', '', ): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, descriptor.import_descriptor_loader, name) class DescriptorLibraryTest(test_util.TestCase): def setUp(self): self.packageless = descriptor.MessageDescriptor() self.packageless.name = 'Packageless' self.library = descriptor.DescriptorLibrary( descriptors={ 'not.real.Packageless': self.packageless, 'Packageless': self.packageless, }) def testLookupPackage(self): self.assertEquals('csv', self.library.lookup_package('csv')) self.assertEquals('protorpc', self.library.lookup_package('protorpc')) self.assertEquals('protorpc.registry', self.library.lookup_package('protorpc.registry')) self.assertEquals('protorpc.registry', self.library.lookup_package('.protorpc.registry')) self.assertEquals( 'protorpc.registry', self.library.lookup_package('protorpc.registry.RegistryService')) self.assertEquals( 'protorpc.registry', self.library.lookup_package( 'protorpc.registry.RegistryService.services')) def testLookupNonPackages(self): for name in ('', 'a', 'protorpc.descriptor.DescriptorLibrary'): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, self.library.lookup_package, name) def testNoPackage(self): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for not.real', self.library.lookup_package, 'not.real.Packageless') self.assertEquals(None, self.library.lookup_package('Packageless')) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stand-alone implementation of in memory protocol messages. Public Classes: Enum: Represents an enumerated type. Variant: Hint for wire format to determine how to serialize. Message: Base class for user defined messages. IntegerField: Field for integer values. FloatField: Field for float values. BooleanField: Field for boolean values. BytesField: Field for binary string values. StringField: Field for UTF-8 string values. MessageField: Field for other message type values. EnumField: Field for enumerated type values. Public Exceptions (indentation indications class hierarchy): EnumDefinitionError: Raised when enumeration is incorrectly defined. FieldDefinitionError: Raised when field is incorrectly defined. InvalidVariantError: Raised when variant is not compatible with field type. InvalidDefaultError: Raised when default is not compatiable with field. InvalidNumberError: Raised when field number is out of range or reserved. MessageDefinitionError: Raised when message is incorrectly defined. DuplicateNumberError: Raised when field has duplicate number with another. ValidationError: Raised when a message or field is not valid. DefinitionNotFoundError: Raised when definition not found. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import inspect import os import sys import traceback import types import weakref from protorpc import util __all__ = ['MAX_ENUM_VALUE', 'MAX_FIELD_NUMBER', 'FIRST_RESERVED_FIELD_NUMBER', 'LAST_RESERVED_FIELD_NUMBER', 'Enum', 'Field', 'FieldList', 'Variant', 'Message', 'IntegerField', 'FloatField', 'BooleanField', 'BytesField', 'StringField', 'MessageField', 'EnumField', 'find_definition', 'Error', 'DecodeError', 'EnumDefinitionError', 'FieldDefinitionError', 'InvalidVariantError', 'InvalidDefaultError', 'InvalidNumberError', 'MessageDefinitionError', 'DuplicateNumberError', 'ValidationError', 'DefinitionNotFoundError', ] # TODO(rafek): Add extended module test to ensure all exceptions # in services extends Error. Error = util.Error class EnumDefinitionError(Error): """Enumeration definition error.""" class FieldDefinitionError(Error): """Field definition error.""" class InvalidVariantError(FieldDefinitionError): """Invalid variant provided to field.""" class InvalidDefaultError(FieldDefinitionError): """Invalid default provided to field.""" class InvalidNumberError(FieldDefinitionError): """Invalid number provided to field.""" class MessageDefinitionError(Error): """Enumeration definition error.""" class DuplicateNumberError(Error): """Duplicate number assigned to field.""" class DefinitionNotFoundError(Error): """Raised when definition is not found.""" class DecodeError(Error): """Error found decoding message from encoded form.""" class ValidationError(Error): """Invalid value for message error.""" def __str__(self): """Prints string with field name if present on exception.""" message = Error.__str__(self) try: field_name = self.field_name except AttributeError: return message else: return 'Field %s: %s' % (field_name, message) # Attributes that are reserved by a class definition that # may not be used by either Enum or Message class definitions. _RESERVED_ATTRIBUTE_NAMES = frozenset( ['__module__', '__doc__']) _POST_INIT_FIELD_ATTRIBUTE_NAMES = frozenset( ['name', '_message_definition', '_MessageField__type', '_EnumField__type', '_EnumField__resolved_default']) _POST_INIT_ATTRIBUTE_NAMES = frozenset( ['_message_definition']) # Maximum enumeration value as defined by the protocol buffers standard. # All enum values must be less than or equal to this value. MAX_ENUM_VALUE = (2 ** 29) - 1 # Maximum field number as defined by the protocol buffers standard. # All field numbers must be less than or equal to this value. MAX_FIELD_NUMBER = (2 ** 29) - 1 # Field numbers between 19000 and 19999 inclusive are reserved by the # protobuf protocol and may not be used by fields. FIRST_RESERVED_FIELD_NUMBER = 19000 LAST_RESERVED_FIELD_NUMBER = 19999 class _DefinitionClass(type): """Base meta-class used for definition meta-classes. The Enum and Message definition classes share some basic functionality. Both of these classes may be contained by a Message definition. After initialization, neither class may have attributes changed except for the protected _message_definition attribute, and that attribute may change only once. """ __initialized = False def __init__(cls, name, bases, dct): """Constructor.""" type.__init__(cls, name, bases, dct) # Base classes may never be initialized. if cls.__bases__ != (object,): cls.__initialized = True def message_definition(cls): """Get outer Message definition that contains this definition. Returns: Containing Message definition if definition is contained within one, else None. """ try: return cls._message_definition() except AttributeError: return None def __setattr__(cls, name, value): """Overridden so that cannot set variables on definition classes after init. Setting attributes on a class must work during the period of initialization to set the enumation value class variables and build the name/number maps. Once __init__ has set the __initialized flag to True prohibits setting any more values on the class. The class is in effect frozen. Args: name: Name of value to set. value: Value to set. """ if cls.__initialized and name not in _POST_INIT_ATTRIBUTE_NAMES: raise AttributeError('May not change values: %s' % name) else: type.__setattr__(cls, name, value) def __delattr__(cls, name): """Overridden so that cannot delete varaibles on definition classes.""" raise TypeError('May not delete attributes on definition class') def definition_name(cls): """Helper method for creating definition name. Names will be generated to include the classes package name, scope (if the class is nested in another definition) and class name. By default, the package name for a definition is derived from its module name. However, this value can be overriden by placing a 'package' attribute in the module that contains the definition class. For example: package = 'some.alternate.package' class MyMessage(Message): ... >>> MyMessage.definition_name() some.alternate.package.MyMessage Returns: Dot-separated fully qualified name of definition. """ outer_definition = cls.message_definition() if not outer_definition: definition_module = sys.modules.get(cls.__module__, None) if definition_module: try: package = definition_module.package except AttributeError: package = definition_module.__name__ if package == '__main__': try: file_name = definition_module.__file__ except AttributeError: pass else: base_name = os.path.basename(file_name) package = '.'.join(base_name.split('.')[:-1]) else: return unicode(cls.__name__) else: return u'%s.%s' % (outer_definition.definition_name(), cls.__name__) return u'%s.%s' % (package, cls.__name__) class _EnumClass(_DefinitionClass): """Meta-class used for defining the Enum base class. Meta-class enables very specific behavior for any defined Enum class. All attributes defined on an Enum sub-class must be integers. Each attribute defined on an Enum sub-class is translated into an instance of that sub-class, with the name of the attribute as its name, and the number provided as its value. It also ensures that only one level of Enum class hierarchy is possible. In other words it is not possible to delcare sub-classes of sub-classes of Enum. This class also defines some functions in order to restrict the behavior of the Enum class and its sub-classes. It is not possible to change the behavior of the Enum class in later classes since any new classes may be defined with only integer values, and no methods. """ def __init__(cls, name, bases, dct): # Can only define one level of sub-classes below Enum. if not (bases == (object,) or bases == (Enum,)): raise EnumDefinitionError('Enum type %s may only inherit from Enum' % (name,)) cls.__by_number = {} cls.__by_name = {} # Enum base class does not need to be initialized or locked. if bases != (object,): # Replace integer with number. for attribute, value in dct.iteritems(): # Module will be in every enum class. if attribute in _RESERVED_ATTRIBUTE_NAMES: continue # Reject anything that is not an int. if not isinstance(value, (int, long)): raise EnumDefinitionError( 'May only use integers in Enum definitions. Found: %s = %s' % (attribute, value)) # Protocol buffer standard recommends non-negative values. # Reject negative values. if value < 0: raise EnumDefinitionError( 'Must use non-negative enum values. Found: %s = %d' % (attribute, value)) if value > MAX_ENUM_VALUE: raise EnumDefinitionError( 'Must use enum values less than or equal %d. Found: %s = %d' % (MAX_ENUM_VALUE, attribute, value)) if value in cls.__by_number: raise EnumDefinitionError( 'Value for %s = %d is already defined: %s' % (attribute, value, cls.__by_number[value].name)) # Create enum instance and list in new Enum type. instance = object.__new__(cls) cls.__init__(instance, attribute, value) cls.__by_name[instance.name] = instance cls.__by_number[instance.number] = instance setattr(cls, attribute, instance) _DefinitionClass.__init__(cls, name, bases, dct) def __iter__(cls): """Iterate over all values of enum. Yields: Enumeration instances of the Enum class in arbitrary order. """ return cls.__by_number.itervalues() def names(cls): """Get all names for Enum. Returns: An iterator for names of the enumeration in arbitrary order. """ return cls.__by_name.iterkeys() def numbers(cls): """Get all numbers for Enum. Returns: An iterator for all numbers of the enumeration in arbitrary order. """ return cls.__by_number.iterkeys() def lookup_by_name(cls, name): """Look up Enum by name. Args: name: Name of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_name[name] def lookup_by_number(cls, number): """Look up Enum by number. Args: number: Number of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_number[number] def __len__(cls): return len(cls.__by_name) class Enum(object): """Base class for all enumerated types.""" __metaclass__ = _EnumClass __slots__ = set(('name', 'number')) def __new__(cls, index): """Acts as look-up routine after class is initialized. The purpose of overriding __new__ is to provide a way to treat Enum subclasses as casting types, similar to how the int type functions. A program can pass a string or an integer and this method with "convert" that value in to an appropriate Enum instance. Args: index: Name or number to look up. During initialization this is always the name of the new enum value. Raises: TypeError: When an inappropriate index value is passed provided. """ # If is enum type of this class, return it. if isinstance(index, cls): return index # If number, look up by number. if isinstance(index, (int, long)): try: return cls.lookup_by_number(index) except KeyError: pass # If name, look up by name. if isinstance(index, basestring): try: return cls.lookup_by_name(index) except KeyError: pass raise TypeError('No such value for %s in Enum %s' % (index, cls.__name__)) def __init__(self, name, number=None): """Initialize new Enum instance. Since this should only be called during class initialization any calls that happen after the class is frozen raises an exception. """ # Immediately return if __init__ was called after _Enum.__init__(). # It means that casting operator version of the class constructor # is being used. if getattr(type(self), '_DefinitionClass__initialized'): return object.__setattr__(self, 'name', name) object.__setattr__(self, 'number', number) def __setattr__(self, name, value): raise TypeError('May not change enum values') def __str__(self): return self.name def __int__(self): return self.number def __repr__(self): return '%s(%s, %d)' % (type(self).__name__, self.name, self.number) def __cmp__(self, other): """Order is by number.""" if isinstance(other, type(self)): return cmp(self.number, other.number) return NotImplemented @classmethod def to_dict(cls): """Make dictionary version of enumerated class. Dictionary created this way can be used with def_num and import_enum. Returns: A dict (name) -> number """ return dict((item.name, item.number) for item in iter(cls)) @staticmethod def def_enum(dct, name): """Define enum class from dictionary. Args: dct: Dictionary of enumerated values for type. name: Name of enum. """ return type(name, (Enum,), dct) # TODO(rafek): Determine to what degree this enumeration should be compatible # with FieldDescriptor.Type in: # # http://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto class Variant(Enum): """Wire format variant. Used by the 'protobuf' wire format to determine how to transmit a single piece of data. May be used by other formats. See: http://code.google.com/apis/protocolbuffers/docs/encoding.html Values: DOUBLE: 64-bit floating point number. FLOAT: 32-bit floating point number. INT64: 64-bit signed integer. UINT64: 64-bit unsigned integer. INT32: 32-bit signed integer. BOOL: Boolean value (True or False). STRING: String of UTF-8 encoded text. MESSAGE: Embedded message as byte string. BYTES: String of 8-bit bytes. UINT32: 32-bit unsigned integer. ENUM: Enum value as integer. SINT32: 32-bit signed integer. Uses "zig-zag" encoding. SINT64: 64-bit signed integer. Uses "zig-zag" encoding. """ DOUBLE = 1 FLOAT = 2 INT64 = 3 UINT64 = 4 INT32 = 5 BOOL = 8 STRING = 9 MESSAGE = 11 BYTES = 12 UINT32 = 13 ENUM = 14 SINT32 = 17 SINT64 = 18 class _MessageClass(_DefinitionClass): """Meta-class used for defining the Message base class. For more details about Message classes, see the Message class docstring. Information contained there may help understanding this class. Meta-class enables very specific behavior for any defined Message class. All attributes defined on an Message sub-class must be field instances, Enum class definitions or other Message class definitions. Each field attribute defined on an Message sub-class is added to the set of field definitions and the attribute is translated in to a slot. It also ensures that only one level of Message class hierarchy is possible. In other words it is not possible to declare sub-classes of sub-classes of Message. This class also defines some functions in order to restrict the behavior of the Message class and its sub-classes. It is not possible to change the behavior of the Message class in later classes since any new classes may be defined with only field, Enums and Messages, and no methods. """ def __new__(cls, name, bases, dct): """Create new Message class instance. The __new__ method of the _MessageClass type is overridden so as to allow the translation of Field instances to slots. """ by_number = {} by_name = {} variant_map = {} if bases != (object,): # Can only define one level of sub-classes below Message. if bases != (Message,): raise MessageDefinitionError( 'Message types may only inherit from Message') enums = [] messages = [] # Must not use iteritems because this loop will change the state of dct. for key, field in dct.items(): if key in _RESERVED_ATTRIBUTE_NAMES: continue if isinstance(field, type) and issubclass(field, Enum): enums.append(key) continue if (isinstance(field, type) and issubclass(field, Message) and field is not Message): messages.append(key) continue # Reject anything that is not a field. if type(field) is Field or not isinstance(field, Field): raise MessageDefinitionError( 'May only use fields in message definitions. Found: %s = %s' % (key, field)) if field.number in by_number: raise DuplicateNumberError( 'Field with number %d declared more than once in %s' % (field.number, name)) field.name = key # Place in name and number maps. by_name[key] = field by_number[field.number] = field # Add enums if any exist. if enums: dct['__enums__'] = sorted(enums) # Add messages if any exist. if messages: dct['__messages__'] = sorted(messages) dct['_Message__by_number'] = by_number dct['_Message__by_name'] = by_name return _DefinitionClass.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): """Initializer required to assign references to new class.""" if bases != (object,): for value in dct.itervalues(): if isinstance(value, _DefinitionClass) and not value is Message: value._message_definition = weakref.ref(cls) for field in cls.all_fields(): field._message_definition = weakref.ref(cls) _DefinitionClass.__init__(cls, name, bases, dct) class Message(object): """Base class for user defined message objects. Used to define messages for efficient transmission across network or process space. Messages are defined using the field classes (IntegerField, FloatField, EnumField, etc.). Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class is intentended to itself be transmitted across network or process space and used directly by clients or even other servers. As such methods and non-field attributes could not be transmitted with the structural information causing discrepancies between different languages and implementations. Initialization and validation: A Message object is considered to be initialized if it has all required fields and any nested messages are also initialized. Calling 'check_initialized' will raise a ValidationException if it is not initialized; 'is_initialized' returns a boolean value indicating if it is valid. Validation automatically occurs when Message objects are created and populated. Validation that a given value will be compatible with a field that it is assigned to can be done through the Field instances validate() method. The validate method used on a message will check that all values of a message and its sub-messages are valid. Assingning an invalid value to a field will raise a ValidationException. Example: # Trade type. class TradeType(Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 class Lot(Message): price = IntegerField(1, required=True) quantity = IntegerField(2, required=True) class Order(Message): symbol = StringProperty(1, required=True) total_quantity = IntegerProperty(2, required=True) trade_type = EnumProperty(TradeType, 3, required=True) lots = MessageProperty(Lot, 4, repeated=True) limit = IntegerField(5) order = Order(symbol='GOOG', total_quantity=10, trade_type=TradeType.BUY) lot1 = Lot(price=304, quantity=7) lot2 = Lot(price = 305, quantity=3) order.lots = [lot1, lot2] # Now object is initialized! order.check_initialized() """ __metaclass__ = _MessageClass def __init__(self, **kwargs): """Initialize internal messages state. Args: A message can be initialized via the constructor by passing in keyword arguments corresponding to fields. For example: class Date(Message): day = IntegerField(1) month = IntegerField(2) year = IntegerField(3) Invoking: date = Date(day=6, month=6, year=1911) is the same as doing: date = Date() date.day = 6 date.month = 6 date.year = 1911 """ # Tag being an essential implementation detail must be private. self.__tags = {} assigned = set() for name, value in kwargs.iteritems(): setattr(self, name, value) assigned.add(name) # initialize repeated fields. for field in self.all_fields(): if field.repeated and field.name not in assigned: setattr(self, field.name, []) def check_initialized(self): """Check class for initialization status. Check that all required fields are initialized Raises: ValidationError: If message is not initialized. """ for name, field in self.__by_name.iteritems(): value = getattr(self, name) if value is None: if field.required: raise ValidationError("Message %s is missing required field %s" % (type(self).__name__, name)) else: try: if isinstance(field, MessageField): if field.repeated: for item in value: item.check_initialized() else: value.check_initialized() except ValidationError, err: if not hasattr(err, 'message_name'): err.message_name = type(self).__name__ raise def is_initialized(self): """Get initialization status. Returns: True if message is valid, else False. """ try: self.check_initialized() except ValidationError: return False else: return True @classmethod def all_fields(cls): """Get all field definition objects. Ordering is arbitrary. Returns: Iterator over all values in arbitrary order. """ return cls.__by_name.itervalues() @classmethod def field_by_name(cls, name): """Get field by name. Returns: Field object associated with name. Raises: KeyError if no field found by that name. """ return cls.__by_name[name] @classmethod def field_by_number(cls, number): """Get field by number. Returns: Field object associated with number. Raises: KeyError if no field found by that number. """ return cls.__by_number[number] def get_assigned_value(self, name): """Get the assigned value of an attribute. Get the underlying value of an attribute. If value has not been set, will not return the default for the field. Args: name: Name of attribute to get. Returns: Value of attribute, None if it has not been set. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) return self.__tags.get(field.number) def reset(self, name): """Reset assigned value for field. Resetting a field will return it to its default value or None. Args: name: Name of field to reset. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: if name not in message_type.__by_name: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) self.__tags.pop(field.number, None) def __setattr__(self, name, value): """Change set behavior for messages. Messages may only be assigned values that are fields. Does not try to validate field when set. Args: name: Name of field to assign to. vlaue: Value to assign to field. Raises: AttributeError when trying to assign value that is not a field. """ if name in self.__by_name or name.startswith('_Message__'): object.__setattr__(self, name, value) else: raise AttributeError("May not assign arbitrary value %s " "to message %s" % (name, type(self).__name__)) def __repr__(self): """Make string representation of message. Example: class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' print my_message >>> <MyMessage ... integer_value: 42 ... string_value: u'A string'> Returns: String representation of message, including the values of all fields and repr of all sub-messages. """ body = ['<', type(self).__name__] for field in sorted(self.all_fields(), key=lambda f: f.number): attribute = field.name value = self.get_assigned_value(field.name) if value is not None: body.append('\n %s: %s' % (attribute, repr(value))) body.append('>') return ''.join(body) def __eq__(self, other): """Equality operator. Does field by field comparison with other message. For equality, must be same type and values of all fields must be equal. Messages not required to be initialized for comparison. Does not attempt to determine equality for values that have default values that are not set. In other words: class HasDefault(Message): attr1 = StringField(1, default='default value') message1 = HasDefault() message2 = HasDefault() message2.attr1 = 'default value' message1 != message2 Args: other: Other message to compare with. """ # TODO(rafek): Implement "equivalent" which does comparisons # taking default values in to consideration. if self is other: return True if type(self) is not type(other): return False return self.__tags == other.__tags def __ne__(self, other): """Not equals operator. Does field by field comparison with other message. For non-equality, must be different type or any value of a field must be non-equal to the same field in the other instance. Messages not required to be initialized for comparison. Args: other: Other message to compare with. """ return not self.__eq__(other) class FieldList(list): """List implementation that validates field values. This list implementation overrides all methods that add values in to a list in order to validate those new elements. Attempting to add or set list values that are not of the correct type will raise ValidationError. """ def __init__(self, field_instance, sequence): """Constructor. Args: field_instance: Instance of field that validates the list. sequence: List or tuple to construct list from. """ if not field_instance.repeated: raise FieldDefinitionError('FieldList may only accept repeated fields') self.__field = field_instance self.__field.validate(sequence) list.__init__(self, sequence) @property def field(self): """Field that validates list.""" return self.__field def __setslice__(self, i, j, sequence): """Validate slice assignment to list.""" self.__field.validate(sequence) list.__setslice__(self, i, j, sequence) def __setitem__(self, index, value): """Validate item assignment to list.""" self.__field.validate_element(value) list.__setitem__(self, index, value) def append(self, value): """Validate item appending to list.""" self.__field.validate_element(value) return list.append(self, value) def extend(self, sequence): """Validate extension of list.""" self.__field.validate(sequence) return list.extend(self, sequence) def insert(self, index, value): """Validate item insertion to list.""" self.__field.validate_element(value) return list.insert(self, index, value) # TODO(rafek): Prevent additional field subclasses. class Field(object): __variant_to_type = {} class __metaclass__(type): def __init__(cls, name, bases, dct): getattr(cls, '_Field__variant_to_type').update( (variant, cls) for variant in getattr(cls, 'VARIANTS', [])) type.__init__(cls, name, bases, dct) __initialized = False @util.positional(2) def __init__(self, number, required=False, repeated=False, variant=None, default=None): """Constructor. The required and repeated parameters are mutually exclusive. Setting both to True will raise a FieldDefinitionError. Sub-class Attributes: Each sub-class of Field must define the following: VARIANTS: Set of variant types accepted by that field. DEFAULT_VARIANT: Default variant type if not specified in constructor. Args: number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive with 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive with 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: InvalidVariantError when invalid variant for field is provided. InvalidDefaultError when invalid default for field is provided. FieldDefinitionError when invalid number provided or mutually exclusive fields are used. InvalidNumberError when the field number is out of range or reserved. """ if not isinstance(number, int) or not 1 <= number <= MAX_FIELD_NUMBER: raise InvalidNumberError('Invalid number for field: %s\n' 'Number must be 1 or greater and %d or less' % (number, MAX_FIELD_NUMBER)) if FIRST_RESERVED_FIELD_NUMBER <= number <= LAST_RESERVED_FIELD_NUMBER: raise InvalidNumberError('Tag number %d is a reserved number.\n' 'Numbers %d to %d are reserved' % (number, FIRST_RESERVED_FIELD_NUMBER, LAST_RESERVED_FIELD_NUMBER)) if repeated and required: raise FieldDefinitionError('Cannot set both repeated and required') if variant is None: variant = self.DEFAULT_VARIANT if repeated and default is not None: raise FieldDefinitionError('Repeated fields may not have defaults') if variant not in self.VARIANTS: raise InvalidVariantError( 'Invalid variant: %s\nValid variants for %s are %r' % (variant, type(self).__name__, sorted(self.VARIANTS))) self.number = number self.required = required self.repeated = repeated self.variant = variant if default is not None: try: self.validate_default(default) except ValidationError, err: raise InvalidDefaultError('Invalid default value for field: %s: %s' % (default, err)) self.__default = default self.__initialized = True def __setattr__(self, name, value): """Setter overidden to prevent assignment to fields after creation. Args: name: Name of attribute to set. value: Value to assign. """ # Special case post-init names. They need to be set after constructor. if name in _POST_INIT_FIELD_ATTRIBUTE_NAMES: object.__setattr__(self, name, value) return # All other attributes must be set before __initialized. if not self.__initialized: # Not initialized yet, allow assignment. object.__setattr__(self, name, value) else: raise AttributeError('Field objects are read-only') def __set__(self, message_instance, value): """Set value on message. Args: message_instance: Message instance to set value on. value: Value to set on message. """ # Reaches in to message instance directly to assign to private tags. if value is None: if self.repeated: raise ValidationError( 'May not assign None to repeated field %s' % self.name) else: message_instance._Message__tags.pop(self.number, None) else: if self.repeated: value = FieldList(self, value) else: self.validate(value) message_instance._Message__tags[self.number] = value def __get__(self, message_instance, message_class): if message_instance is None: return self result = message_instance._Message__tags.get(self.number) if result is None: return self.default else: return result def validate_element(self, value): """Validate single element of field. This is different from validate in that it is used on individual values of repeated fields. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ if not isinstance(value, self.type): if value is None: if self.required: raise ValidationError('Required field is missing') else: raise ValidationError('Expected type %s, found %s (type %s)' % (self.type, value, type(value))) def __validate(self, value, validate_element): """Internal validation function. Validate an internal value using a function to validate individual elements. Args: value: Value to validate. validate_element: Function to use to validate individual elements. Raises: ValidationError if value is not expected type. """ if not self.repeated: validate_element(value) else: # Must be a list or tuple, may not be a string. if isinstance(value, (list, tuple)): for element in value: if element is None: raise ValidationError('Repeated values may not be None') validate_element(element) elif value is not None: raise ValidationError('Field is repeated. Found: %s' % value) def validate(self, value): """Validate value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_element) def validate_default_element(self, value): """Validate value as assigned to field default field. Some fields may allow for delayed resolution of default types necessary in the case of circular definition references. In this case, the default value might be a place holder that is resolved when needed after all the message classes are defined. Args: value: Default value to validate. Raises: ValidationError if value is not expected type. """ self.validate_element(value) def validate_default(self, value): """Validate default value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_default_element) def message_definition(self): """Get Message definition that contains this Field definition. Returns: Containing Message definition for Field. Will return None if for some reason Field is defined outside of a Message class. """ try: return self._message_definition() except AttributeError: return None @property def default(self): """Get default value for field.""" return self.__default @classmethod def lookup_field_type_by_variant(cls, variant): return cls.__variant_to_type[variant] class IntegerField(Field): """Field definition for integer values.""" VARIANTS = frozenset([Variant.INT32, Variant.INT64, Variant.UINT32, Variant.UINT64, Variant.SINT32, Variant.SINT64, ]) DEFAULT_VARIANT = Variant.INT64 type = (int, long) class FloatField(Field): """Field definition for float values.""" VARIANTS = frozenset([Variant.FLOAT, Variant.DOUBLE, ]) DEFAULT_VARIANT = Variant.DOUBLE type = float class BooleanField(Field): """Field definition for boolean values.""" VARIANTS = frozenset([Variant.BOOL]) DEFAULT_VARIANT = Variant.BOOL type = bool class BytesField(Field): """Field definition for byte string values.""" VARIANTS = frozenset([Variant.BYTES]) DEFAULT_VARIANT = Variant.BYTES type = str class StringField(Field): """Field definition for unicode string values.""" VARIANTS = frozenset([Variant.STRING]) DEFAULT_VARIANT = Variant.STRING type = unicode def validate_element(self, value): """Validate StringField allowing for str and unicode. Raises: ValidationError if a str value is not 7-bit ascii. """ # If value is str is it considered valid. Satisfies "required=True". if isinstance(value, str): try: unicode(value) except UnicodeDecodeError, err: validation_error = ValidationError( 'Encountered non-ASCII string %s: %s' % (value, err)) validation_error.field_name = self.name raise validation_error else: super(StringField, self).validate_element(value) class MessageField(Field): """Field definition for sub-message values. Message fields contain instance of other messages. Instances stored on messages stored on message fields are considered to be owned by the containing message instance and should not be shared between owning instances. Message fields must be defined to reference a single type of message. Normally message field are defined by passing the referenced message class in to the constructor. It is possible to define a message field for a type that does not yet exist by passing the name of the message in to the constructor instead of a message class. Resolution of the actual type of the message is deferred until it is needed, for example, during message verification. Names provided to the constructor must refer to a class within the same python module as the class that is using it. Names refer to messages relative to the containing messages scope. For example, the two fields of OuterMessage refer to the same message type: class Outer(Message): inner_relative = MessageField('Inner', 1) inner_absolute = MessageField('Outer.Inner', 2) class Inner(Message): ... When resolving an actual type, MessageField will traverse the entire scope of nested messages to match a message name. This makes it easy for siblings to reference siblings: class Outer(Message): class Inner(Message): sibling = MessageField('Sibling', 1) class Sibling(Message): ... """ VARIANTS = frozenset([Variant.MESSAGE]) DEFAULT_VARIANT = Variant.MESSAGE @util.positional(3) def __init__(self, message_type, number, required=False, repeated=False, variant=None): """Constructor. Args: message_type: Message type for field. Must be subclass of Message. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. Raises: FieldDefinitionError when invalid message_type is provided. """ valid_type = (isinstance(message_type, basestring) or (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message))) if not valid_type: raise FieldDefinitionError('Invalid message class: %s' % message_type) if isinstance(message_type, basestring): self.__type_name = message_type self.__type = None else: self.__type = message_type super(MessageField, self).__init__(number, required=required, repeated=repeated, variant=variant) @property def type(self): """Message type used for field.""" if self.__type is None: message_type = find_definition(self.__type_name, self.message_definition()) if not (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message)): raise FieldDefinitionError('Invalid message class: %s' % message_type) self.__type = message_type return self.__type class EnumField(Field): """Field definition for enum values. Enum fields may have default values that are delayed until the associated enum type is resolved. This is necessary to support certain circular references. For example: class Message1(Message): class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 # This field default value will be validated when default is accessed. animal = EnumField('Message2.Animal', 1, default='HORSE') class Message2(Message): class Animal(Enum): DOG = 1 CAT = 2 HORSE = 3 # This fields default value will be validated right away since Color is # already fully resolved. color = EnumField(Message1.Color, 1, default='RED') """ VARIANTS = frozenset([Variant.ENUM]) DEFAULT_VARIANT = Variant.ENUM def __init__(self, enum_type, number, **kwargs): """Constructor. Args: enum_type: Enum type for field. Must be subclass of Enum. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: FieldDefinitionError when invalid enum_type is provided. """ valid_type = (isinstance(enum_type, basestring) or (enum_type is not Enum and isinstance(enum_type, type) and issubclass(enum_type, Enum))) if not valid_type: raise FieldDefinitionError('Invalid enum type: %s' % enum_type) if isinstance(enum_type, basestring): self.__type_name = enum_type self.__type = None else: self.__type = enum_type super(EnumField, self).__init__(number, **kwargs) def validate_default_element(self, value): """Validate default element of Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is validated against that type. Args: value: Value to validate. Raises: ValidationError if value is not expected message type. """ if isinstance(value, (basestring, int, long)): # Validation of the value does not happen for delayed resolution # enumerated types. Ignore if type is not yet resolved. if self.__type: self.__type(value) return super(EnumField, self).validate_default_element(value) @property def type(self): """Enum type used for field.""" if self.__type is None: found_type = find_definition(self.__type_name, self.message_definition()) if not (found_type is not Enum and isinstance(found_type, type) and issubclass(found_type, Enum)): raise FieldDefinitionError('Invalid enum type: %s' % found_type) self.__type = found_type return self.__type @property def default(self): """Default for enum field. Will cause resolution of Enum type and unresolved default value. """ try: return self.__resolved_default except AttributeError: resolved_default = super(EnumField, self).default if isinstance(resolved_default, (basestring, int, long)): resolved_default = self.type(resolved_default) self.__resolved_default = resolved_default return self.__resolved_default @util.positional(2) def find_definition(name, relative_to=None, importer=__import__): """Find definition by name in module-space. The find algorthm will look for definitions by name relative to a message definition or by fully qualfied name. If no definition is found relative to the relative_to parameter it will do the same search against the container of relative_to. If relative_to is a nested Message, it will search its message_definition(). If that message has no message_definition() it will search its module. If relative_to is a module, it will attempt to look for the containing module and search relative to it. If the module is a top-level module, it will look for the a message using a fully qualified name. If no message is found then, the search fails and DefinitionNotFoundError is raised. For example, when looking for any definition 'foo.bar.ADefinition' relative to an actual message definition abc.xyz.SomeMessage: find_definition('foo.bar.ADefinition', SomeMessage) It is like looking for the following fully qualified names: abc.xyz.SomeMessage. foo.bar.ADefinition abc.xyz. foo.bar.ADefinition abc. foo.bar.ADefinition foo.bar.ADefinition When resolving the name relative to Message definitions and modules, the algorithm searches any Messages or sub-modules found in its path. Non-Message values are not searched. A name that begins with '.' is considered to be a fully qualified name. The name is always searched for from the topmost package. For example, assume two message types: abc.xyz.SomeMessage xyz.SomeMessage Searching for '.xyz.SomeMessage' relative to 'abc' will resolve to 'xyz.SomeMessage' and not 'abc.xyz.SomeMessage'. For this kind of name, the relative_to parameter is effectively ignored and always set to None. For more information about package name resolution, please see: http://code.google.com/apis/protocolbuffers/docs/proto.html#packages Args: name: Name of definition to find. May be fully qualified or relative name. relative_to: Search for definition relative to message definition or module. None will cause a fully qualified name search. importer: Import function to use for resolving modules. Returns: Enum or Message class definition associated with name. Raises: DefinitionNotFoundError if no definition is found in any search path. """ # Check parameters. if not (relative_to is None or isinstance(relative_to, types.ModuleType) or isinstance(relative_to, type) and issubclass(relative_to, Message)): raise TypeError('relative_to must be None, Message definition or module. ' 'Found: %s' % relative_to) name_path = name.split('.') # Handle absolute path reference. if not name_path[0]: relative_to = None name_path = name_path[1:] def search_path(): """Performs a single iteration searching the path from relative_to. This is the function that searches up the path from a relative object. fully.qualified.object . relative.or.nested.Definition ----------------------------> ^ | this part of search --+ Returns: Message or Enum at the end of name_path, else None. """ next = relative_to for node in name_path: # Look for attribute first. attribute = getattr(next, node, None) if attribute is not None: next = attribute else: # If module, look for sub-module. if next is None or isinstance(next, types.ModuleType): if next is None: module_name = node else: module_name = '%s.%s' % (next.__name__, node) try: fromitem = module_name.split('.')[-1] next = importer(module_name, '', '', [str(fromitem)]) except ImportError: return None else: return None if (not isinstance(next, types.ModuleType) and not (isinstance(next, type) and issubclass(next, (Message, Enum)))): return None return next while True: found = search_path() if isinstance(found, type) and issubclass(found, (Enum, Message)): return found else: # Find next relative_to to search against. # # fully.qualified.object . relative.or.nested.Definition # <--------------------- # ^ # | # does this part of search if relative_to is None: # Fully qualified search was done. Nothing found. Fail. raise DefinitionNotFoundError('Could not find definition for %s' % (name,)) else: if isinstance(relative_to, types.ModuleType): # Find parent module. module_path = relative_to.__name__.split('.')[:-1] if not module_path: relative_to = None else: # Should not raise ImportError. If it does... weird and # unexepected. Propagate. relative_to = importer( '.'.join(module_path), '', '', [module_path[-1]]) elif (isinstance(relative_to, type) and issubclass(relative_to, Message)): parent = relative_to.message_definition() if parent is None: last_module_name = relative_to.__module__.split('.')[-1] relative_to = importer( relative_to.__module__, '', '', [last_module_name]) else: relative_to = parent
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protourlencode.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import logging import unittest import urllib from protorpc import messages from protorpc import protourlencode from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protourlencode class SuperMessage(messages.Message): """A test message with a nested message field.""" sub_message = messages.MessageField(test_util.OptionalMessage, 1) sub_messages = messages.MessageField(test_util.OptionalMessage, 2, repeated=True) class SuperSuperMessage(messages.Message): """A test message with two levels of nested.""" sub_message = messages.MessageField(SuperMessage, 1) sub_messages = messages.MessageField(SuperMessage, 2, repeated=True) class URLEncodedRequestBuilderTest(test_util.TestCase): """Test the URL Encoded request builder.""" def testMakePath(self): builder = protourlencode.URLEncodedRequestBuilder(SuperSuperMessage(), prefix='pre.') self.assertEquals(None, builder.make_path('')) self.assertEquals(None, builder.make_path('no_such_field')) self.assertEquals(None, builder.make_path('pre.no_such_field')) # Missing prefix. self.assertEquals(None, builder.make_path('sub_message')) # Valid parameters. self.assertEquals((('sub_message', None),), builder.make_path('pre.sub_message')) self.assertEquals((('sub_message', None), ('sub_messages', 1)), builder.make_path('pre.sub_message.sub_messages-1')) self.assertEquals( (('sub_message', None), ('sub_messages', 1), ('int64_value', None)), builder.make_path('pre.sub_message.sub_messages-1.int64_value')) # Missing index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_messages.integer_field')) # Has unexpected index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_message-1.integer_field')) def testAddParameter_SimpleAttributes(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value', ['10'])) self.assertTrue(builder.add_parameter('pre.string_value', ['a string'])) self.assertTrue(builder.add_parameter('pre.enum_value', ['VAL1'])) self.assertEquals(10, message.int64_value) self.assertEquals('a string', message.string_value) self.assertEquals(test_util.OptionalMessage.SimpleEnum.VAL1, message.enum_value) def testAddParameter_InvalidAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') def assert_empty(): self.assertEquals(None, getattr(message, 'sub_message')) self.assertEquals([], getattr(message, 'sub_messages')) self.assertFalse(builder.add_parameter('pre.nothing', ['x'])) assert_empty() self.assertFalse(builder.add_parameter('pre.sub_messages', ['x'])) self.assertFalse(builder.add_parameter('pre.sub_messages-1.nothing', ['x'])) assert_empty() def testAddParameter_NestedAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Set an empty message fields. self.assertTrue(builder.add_parameter('pre.sub_message', [''])) self.assertTrue(isinstance(message.sub_message, SuperMessage)) # Add a basic attribute. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.int64_value', ['10'])) self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.string_value', ['hello'])) self.assertTrue(10, message.sub_message.sub_message.int64_value) self.assertTrue('hello', message.sub_message.sub_message.string_value) def testAddParameter_NestedMessages(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Add a repeated empty message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-0', [''])) sub_message = message.sub_message.sub_messages[0] self.assertTrue(1, len(message.sub_message.sub_messages)) self.assertTrue(isinstance(sub_message, test_util.OptionalMessage)) self.assertEquals(None, getattr(sub_message, 'int64_value')) self.assertEquals(None, getattr(sub_message, 'string_value')) self.assertEquals(None, getattr(sub_message, 'enum_value')) # Add a repeated message with value. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.int64_value', ['10'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertTrue(10, message.sub_message.sub_messages[1].int64_value) # Add another value to the same nested message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.string_value', ['a string'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertEquals(10, message.sub_message.sub_messages[1].int64_value) self.assertEquals('a string', message.sub_message.sub_messages[1].string_value) def testAddParameter_RepeatedValues(self): message = test_util.RepeatedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value-0', ['20'])) self.assertTrue(builder.add_parameter('pre.int64_value-1', ['30'])) self.assertEquals([20, 30], message.int64_value) self.assertTrue(builder.add_parameter('pre.string_value-0', ['hi'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['lo'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['dups overwrite'])) self.assertEquals(['hi', 'dups overwrite'], message.string_value) def testAddParameter_InvalidValuesMayRepeat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('nothing', [1, 2, 3])) def testAddParameter_RepeatedParameters(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', [1, 2, 3]) self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', []) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field.""" message = test_util.HasNestedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, 'pre.') self.assertFalse(builder.add_parameter('pre.nested.a_value.whatever', ['1'])) def testInvalidFieldFormat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('pre.illegal%20', ['1'])) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field There is an odd corner case where if trying to insert a repeated value on an nested repeated message that would normally succeed in being created should fail. This case can only be tested when the first message of the nested messages already exists. Another case is trying to access an indexed value nested within a non-message field. """ class HasRepeated(messages.Message): values = messages.IntegerField(1, repeated=True) class HasNestedRepeated(messages.Message): nested = messages.MessageField(HasRepeated, 1, repeated=True) message = HasNestedRepeated() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.nested-0.values-0', ['1'])) # Try to create an indexed value on a non-message field. self.assertFalse(builder.add_parameter('pre.nested-0.values-0.unknown-0', ['1'])) # Try to create an out of range indexed field on an otherwise valid # repeated message field. self.assertFalse(builder.add_parameter('pre.nested-1.values-1', ['1'])) class ProtourlencodeConformanceTest(test_util.TestCase, test_util.ProtoConformanceTestBase): PROTOLIB = protourlencode encoded_partial = urllib.urlencode([('double_value', 1.23), ('int64_value', -100000000000), ('int32_value', 1020), ('string_value', u'a string'), ('enum_value', 'VAL2'), ]) encoded_full = urllib.urlencode([('double_value', 1.23), ('float_value', -2.5), ('int64_value', -100000000000), ('uint64_value', 102020202020), ('int32_value', 1020), ('bool_value', 'true'), ('string_value', u'a string\u044f'.encode('utf-8')), ('bytes_value', 'a bytes\xff\xfe'), ('enum_value', 'VAL2'), ]) encoded_repeated = urllib.urlencode([('double_value-0', 1.23), ('double_value-1', 2.3), ('float_value-0', -2.5), ('float_value-1', 0.5), ('int64_value-0', -100000000000), ('int64_value-1', 20), ('uint64_value-0', 102020202020), ('uint64_value-1', 10), ('int32_value-0', 1020), ('int32_value-1', 718), ('bool_value-0', 'true'), ('bool_value-1', 'false'), ('string_value-0', u'a string\u044f'.encode('utf-8')), ('string_value-1', u'another string'.encode('utf-8')), ('bytes_value-0', 'a bytes\xff\xfe'), ('bytes_value-1', 'another bytes'), ('enum_value-0', 'VAL2'), ('enum_value-1', 'VAL1'), ]) encoded_nested = urllib.urlencode([('nested.a_value', 'a string'), ]) encoded_repeated_nested = urllib.urlencode( [('repeated_nested-0.a_value', 'a string'), ('repeated_nested-1.a_value', 'another string'), ]) unexpected_tag_message = 'unexpected=whatever' encoded_default_assigned = urllib.urlencode([('a_value', 'a default'), ]) encoded_nested_empty = urllib.urlencode([('nested', '')]) encoded_repeated_nested_empty = urllib.urlencode([('repeated_nested-0', ''), ('repeated_nested-1', '')]) encoded_extend_message = urllib.urlencode([('int64_value-0', 400), ('int64_value-1', 50), ('int64_value-2', 6000)]) encoded_string_types = urllib.urlencode( [('string_value', 'Latin')]) def testParameterPrefix(self): """Test using the 'prefix' parameter to encode_message.""" class MyMessage(messages.Message): number = messages.IntegerField(1) names = messages.StringField(2, repeated=True) message = MyMessage() message.number = 10 message.names = [u'Fred', u'Lisa'] encoded_message = protourlencode.encode_message(message, prefix='prefix-') self.assertEquals({'prefix-number': ['10'], 'prefix-names-0': ['Fred'], 'prefix-names-1': ['Lisa'], }, cgi.parse_qs(encoded_message)) self.assertEquals(message, protourlencode.decode_message(MyMessage, encoded_message, prefix='prefix-')) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Service regsitry for service discovery. The registry service can be deployed on a server in order to provide a central place where remote clients can discover available. On the server side, each service is registered by their name which is unique to the registry. Typically this name provides enough information to identify the service and locate it within a server. For example, for an HTTP based registry the name is the URL path on the host where the service is invocable. The registry is also able to resolve the full descriptor.FileSet necessary to describe the service and all required data-types (messages and enums). A configured registry is itself a remote service and should reference itself. """ import sys import weakref from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import util __all__ = [ 'ServiceMapping', 'ServicesResponse', 'GetFileSetRequest', 'GetFileSetResponse', 'RegistryService', ] class ServiceMapping(messages.Message): """Description of registered service. Fields: name: Name of service. On HTTP based services this will be the URL path used for invocation. definition: Fully qualified name of the service definition. Useful for clients that can look up service definitions based on an existing repository of definitions. """ name = messages.StringField(1, required=True) definition = messages.StringField(2, required=True) class ServicesResponse(messages.Message): """Response containing all registered services. May also contain complete descriptor file-set for all services known by the registry. Fields: services: Service mappings for all registered services in registry. file_set: Descriptor file-set describing all services, messages and enum types needed for use with all requested services if asked for in the request. """ services = messages.MessageField(ServiceMapping, 1, repeated=True) class GetFileSetRequest(messages.Message): """Request for service descriptor file-set. Request to retrieve file sets for specific services. Fields: names: Names of services to retrieve file-set for. """ names = messages.StringField(1, repeated=True) class GetFileSetResponse(messages.Message): """Descriptor file-set for all names in GetFileSetRequest. Fields: file_set: Descriptor file-set containing all descriptors for services, messages and enum types needed for listed names in request. """ file_set = messages.MessageField(descriptor.FileSet, 1, required=True) class RegistryService(remote.Service): """Registry service. Maps names to services and is able to describe all descriptor file-sets necessary to use contined services. On an HTTP based server, the name is the URL path to the service. """ @util.positional(2) def __init__(self, registry, modules=None): """Constructor. Args: registry: Map of name to service class. This map is not copied and may be modified after the reigstry service has been configured. modules: Module dict to draw descriptors from. Defaults to sys.modules. """ # Private Attributes: # __registry: Map of name to service class. Refers to same instance as # registry parameter. # __modules: Mapping of module name to module. # __definition_to_modules: Mapping of definition types to set of modules # that they refer to. This cache is used to make repeated look-ups # faster and to prevent circular references from causing endless loops. self.__registry = registry if modules is None: modules = sys.modules self.__modules = modules # This cache will only last for a single request. self.__definition_to_modules = {} def __find_modules_for_message(self, message_type): """Find modules referred to by a message type. Determines the entire list of modules ultimately referred to by message_type by iterating over all of its message and enum fields. Includes modules referred to fields within its referred messages. Args: message_type: Message type to find all referring modules for. Returns: Set of modules referred to by message_types by traversing all its message and enum fields. """ # TODO(rafek): Maybe this should be a method on Message and Service? def get_dependencies(message_type, seen=None): """Get all dependency definitions of a message type. This function works by collecting the types of all enumeration and message fields defined within the message type. When encountering a message field, it will recursivly find all of the associated message's dependencies. It will terminate on circular dependencies by keeping track of what definitions it already via the seen set. Args: message_type: Message type to get dependencies for. seen: Set of definitions that have already been visited. Returns: All dependency message and enumerated types associated with this message including the message itself. """ if seen is None: seen = set() seen.add(message_type) for field in message_type.all_fields(): if isinstance(field, messages.MessageField): if field.type not in seen: get_dependencies(field.type, seen) elif isinstance(field, messages.EnumField): seen.add(field.type) return seen found_modules = self.__definition_to_modules.setdefault(message_type, set()) if not found_modules: dependencies = get_dependencies(message_type) found_modules.update(self.__modules[definition.__module__] for definition in dependencies) return found_modules def __describe_file_set(self, names): """Get file-set for named services. Args: names: List of names to get file-set for. Returns: descriptor.FileSet containing all the descriptors for all modules ultimately referred to by all service types request by names parameter. """ service_modules = set() if names: for service in (self.__registry[name] for name in names): found_modules = self.__definition_to_modules.setdefault(service, set()) if not found_modules: found_modules.add(self.__modules[service.__module__]) for method_name in service.all_remote_methods(): method = getattr(service, method_name) for message_type in (method.remote.request_type, method.remote.response_type): found_modules.update( self.__find_modules_for_message(message_type)) service_modules.update(found_modules) return descriptor.describe_file_set(service_modules) @property def registry(self): """Get service registry associated with this service instance.""" return self.__registry @remote.method(message_types.VoidMessage, ServicesResponse) def services(self, request): """Get all registered services.""" response = ServicesResponse() response.services = [] for name, service_class in self.__registry.iteritems(): mapping = ServiceMapping() mapping.name = name.decode('utf-8') mapping.definition = service_class.definition_name().decode('utf-8') response.services.append(mapping) return response @remote.method(GetFileSetRequest, GetFileSetResponse) def get_file_set(self, request): """Get file-set for registered servies.""" response = GetFileSetResponse() response.file_set = self.__describe_file_set(request.names) return response
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import with_statement __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import descriptor from protorpc import generate from protorpc import messages from protorpc import util __all__ = ['format_python_file'] def _write_enums(enum_descriptors, out): """Write nested and non-nested Enum types. Args: enum_descriptors: List of EnumDescriptor objects from which to generate enums. out: Indent writer used for generating text. """ # Write enums. for enum in enum_descriptors or []: out << '' out << '' out << 'class %s(messages.Enum):' % enum.name out << '' with out.indent(): if not enum.values: out << 'pass' else: for enum_value in enum.values: out << '%s = %s' % (enum_value.name, enum_value.number) def _write_fields(field_descriptors, out): """Write fields for Message types. Args: field_descriptors: List of FieldDescriptor objects from which to generate fields. out: Indent writer used for generating text. """ out << '' for field in field_descriptors or []: field_type = messages.Field.lookup_field_type_by_variant(field.variant) type_format = '' label_format = '' if issubclass(field_type, (messages.EnumField, messages.MessageField)): type_format = '\'%s\', ' % field.type_name if field.label == descriptor.FieldDescriptor.Label.REQUIRED: label_format = ', required=True' elif field.label == descriptor.FieldDescriptor.Label.REPEATED: label_format = ', repeated=True' if field_type.DEFAULT_VARIANT != field.variant: variant_format = ', variant=messages.Variant.%s' % field.variant else: variant_format = '' if field.default_value: if field_type in [messages.BytesField, messages.StringField, ]: default_value = repr(field.default_value) elif field_type is messages.EnumField: try: default_value = str(int(field.default_value)) except ValueError: default_value = repr(field.default_value) else: default_value = field.default_value default_format = ', default=%s' % (default_value,) else: default_format = '' out << '%s = messages.%s(%s%s%s%s%s)' % (field.name, field_type.__name__, type_format, field.number, label_format, variant_format, default_format) def _write_messages(message_descriptors, out): """Write nested and non-nested Message types. Args: message_descriptors: List of MessageDescriptor objects from which to generate messages. out: Indent writer used for generating text. """ for message in message_descriptors or []: out << '' out << '' out << 'class %s(messages.Message):' % message.name with out.indent(): if not (message.enum_types or message.message_types or message.fields): out << '' out << 'pass' else: _write_enums(message.enum_types, out) _write_messages(message.message_types, out) _write_fields(message.fields, out) def _write_methods(method_descriptors, out): """Write methods of Service types. All service method implementations raise NotImplementedError. Args: method_descriptors: List of MethodDescriptor objects from which to generate methods. out: Indent writer used for generating text. """ for method in method_descriptors: out << '' out << "@remote.method('%s', '%s')" % (method.request_type, method.response_type) out << 'def %s(self, request):' % (method.name,) with out.indent(): out << ('raise NotImplementedError' "('Method %s is not implemented')" % (method.name)) def _write_services(service_descriptors, out): """Write Service types. Args: service_descriptors: List of ServiceDescriptor instances from which to generate services. out: Indent writer used for generating text. """ for service in service_descriptors or []: out << '' out << '' out << 'class %s(remote.Service):' % service.name with out.indent(): if service.methods: _write_methods(service.methods, out) else: out << '' out << 'pass' @util.positional(2) def format_python_file(file_descriptor, output, indent_space=2): """Format FileDescriptor object as a single Python module. Services generated by this function will raise NotImplementedError. All Python classes generated by this function use delayed binding for all message fields, enum fields and method parameter types. For example a service method might be generated like so: class MyService(remote.Service): @remote.method('my_package.MyRequestType', 'my_package.MyResponseType') def my_method(self, request): raise NotImplementedError('Method my_method is not implemented') Args: file_descriptor: FileDescriptor instance to format as python module. output: File-like object to write module source code to. indent_space: Number of spaces for each level of Python indentation. """ out = generate.IndentWriter(output, indent_space=indent_space) out << 'from protorpc import messages' if file_descriptor.service_types: out << 'from protorpc import remote' if file_descriptor.package: out << "package = '%s'" % file_descriptor.package _write_enums(file_descriptor.enum_types, out) _write_messages(file_descriptor.message_types, out) _write_services(file_descriptor.service_types, out)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = registry class MyService1(remote.Service): """Test service that refers to messages in another module.""" @remote.method(test_util.NestedMessage, test_util.NestedMessage) def a_method(self, request): pass class MyService2(remote.Service): """Test service that does not refer to messages in another module.""" class RegistryServiceTest(test_util.TestCase): def setUp(self): self.registry = { 'my-service1': MyService1, 'my-service2': MyService2, } self.modules = { __name__: sys.modules[__name__], test_util.__name__: test_util, } self.registry_service = registry.RegistryService(self.registry, modules=self.modules) def CheckServiceMappings(self, mappings): service1_mapping = registry.ServiceMapping() service1_mapping.name = 'my-service1' service1_mapping.definition = '__main__.MyService1' service2_mapping = registry.ServiceMapping() service2_mapping.name = 'my-service2' service2_mapping.definition = '__main__.MyService2' mappings.sort(key=lambda mapping: mapping.name) self.assertEquals(mappings[0], service1_mapping) self.assertEquals(mappings[1], service2_mapping) def testServices(self): response = self.registry_service.services(message_types.VoidMessage()) self.CheckServiceMappings(response.services) def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_None(self): request = registry.GetFileSetRequest() response = self.registry_service.get_file_set(request) self.assertEquals(descriptor.FileSet(), response.file_set) def testGetFileSet_ReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service1'] response = self.registry_service.get_file_set(request) # Will suck in and describe the test_util module. expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_DoNotReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service2'] response = self.registry_service.get_file_set(request) # Service does not reference test_util, so will only describe this module. expected_file_set = descriptor.describe_file_set([self.modules[__name__]]) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.remote.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import types import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import test_util from protorpc import transport import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = remote class Request(messages.Message): """Test request message.""" value = messages.StringField(1) class Response(messages.Message): """Test response message.""" value = messages.StringField(1) class MyService(remote.Service): @remote.method(Request, Response) def remote_method(self, request): response = Response() response.value = request.value return response class SimpleRequest(messages.Message): """Simple request message type used for tests.""" param1 = messages.StringField(1) param2 = messages.StringField(2) class SimpleResponse(messages.Message): """Simple response message type used for tests.""" class BasicService(remote.Service): """A basic service with decorated remote method.""" def __init__(self): self.request_ids = [] @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, request): self.request_ids.append(id(request)) return SimpleResponse() class RpcErrorTest(test_util.TestCase): def testFromStatus(self): for state in remote.RpcState: exception = remote.RpcError.from_state self.assertEquals(remote.ServerError, remote.RpcError.from_state('SERVER_ERROR')) class ApplicationErrorTest(test_util.TestCase): def testErrorCode(self): self.assertEquals('blam', remote.ApplicationError('an error', 'blam').error_name) def testStr(self): self.assertEquals('an error', str(remote.ApplicationError('an error', 1))) def testRepr(self): self.assertEquals("ApplicationError('an error', 1)", repr(remote.ApplicationError('an error', 1))) self.assertEquals("ApplicationError('an error')", repr(remote.ApplicationError('an error'))) class RemoteTest(test_util.TestCase): """Test remote method decorator.""" def testRemote(self): """Test use of remote decorator.""" self.assertEquals(SimpleRequest, BasicService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, BasicService.remote_method.remote.response_type) self.assertTrue(isinstance(BasicService.remote_method.remote.method, types.FunctionType)) def testRemoteMessageResolution(self): """Test use of remote decorator to resolve message types by name.""" class OtherService(remote.Service): @remote.method('SimpleRequest', 'SimpleResponse') def remote_method(self, request): pass self.assertEquals(SimpleRequest, OtherService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, OtherService.remote_method.remote.response_type) def testRemoteMessageResolution_NotFound(self): """Test failure to find message types.""" class OtherService(remote.Service): @remote.method('NoSuchRequest', 'NoSuchResponse') def remote_method(self, request): pass self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchRequest', getattr, OtherService.remote_method.remote, 'request_type') self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchResponse', getattr, OtherService.remote_method.remote, 'response_type') def testInvocation(self): """Test that invocation passes request through properly.""" service = BasicService() request = SimpleRequest() self.assertEquals(SimpleResponse(), service.remote_method(request)) self.assertEquals([id(request)], service.request_ids) def testInvocation_WrongRequestType(self): """Wrong request type passed to remote method.""" service = BasicService() self.assertRaises(remote.RequestError, service.remote_method, 'wrong') self.assertRaises(remote.RequestError, service.remote_method, None) self.assertRaises(remote.RequestError, service.remote_method, SimpleResponse()) def testInvocation_WrongResponseType(self): """Wrong response type returned from remote method.""" class AnotherService(object): @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, unused_request): return self.return_this service = AnotherService() service.return_this = 'wrong' self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = None self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = SimpleRequest() self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) def testBadRequestType(self): """Test bad request types used in remote definition.""" for request_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(request_type, SimpleResponse) def remote_method(self, request): pass self.assertRaises(TypeError, declare) def testBadResponseType(self): """Test bad response types used in remote definition.""" for response_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(SimpleRequest, response_type) def remote_method(self, request): pass self.assertRaises(TypeError, declare) class GetRemoteMethodTest(test_util.TestCase): """Test for is_remote_method.""" def testGetRemoteMethod(self): """Test valid remote method detection.""" class Service(object): @remote.method(Request, Response) def remote_method(self, request): pass self.assertEquals(Service.remote_method.remote, remote.get_remote_method_info(Service.remote_method)) self.assertTrue(Service.remote_method.remote, remote.get_remote_method_info(Service().remote_method)) def testGetNotRemoteMethod(self): """Test positive result on a remote method.""" class NotService(object): def not_remote_method(self, request): pass def fn(self): pass class NotReallyRemote(object): """Test negative result on many bad values for remote methods.""" def not_really(self, request): pass not_really.remote = 'something else' for not_remote in [NotService.not_remote_method, NotService().not_remote_method, NotReallyRemote.not_really, NotReallyRemote().not_really, None, 1, 'a string', fn]: self.assertEquals(None, remote.get_remote_method_info(not_remote)) class RequestStateTest(test_util.TestCase): """Test request state.""" def testConstructor(self): """Test constructor.""" state = remote.RequestState(remote_host='remote-host', remote_address='remote-address', server_host='server-host', server_port=10) self.assertEquals('remote-host', state.remote_host) self.assertEquals('remote-address', state.remote_address) self.assertEquals('server-host', state.server_host) self.assertEquals(10, state.server_port) state = remote.RequestState() self.assertEquals(None, state.remote_host) self.assertEquals(None, state.remote_address) self.assertEquals(None, state.server_host) self.assertEquals(None, state.server_port) def testConstructorError(self): """Test unexpected keyword argument.""" self.assertRaises(TypeError, remote.RequestState, x=10) def testRepr(self): """Test string representation.""" self.assertEquals('<remote.RequestState>', repr(remote.RequestState())) self.assertEquals('<remote.RequestState remote_host=abc>', repr(remote.RequestState(remote_host='abc'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def>', repr(remote.RequestState(remote_host='abc', remote_address='def'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi ' 'server_port=102>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi', server_port=102))) class ServiceTest(test_util.TestCase): """Test Service class.""" def testServiceBase_AllRemoteMethods(self): """Test that service base class has no remote methods.""" self.assertEquals({}, remote.Service.all_remote_methods()) def testAllRemoteMethods(self): """Test all_remote_methods with properly Service subclass.""" self.assertEquals({'remote_method': MyService.remote_method}, MyService.all_remote_methods()) def testAllRemoteMethods_SubClass(self): """Test all_remote_methods on a sub-class of a service.""" class SubClass(MyService): @remote.method(Request, Response) def sub_class_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, 'sub_class_method': SubClass.sub_class_method, }, SubClass.all_remote_methods()) def testOverrideMethod(self): """Test that trying to override a remote method with remote decorator.""" class SubClass(MyService): def remote_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, }, SubClass.all_remote_methods()) def testOverrideMethodWithRemote(self): """Test trying to override a remote method with remote decorator.""" def do_override(): class SubClass(MyService): @remote.method(Request, Response) def remote_method(self, request): pass self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Do not use remote decorator when ' 'overloading remote method remote_method ' 'on service SubClass', do_override) def testOverrideMethodWithInvalidValue(self): """Test trying to override a remote method with remote decorator.""" def do_override(bad_value): class SubClass(MyService): remote_method = bad_value for bad_value in [None, 1, 'string', {}]: self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Must override remote_method in ' 'SubClass with a method', do_override, bad_value) def testCallingRemoteMethod(self): """Test invoking a remote method.""" expected = Response() expected.value = 'what was passed in' request = Request() request.value = 'what was passed in' service = MyService() self.assertEquals(expected, service.remote_method(request)) def testFactory(self): """Test using factory to pass in state.""" class StatefulService(remote.Service): def __init__(self, a, b, c=None): self.a = a self.b = b self.c = c state = [1, 2, 3] factory = StatefulService.new_factory(1, state) self.assertEquals('Creates new instances of service StatefulService.\n\n' 'Returns:\n' ' New instance of __main__.StatefulService.', factory.func_doc) self.assertEquals('StatefulService_service_factory', factory.func_name) self.assertEquals(StatefulService, factory.service_class) service = factory() self.assertEquals(1, service.a) self.assertEquals(id(state), id(service.b)) self.assertEquals(None, service.c) factory = StatefulService.new_factory(2, b=3, c=4) service = factory() self.assertEquals(2, service.a) self.assertEquals(3, service.b) self.assertEquals(4, service.c) def testFactoryError(self): """Test misusing a factory.""" # Passing positional argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(1)) # Passing keyword argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(x=1)) class StatefulService(remote.Service): def __init__(self, a): pass # Missing required parameter. self.assertRaises(TypeError, StatefulService.new_factory()) def testDefinitionName(self): """Test getting service definition name.""" class TheService(remote.Service): pass self.assertEquals('__main__.TheService', TheService.definition_name()) def testDefinitionNameWithPackage(self): """Test getting service definition name when package defined.""" global package package = 'my.package' try: class TheService(remote.Service): pass self.assertEquals('my.package.TheService', TheService.definition_name()) finally: del package def testDefinitionNameWithNoModule(self): """Test getting service definition name when package defined.""" module = sys.modules[__name__] try: del sys.modules[__name__] class TheService(remote.Service): pass self.assertEquals('TheService', TheService.definition_name()) finally: sys.modules[__name__] = module class StubTest(test_util.TestCase): def setUp(self): self.mox = mox.Mox() self.transport = self.mox.CreateMockAnything() def testDefinitionName(self): self.assertEquals(BasicService.definition_name(), BasicService.Stub.definition_name()) def testRemoteMethods(self): self.assertEquals(BasicService.all_remote_methods(), BasicService.Stub.all_remote_methods()) def testSync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(request)) self.mox.VerifyAll() def testSync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(request)) self.mox.VerifyAll() def testAsync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequestAndKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'May not provide both args and kwargs', stub.async.remote_method, request, param1='val1', param2='val2') self.mox.VerifyAll() def testAsync_WithTooManyPositionals(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'remote_method\(\) takes at most 2 positional arguments \(3 given\)', stub.async.remote_method, request, 'another value') self.mox.VerifyAll() class IsErrorStatusTest(test_util.TestCase): def testIsError(self): for state in (s for s in remote.RpcState if s > remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertTrue(remote.is_error_status(status)) def testIsNotError(self): for state in (s for s in remote.RpcState if s <= remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertFalse(remote.is_error_status(status)) def testStateNone(self): self.assertRaises(messages.ValidationError, remote.is_error_status, remote.RpcStatus()) class CheckRpcStatusTest(test_util.TestCase): def testStateNone(self): self.assertRaises(messages.ValidationError, remote.check_rpc_status, remote.RpcStatus()) def testNoError(self): for state in (remote.RpcState.OK, remote.RpcState.RUNNING): remote.check_rpc_status(remote.RpcStatus(state=state)) def testErrorState(self): status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='a request error') self.assertRaisesWithRegexpMatch(remote.RequestError, 'a request error', remote.check_rpc_status, status) def testApplicationErrorState(self): status = remote.RpcStatus(state=remote.RpcState.APPLICATION_ERROR, error_message='an application error', error_name='blam') try: remote.check_rpc_status(status) self.fail('Should have raised application error.') except remote.ApplicationError, err: self.assertEquals('an application error', str(err)) self.assertEquals('blam', err.error_name) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stub library.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import sys import urllib2 from protorpc import descriptor from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import util __all__ = [ 'define_enum', 'define_field', 'define_file', 'define_message', 'define_service', 'import_file', 'import_file_set', ] # Map variant back to message field classes. def _build_variant_map(): """Map variants to fields. Returns: Dictionary mapping field variant to its associated field type. """ result = {} for name in dir(messages): value = getattr(messages, name) if isinstance(value, type) and issubclass(value, messages.Field): for variant in getattr(value, 'VARIANTS', []): result[variant] = value return result _VARIANT_MAP = _build_variant_map() def _get_or_define_module(full_name, modules): """Helper method for defining new modules. Args: full_name: Fully qualified name of module to create or return. modules: Dictionary of all modules. Defaults to sys.modules. Returns: Named module if found in 'modules', else creates new module and inserts in 'modules'. Will also construct parent modules if necessary. """ module = modules.get(full_name) if not module: module = new.module(full_name) modules[full_name] = module split_name = full_name.rsplit('.', 1) if len(split_name) > 1: parent_module_name, sub_module_name = split_name parent_module = _get_or_define_module(parent_module_name, modules) setattr(parent_module, sub_module_name, module) return module def define_enum(enum_descriptor, module_name): """Define Enum class from descriptor. Args: enum_descriptor: EnumDescriptor to build Enum class from. module_name: Module name to give new descriptor class. Returns: New messages.Enum sub-class as described by enum_descriptor. """ enum_values = enum_descriptor.values or [] class_dict = dict((value.name, value.number) for value in enum_values) class_dict['__module__'] = module_name return type(str(enum_descriptor.name), (messages.Enum,), class_dict) def define_field(field_descriptor): """Define Field instance from descriptor. Args: field_descriptor: FieldDescriptor class to build field instance from. Returns: New field instance as described by enum_descriptor. """ field_class = _VARIANT_MAP[field_descriptor.variant] params = {'number': field_descriptor.number, 'variant': field_descriptor.variant, } if field_descriptor.label == descriptor.FieldDescriptor.Label.REQUIRED: params['required'] = True elif field_descriptor.label == descriptor.FieldDescriptor.Label.REPEATED: params['repeated'] = True if field_class in (messages.EnumField, messages.MessageField): return field_class(field_descriptor.type_name, **params) else: return field_class(**params) def define_message(message_descriptor, module_name): """Define Message class from descriptor. Args: message_descriptor: MessageDescriptor to describe message class from. module_name: Module name to give to new descriptor class. Returns: New messages.Message sub-class as described by message_descriptor. """ class_dict = {'__module__': module_name} for enum in message_descriptor.enum_types or []: enum_instance = define_enum(enum, module_name) class_dict[enum.name] = enum_instance # TODO(rafek): support nested messages when supported by descriptor. for field in message_descriptor.fields or []: field_instance = define_field(field) class_dict[field.name] = field_instance class_name = message_descriptor.name.encode('utf-8') return type(class_name, (messages.Message,), class_dict) def define_service(service_descriptor, module): """Define a new service proxy. Args: service_descriptor: ServiceDescriptor class that describes the service. module: Module to add service to. Request and response types are found relative to this module. Returns: Service class proxy capable of communicating with a remote server. """ class_dict = {'__module__': module.__name__} class_name = service_descriptor.name.encode('utf-8') for method_descriptor in service_descriptor.methods or []: request_definition = messages.find_definition( method_descriptor.request_type, module) response_definition = messages.find_definition( method_descriptor.response_type, module) method_name = method_descriptor.name.encode('utf-8') def remote_method(self, request): """Actual service method.""" raise NotImplementedError('Method is not implemented') remote_method.__name__ = method_name remote_method_decorator = remote.method(request_definition, response_definition) class_dict[method_name] = remote_method_decorator(remote_method) service_class = type(class_name, (remote.Service,), class_dict) return service_class def define_file(file_descriptor, module=None): """Define module from FileDescriptor. Args: file_descriptor: FileDescriptor instance to describe module from. module: Module to add contained objects to. Module name overrides value in file_descriptor.package. Definitions are added to existing module if provided. Returns: If no module provided, will create a new module with its name set to the file descriptor's package. If a module is provided, returns the same module. """ if module is None: module = new.module(file_descriptor.package) for enum_descriptor in file_descriptor.enum_types or []: enum_class = define_enum(enum_descriptor, module.__name__) setattr(module, enum_descriptor.name, enum_class) for message_descriptor in file_descriptor.message_types or []: message_class = define_message(message_descriptor, module.__name__) setattr(module, message_descriptor.name, message_class) for service_descriptor in file_descriptor.service_types or []: service_class = define_service(service_descriptor, module) setattr(module, service_descriptor.name, service_class) return module @util.positional(1) def import_file(file_descriptor, modules=None): """Import FileDescriptor in to module space. This is like define_file except that a new module and any required parent modules are created and added to the modules parameter or sys.modules if not provided. Args: file_descriptor: FileDescriptor instance to describe module from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. Returns: Module found in modules, else a new module. """ if not file_descriptor.package: raise ValueError('File descriptor must have package name') if modules is None: modules = sys.modules module = _get_or_define_module(file_descriptor.package.encode('utf-8'), modules) return define_file(file_descriptor, module) @util.positional(1) def import_file_set(file_set, modules=None, _open=open): """Import FileSet in to module space. Args: file_set: If string, open file and read serialized FileSet. Otherwise, a FileSet instance to import definitions from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. _open: Used for dependency injection during tests. """ if isinstance(file_set, basestring): encoded_file = _open(file_set, 'rb') try: encoded_file_set = encoded_file.read() finally: encoded_file.close() file_set = protobuf.decode_message(descriptor.FileSet, encoded_file_set) for file_descriptor in file_set.files: # Do not reload built in protorpc classes. if not file_descriptor.package.startswith('protorpc.'): import_file(file_descriptor, modules=modules)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.dynamic.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import dynamic from protorpc import test_util from google.protobuf import descriptor class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = dynamic class Color(dynamic.Enum): RED = 1 GREEN = 2L BLUE = 4 class EnumTest(test_util.TestCase): """Tests for Enum class.""" def testDefinition(self): """Test correct Enum definition.""" self.assertEquals(1, Color.RED) self.assertEquals(2, Color.GREEN) self.assertEquals(4, Color.BLUE) self.assertEquals([('RED', 1), ('GREEN', 2), ('BLUE', 4)], Color._VALUES) def testIntegerOnly(self): """Test that only integers are permitted values for class.""" def do_test(): class Stuff(dynamic.Enum): A_STRING = 'a string' self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoEnumSubclass(self): """Test that it is not possible to sub-class Enum types.""" def do_test(): class MoreColor(Color): MAGENTA = 4 self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoConstructor(self): """Test that it is not possible to construct Enum values.""" self.assertRaises(NotImplementedError, Color) class MessageTest(test_util.TestCase): """Tests for Message class.""" def testEmptyDefinition(self): """Test the creation of an empty message definition.""" class MyMessage(dynamic.Message): pass self.assertEquals([], MyMessage.DESCRIPTOR.enum_types) self.assertEquals([], MyMessage.DESCRIPTOR.nested_types) self.assertEquals([], MyMessage.DESCRIPTOR.fields) self.assertEquals('MyMessage', MyMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage', MyMessage.DESCRIPTOR.full_name) def testNestedDefinition(self): """Test nesting message definitions in another.""" class MyMessage(dynamic.Message): class NestedMessage(dynamic.Message): pass self.assertEquals('NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage.NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.full_name) self.assertEquals(MyMessage.DESCRIPTOR, MyMessage.NestedMessage.DESCRIPTOR.containing_type) self.assertEquals([MyMessage.NestedMessage.DESCRIPTOR], MyMessage.DESCRIPTOR.nested_types) def testNestedEnum(self): """Test nesting an Enum type within a definition.""" class MyMessage(dynamic.Message): class Greek(dynamic.Enum): ALPHA = 1 BETA = 2 GAMMA = 4 self.assertFalse(hasattr(MyMessage, 'Greek')) self.assertEquals(1, len(MyMessage.DESCRIPTOR.enum_types)) Greek = MyMessage.DESCRIPTOR.enum_types[0] self.assertEquals('Greek', Greek.name) self.assertEquals('__main__.MyMessage.Greek', Greek.full_name) self.assertEquals(MyMessage.DESCRIPTOR, Greek.containing_type) self.assertEquals(3, len(Greek.values)) ALPHA = Greek.values[0] BETA = Greek.values[1] GAMMA = Greek.values[2] self.assertEquals(Greek, ALPHA.type) self.assertEquals(Greek, BETA.type) self.assertEquals(Greek, GAMMA.type) self.assertEquals(1, MyMessage.ALPHA) self.assertEquals(2, MyMessage.BETA) self.assertEquals(4, MyMessage.GAMMA) def testOptionalFields(self): """Test optional fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.StringField(2) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1 = 102 self.assertTrue(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRequiredFields(self): """Test required fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True) f2 = dynamic.StringField(2, required=True) m1 = MyMessage() self.assertFalse(m1.IsInitialized()) m1.f1 = 102 self.assertFalse(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRepeatedFields(self): """Test repeated fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True) f2 = dynamic.StringField(2, repeated=True) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1.append(102) self.assertTrue(m1.IsInitialized()) m1.f2.append('a string') self.assertTrue(m1.IsInitialized()) def testFieldDescriptor(self): """Test field descriptors after message class creation.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) class Nested(dynamic.Message): f2 = dynamic.IntegerField(1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) self.assertEquals(1, len(MyMessage.Nested.DESCRIPTOR.fields)) f1 = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('f1', f1.name) self.assertEquals('__main__.MyMessage.f1', f1.full_name) self.assertEquals(MyMessage.DESCRIPTOR, f1.containing_type) f2 = MyMessage.Nested.DESCRIPTOR.fields[0] self.assertEquals('f2', f2.name) self.assertEquals('__main__.MyMessage.Nested.f2', f2.full_name) self.assertEquals(MyMessage.Nested.DESCRIPTOR, f2.containing_type) def testRequiredAndRepeated(self): """Test using required and repeated flags together.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True, repeated=True) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDefaults(self): """Test using default values.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, default=10) m = MyMessage() self.assertEquals(10, m.f1) def testNoDefaultList(self): """Test that default does not work for repeated fields.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True, default=[1, 2, 3]) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testUnexpectedFieldArgument(self): """Test that unknown keyword arguments may not be used.""" self.assertRaises(TypeError, dynamic.IntegerField, 1, whatever=10) def testOverrideVariant(self): """Test overriding the variant of a field.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.IntegerField(2, variant=descriptor.FieldDescriptor.TYPE_UINT32) self.assertEquals(descriptor.FieldDescriptor.TYPE_INT64, MyMessage.DESCRIPTOR.fields[0].type) self.assertEquals(descriptor.FieldDescriptor.TYPE_UINT32, MyMessage.DESCRIPTOR.fields[1].type) def testOverrideWrongVariant(self): """Test assigning an incompatible variant.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField( 1, variant=descriptor.FieldDescriptor.TYPE_STRING) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowNonDefinitionValues(self): """Test that non-definitions may not be assigned to class.""" def do_test(): class MyMessage(dynamic.Message): f1 = 'A non-field value.' self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowMethods(self): """Test that methods may not be defined on class.""" def do_test(): class MyMessage(dynamic.Message): def i_dont_think_so(self): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotSubclassing(self): """Test that messages may not be sub-classed.""" class MyMessage(dynamic.Message): pass def do_test(): class SubClass(MyMessage): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testEnumField(self): """Test a basic enum field.""" class MyMessage(dynamic.Message): class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) color = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('color', color.name) self.assertEquals('__main__.MyMessage.color', color.full_name) Color = color.enum_type self.assertEquals(MyMessage.DESCRIPTOR.enum_types[0], Color) def testEnumFieldWithNonEnum(self): """Test enum field with a non-enum class.""" def do_test(): class MyMessage(dynamic.Message): class Color(object): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertRaises(TypeError, do_test) def testEnumFieldWithNonNestedEnum(self): """Test enum field with a non-peer Enum class.""" class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 def do_test(): class MyMessage(dynamic.Message): color = dynamic.EnumField(Color, 1) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testNoAbitraryAssignment(self): """Test that not possible to assing non-field attributes.""" class MyMessage(dynamic.Message): pass self.assertRaises(AttributeError, setattr, MyMessage(), 'a', 10) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Protocol buffer support for message types. For more details about protocol buffer encoding and decoding please see: http://code.google.com/apis/protocolbuffers/docs/encoding.html Public Exceptions: DecodeError: Raised when a decode error occurs from incorrect protobuf format. Public Functions: encode_message: Encodes a message in to a protocol buffer string. decode_message: Decode from a protocol buffer string to a message. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import array import cStringIO from protorpc import messages # TODO(rafek): Do something about this dependency maybe. from google.net.proto import ProtocolBuffer __all__ = ['CONTENT_TYPE', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/x-google-protobuf' class _Encoder(ProtocolBuffer.Encoder): """Extension of protocol buffer encoder. Original protocol buffer encoder does not have complete set of methods for handling required encoding. This class adds them. """ # TODO(rafek): Implement the missing encoding types. def no_encoding(self, value): """No encoding available for type. Args: value: Value to encode. Raises: NotImplementedError at all times. """ raise NotImplementedError() def encode_enum(self, value): """Encode an enum value. Args: value: Enum to encode. """ self.putVarInt32(value.number) def encode_message(self, value): """Encode a Message in to an embedded message. Args: value: Message instance to encode. """ self.putPrefixedString(encode_message(value)) def encode_unicode_string(self, value): """Helper to properly pb encode unicode strings to UTF-8. Args: value: String value to encode. """ if isinstance(value, unicode): value = value.encode('utf-8') self.putPrefixedString(value) class _Decoder(ProtocolBuffer.Decoder): """Extension of protocol buffer decoder. Original protocol buffer decoder does not have complete set of methods for handling required decoding. This class adds them. """ # TODO(rafek): Implement the missing encoding types. def no_decoding(self): """No decoding available for type. Raises: NotImplementedError at all times. """ raise NotImplementedError() def decode_string(self): """Decode a unicode string. Returns: Next value in stream as a unicode string. """ return self.getPrefixedString().decode('UTF-8') def decode_boolean(self): """Decode a boolean value. Returns: Next value in stream as a boolean. """ return bool(self.getBoolean()) # Number of bits used to describe a protocol buffer bits used for the variant. _WIRE_TYPE_BITS = 3 _WIRE_TYPE_MASK = 7 # Maps variant to underlying wire type. Many variants map to same type. _VARIANT_TO_WIRE_TYPE = { messages.Variant.DOUBLE: _Encoder.DOUBLE, messages.Variant.FLOAT: _Encoder.FLOAT, messages.Variant.INT64: _Encoder.NUMERIC, messages.Variant.UINT64: _Encoder.NUMERIC, messages.Variant.INT32: _Encoder.NUMERIC, messages.Variant.BOOL: _Encoder.NUMERIC, messages.Variant.STRING: _Encoder.STRING, messages.Variant.MESSAGE: _Encoder.STRING, messages.Variant.BYTES: _Encoder.STRING, messages.Variant.UINT32: _Encoder.NUMERIC, messages.Variant.ENUM: _Encoder.NUMERIC, messages.Variant.SINT32: _Encoder.NUMERIC, messages.Variant.SINT64: _Encoder.NUMERIC, } # Maps variant to encoder method. _VARIANT_TO_ENCODER_MAP = { messages.Variant.DOUBLE: _Encoder.putDouble, messages.Variant.FLOAT: _Encoder.putFloat, messages.Variant.INT64: _Encoder.putVarInt64, messages.Variant.UINT64: _Encoder.putVarUint64, messages.Variant.INT32: _Encoder.putVarInt32, messages.Variant.BOOL: _Encoder.putBoolean, messages.Variant.STRING: _Encoder.encode_unicode_string, messages.Variant.MESSAGE: _Encoder.encode_message, messages.Variant.BYTES: _Encoder.encode_unicode_string, messages.Variant.UINT32: _Encoder.no_encoding, messages.Variant.ENUM: _Encoder.encode_enum, messages.Variant.SINT32: _Encoder.no_encoding, messages.Variant.SINT64: _Encoder.no_encoding, } # Basic wire format decoders. Used for skipping unknown values. _WIRE_TYPE_TO_DECODER_MAP = { _Encoder.NUMERIC: _Decoder.getVarInt32, _Encoder.DOUBLE: _Decoder.getDouble, _Encoder.STRING: _Decoder.getPrefixedString, _Encoder.FLOAT: _Decoder.getFloat, } # Wire type to name mapping for error messages. _WIRE_TYPE_NAME = { _Encoder.NUMERIC: 'NUMERIC', _Encoder.DOUBLE: 'DOUBLE', _Encoder.STRING: 'STRING', _Encoder.FLOAT: 'FLOAT', } # Maps variant to decoder method. _VARIANT_TO_DECODER_MAP = { messages.Variant.DOUBLE: _Decoder.getDouble, messages.Variant.FLOAT: _Decoder.getFloat, messages.Variant.INT64: _Decoder.getVarInt64, messages.Variant.UINT64: _Decoder.getVarUint64, messages.Variant.INT32: _Decoder.getVarInt32, messages.Variant.BOOL: _Decoder.decode_boolean, messages.Variant.STRING: _Decoder.decode_string, messages.Variant.MESSAGE: _Decoder.getPrefixedString, messages.Variant.BYTES: _Decoder.getPrefixedString, messages.Variant.UINT32: _Decoder.no_decoding, messages.Variant.ENUM: _Decoder.getVarInt32, messages.Variant.SINT32: _Decoder.no_decoding, messages.Variant.SINT64: _Decoder.no_decoding, } def encode_message(message): """Encode Message instance to protocol buffer. Args: Message instance to encode in to protocol buffer. Returns: String encoding of Message instance in protocol buffer format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() encoder = _Encoder() for field in sorted(message.all_fields(), key=lambda field: field.number): value = message.get_assigned_value(field.name) if value is not None: # Encode tag. tag = ((field.number << _WIRE_TYPE_BITS) | _VARIANT_TO_WIRE_TYPE[field.variant]) # Write value to wire. if field.repeated: values = value else: values = [value] for next in values: encoder.putVarInt32(tag) field_encoder = _VARIANT_TO_ENCODER_MAP[field.variant] field_encoder(encoder, next) return encoder.buffer().tostring() def decode_message(message_type, encoded_message): """Decode protocol buffer to Message instance. Args: message_type: Message type to decode data to. encoded_message: Encoded version of message as string. Returns: Decoded instance of message_type. Raises: DecodeError if an error occurs during decoding, such as incompatible wire format for a field. messages.ValidationError if merged message is not initialized. """ message = message_type() message_array = array.array('B') message_array.fromstring(encoded_message) try: decoder = _Decoder(message_array, 0, len(message_array)) while decoder.avail() > 0: # Decode tag and variant information. encoded_tag = decoder.getVarInt32() tag = encoded_tag >> _WIRE_TYPE_BITS wire_type = encoded_tag & _WIRE_TYPE_MASK try: found_wire_type_decoder = _WIRE_TYPE_TO_DECODER_MAP[wire_type] except: raise messages.DecodeError('No such wire type %d' % wire_type) if tag < 1: raise messages.DecodeError('Invalid tag value %d' % tag) try: field = message.field_by_number(tag) except KeyError: # Unexpected tags are ok, just ignored unless below 0. field = None wire_type_decoder = found_wire_type_decoder else: expected_wire_type = _VARIANT_TO_WIRE_TYPE[field.variant] if expected_wire_type != wire_type: raise messages.DecodeError('Expected wire type %s but found %s' % ( _WIRE_TYPE_NAME[expected_wire_type], _WIRE_TYPE_NAME[wire_type])) wire_type_decoder = _VARIANT_TO_DECODER_MAP[field.variant] value = wire_type_decoder(decoder) # Skip additional processing if unknown field. if not field: continue # Special case Enum and Message types. if isinstance(field, messages.EnumField): value = field.type(value) elif isinstance(field, messages.MessageField): nested_message = decode_message(field.type, value) value = nested_message # Merge value in to message. if field.repeated: values = getattr(message, field.name) if values is None: setattr(message, field.name, [value]) else: values.append(value) else: setattr(message, field.name, value) except ProtocolBuffer.ProtocolBufferDecodeError, err: raise messages.DecodeError('Decoding error: %s' % str(err)) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # application: tunes-db-client version: 1 api_version: 1 runtime: python handlers: - url: /(.*\.(png|css|js)) static_files: \1 upload: (.*\.(png|css|js)) - url: /.* script: main.py
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import os from google.appengine.api import apiproxy_stub_map from google.appengine.api import datastore_file_stub from protorpc import test_util # DO NOT SUBMIT: Just letting reviewer know that most of this # came from apphosting/ext/api_testutil.py class DatastoreTest(test_util.TestCase): """Base class for tests that require datastore.""" __apiproxy_initialized = False def setUp(self): """Set up the datastore.""" self.app_id = 'my-app' # Set environment variable for app id. os.environ['APPLICATION_ID'] = self.app_id # Don't use the filesystem with this stub. self.datastore_stub = datastore_file_stub.DatastoreFileStub( self.app_id, None) # Register stub. self.ResetApiProxyStubMap() apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', self.datastore_stub) def ResetApiProxyStubMap(self): """Reset the proxy stub-map. Args: force: When True, always reset the stubs regardless of their status. Must be called before stubs can be configured. Every time a new test is created, it is necessary to run with a brand new stub. The problem is that RegisterStub won't allow stubs to be replaced. If the global instance is not reset, it raises an exception when a run a new test gets run that wants to use a new stub. Calling this method more than once per APITest instance will only cause a new stub-map to be created once. Therefore it is called automatically during each Configure method. """ if self.__apiproxy_initialized: return self.__apiproxy_initialized = True apiproxy_stub_map.apiproxy = apiproxy_stub_map.GetDefaultAPIProxy()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tunes DB service implementation. This module contains all the protocol buffer and service definitions necessary for the Tunes DB service. """ import base64 import sys from google.appengine.ext import db from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import protobuf from protorpc import remote import model class Artist(messages.Message): """Musician or music group responsible for music production. Fields: artist_id: Unique opaque identifier for artist. name: User friendly name of artist. album_count: Number of albums produced by artist. """ artist_id = messages.StringField(1, required=True) name = messages.StringField(2, required=True) album_count = messages.IntegerField(3) class Album(messages.Message): """Album produced by a musician or music group. Fields: album_id: Unique opaque identifier for artist. artist_id: Artist id of musician or music group that produced album. name: Name of album. released: Year when album was released. """ album_id = messages.StringField(1, required=True) artist_id = messages.StringField(2, required=True) name = messages.StringField(3, required=True) released = messages.IntegerField(4) class AddArtistRequest(messages.Message): """Request to add a new Artist to library. Fields: name: User friendly name of artist. """ name = messages.StringField(1, required=True) class AddArtistResponse(messages.Message): """Response sent after creation of new artist in library. Fields: artist_id: Unique opaque ID of new artist. """ artist_id = messages.StringField(1, required=True) class UpdateArtistRequest(messages.Message): """Update an existing artist. Fields: artist: Complete information about artist to update. """ artist = messages.MessageField(Artist, 1, required=True) class UpdateArtistResponse(messages.Message): """Artist update response. Fields: artist_updated: Artist was found and updated. """ artist_updated = messages.BooleanField(1, required=True) class DeleteArtistRequest(messages.Message): """Delete artist from library. Fields: artist_id: Unique opaque ID of artist to delete. """ artist_id = messages.StringField(1, required=True) class DeleteArtistResponse(messages.Message): """Artist deletion response. Fields: artist_deleted: Artist was found and deleted. """ artist_deleted = messages.BooleanField(1, default=True) class FetchArtistRequest(messages.Message): """Fetch an artist from the library. Fields: artist_id: Unique opaque ID of artist to fetch. """ artist_id = messages.StringField(1, required=True) class FetchArtistResponse(messages.Message): """Fetched artist from library. Fields: artist: Artist found in library. """ artist = messages.MessageField(Artist, 1) class SearchArtistsRequest(messages.Message): """Artist search request. Fields: continuation: Continuation from the response of a previous call to search_artists remote method. fetch_size: Maximum number of records to retrieve. name_prefix: Name prefix of artists to search. If none provided and no continuation provided, search will be of all artists in library. If continuation is provided, name_prefix should be empty, if not, value is ignored. """ continuation = messages.StringField(1) fetch_size = messages.IntegerField(2, default=10) name_prefix = messages.StringField(3, default=u'') class SearchArtistsResponse(messages.Message): """Response from searching artists. Fields: artists: Artists found from search up to fetch_size. continuation: Opaque string that can be used with a new search request that will continue finding new artists where this response left off. Will not be set if there were no results from the search or fewer artists were returned in the response than requested, indicating the end of the query. """ artists = messages.MessageField(Artist, 1, repeated=True) continuation = messages.StringField(2) class AddAlbumRequest(messages.Message): """Request to add a new album to library. Fields: name: User friendly name of album. artist_id: Artist id of artist that produced record. released: Year album was released. """ name = messages.StringField(1, required=True) artist_id = messages.StringField(2, required=True) released = messages.IntegerField(3) class AddAlbumResponse(messages.Message): """Response sent after creation of new album in library. Fields: album_id: Unique opaque ID of new album. """ album_id = messages.StringField(1, required=True) class UpdateAlbumRequest(messages.Message): """Update an existing album. Fields: album: Complete information about album to update. """ album = messages.MessageField(Album, 1, required=True) class UpdateAlbumResponse(messages.Message): """Album update response. Fields: album_updated: Album was found and updated. """ album_updated = messages.BooleanField(1, required=True) class DeleteAlbumRequest(messages.Message): """Delete album from library. Fields: album_id: Unique opaque ID of album to delete. """ album_id = messages.StringField(1, required=True) class DeleteAlbumResponse(messages.Message): """Album deletion response. Fields: album_deleted: Album was found and deleted. """ album_deleted = messages.BooleanField(1, default=True) class FetchAlbumRequest(messages.Message): """Fetch an album from the library. Fields: album_id: Unique opaque ID of album to fetch. """ album_id = messages.StringField(1, required=True) class FetchAlbumResponse(messages.Message): """Fetched album from library. Fields: album: Album found in library. """ album = messages.MessageField(Album, 1) class SearchAlbumsRequest(messages.Message): """Album search request. Fields: continuation: Continuation from the response of a previous call to search_albums remote method. fetch_size: Maximum number of records to retrieve. name_prefix: Name prefix of albms to search. If none provided and no continuation provided, search will be of all albums in library. If continuation is provided, name_prefix should be empty, if not, value is ignored. artist_id: Restrict search to albums of single artist. """ continuation = messages.StringField(1) fetch_size = messages.IntegerField(2, default=10) name_prefix = messages.StringField(3, default=u'') artist_id = messages.StringField(4) class SearchAlbumsResponse(messages.Message): """Response from searching artists. Fields: albums: Albums found from search up to fetch_size. continuation: Opaque string that can be used with a new search request that will continue finding new albums where this response left off. Will not be set if there were no results from the search or fewer albums were returned in the response than requested, indicating the end of the query. """ albums = messages.MessageField(Album, 1, repeated=True) continuation = messages.StringField(2) class MusicLibraryService(remote.Service): """Music library service.""" __file_set = None def __artist_from_model(self, artist_model): """Helper that copies an Artist model to an Artist message. Args: artist_model: model.ArtistInfo instance to convert in to an Artist message. Returns: New Artist message with contents of artist_model copied in to it. """ return Artist(artist_id=unicode(artist_model.key()), name=artist_model.name, album_count=artist_model.album_count) def __album_from_model(self, album_model): """Helper that copies an Album model to an Album message. Args: album_model: model.AlbumInfo instance to convert in to an Album message. Returns: New Album message with contents of album_model copied in to it. """ artist_id = model.AlbumInfo.artist.get_value_for_datastore(album_model) return Album(album_id=unicode(album_model.key()), artist_id=artist_id, name=album_model.name, released=album_model.released or None) @classmethod def __search_info(cls, request, info_class, model_to_message, customize_query=None): """Search over an Info subclass. Since all search request classes are very similar, it's possible to generalize how to do searches over them. Args: request: Search request received from client. info_class: The model.Info subclass to search. model_to_method: Function (model) -> message that transforms an instance of info_class in to the appropriate messages.Message subclass. customize_query: Function (request, query) -> None that adds additional filters to Datastore query based on specifics of that search message. Returns: Tuple (results, continuation): results: A list of messages satisfying the parameters of the request. None if there are no results. continuation: Continuation string for response if there are more results available. None if there are no more results available. """ # TODO(rafek): fetch_size from this request should take priority # over what is stored in continuation. if request.continuation: encoded_search, continuation = request.continuation.split(':', 1) decoded_search = base64.urlsafe_b64decode(encoded_search.encode('utf-8')) request = protobuf.decode_message(type(request), decoded_search) else: continuation = None encoded_search = unicode(base64.urlsafe_b64encode( protobuf.encode_message(request))) name_prefix = request.name_prefix query = info_class.search(name_prefix) query.order('name') if customize_query: customize_query(request, query) if continuation: # TODO(rafek): Pure query cursors are not safe for model with # query restrictions. Would technically need to be encrypted. query.with_cursor(continuation) fetch_size = request.fetch_size model_instance = query.fetch(fetch_size) results = None continuation = None if model_instance: results = [model_to_message(i) for i in model_instance] if len(model_instance) == fetch_size: cursor = query.cursor() continuation = u'%s:%s' % (encoded_search, query.cursor()) return results, continuation @remote.method(AddArtistRequest, AddArtistResponse) def add_artist(self, request): """Add artist to library.""" artist_name = request.name def do_add(): artist = model.ArtistInfo(name=artist_name) artist.put() return artist artist = db.run_in_transaction(do_add) return AddArtistResponse(artist_id = unicode(artist.key())) @remote.method(UpdateArtistRequest, UpdateArtistResponse) def update_artist(self, request): """Update artist from library.""" def do_deletion(): artist = model.ArtistInfo.get(request.artist.artist_id) if artist: artist.name = request.artist.name artist.put() return True else: return False return UpdateArtistResponse( artist_updated=db.run_in_transaction(do_deletion)) @remote.method(DeleteArtistRequest, DeleteArtistResponse) def delete_artist(self, request): """Delete artist from library.""" def do_deletion(): artist = model.ArtistInfo.get(request.artist_id) if artist: db.delete(model.Info.all(keys_only=True).ancestor(artist)) return True else: return False return DeleteArtistResponse( artist_deleted = db.run_in_transaction(do_deletion)) @remote.method(FetchArtistRequest, FetchArtistResponse) def fetch_artist(self, request): """Fetch artist from library.""" artist_model = model.ArtistInfo.get(request.artist_id) if isinstance(artist_model, model.ArtistInfo): artist = self.__artist_from_model(artist_model) else: artist = None return FetchArtistResponse(artist=artist) @remote.method(SearchArtistsRequest, SearchArtistsResponse) def search_artists(self, request): """Search library for artists.""" results, continuation = self.__search_info(request, model.ArtistInfo, self.__artist_from_model) return SearchArtistsResponse(artists=results or None, continuation=continuation or None) @remote.method(AddAlbumRequest, AddAlbumResponse) def add_album(self, request): """Add album to library.""" def create_album(): if not request.artist_id: raise ValueError('Request does not have artist-id.') artist = model.ArtistInfo.get(request.artist_id) if not artist: raise ValueError('No artist found for %s.' % request.artist_id) artist.album_count += 1 artist.put() album = model.AlbumInfo(name=request.name, released=request.released, artist=artist, parent=artist) album.put() return album album = db.run_in_transaction(create_album) return AddAlbumResponse(album_id=unicode(album.key())) @remote.method(UpdateAlbumRequest, UpdateAlbumResponse) def update_album(self, request): """Update album from library.""" def do_deletion(): album = model.AlbumInfo.get(request.album.album_id) if album: album.name = request.album.name album.released = request.album.released album.put() return True else: return False return UpdateAlbumResponse(album_updated=db.run_in_transaction(do_deletion)) @remote.method(DeleteAlbumRequest, DeleteAlbumResponse) def delete_album(self, request): """Delete album from library.""" def do_deletion(): album = model.AlbumInfo.get(request.album_id) artist = album.artist artist.album_count -= 1 artist.put() if album: db.delete(model.Info.all(keys_only=True).ancestor(album)) return True else: return False return DeleteAlbumResponse(album_deleted=db.run_in_transaction(do_deletion)) @remote.method(FetchAlbumRequest, FetchAlbumResponse) def fetch_album(self, request): """Fetch album from library.""" album_model = model.AlbumInfo.get(request.album_id) if isinstance(album_model, model.AlbumInfo): album = self.__album_from_model(album_model) else: album = None return FetchAlbumResponse(album=album) @remote.method(SearchAlbumsRequest, SearchAlbumsResponse) def search_albums(self, request): """Search library for albums.""" def customize_query(request, query): if request.artist_id: query.filter('artist', db.Key(request.artist_id)) response = SearchAlbumsResponse() results, continuation = self.__search_info(request, model.AlbumInfo, self.__album_from_model, customize_query) return SearchAlbumsResponse(albums=results or None, continuation=continuation or None)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Forms interface library for Services API demo. This contains a set of handlers that will add an automatically generated form for each API call of a service for any number of services that is defined within an App engine webapp. The fields of the form interface generated for each RPC directly represents a corresponding field in the underlying request protocol message. The form is generated in a way such that when a POST is sent to the server, the body will be compatible with service_handlers.URLEncodedRPCMapper. Usage: main.py: class Hello(messages.Message): greeting = messages.StringField(1, required=True) class HelloService(remote.Service): @remote.method(message_types.VoidMessageType, Hello) def world(self, request): response = Hello() response.greeting = 'Hello World!' return response registry = ServiceRegistry() registry.register_service('/hello', HelloService) application = webapp.WSGIApplication(services_registry.mappings()) main.html: <form action='http://mywebapp.example.com/hello/world' method='POST'> <input type="submit" name="submit" value="Get Greeting"> </form> Response from post (content-type application/x-www-form-urlencoded): greeting=Hello World! """ __author__ = 'rafek@google.com (Rafe Kaplan)' import logging import os import sys from google.appengine.ext import webapp from google.appengine.ext.webapp import template from protorpc import descriptor from protorpc import messages from protorpc import remote from protorpc import service_handlers from django.utils import simplejson as json _LOCAL_DIRECTORY = os.path.dirname(__file__) def message_to_dict(message): """Convert a Message instance to a simple dictionary. Nested objects are themselves converted to dictionaries. Enumerated types are returned as their string representation. The main way to achieve building the a nested object is to define an inner recursive function. The outer function's only responsibility is to check the parameters and ensure the message is initialized. Values that are set to their defaults are not stored in the dictionary. Args: message: Message instance to convert to dictionary. Returns: Dictionary representing the content of the message. Raises: TypeError if no message is provided. """ def dict_value(value): """Convert any value to what is appropriate for use in the result. This function recurses when converting elements of repeated fields and nested messages. Args: value: Any message or value stored in a message, including lists from repeated fields. Returns: A value appropriate for storing in the dictionary representation of a message. Raises: TypeError if an unexpected type is found in a Message. """ if value is None or isinstance(value, (int, long, float, basestring)): return value if isinstance(value, (list, tuple)): return [dict_value(item) for item in value] if isinstance(value, messages.Enum): return str(value) if isinstance(value, messages.Message): result = {} for field in value.all_fields(): field_value = getattr(value, field.name) field_dict_value = dict_value(field_value) if field_value is None or field_value in (field.default, (), []): continue result[field.name] = field_dict_value return result raise TypeError('Unexpected type') if not isinstance(message, messages.Message): raise TypeError('Must provide message.') message.check_initialized() return dict_value(message) class ServiceRegistry(object): """Service registry for tracking all services in a webapp. Central registry where service classes are registered at runtime. The registry can provide standard mappings giving a web based forms interface to all services registered with it. Services are registered by mapping a URL path to the service class. """ def __init__(self, forms_path='/forms'): """Constructor. Args: forms_path: Path on server where the main page for the forms interface is. Attributes: forms_path: The main forms page that will show all services and methods. """ # __registration_order: List of service URL path mappings used to preserve # the order in which services were registered. Services are listed in the # web-app mapping in the original order that they were registered. # __services: Mapping (url_path) -> service_factory: # url_path: URL path that service is mapped to in web-app. # service_factory: A service handler factory for a service. self.forms_path = forms_path self.__registration_order = [] self.__services = {} @property def services(self): """Map of services registered in registry.""" return dict(self.__services) def register_service(self, url_path, service_class): """Register a service with the registry. Args: url_path: URL path to map service to within webapp. This is the URL that most clients that are not using the form interface will use to send requests to. service_class: The service class to map. """ service_factory = service_handlers.ServiceHandlerFactory(service_class) service_factory.add_request_mapper(FormURLEncodedRPCMapper()) service_factory.add_request_mapper(service_handlers.URLEncodedRPCMapper()) service_factory.add_request_mapper(service_handlers.ProtobufRPCMapper()) service_factory.add_request_mapper(service_handlers.JSONRPCMapper()) self.__registration_order.append(url_path) self.__services[url_path] = service_factory def mappings(self): """Generate a list of mappings for all services and form interfaces. Returns: A list of web-app mappings. Creates the following mappings: - A main page as specific by self.forms_path containing a list of all services and methods with links to form pages for each method. - A mapping to each service as defined during registration. - Under each service URL a form/file_set URL used for fetching the complete JSON representation of the file-set descriptor for that service. """ mappings = [(self.forms_path, FormHandler.factory(self)), ] for url_path in self.__registration_order: # TODO(rafek): Probably should map all these to /forms/file_set instead. invoke_path = '%s/form/method.(.*)' % url_path file_set_path = '%s/form/file_set' % url_path mappings.extend([ (invoke_path, InvocationHandler.factory(self, url_path)), (file_set_path, FileSetHandler.factory(self, url_path)), ]) service_factory = self.__services[url_path] rpc_mapping = service_factory.mapping(url_path) mappings.append(rpc_mapping) return mappings class _HandlerBase(webapp.RequestHandler): """Base class for all form handlers.""" def __init__(self, registry): """Constructor. Args: registry: Service registry to use with form interface. """ self.registry = registry @classmethod def template_file(self, name): """Resolve the path to a template file. Args: name: Relative file name to template file from this Python file. """ return os.path.join(_LOCAL_DIRECTORY, name) @classmethod def factory(cls, *args): """Create factory for service handler. Args: args: Parameters to pass to constructor of handler when instantiated by the webapp framework. Returns: Factory that creates new instances of form handler. """ def form_handler_factory(): return cls(*args) return form_handler_factory @property def service_class(self): return self.registry.services[self.url_path].service_factory class FormHandler(_HandlerBase): """Main forms page handler. Displays all services with links to methods web forms. """ def get(self): registry_view = [] services = self.registry.services for url_path in sorted(services): service_class = services[url_path].service_factory service_descriptor = descriptor.describe_service(service_class) registry_view.append((url_path, service_class.definition_name(), service_descriptor)) self.response.out.write( template.render(self.template_file('forms_static/services.html'), {'registry': registry_view, 'forms_path': self.registry.forms_path, })) # TODO(rafek): Better and easier to just embed the file-set in the main # invocation handler GET response. class FileSetHandler(_HandlerBase): """Retrieve the whole file-set descriptors in JSON for a service. The invocation GET response contains an AJAX call to fetch a JSON representation of the file-set for a service in order to dynamically build a form. This handler will send that JSON response. See apphosting.ext.services.descriptor.FileSet for more information about file-sets. """ def __init__(self, registry, url_path): """Constructor. Args: registry: Service registry to use with form interface. url_path: URL path of service to get file-set for. """ super(FileSetHandler, self).__init__(registry) self.url_path = url_path def get(self): # TODO(rafek): This needs to look for external references. Right now just # builds descriptor for immediate service module which works for the demo. module = sys.modules[self.service_class.__module__] file_set = descriptor.describe_file_set([module]) self.response.headers['content-type'] = 'application/json' self.response.out.write(json.dumps(message_to_dict(file_set))) class FormURLEncodedRPCMapper(service_handlers.URLEncodedRPCMapper): def build_response(self, handler, response): """Build text/plain response. Response is returned as a pretty-formatted JSON string as a text/plain content-type. Args: handler: RequestHandler instance that is servicing request. response: Response message as returned from the service object. """ handler.response.headers['Content-Type'] = 'text/plain' handler.response.out.write(json.dumps(message_to_dict(response), indent=2)) def match_request(self, handler): """Match a URL encoded request. Requests for the human readable response format should have a parameter 'response_format' set to 'text/plain'. Args: handler: RequestHandler instance that is servicing request. Returns: True if the request is a valid URL encoded RPC request, else False. """ if not handler.request.get('response_format') == 'text/plain': return False return super(FormURLEncodedRPCMapper, self).match_request(handler) # TODO(rafek): Maybe there is no need to have a separate POST handler here. # Implement the JSON protobuf protocol and have the invocation handler generate # the form and the response entirely dyanamically. class InvocationHandler(_HandlerBase): """Present method form and handle form POST RPCs.""" def __init__(self, registry, url_path): """Constructor. Args: registry: Service registry to use with form interface. url_path: URL path of service to get file-set for. """ super(InvocationHandler, self).__init__(registry) self.url_path = url_path def get(self, remote_method): """Display a form for requested method of service. The invoke.html template will contain a function that will call back to the file-set handler of the service. The invoke.js script will complete the RPC form for the method when it receives the file-set. Args: remote_method: Name of method for service to display. """ service_method = getattr(self.service_class, remote_method) method = descriptor.describe_method(service_method) self.response.out.write( template.render(self.template_file('forms_static/invoke.html'), {'service_path': self.url_path, 'service_name': self.service_class.definition_name(), 'forms_path': self.registry.forms_path, 'method': method, 'script': 'invoke.js' }))
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # __author__ = 'rafek@google.com (Rafe Kaplan)' from google.appengine.ext import webapp from google.appengine.ext.webapp import util class MainHandler(webapp.RequestHandler): def get(self): self.redirect('/protorpc/form') application = webapp.WSGIApplication( [('/', MainHandler)], debug=True) def main(): util.run_wsgi_app(application) if __name__ == '__main__': main()
Python
#!/usr/bin/python2.4 # # Copyright 2010 Google Inc. All Rights Reserved. """Tests for google3.apphosting.demos.tunes_db.server.tunes_db.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import message_types from protorpc import descriptor import datastore_test_util import model import tunes_db class MusicLibraryServiceTest(datastore_test_util.DatastoreTest): """Music library service tests.""" def setUp(self): """Set up music library.""" super(MusicLibraryServiceTest, self).setUp() self.service = tunes_db.MusicLibraryService() self.void = message_types.VoidMessage() # Create a bunch of artists. # # Definitions are not defined in order to test that search order is # correct. self.aem = model.ArtistInfo(name='Amadou et Miriam') self.aem.put() self.abba = model.ArtistInfo(name='Abba') self.abba.put() self.furnaces = model.ArtistInfo(name='The Fiery Furnaces') self.furnaces.put() self.go_team = model.ArtistInfo(name=' The Go! Team ') self.go_team.put() self.wendy_carlos = model.ArtistInfo(name='Wendy Carlos') self.wendy_carlos.put() self.beatles = model.ArtistInfo(name='The Beatles') self.beatles.put() # Albums. # The Go! Team. self.get_it_together = model.AlbumInfo(name='Get It Together', released=2000, artist=self.go_team, parent=self.go_team) self.get_it_together.put() self.thunder_lightening_strike = model.AlbumInfo( name='Thunder, Lightning, Strike', released=2004, artist=self.go_team, parent=self.go_team) self.thunder_lightening_strike.put() self.proof_of_youth = model.AlbumInfo(name='Proof of Youth', released=2007, artist=self.go_team, parent=self.go_team) self.proof_of_youth.put() # The Beatles. self.help = model.AlbumInfo(name='Help', released=1965, artist=self.beatles, parent=self.beatles) self.help.put() self.yellow_submarine = model.AlbumInfo(name='Yellow Submarine', released=1969, artist=self.beatles, parent=self.beatles) self.yellow_submarine.put() def AssertArtistMatches(self, model_instance, message): """Helper function to assert that artist message matches a model instance. Args: model_instance: Datastore model instance of artist to check against. message: Artist message to check match for. """ self.assertEquals(str(model_instance.key()), message.artist_id) self.assertEquals(str(model_instance.name), message.name) def AssertAlbumMatches(self, model_instance, message): """Helper function to assert that album message matches a model instance. Args: model_instance: Datastore model instance of album to check against. message: Album message to check match for. """ self.assertEquals(model_instance.name, message.name) self.assertEquals(str(model_instance.key()), message.album_id) self.assertEquals(model_instance.released, message.released) self.assertEquals(str(model_instance.artist.key()), message.artist_id) def testAddArtist(self): """Test the add_artist remote method.""" request = tunes_db.AddArtistRequest() request.name = 'Elvis Costello' response = self.service.add_artist(request) response.check_initialized() elvis = model.ArtistInfo.get(response.artist_id) self.assertEquals('Elvis Costello', elvis.name) def testUpdateArtist(self): """Test updating artist.""" artist = tunes_db.Artist() artist.artist_id = str(self.wendy_carlos.key()) artist.name = u'Walter Carlos' request = tunes_db.UpdateArtistRequest() request.artist = artist response = self.service.update_artist(request) response.check_initialized() self.assertTrue(response.artist_updated) walter_carlos = model.ArtistInfo.get(self.wendy_carlos.key()) self.assertEquals(u'Walter Carlos', walter_carlos.name) def testUpdateArtist_NotFound(self): """Test updating artist when artist no longer exists.""" wendy_carlos_key = self.wendy_carlos.key() self.wendy_carlos.delete() artist = tunes_db.Artist() artist.artist_id = str(wendy_carlos_key) artist.name = u'Walter Carlos' request = tunes_db.UpdateArtistRequest() request.artist = artist response = self.service.update_artist(request) response.check_initialized() self.assertFalse(response.artist_updated) self.assertEquals(None, model.ArtistInfo.get(wendy_carlos_key)) def testDeleteArtist(self): """Test the delete_artist remote method.""" request = tunes_db.DeleteArtistRequest() request.artist_id = str(self.go_team.key()) response = self.service.delete_artist(request) response.check_initialized() self.assertEquals(True, response.artist_deleted) self.assertEquals(None, model.AlbumInfo.get(self.go_team.key())) self.assertEquals(None, model.AlbumInfo.gql('WHERE artist = :1', self.go_team.key()).get()) response = self.service.delete_artist(request) self.assertEquals(False, response.artist_deleted) def testFetchArtist(self): """Test the fetch_artist remote method.""" request = tunes_db.FetchArtistRequest() request.artist_id = str(self.wendy_carlos.key()) response = self.service.fetch_artist(request) response.check_initialized() self.AssertArtistMatches(self.wendy_carlos, response.artist) def testSearchArtist_NothingFound(self): """Test the search_artists remote method when no artists are found.""" request = tunes_db.SearchArtistsRequest() request.name_prefix = u'Duke' response = self.service.search_artists(request) response.check_initialized() self.assertFalse(hasattr(response, 'artist_count')) def testSearchArtist_All(self): """Test searching all artists in the music library.""" request = tunes_db.SearchArtistsRequest() request.fetch_size = 3 response = self.service.search_artists(request) response.check_initialized() self.assertEquals(3, len(response.artists)) self.AssertArtistMatches(self.abba, response.artists[0]) self.AssertArtistMatches(self.aem, response.artists[1]) self.AssertArtistMatches(self.beatles, response.artists[2]) request = tunes_db.SearchArtistsRequest() request.continuation = response.continuation request.fetch_size = 3 response = self.service.search_artists(request) response.check_initialized() self.assertEquals(3, len(response.artists)) self.AssertArtistMatches(self.furnaces, response.artists[0]) self.AssertArtistMatches(self.go_team, response.artists[1]) self.AssertArtistMatches(self.wendy_carlos, response.artists[2]) request = tunes_db.SearchArtistsRequest() request.continuation = response.continuation response = self.service.search_artists(request) response.check_initialized() self.assertEquals(None, response.artists) def testSearchArtist_NamePrefix(self): """Test searching artists and matching by name prefix.""" request = tunes_db.SearchArtistsRequest() request.name_prefix = u' { tHe! ' request.fetch_size = 2 response = self.service.search_artists(request) response.check_initialized() self.assertEquals(2, len(response.artists)) self.AssertArtistMatches(self.beatles, response.artists[0]) self.AssertArtistMatches(self.furnaces, response.artists[1]) request = tunes_db.SearchArtistsRequest() request.continuation = response.continuation response = self.service.search_artists(request) response.check_initialized() self.assertEquals(1, len(response.artists)) self.assertEquals(None, response.continuation) self.AssertArtistMatches(self.go_team, response.artists[0]) def testAddAlbum(self): """Test creating an album.""" request = tunes_db.AddAlbumRequest() request.artist_id = str(self.furnaces.key()) request.name = u'Blueberry Boat' request.released = 2004 response = self.service.add_album(request) response.check_initialized() blueberry = model.AlbumInfo.get(response.album_id) self.assertEquals(self.furnaces.key(), blueberry.artist.key()) self.assertEquals('Blueberry Boat', blueberry.name) self.assertEquals(2004, blueberry.released) def testUpdateAlbum(self): """Test updating album.""" album = tunes_db.Album() album.album_id = str(self.proof_of_youth.key()) album.name = u'Proof of Age' album.released = 1908 request = tunes_db.UpdateAlbumRequest() request.album = album response = self.service.update_album(request) response.check_initialized() self.assertTrue(response.album_updated) proof_of_age = model.AlbumInfo.get(self.proof_of_youth.key()) self.assertEquals(u'Proof of Age', proof_of_age.name) self.assertEquals(1908, proof_of_age.released) def testUpdateAlbum_NotFound(self): """Test updating album when album no longer exists.""" proof_of_youth_key = self.proof_of_youth.key() self.proof_of_youth.delete() album = tunes_db.Album() album.album_id = str(proof_of_youth_key) album.name = u'Proof of Age' album.released = 1908 request = tunes_db.UpdateAlbumRequest() request.album = album response = self.service.update_album(request) response.check_initialized() self.assertFalse(response.album_updated) self.assertEquals(None, model.AlbumInfo.get(proof_of_youth_key)) def testSearchAlbum_All(self): """Test searching all albums in library.""" request = tunes_db.SearchAlbumsRequest() request.fetch_size = 2 response = self.service.search_albums(request) response.check_initialized() self.assertEquals(2, len(response.albums)) self.AssertAlbumMatches(self.get_it_together, response.albums[0]) self.AssertAlbumMatches(self.help, response.albums[1]) request = tunes_db.SearchAlbumsRequest() request.continuation = response.continuation response = self.service.search_albums(request) response.check_initialized() self.assertEquals(2, len(response.albums)) self.AssertAlbumMatches(self.proof_of_youth, response.albums[0]) self.AssertAlbumMatches(self.thunder_lightening_strike, response.albums[1]) request = tunes_db.SearchAlbumsRequest() request.continuation = response.continuation response = self.service.search_albums(request) response.check_initialized() self.assertEquals(1, len(response.albums)) self.assertEquals(None, response.continuation) self.AssertAlbumMatches(self.yellow_submarine, response.albums[0]) def testSearchAlbum_FilterArtist(self): """Test searching albums for specific artist in library.""" request = tunes_db.SearchAlbumsRequest() request.fetch_size = 2 request.artist_id = unicode(self.go_team.key()) response = self.service.search_albums(request) response.check_initialized() self.assertEquals(2, len(response.albums)) self.AssertAlbumMatches(self.get_it_together, response.albums[0]) self.AssertAlbumMatches(self.proof_of_youth, response.albums[1]) request = tunes_db.SearchAlbumsRequest() request.continuation = response.continuation response = self.service.search_albums(request) response.check_initialized() self.assertEquals(1, len(response.albums)) self.assertEquals(None, response.continuation) self.AssertAlbumMatches(self.thunder_lightening_strike, response.albums[0]) def testGetFileSet(self): """Test getting the package set for the music service.""" request = message_types.VoidMessage() expected = descriptor.describe_file_set([tunes_db]) response = self.service.get_file_set(request) response.check_initialized() self.assertEquals(expected, response) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import os import re from google.appengine.ext import webapp from google.appengine.ext.webapp import util from protorpc import service_handlers import protorpc_appstats # This regular expression is used to extract the full path of # an incoming request so that the service can be correctly # registered with its internal registry. The only assumption # that is made about the placement of the appstats service by # this main module is that the last element of the service path # is 'service'. _METHOD_REGEX = r'\.[^/]+' _SERVICE_PATH_REGEX = re.compile(r'(.*)/service(%s|/protorpc(%s)?)?' % (_METHOD_REGEX, _METHOD_REGEX)) def parse_service_path(path_info): """Parse the service path from PATH_INFO in the environment. The appstats service may be placed at any URL path within a webapp application. It isn't possible to know what the actual path is until the actual request time. This function attempts to parse the incoming request to determine where the appstats service is configured. If it can successfully determine its location, it will attempt to map protorpc RegistryService underneath its service path. The appstats service is always expected to be <path>/service. The RegistryService is mapped to <path>/service/protorpc. Args: path_info: PATH_INFO extracted from the CGI environment. Returns: A pair paths (appstats_service_path, registry_service_path): appstats_service_path: The full path of the appstats service. If the full path cannot be determined this will be '.*/service'. registry_service_path: The full path of the appstats registry service. If the path of the appstats service cannot be determined this will be None. """ match = _SERVICE_PATH_REGEX.match(path_info) if match: appstats_service_path = '%s/service' % (match.group(1),) # Creates a "local" registry service. registry_service_path = '%s/protorpc' % (appstats_service_path,) else: # Not possible to determine full service name for registry. Do # not create registry for service. appstats_service_path = '.*/service' registry_service_path = None return appstats_service_path, registry_service_path def main(): path_info = os.environ.get('PATH_INFO', '') service_path, registry_path = parse_service_path(path_info) # Create webapp URL mappings for service and private registry. mapping = service_handlers.service_mapping( [(service_path, protorpc_appstats.AppStatsService)], registry_path) application = webapp.WSGIApplication(mapping) util.run_wsgi_app(application) if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import cStringIO import logging import os from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import stub from google.appengine.api import memcache from google.appengine.ext.appstats import recording # Import contents of appstats.descriptor in to this module from binary appstats # protobuf descriptor. Definitions are imported into module apphosting. stub.import_file_set(os.path.join(os.path.dirname(__file__), 'appstats.descriptor')) import apphosting class Summary(messages.Message): """Response for AppStatsService.get_summary. Fields: stats: List of RequestStatProto objects summarizing application activity. """ stats = messages.MessageField(apphosting.RequestStatProto, 1, repeated=True) class GetDetailsRequest(messages.Message): """Request for AppStatsService.get_details. Fields: timestamp: Timestamp of appstats detail to retrieve. """ timestamp = messages.IntegerField(1, required=True) class Details(messages.Message): """Response for AppStatsService.get_details. Fields: stat: Individual stat details if found, else None. """ stat = messages.MessageField(apphosting.RequestStatProto, 1) # TODO(rafek): Remove this function when recording.load_summary_protos is # refactored in the App Engine SDK. def load_summary_protos(): """Load all valid summary records from memcache. Returns: A list of RequestStatProto instances, in reverse chronological order (i.e. most recent first). NOTE: This is limited to returning at most config.KEY_MODULUS records, since there are only that many distinct keys. See also make_key(). """ tmpl = '%s%s%s' % (recording.config.KEY_PREFIX, recording.config.KEY_TEMPLATE, recording.config.PART_SUFFIX) keys = [tmpl % i for i in range(0, recording.config.KEY_DISTANCE * recording.config.KEY_MODULUS, recording.config.KEY_DISTANCE)] results = memcache.get_multi(keys, namespace=recording.config.KEY_NAMESPACE) records = [] for rec in results.itervalues(): try: pb = protobuf.decode_message(apphosting.RequestStatProto, rec) except Exception, err: logging.warn('Bad record: %s', err) else: records.append(pb) logging.info('Loaded %d raw records, %d valid', len(results), len(records)) # Sorts by time, newest first. records.sort(key=lambda pb: -pb.start_timestamp_milliseconds) return records # TODO(rafek): Remove this function when recording.load_full_protos is # refactored in the App Engine SDK. def load_full_proto(timestamp): """Load the full record for a given timestamp. Args: timestamp: The start_timestamp of the record, as a float in seconds (see make_key() for details). Returns: A RequestStatProto instance if the record exists and can be loaded; None otherwise. """ full_key = recording.make_key(timestamp) + recording.config.FULL_SUFFIX full_binary = memcache.get(full_key, namespace=recording.config.KEY_NAMESPACE) if full_binary is None: logging.info('No full record at %s', full_key) return None try: full = protobuf.decode_message(apphosting.RequestStatProto, full_binary) except Exception, err: logging.warn('Bad full record at %s: %s', full_key, err) return None if full.start_timestamp_milliseconds != int(timestamp * 1000): logging.warn('Hash collision, record at %d has timestamp %d', int(timestamp * 1000), full.start_timestamp_milliseconds) return None # Hash collision -- the requested record no longer exists. return full class AppStatsService(remote.Service): """Service for getting access to AppStats data.""" @remote.method(message_types.VoidMessage, Summary) def get_summary(self, request): """Get appstats summary.""" response = Summary() response.stats = load_summary_protos() return response @remote.method(GetDetailsRequest, Details) def get_details(self, request): """Get appstats details for a particular timestamp.""" response = Details() recording_timestamp = request.timestamp * 0.001 logging.error('Fetching recording from %f', recording_timestamp) response.stat = load_full_proto(recording_timestamp) return response
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Simple protocol message types.""" __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import messages class VoidMessage(messages.Message): """Empty message."""
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.util.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import random import unittest from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = util class UtilTest(test_util.TestCase): def testDecoratedFunction_LengthZero(self): @util.positional(0) def fn(kwonly=1): return [kwonly] self.assertEquals([1], fn()) self.assertEquals([2], fn(kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 0 positional ' r'arguments \(1 given\)', fn, 1) def testDecoratedFunction_LengthOne(self): @util.positional(1) def fn(pos, kwonly=1): return [pos, kwonly] self.assertEquals([1, 1], fn(1)) self.assertEquals([2, 2], fn(2, kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 1 positional ' r'argument \(2 given\)', fn, 2, 3) def testDecoratedFunction_LengthTwoWithDefault(self): @util.positional(2) def fn(pos1, pos2=1, kwonly=1): return [pos1, pos2, kwonly] self.assertEquals([1, 1, 1], fn(1)) self.assertEquals([2, 2, 1], fn(2, 2)) self.assertEquals([2, 3, 4], fn(2, 3, kwonly=4)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 2 positional ' r'arguments \(3 given\)', fn, 2, 3, 4) def testDecoratedMethod(self): class MyClass(object): @util.positional(2) def meth(self, pos1, kwonly=1): return [pos1, kwonly] self.assertEquals([1, 1], MyClass().meth(1)) self.assertEquals([2, 2], MyClass().meth(2, kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'meth\(\) takes at most 2 positional ' r'arguments \(3 given\)', MyClass().meth, 2, 3) def testDefaultDecoration(self): @util.positional def fn(a, b, c=None): return a, b, c self.assertEquals((1, 2, 3), fn(1, 2, c=3)) self.assertEquals((3, 4, None), fn(3, b=4)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 2 positional ' r'arguments \(3 given\)', fn, 2, 3, 4) class AcceptItemTest(test_util.TestCase): def CheckAttributes(self, item, main_type, sub_type, q=1, values={}, index=1): self.assertEquals(index, item.index) self.assertEquals(main_type, item.main_type) self.assertEquals(sub_type, item.sub_type) self.assertEquals(q, item.q) self.assertEquals(values, item.values) def testParse(self): self.CheckAttributes(util.AcceptItem('*/*', 1), None, None) self.CheckAttributes(util.AcceptItem('text/*', 1), 'text', None) self.CheckAttributes(util.AcceptItem('text/plain', 1), 'text', 'plain') self.CheckAttributes( util.AcceptItem('text/plain; q=0.3', 1), 'text', 'plain', 0.3, values={'q': '0.3'}) self.CheckAttributes( util.AcceptItem('text/plain; level=2', 1), 'text', 'plain', values={'level': '2'}) self.CheckAttributes( util.AcceptItem('text/plain', 10), 'text', 'plain', index=10) def testCaseInsensitive(self): self.CheckAttributes(util.AcceptItem('Text/Plain', 1), 'text', 'plain') def testBadValue(self): self.assertRaises(util.AcceptError, util.AcceptItem, 'bad value', 1) self.assertRaises(util.AcceptError, util.AcceptItem, 'bad value/', 1) self.assertRaises(util.AcceptError, util.AcceptItem, '/bad value', 1) def testSortKey(self): item = util.AcceptItem('main/sub; q=0.2; level=3', 11) self.assertEquals((False, False, -0.2, False, 11), item.sort_key) item = util.AcceptItem('main/*', 12) self.assertEquals((False, True, -1, True, 12), item.sort_key) item = util.AcceptItem('*/*', 1) self.assertEquals((True, True, -1, True, 1), item.sort_key) def testSort(self): i1 = util.AcceptItem('text/*', 1) i2 = util.AcceptItem('text/html', 2) i3 = util.AcceptItem('text/html; q=0.9', 3) i4 = util.AcceptItem('text/html; q=0.3', 4) i5 = util.AcceptItem('text/xml', 5) i6 = util.AcceptItem('text/html; level=1', 6) i7 = util.AcceptItem('*/*', 7) items = [i1, i2 ,i3 ,i4 ,i5 ,i6, i7] random.shuffle(items) self.assertEquals([i6, i2, i5, i3, i4, i1, i7], sorted(items)) def testMatchAll(self): item = util.AcceptItem('*/*', 1) self.assertTrue(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertTrue(item.match('image/png')) self.assertTrue(item.match('image/png; q=0.3')) def testMatchMainType(self): item = util.AcceptItem('text/*', 1) self.assertTrue(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertFalse(item.match('image/png')) self.assertFalse(item.match('image/png; q=0.3')) def testMatchFullType(self): item = util.AcceptItem('text/plain', 1) self.assertFalse(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertFalse(item.match('image/png')) self.assertFalse(item.match('image/png; q=0.3')) def testMatchCaseInsensitive(self): item = util.AcceptItem('text/plain', 1) self.assertTrue(item.match('tExt/pLain')) def testStr(self): self.assertEquals('*/*', str(util.AcceptItem('*/*', 1))) self.assertEquals('text/*', str(util.AcceptItem('text/*', 1))) self.assertEquals('text/plain', str(util.AcceptItem('text/plain', 1))) self.assertEquals('text/plain; q=0.2', str(util.AcceptItem('text/plain; q=0.2', 1))) self.assertEquals('text/plain; q=0.2; level=1', str(util.AcceptItem('text/plain; level=1; q=0.2', 1))) def testRepr(self): self.assertEquals("AcceptItem('*/*', 1)", repr(util.AcceptItem('*/*', 1))) self.assertEquals("AcceptItem('text/plain', 11)", repr(util.AcceptItem('text/plain', 11))) def testValues(self): item = util.AcceptItem('text/plain; a=1; b=2; c=3;', 1) values = item.values self.assertEquals(dict(a="1", b="2", c="3"), values) values['a'] = "7" self.assertNotEquals(values, item.values) class ParseAcceptHeaderTest(test_util.TestCase): def testIndex(self): accept_header = """text/*, text/html, text/html; q=0.9, text/xml, text/html; level=1, */*""" accepts = util.parse_accept_header(accept_header) self.assertEquals(6, len(accepts)) self.assertEquals([4, 1, 3, 2, 0, 5], [a.index for a in accepts]) class ChooseContentTypeTest(test_util.TestCase): def testIgnoreUnrequested(self): self.assertEquals('application/json', util.choose_content_type( 'text/plain, application/json, */*', ['application/X-Google-protobuf', 'application/json' ])) def testUseCorrectPreferenceIndex(self): self.assertEquals('application/json', util.choose_content_type( '*/*, text/plain, application/json', ['application/X-Google-protobuf', 'application/json' ])) def testPreferFirstInList(self): self.assertEquals('application/X-Google-protobuf', util.choose_content_type( '*/*', ['application/X-Google-protobuf', 'application/json' ])) def testCaseInsensitive(self): self.assertEquals('application/X-Google-protobuf', util.choose_content_type( 'application/x-google-protobuf', ['application/X-Google-protobuf', 'application/json' ])) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Transport library for ProtoRPC. Contains underlying infrastructure used for communicating RPCs over low level transports such as HTTP. Includes HTTP transport built over urllib2. """ import logging import sys import urllib2 from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import util __all__ = [ 'RpcStateError', 'HttpTransport', 'Rpc', 'Transport', ] class RpcStateError(messages.Error): """Raised when trying to put RPC in to an invalid state.""" class Rpc(object): """Represents a client side RPC. An RPC is created by the transport class and is used with a single RPC. While an RPC is still in process, the response is set to None. When it is complete the response will contain the response message. """ def __init__(self, request): """Constructor. Args: request: Request associated with this RPC. """ self.__request = request self.__response = None self.__state = remote.RpcState.RUNNING self.__error_message = None self.__error_name = None @property def request(self): """Request associated with RPC.""" return self.__request @property def response(self): """Response associated with RPC.""" return self.__response @property def state(self): return self.__state @property def error_message(self): return self.__error_message @property def error_name(self): return self.__error_name def __set_state(self, state, error_message=None, error_name=None): if self.__state != remote.RpcState.RUNNING: raise RpcStateError( 'RPC must be in RUNNING state to change to %s' % state) if state == remote.RpcState.RUNNING: raise RpcStateError('RPC is already in RUNNING state') self.__state = state self.__error_message = error_message self.__error_name = error_name def set_response(self, response): # TODO: Even more specific type checking. if not isinstance(response, messages.Message): raise TypeError('Expected Message type, received %r' % (response)) self.__response = response self.__set_state(remote.RpcState.OK) def set_status(self, status): status.check_initialized() self.__set_state(status.state, status.error_message, status.error_name) class Transport(object): """Transport base class. Provides basic support for implementing a ProtoRPC transport such as one that can send and receive messages over HTTP. Implementations override _transport_rpc. This method receives an encoded response as determined by the transports configured protocol. The transport is expected to set the rpc response or raise an exception before termination. Asynchronous transports are not supported. """ @util.positional(1) def __init__(self, protocol=protobuf): """Constructor. Args: protocol: The protocol implementation. Must implement encode_message and decode_message. """ self.__protocol = protocol @property def protocol(self): """Protocol associated with this transport.""" return self.__protocol def send_rpc(self, remote_info, request): """Initiate sending an RPC over the transport. Args: remote_info: RemoteInfo instance describing remote method. request: Request message to send to service. Returns: An Rpc instance intialized with request and response. """ request.check_initialized() encoded_request = self.__protocol.encode_message(request) rpc = Rpc(request) self._transport_rpc(remote_info, encoded_request, rpc) return rpc def _transport_rpc(self, remote_info, encoded_request, rpc): """Transport RPC method. Args: remote_info: RemoteInfo instance describing remote method. encoded_request: Request message as encoded by transport protocol. rpc: Rpc instance associated with a single request. """ raise NotImplementedError() class HttpTransport(Transport): """Transport for communicating with HTTP servers.""" @util.positional(2) def __init__(self, service_url, protocol=protobuf): """Constructor. Args: service_url: URL where the service is located. All communication via the transport will go to this URL. protocol: The protocol implementation. Must implement encode_message and decode_message. """ super(HttpTransport, self).__init__(protocol=protocol) self.__service_url = service_url def __http_error_to_exception(self, http_error): error_code = http_error.code content_type = http_error.hdrs.get('content-type') if error_code == 500 and content_type == self.protocol.CONTENT_TYPE: try: rpc_status = self.protocol.decode_message(remote.RpcStatus, http_error.msg) except Exception, decode_err: logging.warning( 'An error occurred trying to parse status: %s\n%s', str(decode_err), http_error.msg) else: # TODO: Move the check_rpc_status to the Rpc.response property. # Will raise exception if rpc_status is in an error state. remote.check_rpc_status(rpc_status) def _transport_rpc(self, remote_info, encoded_request, rpc): """HTTP transport rpc method. Uses urllib2 as underlying HTTP transport. """ method_url = '%s.%s' % (self.__service_url, remote_info.method.func_name) http_request = urllib2.Request(method_url, encoded_request) http_request.add_header('content-type', self.protocol.CONTENT_TYPE) try: http_response = urllib2.urlopen(http_request) except urllib2.HTTPError, err: self.__http_error_to_exception(err) # TODO: Map other types of errors to appropriate exceptions. _, _, trace_back = sys.exc_info() raise remote.ServerError, (str(err), err), trace_back except urllib2.URLError, err: _, _, trace_back = sys.exc_info() if isinstance(err, basestring): error_message = err else: error_message = err.args[0] raise remote.NetworkError, (error_message, err), trace_back encoded_response = http_response.read() response = self.protocol.decode_message(remote_info.response_type, encoded_response) rpc.set_response(response)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # __author__ = 'rafek@google.com (Rafe Kaplan)' import contextlib from protorpc import messages from protorpc import util __all__ = ['IndentationError', 'IndentWriter', ] class IndentationError(messages.Error): """Raised when end_indent is called too many times.""" class IndentWriter(object): """Utility class to make it easy to write formatted indented text. IndentWriter delegates to a file-like object and is able to keep track of the level of indentation. Each call to write_line will write a line terminated by a new line proceeded by a number of spaces indicated by the current level of indentation. IndexWriter overloads the << operator to make line writing operations clearer. The indent method returns a context manager that can be used by the Python with statement that makes generating python code easier to use. For example: index_writer << 'def factorial(n):' with index_writer.indent(): index_writer << 'if n <= 1:' with index_writer.indent(): index_writer << 'return 1' index_writer << 'else:' with index_writer.indent(): index_writer << 'return factorial(n - 1)' This would generate: def factorial(n): if n <= 1: return 1 else: return factorial(n - 1) """ @util.positional(2) def __init__(self, output, indent_space=2): """Constructor. Args: output: File-like object to wrap. indent_space: Number of spaces each level of indentation will be. """ # Private attributes: # # __output: The wrapped file-like object. # __indent_space: String to append for each level of indentation. # __indentation: The current full indentation string. self.__output = output self.__indent_space = indent_space * ' ' self.__indentation = 0 @property def indent_level(self): """Current level of indentation for IndentWriter.""" return self.__indentation def write_line(self, line): """Write line to wrapped file-like object using correct indentation. The line is written with the current level of indentation printed before it and terminated by a new line. Args: line: Line to write to wrapped file-like object. """ self.__output.write(self.__indentation * self.__indent_space) self.__output.write(line) self.__output.write('\n') def begin_indent(self): """Begin a level of indentation.""" self.__indentation += 1 def end_indent(self): """Undo the most recent level of indentation. Raises: IndentationError when called with no indentation levels. """ if not self.__indentation: raise IndentationError('Unable to un-indent further') self.__indentation -= 1 @contextlib.contextmanager def indent(self): """Create indentation level compatible with the Python 'with' keyword.""" self.begin_indent() yield self.end_indent() def __lshift__(self, line): """Syntactic sugar for write_line method. Args: line: Line to write to wrapped file=like object. """ self.write_line(line)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """URL encoding support for messages types. Protocol support for URL encoded form parameters. Nested Fields: Nested fields are repesented by dot separated names. For example, consider the following messages: class WebPage(Message): title = StringField(1) tags = StringField(2, repeated=True) class WebSite(Message): name = StringField(1) home = MessageField(WebPage, 2) pages = MessageField(WebPage, 3, repeated=True) And consider the object: page = WebPage() page.title = 'Welcome to NewSite 2010' site = WebSite() site.name = 'NewSite 2010' site.home = page The URL encoded representation of this constellation of objects is. name=NewSite+2010&home.title=Welcome+to+NewSite+2010 An object that exists but does not have any state can be represented with a reference to its name alone with no value assigned to it. For example: page = WebSite() page.name = 'My Empty Site' page.home = WebPage() is represented as: name=My+Empty+Site&home= This represents a site with an empty uninitialized home page. Repeated Fields: Repeated fields are represented by the name of and the index of each value separated by a dash. For example, consider the following message: home = Page() home.title = 'Nome' news = Page() news.title = 'News' news.tags = ['news', 'articles'] instance = WebSite() instance.name = 'Super fun site' instance.pages = [home, news, preferences] An instance of this message can be represented as: name=Super+fun+site&page-0.title=Home&pages-1.title=News&... pages-1.tags-0=new&pages-1.tags-1=articles Helper classes: URLEncodedRequestBuilder: Used for encapsulating the logic used for building a request message from a URL encoded RPC. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import re import urllib from protorpc import messages from protorpc import util __all__ = ['CONTENT_TYPE', 'URLEncodedRequestBuilder', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/x-www-form-urlencoded' _FIELD_NAME_REGEX = re.compile(r'^([a-zA-Z_][a-zA-Z_0-9]*)(?:-([0-9]+))?$') class URLEncodedRequestBuilder(object): """Helper that encapsulates the logic used for building URL encoded messages. This helper is used to map query parameters from a URL encoded RPC to a message instance. """ @util.positional(2) def __init__(self, message, prefix=''): """Constructor. Args: message: Message instance to build from parameters. prefix: Prefix expected at the start of valid parameters. """ self.__parameter_prefix = prefix # The empty tuple indicates the root message, which has no path. # __messages is a full cache that makes it very easy to look up message # instances by their paths. See make_path for details about what a path # is. self.__messages = {(): message} # This is a cache that stores paths which have been checked for # correctness. Correctness means that an index is present for repeated # fields on the path and absent for non-repeated fields. The cache is # also used to check that indexes are added in the right order so that # dicontiguous ranges of indexes are ignored. self.__checked_indexes = set([()]) def make_path(self, parameter_name): """Parse a parameter name and build a full path to a message value. The path of a method is a tuple of 2-tuples describing the names and indexes within repeated fields from the root message (the message being constructed by the builder) to an arbitrarily nested message within it. Each 2-tuple node of a path (name, index) is: name: The name of the field that refers to the message instance. index: The index within a repeated field that refers to the message instance, None if not a repeated field. For example, consider: class VeryInner(messages.Message): ... class Inner(messages.Message): very_inner = messages.MessageField(VeryInner, 1, repeated=True) class Outer(messages.Message): inner = messages.MessageField(Inner, 1) If this builder is building an instance of Outer, that instance is referred to in the URL encoded parameters without a path. Therefore its path is (). The child 'inner' is referred to by its path (('inner', None)). The first child of repeated field 'very_inner' on the Inner instance is referred to by (('inner', None), ('very_inner', 0)). Examples: # Correct reference to model where nation is a Message, district is # repeated Message and county is any not repeated field type. >>> make_path('nation.district-2.county') (('nation', None), ('district', 2), ('county', None)) # Field is not part of model. >>> make_path('nation.made_up_field') None # nation field is not repeated and index provided. >>> make_path('nation-1') None # district field is repeated and no index provided. >>> make_path('nation.district') None Args: parameter_name: Name of query parameter as passed in from the request. in order to make a path, this parameter_name must point to a valid field within the message structure. Nodes of the path that refer to repeated fields must be indexed with a number, non repeated nodes must not have an index. Returns: Parsed version of the parameter_name as a tuple of tuples: attribute: Name of attribute associated with path. index: Postitive integer index when it is a repeated field, else None. Will return None if the parameter_name does not have the right prefix, does not point to a field within the message structure, does not have an index if it is a repeated field or has an index but is not a repeated field. """ if parameter_name.startswith(self.__parameter_prefix): parameter_name = parameter_name[len(self.__parameter_prefix):] else: return None path = [] name = [] message_type = type(self.__messages[()]) # Get root message. for item in parameter_name.split('.'): # This will catch sub_message.real_message_field.not_real_field if not message_type: return None item_match = _FIELD_NAME_REGEX.match(item) if not item_match: return None attribute = item_match.group(1) index = item_match.group(2) if index: index = int(index) try: field = message_type.field_by_name(attribute) except KeyError: return None if field.repeated != (index is not None): return None if isinstance(field, messages.MessageField): message_type = field.type else: message_type = None # Path is valid so far. Append node and continue. path.append((attribute, index)) return tuple(path) def __check_index(self, parent_path, name, index): """Check correct index use and value relative to a given path. Check that for a given path the index is present for repeated fields and that it is in range for the existing list that it will be inserted in to or appended to. Args: parent_path: Path to check against name and index. name: Name of field to check for existance. index: Index to check. If field is repeated, should be a number within range of the length of the field, or point to the next item for appending. """ # Don't worry about non-repeated fields. # It's also ok if index is 0 because that means next insert will append. if not index: return True parent = self.__messages.get(parent_path, None) value_list = getattr(parent, name, None) # If the list does not exist then the index should be 0. Since it is # not, path is not valid. if not value_list: return False # The index must either point to an element of the list or to the tail. return len(value_list) >= index def __check_indexes(self, path): """Check that all indexes are valid and in the right order. This method must iterate over the path and check that all references to indexes point to an existing message or to the end of the list, meaning the next value should be appended to the repeated field. Args: path: Path to check indexes for. Tuple of 2-tuples (name, index). See make_path for more information. Returns: True if all the indexes of the path are within range, else False. """ if path in self.__checked_indexes: return True # Start with the root message. parent_path = () for name, index in path: next_path = parent_path + ((name, index),) # First look in the checked indexes cache. if next_path not in self.__checked_indexes: if not self.__check_index(parent_path, name, index): return False self.__checked_indexes.add(next_path) parent_path = next_path return True def __get_or_create_path(self, path): """Get a message from the messages cache or create it and add it. This method will also create any parent messages based on the path. When a new instance of a given message is created, it is stored in __message by its path. Args: path: Path of message to get. Path must be valid, in other words __check_index(path) returns true. Tuple of 2-tuples (name, index). See make_path for more information. Returns: Message instance if the field being pointed to by the path is a message, else will return None for non-message fields. """ message = self.__messages.get(path, None) if message: return message parent_path = () parent = self.__messages[()] # Get the root object for name, index in path: field = parent.field_by_name(name) next_path = parent_path + ((name, index),) next_message = self.__messages.get(next_path, None) if next_message is None: message_type = field.type next_message = message_type() self.__messages[next_path] = next_message if not field.repeated: setattr(parent, field.name, next_message) else: list_value = getattr(parent, field.name, None) if list_value is None: setattr(parent, field.name, [next_message]) else: list_value.append(next_message) parent_path = next_path parent = next_message return parent def add_parameter(self, parameter, values): """Add a single parameter. Adds a single parameter and its value to the request message. Args: parameter: Query string parameter to map to request. values: List of values to assign to request message. Returns: True if parameter was valid and added to the message, else False. Raises: DecodeError if the parameter refers to a valid field, and the values parameter does not have one and only one value. Non-valid query parameters may have multiple values and should not cause an error. """ path = self.make_path(parameter) if not path: return False # Must check that all indexes of all items in the path are correct before # instantiating any of them. For example, consider: # # class Repeated(object): # ... # # class Inner(object): # # repeated = messages.MessageField(Repeated, 1, repeated=True) # # class Outer(object): # # inner = messages.MessageField(Inner, 1) # # instance = Outer() # builder = URLEncodedRequestBuilder(instance) # builder.add_parameter('inner.repeated') # # assert not hasattr(instance, 'inner') # # The check is done relative to the instance of Outer pass in to the # constructor of the builder. This instance is not referred to at all # because all names are assumed to be relative to it. # # The 'repeated' part of the path is not correct because it is missing an # index. Because it is missing an index, it should not create an instance # of Repeated. In this case add_parameter will return False and have no # side effects. # # A correct path that would cause a new Inner instance to be inserted at # instance.inner and a new Repeated instance to be appended to the # instance.inner.repeated list would be 'inner.repeated-0'. if not self.__check_indexes(path): return False # Ok to build objects. parent_path = path[:-1] parent = self.__get_or_create_path(parent_path) name, index = path[-1] field = parent.field_by_name(name) if len(values) != 1: raise messages.DecodeError( 'Found repeated values for field %s.' % field.name) value = values[0] if isinstance(field, messages.IntegerField): converted_value = int(value) elif isinstance(field, messages.MessageField): # Just make sure it's instantiated. Assignment to field or # appending to list is done in __get_or_create_path. self.__get_or_create_path(path) return True elif isinstance(field, messages.StringField): converted_value = value.decode('utf-8') elif isinstance(field, messages.BooleanField): converted_value = value.lower() == 'true' and True or False else: converted_value = field.type(value) if field.repeated: value_list = getattr(parent, field.name, None) if value_list is None: setattr(parent, field.name, [converted_value]) else: if index == len(value_list): value_list.append(converted_value) else: # Index should never be above len(value_list) because it was # verified during the index check above. value_list[index] = converted_value else: setattr(parent, field.name, converted_value) return True @util.positional(1) def encode_message(message, prefix=''): """Encode Message instance to url-encoded string. Args: message: Message instance to encode in to url-encoded string. prefix: Prefix to append to field names of contained values. Returns: String encoding of Message in URL encoded format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() parameters = [] def build_message(parent, prefix): """Recursively build parameter list for URL response. Args: parent: Message to build parameters for. prefix: Prefix to append to field names of contained values. Returns: True if some value of parent was added to the parameters list, else False, meaning the object contained no values. """ has_any_values = False for field in sorted(parent.all_fields(), key=lambda f: f.number): next_value = parent.get_assigned_value(field.name) if next_value is None: continue # Found a value. Ultimate return value should be True. has_any_values = True # Normalize all values in to a list. if not field.repeated: next_value = [next_value] for index, item in enumerate(next_value): # Create a name with an index if it is a repeated field. if field.repeated: field_name = '%s%s-%s' % (prefix, field.name, index) else: field_name = prefix + field.name if isinstance(field, messages.MessageField): # Message fields must be recursed in to in order to construct # their component parameter values. if not build_message(item, field_name + '.'): # The nested message is empty. Append an empty value to # represent it. parameters.append((field_name, '')) elif isinstance(field, messages.BooleanField): parameters.append((field_name, item and 'true' or 'false')) else: if isinstance(item, unicode): item = item.encode('utf-8') parameters.append((field_name, str(item))) return has_any_values build_message(message, prefix) return urllib.urlencode(parameters) def decode_message(message_type, encoded_message, **kwargs): """Decode urlencoded content to message. Args: message_type: Message instance to merge URL encoded content into. encoded_message: URL encoded message. prefix: Prefix to append to field names of contained values. Returns: Decoded instance of message_type. """ message = message_type() builder = URLEncodedRequestBuilder(message, **kwargs) arguments = cgi.parse_qs(encoded_message, keep_blank_values=True) for argument, values in sorted(arguments.iteritems()): builder.add_parameter(argument, values) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Services descriptor definitions. Contains message definitions and functions for converting service classes into transmittable message format. Describing an Enum instance, Enum class, Field class or Message class will generate an appropriate descriptor object that describes that class. This message can itself be used to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages. The descriptors are modeled to be binary compatible with: http://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto NOTE: The names of types and fields are not always the same between these descriptors and the ones defined in descriptor.proto. This was done in order to make source code files that use these descriptors easier to read. For example, it is not necessary to prefix TYPE to all the values in FieldDescriptor.Variant as is done in descriptor.proto FieldDescriptorProto.Type. Example: class Pixel(messages.Message): x = messages.IntegerField(1, required=True) y = messages.IntegerField(2, required=True) color = messages.BytesField(3) # Describe Pixel class using message descriptor. fields = [] field = FieldDescriptor() field.name = 'x' field.number = 1 field.label = FieldDescriptor.Label.REQUIRED field.variant = FieldDescriptor.Variant.INT64 field = FieldDescriptor() field.name = 'y' field.number = 2 field.label = FieldDescriptor.Label.REQUIRED field.variant = FieldDescriptor.Variant.INT64 field = FieldDescriptor() field.name = 'color' field.number = 3 field.label = FieldDescriptor.Label.OPTIONAL field.variant = FieldDescriptor.Variant.BYTES message = MessageDescriptor() message.name = 'Pixel' message.fields = fields # Describing is the equivalent of building the above message. message == describe_message(Pixel) Public Classes: EnumValueDescriptor: Describes Enum values. EnumDescriptor: Describes Enum classes. FieldDescriptor: Describes field instances. FileDescriptor: Describes a single 'file' unit. FileSet: Describes a collection of file descriptors. MessageDescriptor: Describes Message classes. MethodDescriptor: Describes a method of a service. ServiceDescriptor: Describes a services. Public Functions: describe_enum_value: Describe an individual enum-value. describe_enum: Describe an Enum class. describe_field: Describe a Field definition. describe_file: Describe a 'file' unit from a Python module or object. describe_file_set: Describe a file set from a list of modules or objects. describe_message: Describe a Message definition. describe_method: Describe a Method definition. describe_service: Describe a Service definition. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import codecs import types from protorpc import messages from protorpc import util __all__ = ['EnumDescriptor', 'EnumValueDescriptor', 'FieldDescriptor', 'MessageDescriptor', 'MethodDescriptor', 'FileDescriptor', 'FileSet', 'ServiceDescriptor', 'DescriptorLibrary', 'describe_enum', 'describe_enum_value', 'describe_field', 'describe_message', 'describe_method', 'describe_file', 'describe_file_set', 'describe_service', 'describe', 'import_descriptor_loader', ] # NOTE: MessageField is missing because message fields cannot have # a default value at this time. # TODO(rafek): Support default message values. # # Map to functions that convert default values of fields of a given type # to a string. The function must return a value that is compatible with # FieldDescriptor.default_value and therefore a unicode string. _DEFAULT_TO_STRING_MAP = { messages.IntegerField: unicode, messages.FloatField: unicode, messages.BooleanField: lambda value: value and u'true' or u'false', messages.BytesField: lambda value: codecs.escape_encode(value)[0], messages.StringField: lambda value: value, messages.EnumField: lambda value: unicode(value.number), } class EnumValueDescriptor(messages.Message): """Enum value descriptor. Fields: name: Name of enumeration value. number: Number of enumeration value. """ # TODO(rafek): Why are these listed as optional in descriptor.proto. # Harmonize? name = messages.StringField(1, required=True) number = messages.IntegerField(2, required=True, variant=messages.Variant.INT32) class EnumDescriptor(messages.Message): """Enum class descriptor. Fields: name: Name of Enum without any qualification. values: Values defined by Enum class. """ name = messages.StringField(1) values = messages.MessageField(EnumValueDescriptor, 2, repeated=True) class FieldDescriptor(messages.Message): """Field definition descriptor. Enums: Variant: Wire format hint sub-types for field. Label: Values for optional, required and repeated fields. Fields: name: Name of field. number: Number of field. variant: Variant of field. type_name: Type name for message and enum fields. default_value: String representation of default value. """ Variant = messages.Variant class Label(messages.Enum): """Field label.""" OPTIONAL = 1 REQUIRED = 2 REPEATED = 3 name = messages.StringField(1, required=True) number = messages.IntegerField(3, required=True, variant=messages.Variant.INT32) label = messages.EnumField(Label, 4, default=Label.OPTIONAL) variant = messages.EnumField(Variant, 5) type_name = messages.StringField(6) # For numeric types, contains the original text representation of the value. # For booleans, "true" or "false". # For strings, contains the default text contents (not escaped in any way). # For bytes, contains the C escaped value. All bytes < 128 are that are # traditionally considered unprintable are also escaped. default_value = messages.StringField(7) class MessageDescriptor(messages.Message): """Message definition descriptor. Fields: name: Name of Message without any qualification. fields: Fields defined for message. message_types: Nested Message classes defined on message. enum_types: Nested Enum classes defined on message. """ name = messages.StringField(1) fields = messages.MessageField(FieldDescriptor, 2, repeated=True) message_types = messages.MessageField( 'protorpc.descriptor.MessageDescriptor', 3, repeated=True) enum_types = messages.MessageField(EnumDescriptor, 4, repeated=True) class MethodDescriptor(messages.Message): """Service method definition descriptor. Fields: name: Name of service method. request_type: Fully qualified or relative name of request message type. response_type: Fully qualified or relative name of response message type. """ name = messages.StringField(1) request_type = messages.StringField(2) response_type = messages.StringField(3) class ServiceDescriptor(messages.Message): """Service definition descriptor. Fields: name: Name of Service without any qualification. methods: Remote methods of Service. """ name = messages.StringField(1) methods = messages.MessageField(MethodDescriptor, 2, repeated=True) class FileDescriptor(messages.Message): """Description of file containing protobuf definitions. Fields: package: Fully qualified name of package that definitions belong to. message_types: Message definitions contained in file. enum_types: Enum definitions contained in file. service_types: Service definitions contained in file. """ package = messages.StringField(2) # TODO(rafek): Add dependency field message_types = messages.MessageField(MessageDescriptor, 4, repeated=True) enum_types = messages.MessageField(EnumDescriptor, 5, repeated=True) service_types = messages.MessageField(ServiceDescriptor, 6, repeated=True) class FileSet(messages.Message): """A collection of FileDescriptors. Fields: files: Files in file-set. """ files = messages.MessageField(FileDescriptor, 1, repeated=True) def describe_enum_value(enum_value): """Build descriptor for Enum instance. Args: enum_value: Enum value to provide descriptor for. Returns: Initialized EnumValueDescriptor instance describing the Enum instance. """ enum_value_descriptor = EnumValueDescriptor() enum_value_descriptor.name = unicode(enum_value.name) enum_value_descriptor.number = enum_value.number return enum_value_descriptor def describe_enum(enum_definition): """Build descriptor for Enum class. Args: enum_definition: Enum class to provide descriptor for. Returns: Initialized EnumDescriptor instance describing the Enum class. """ enum_descriptor = EnumDescriptor() enum_descriptor.name = enum_definition.definition_name().split('.')[-1] values = [] for number in enum_definition.numbers(): value = enum_definition.lookup_by_number(number) values.append(describe_enum_value(value)) if values: enum_descriptor.values = values return enum_descriptor def describe_field(field_definition): """Build descriptor for Field instance. Args: field_definition: Field instance to provide descriptor for. Returns: Initialized FieldDescriptor instance describing the Field instance. """ field_descriptor = FieldDescriptor() field_descriptor.name = field_definition.name field_descriptor.number = field_definition.number field_descriptor.variant = field_definition.variant if isinstance(field_definition, (messages.EnumField, messages.MessageField)): field_descriptor.type_name = field_definition.type.definition_name() if field_definition.default is not None: field_descriptor.default_value = _DEFAULT_TO_STRING_MAP[ type(field_definition)](field_definition.default) # Set label. if field_definition.repeated: field_descriptor.label = FieldDescriptor.Label.REPEATED elif field_definition.required: field_descriptor.label = FieldDescriptor.Label.REQUIRED else: field_descriptor.label = FieldDescriptor.Label.OPTIONAL return field_descriptor def describe_message(message_definition): """Build descriptor for Message class. Args: message_definition: Message class to provide descriptor for. Returns: Initialized MessageDescriptor instance describing the Message class. """ message_descriptor = MessageDescriptor() message_descriptor.name = message_definition.definition_name().split('.')[-1] fields = sorted(message_definition.all_fields(), key=lambda v: v.number) if fields: message_descriptor.fields = [describe_field(field) for field in fields] try: nested_messages = message_definition.__messages__ except AttributeError: pass else: message_descriptors = [] for name in nested_messages: value = getattr(message_definition, name) message_descriptors.append(describe_message(value)) message_descriptor.message_types = message_descriptors try: nested_enums = message_definition.__enums__ except AttributeError: pass else: enum_descriptors = [] for name in nested_enums: value = getattr(message_definition, name) enum_descriptors.append(describe_enum(value)) message_descriptor.enum_types = enum_descriptors return message_descriptor def describe_method(method): """Build descriptor for service method. Args: method: Remote service method to describe. Returns: Initialized MethodDescriptor instance describing the service method. """ method_info = method.remote descriptor = MethodDescriptor() descriptor.name = method_info.method.func_name descriptor.request_type = method_info.request_type.definition_name() descriptor.response_type = method_info.response_type.definition_name() return descriptor def describe_service(service_class): """Build descriptor for service. Args: service_class: Service class to describe. Returns: Initialized ServiceDescriptor instance describing the service. """ descriptor = ServiceDescriptor() descriptor.name = service_class.__name__ methods = [] remote_methods = service_class.all_remote_methods() for name in sorted(remote_methods.iterkeys()): if name == 'get_descriptor': continue method = remote_methods[name] methods.append(describe_method(method)) if methods: descriptor.methods = methods return descriptor def describe_file(module): """Build a file from a specified Python module. Args: module: Python module to describe. Returns: Initialized FileDescriptor instance describing the module. """ # May not import remote at top of file because remote depends on this # file # TODO(rafek): Straighten out this dependency. Possibly move these functions # from descriptor to their own module. from protorpc import remote descriptor = FileDescriptor() try: descriptor.package = module.package except AttributeError: descriptor.package = module.__name__ if not descriptor.package: descriptor.package = None message_descriptors = [] enum_descriptors = [] service_descriptors = [] # Need to iterate over all top level attributes of the module looking for # message, enum and service definitions. Each definition must be itself # described. for name in sorted(dir(module)): value = getattr(module, name) if isinstance(value, type): if issubclass(value, messages.Message): message_descriptors.append(describe_message(value)) elif issubclass(value, messages.Enum): enum_descriptors.append(describe_enum(value)) elif issubclass(value, remote.Service): service_descriptors.append(describe_service(value)) if message_descriptors: descriptor.message_types = message_descriptors if enum_descriptors: descriptor.enum_types = enum_descriptors if service_descriptors: descriptor.service_types = service_descriptors return descriptor def describe_file_set(modules): """Build a file set from a specified Python modules. Args: modules: Iterable of Python module to describe. Returns: Initialized FileSet instance describing the modules. """ descriptor = FileSet() file_descriptors = [] for module in modules: file_descriptors.append(describe_file(module)) if file_descriptors: descriptor.files = file_descriptors return descriptor def describe(value): """Describe any value as a descriptor. Helper function for describing any object with an appropriate descriptor object. Args: value: Value to describe as a descriptor. Returns: Descriptor message class if object is describable as a descriptor, else None. """ from protorpc import remote if isinstance(value, types.ModuleType): return describe_file(value) elif callable(value) and hasattr(value, 'remote'): return describe_method(value) elif isinstance(value, messages.Field): return describe_field(value) elif isinstance(value, messages.Enum): return describe_enum_value(value) elif isinstance(value, type): if issubclass(value, messages.Message): return describe_message(value) elif issubclass(value, messages.Enum): return describe_enum(value) elif issubclass(value, remote.Service): return describe_service(value) return None @util.positional(1) def import_descriptor_loader(definition_name, importer=__import__): """Find objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary. Args: definition_name: Name of definition to find. importer: Import function used for importing new modules. Returns: Appropriate descriptor for any describable type located by name. Raises: DefinitionNotFoundError when a name does not refer to either a definition or a module. """ # Attempt to import descriptor as a module. if definition_name.startswith('.'): definition_name = definition_name[1:] if not definition_name.startswith('.'): leaf = definition_name.split('.')[-1] if definition_name: try: module = importer(definition_name, '', '', [leaf]) except ImportError: pass else: return describe(module) try: # Attempt to use messages.find_definition to find item. return describe(messages.find_definition(definition_name, importer=__import__)) except messages.DefinitionNotFoundError, err: # There are things that find_definition will not find, but if the parent # is loaded, its children can be searched for a match. split_name = definition_name.rsplit('.', 1) if len(split_name) > 1: parent, child = split_name try: parent_definition = import_descriptor_loader(parent, importer=importer) except messages.DefinitionNotFoundError: # Fall through to original error. pass else: # Check the parent definition for a matching descriptor. if isinstance(parent_definition, FileDescriptor): search_list = parent_definition.service_types or [] elif isinstance(parent_definition, ServiceDescriptor): search_list = parent_definition.methods or [] elif isinstance(parent_definition, EnumDescriptor): search_list = parent_definition.values or [] elif isinstance(parent_definition, MessageDescriptor): search_list = parent_definition.fields or [] else: search_list = [] for definition in search_list: if definition.name == child: return definition # Still didn't find. Reraise original exception. raise err class DescriptorLibrary(object): """A descriptor library is an object that contains known definitions. A descriptor library contains a cache of descriptor objects mapped by definition name. It contains all types of descriptors except for file sets. When a definition name is requested that the library does not know about it can be provided with a descriptor loader which attempt to resolve the missing descriptor. """ @util.positional(1) def __init__(self, descriptors=None, descriptor_loader=import_descriptor_loader): """Constructor. Args: descriptors: A dictionary or dictionary-like object that can be used to store and cache descriptors by definition name. definition_loader: A function used for resolving missing descriptors. The function takes a definition name as its parameter and returns an appropriate descriptor. It may raise DefinitionNotFoundError. """ self.__descriptor_loader = descriptor_loader self.__descriptors = descriptors or {} def lookup_descriptor(self, definition_name): """Lookup descriptor by name. Get descriptor from library by name. If descriptor is not found will attempt to find via descriptor loader if provided. Args: definition_name: Definition name to find. Returns: Descriptor that describes definition name. Raises: DefinitionNotFoundError if not descriptor exists for definition name. """ try: return self.__descriptors[definition_name] except KeyError: pass if self.__descriptor_loader: definition = self.__descriptor_loader(definition_name) self.__descriptors[definition_name] = definition return definition else: raise messages.DefinitionNotFoundError( 'Could not find definition for %s' % definition_name) def lookup_package(self, definition_name): """Determines the package name for any definition. Determine the package that any definition name belongs to. May check parent for package name and will resolve missing descriptors if provided descriptor loader. Args: definition_name: Definition name to find package for. """ while True: descriptor = self.lookup_descriptor(definition_name) if isinstance(descriptor, FileDescriptor): return descriptor.package else: index = definition_name.rfind('.') if index < 0: return None definition_name = definition_name[:index]
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import StringIO import types import unittest import urllib2 from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import test_util from protorpc import remote from protorpc import transport import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = transport class Message(messages.Message): value = messages.StringField(1) class Service(remote.Service): @remote.method(Message, Message) def method(self, request): pass class RpcTest(test_util.TestCase): def setUp(self): self.request = Message(value=u'request') self.response = Message(value=u'response') self.status = remote.RpcStatus(state=remote.RpcState.APPLICATION_ERROR, error_message='an error', error_name='blam') self.rpc = transport.Rpc(self.request) def testConstructor(self): self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.RUNNING, self.rpc.state) self.assertEquals(None, self.rpc.response) self.assertEquals(None, self.rpc.error_message) self.assertEquals(None, self.rpc.error_name) def testSetResponse(self): self.rpc.set_response(self.response) self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.OK, self.rpc.state) self.assertEquals(self.response, self.rpc.response) self.assertEquals(None, self.rpc.error_message) self.assertEquals(None, self.rpc.error_name) def testSetResponseAlreadySet(self): self.rpc.set_response(self.response) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetResponseAlreadyError(self): self.rpc.set_status(self.status) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetStatus(self): self.rpc.set_status(self.status) self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.APPLICATION_ERROR, self.rpc.state) self.assertEquals(None, self.rpc.response) self.assertEquals('an error', self.rpc.error_message) self.assertEquals('blam', self.rpc.error_name) def testSetStatusAlreadySet(self): self.rpc.set_response(self.response) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetNonMessage(self): self.assertRaisesWithRegexpMatch( TypeError, 'Expected Message type, received 10', self.rpc.set_response, 10) def testSetStatusAlreadyError(self): self.rpc.set_status(self.status) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetUninitializedStatus(self): self.assertRaises(messages.ValidationError, self.rpc.set_status, remote.RpcStatus()) class TransportTest(test_util.TestCase): def do_test(self, protocol, trans): request = Message() request.value = u'request' response = Message() response.value = u'response' encoded_request = protocol.encode_message(request) encoded_response = protocol.encode_message(response) self.assertEquals(protocol, trans.protocol) received_rpc = [None] def transport_rpc(remote, data, rpc): received_rpc[0] = rpc self.assertEquals(remote, Service.method.remote) self.assertEquals(encoded_request, data) self.assertTrue(isinstance(rpc, transport.Rpc)) self.assertEquals(request, rpc.request) self.assertEquals(None, rpc.response) rpc.set_response(response) trans._transport_rpc = transport_rpc rpc = trans.send_rpc(Service.method.remote, request) self.assertEquals(received_rpc[0], rpc) def testDefaultProtocol(self): self.do_test(protobuf, transport.Transport()) def testAlternateProtocol(self): self.do_test(protojson, transport.Transport(protocol=protojson)) class HttpTransportTest(test_util.TestCase): def setUp(self): self.mox = mox.Mox() self.mox.StubOutWithMock(urllib2, 'urlopen') def tearDown(self): self.mox.UnsetStubs() self.mox.VerifyAll() @remote.method(Message, Message) def my_method(self, request): self.fail('self.my_method should not be directly invoked.') def do_test_send_rpc(self, protocol): trans = transport.HttpTransport('http://myserver/myservice', protocol=protocol) request = Message(value=u'The request value') encoded_request = protocol.encode_message(request) response = Message(value=u'The response value') encoded_response = protocol.encode_message(response) def verify_request(urllib2_request): self.assertEquals('http://myserver/myservice.my_method', urllib2_request.get_full_url()) self.assertEquals(urllib2_request.get_data(), encoded_request) self.assertEquals(protocol.CONTENT_TYPE, urllib2_request.headers['Content-type']) return True # First call succeeds. urllib2.urlopen(mox.Func(verify_request)).AndReturn( StringIO.StringIO(encoded_response)) # Second call raises a normal HTTP error. urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, 'a server error', {}, StringIO.StringIO(''))) # Third call raises a 500 error with message. status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='an error') urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, protocol.encode_message(status), {'content-type': protocol.CONTENT_TYPE}, StringIO.StringIO(''))) # Fourth call is not parsable. status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='an error') urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, 'a text message is here anyway', {'content-type': protocol.CONTENT_TYPE}, StringIO.StringIO(''))) self.mox.ReplayAll() actual_rpc = trans.send_rpc(self.my_method.remote, request) self.assertEquals(response, actual_rpc.response) try: trans.send_rpc(self.my_method.remote, request) except remote.ServerError, err: self.assertEquals('HTTP Error 500: a server error', str(err)) self.assertTrue(isinstance(err.cause, urllib2.HTTPError)) else: self.fail('ServerError expected') try: trans.send_rpc(self.my_method.remote, request) except remote.RequestError, err: self.assertEquals('an error', str(err)) self.assertEquals(None, err.cause) else: self.fail('RequestError expected') try: trans.send_rpc(self.my_method.remote, request) except remote.ServerError, err: self.assertEquals('HTTP Error 500: a text message is here anyway', str(err)) self.assertTrue(isinstance(err.cause, urllib2.HTTPError)) else: self.fail('ServerError expected') def testSendProtobuf(self): self.do_test_send_rpc(protobuf) def testSendProtojson(self): self.do_test_send_rpc(protojson) def testURLError(self): trans = transport.HttpTransport('http://myserver/myservice', protocol=protojson) urllib2.urlopen(mox.IsA(urllib2.Request)).AndRaise( urllib2.URLError('a bad connection')) self.mox.ReplayAll() request = Message(value=u'The request value') try: trans.send_rpc(self.my_method.remote, request) except remote.NetworkError, err: self.assertEquals('a bad connection', str(err)) self.assertTrue(isinstance(err.cause, urllib2.URLError)) else: self.fail('Network error expected') def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Handlers for remote services. This module contains classes that may be used to build a service on top of the App Engine Webapp framework. The services request handler can be configured to handle requests in a number of different request formats. All different request formats must have a way to map the request to the service handlers defined request message.Message class. The handler can also send a response in any format that can be mapped from the response message.Message class. Participants in an RPC: There are four classes involved with the life cycle of an RPC. Service factory: A user-defined service factory that is responsible for instantiating an RPC service. The methods intended for use as RPC methods must be decorated by the 'remote' decorator. RPCMapper: Responsible for determining whether or not a specific request matches a particular RPC format and translating between the actual request/response and the underlying message types. A single instance of an RPCMapper sub-class is required per service configuration. Each mapper must be usable across multiple requests. ServiceHandler: A webapp.RequestHandler sub-class that responds to the webapp framework. It mediates between the RPCMapper and service implementation class during a request. As determined by the Webapp framework, a new ServiceHandler instance is created to handle each user request. A handler is never used to handle more than one request. ServiceHandlerFactory: A class that is responsible for creating new, properly configured ServiceHandler instance for each request. The factory is configured by providing it with a set of RPCMapper instances. When the Webapp framework invokes the service handler, the handler creates a new service class instance. The service class instance is provided with a reference to the handler. A single instance of an RPCMapper sub-class is required to configure each service. Each mapper instance must be usable across multiple requests. RPC mappers: RPC mappers translate between a single HTTP based RPC protocol and the underlying service implementation. Each RPC mapper must configured with the following information to determine if it is an appropriate mapper for a given request: http_methods: Set of HTTP methods supported by handler. content_types: Set of supported content types. default_content_type: Default content type for handler responses. Built-in mapper implementations: URLEncodedRPCMapper: Matches requests that are compatible with post forms with the 'application/x-www-form-urlencoded' content-type (this content type is the default if none is specified. It translates post parameters into request parameters. ProtobufRPCMapper: Matches requests that are compatible with post forms with the 'application/x-google-protobuf' content-type. It reads the contents of a binary post request. Public Exceptions: Error: Base class for service handler errors. ServiceConfigurationError: Raised when a service not correctly configured. RequestError: Raised by RPC mappers when there is an error in its request or request format. ResponseError: Raised by RPC mappers when there is an error in its response. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import array import cgi import itertools import logging import re import sys import urllib import weakref from google.appengine.ext import webapp from google.appengine.ext.webapp import util as webapp_util from protorpc import forms from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import protourlencode from protorpc import registry from protorpc import remote from protorpc import util __all__ = [ 'Error', 'RequestError', 'ResponseError', 'ServiceConfigurationError', 'DEFAULT_REGISTRY_PATH', 'ProtobufRPCMapper', 'RPCMapper', 'ServiceHandler', 'ServiceHandlerFactory', 'URLEncodedRPCMapper', 'JSONRPCMapper', 'service_mapping', 'run_services', ] class Error(Exception): """Base class for all errors in service handlers module.""" class ServiceConfigurationError(Error): """When service configuration is incorrect.""" class RequestError(Error): """Error occurred when building request.""" class ResponseError(Error): """Error occurred when building response.""" _URLENCODED_CONTENT_TYPE = protourlencode.CONTENT_TYPE _PROTOBUF_CONTENT_TYPE = protobuf.CONTENT_TYPE _JSON_CONTENT_TYPE = protojson.CONTENT_TYPE _EXTRA_JSON_CONTENT_TYPES = ['application/x-javascript', 'text/javascript', 'text/x-javascript', 'text/x-json', 'text/json', ] # The whole method pattern is an optional regex. It contains a single # group used for mapping to the query parameter. This is passed to the # parameters of 'get' and 'post' on the ServiceHandler. _METHOD_PATTERN = r'(?:\.([^?]*))?' DEFAULT_REGISTRY_PATH = forms.DEFAULT_REGISTRY_PATH class RPCMapper(object): """Interface to mediate between request and service object. Request mappers are implemented to support various types of RPC protocols. It is responsible for identifying whether a given request matches a particular protocol, resolve the remote method to invoke and mediate between the request and appropriate protocol messages for the remote method. """ @util.positional(4) def __init__(self, http_methods, default_content_type, protocol, content_types=None): """Constructor. Args: http_methods: Set of HTTP methods supported by mapper. default_content_type: Default content type supported by mapper. protocol: The protocol implementation. Must implement encode_message and decode_message. content_types: Set of additionally supported content types. """ self.__http_methods = frozenset(http_methods) self.__default_content_type = default_content_type self.__protocol = protocol if content_types is None: content_types = [] self.__content_types = frozenset([self.__default_content_type] + content_types) @property def http_methods(self): return self.__http_methods @property def default_content_type(self): return self.__default_content_type @property def content_types(self): return self.__content_types def build_request(self, handler, request_type): """Build request message based on request. Each request mapper implementation is responsible for converting a request to an appropriate message instance. Args: handler: RequestHandler instance that is servicing request. Must be initialized with request object and been previously determined to matching the protocol of the RPCMapper. request_type: Message type to build. Returns: Instance of request_type populated by protocol buffer in request body. Raises: RequestError if the mapper implementation is not able to correctly convert the request to the appropriate message. """ try: return self.__protocol.decode_message(request_type, handler.request.body) except (messages.ValidationError, messages.DecodeError), err: raise RequestError('Unable to parse request content: %s' % err) def build_response(self, handler, response): """Build response based on service object response message. Each request mapper implementation is responsible for converting a response message to an appropriate handler response. Args: handler: RequestHandler instance that is servicing request. Must be initialized with request object and been previously determined to matching the protocol of the RPCMapper. response: Response message as returned from the service object. Raises: ResponseError if the mapper implementation is not able to correctly convert the message to an appropriate response. """ try: encoded_message = self.__protocol.encode_message(response) except messages.ValidationError, err: raise ResponseError('Unable to encode message: %s' % err) else: handler.response.headers['Content-Type'] = self.default_content_type handler.response.out.write(encoded_message) class ServiceHandlerFactory(object): """Factory class used for instantiating new service handlers. Normally a handler class is passed directly to the webapp framework so that it can be simply instantiated to handle a single request. The service handler, however, must be configured with additional information so that it knows how to instantiate a service object. This class acts the same as a normal RequestHandler class by overriding the __call__ method to correctly configures a ServiceHandler instance with a new service object. The factory must also provide a set of RPCMapper instances which examine a request to determine what protocol is being used and mediates between the request and the service object. The mapping of a service handler must have a single group indicating the part of the URL path that maps to the request method. This group must exist but can be optional for the request (the group may be followed by '?' in the regular expression matching the request). Usage: stock_factory = ServiceHandlerFactory(StockService) ... configure stock_factory by adding RPCMapper instances ... application = webapp.WSGIApplication( [stock_factory.mapping('/stocks')]) Default usage: application = webapp.WSGIApplication( [ServiceHandlerFactory.default(StockService).mapping('/stocks')]) """ def __init__(self, service_factory): """Constructor. Args: service_factory: Service factory to instantiate and provide to service handler. """ self.__service_factory = service_factory self.__request_mappers = [] def all_request_mappers(self): """Get all request mappers. Returns: Iterator of all request mappers used by this service factory. """ return iter(self.__request_mappers) def add_request_mapper(self, mapper): """Add request mapper to end of request mapper list.""" self.__request_mappers.append(mapper) def __call__(self): """Construct a new service handler instance.""" return ServiceHandler(self, self.__service_factory()) @property def service_factory(self): """Service factory associated with this factory.""" return self.__service_factory @staticmethod def __check_path(path): """Check a path parameter. Make sure a provided path parameter is compatible with the webapp URL mapping. Args: path: Path to check. This is a plain path, not a regular expression. Raises: ValueError if path does not start with /, path ends with /. """ if path.endswith('/'): raise ValueError('Path %s must not end with /.' % path) def mapping(self, path): """Convenience method to map service to application. Args: path: Path to map service to. It must be a simple path with a leading / and no trailing /. Returns: Mapping from service URL to service handler factory. """ self.__check_path(path) service_url_pattern = path + _METHOD_PATTERN return service_url_pattern, self @classmethod def default(cls, service_factory, parameter_prefix=''): """Convenience method to map default factory configuration to application. Creates a standardized default service factory configuration that pre-maps the URL encoded protocol handler to the factory. Args: service_factory: Service factory to instantiate and provide to service handler. method_parameter: The name of the form parameter used to determine the method to invoke used by the URLEncodedRPCMapper. If None, no parameter is used and the mapper will only match against the form path-name. Defaults to 'method'. parameter_prefix: If provided, all the parameters in the form are expected to begin with that prefix by the URLEncodedRPCMapper. Returns: Mapping from service URL to service handler factory. """ factory = cls(service_factory) factory.add_request_mapper(URLEncodedRPCMapper(parameter_prefix)) factory.add_request_mapper(ProtobufRPCMapper()) factory.add_request_mapper(JSONRPCMapper()) return factory class ServiceHandler(webapp.RequestHandler): """Web handler for RPC service. Overridden methods: get: All requests handled by 'handle' method. HTTP method stored in attribute. Takes remote_method parameter as derived from the URL mapping. post: All requests handled by 'handle' method. HTTP method stored in attribute. Takes remote_method parameter as derived from the URL mapping. redirect: Not implemented for this service handler. New methods: handle: Handle request for both GET and POST. Attributes (in addition to attributes in RequestHandler): service: Service instance associated with request being handled. method: Method of request. Used by RPCMapper to determine match. remote_method: Sub-path as provided to the 'get' and 'post' methods. """ def __init__(self, factory, service): """Constructor. Args: factory: Instance of ServiceFactory used for constructing new service instances used for handling requests. """ self.__factory = factory self.service = service def get(self, remote_method): """Handler method for GET requests. Delegates to handle. Sets new class attributes: Args: remote_method: Sub-path after service mapping has been matched. """ self.handle('GET', remote_method) def post(self, remote_method): """Handler method for POST requests. Delegates to handle. Sets new class attributes: Args: remote_method: Sub-path after service mapping has been matched. """ self.handle('POST', remote_method) def redirect(self, uri, permanent=False): """Not supported for services.""" raise NotImplementedError('Services do not currently support redirection.') def __match_request(self, mapper, http_method, remote_method): content_type = self.request.headers.get('content-type', None) if not content_type: content_type = self.request.environ.get('HTTP_CONTENT_TYPE', None) if not content_type: return False # Lop off parameters from the end (for example content-encoding) content_type = content_type.split(';', 1)[0] return bool(http_method in mapper.http_methods and # Must have correct content type. content_type and content_type.lower() in mapper.content_types and # Must have remote method name. remote_method) def handle(self, http_method, remote_method): """Handle a service request. The handle method will handle either a GET or POST response. It is up to the individual mappers from the handler factory to determine which request methods they can service. If the protocol is not recognized, the request does not provide a correct request for that protocol or the service object does not support the requested RPC method, will return error code 400 in the response. """ # Provide server state to the service. If the service object does not have # an "initialize_request_state" method, will not attempt to assign state. try: state_initializer = self.service.initialize_request_state except AttributeError: pass else: server_port = self.request.environ.get('SERVER_PORT', None) if server_port: server_port = int(server_port) request_state = remote.RequestState( remote_host=self.request.environ.get('REMOTE_HOST', None), remote_address=self.request.environ.get('REMOTE_ADDR', None), server_host=self.request.environ.get('SERVER_HOST', None), server_port=server_port) state_initializer(request_state) # Search for mapper to mediate request. for mapper in self.__factory.all_request_mappers(): if self.__match_request(mapper, http_method, remote_method): break else: message = 'Unrecognized RPC format.' logging.error(message) self.response.set_status(400, message) return try: try: method = getattr(self.service, remote_method) method_info = method.remote except AttributeError, err: message = 'Unrecognized RPC method: %s' % remote_method logging.error(message) self.response.set_status(400, message) return request = mapper.build_request(self, method_info.request_type) except (RequestError, messages.DecodeError), err: logging.error('Error building request: %s', err) self.response.set_status(400, 'Invalid RPC request.') return response = method(request) try: mapper.build_response(self, response) except ResponseError, err: logging.error('Error building response: %s', err) self.response.set_status(500, 'Invalid RPC response.') # TODO(rafek): Support tag-id only forms. class URLEncodedRPCMapper(RPCMapper): """Request mapper for application/x-www-form-urlencoded forms. This mapper is useful for building forms that can invoke RPC. Many services are also configured to work using URL encoded request information because of its perceived ease of programming and debugging. The mapper must be provided with at least method_parameter or remote_method_pattern so that it is possible to determine how to determine the requests RPC method. If both are provided, the service will respond to both method request types, however, only one may be present in a given request. If both types are detected, the request will not match. """ def __init__(self, parameter_prefix=''): """Constructor. Args: parameter_prefix: If provided, all the parameters in the form are expected to begin with that prefix. """ # Private attributes: # __parameter_prefix: parameter prefix as provided by constructor # parameter. super(URLEncodedRPCMapper, self).__init__(['POST'], _URLENCODED_CONTENT_TYPE, self) self.__parameter_prefix = parameter_prefix def encode_message(self, message): """Encode a message using parameter prefix. Args: message: Message to URL Encode. Returns: URL encoded message. """ return protourlencode.encode_message(message, prefix=self.__parameter_prefix) @property def parameter_prefix(self): """Prefix all form parameters are expected to begin with.""" return self.__parameter_prefix def build_request(self, handler, request_type): """Build request from URL encoded HTTP request. Constructs message from names of URL encoded parameters. If this service handler has a parameter prefix, parameters must begin with it or are ignored. Args: handler: RequestHandler instance that is servicing request. request_type: Message type to build. Returns: Instance of request_type populated by protocol buffer in request parameters. Raises: RequestError if message type contains nested message field or repeated message field. Will raise RequestError if there are any repeated parameters. """ request = request_type() builder = protourlencode.URLEncodedRequestBuilder( request, prefix=self.__parameter_prefix) for argument in sorted(handler.request.arguments()): values = handler.request.get_all(argument) try: builder.add_parameter(argument, values) except messages.DecodeError, err: raise RequestError(str(err)) return request class ProtobufRPCMapper(RPCMapper): """Request mapper for application/x-protobuf service requests. This mapper will parse protocol buffer from a POST body and return the request as a protocol buffer. """ def __init__(self): super(ProtobufRPCMapper, self).__init__(['POST'], _PROTOBUF_CONTENT_TYPE, protobuf) class JSONRPCMapper(RPCMapper): """Request mapper for application/x-protobuf service requests. This mapper will parse protocol buffer from a POST body and return the request as a protocol buffer. """ def __init__(self): super(JSONRPCMapper, self).__init__( ['POST'], _JSON_CONTENT_TYPE, protojson, content_types=_EXTRA_JSON_CONTENT_TYPES) def service_mapping(services, registry_path=DEFAULT_REGISTRY_PATH): """Create a services mapping for use with webapp. Creates basic default configuration and registration for ProtoRPC services. Each service listed in the service mapping has a standard service handler factory created for it. The list of mappings can either be an explicit path to service mapping or just services. If mappings are just services, they will automatically be mapped to their default name. For exampel: package = 'my_package' class MyService(remote.Service): ... server_mapping([('/my_path', MyService), # Maps to /my_path MyService, # Maps to /my_package/MyService ]) Specifying a service mapping: Normally services are mapped to URL paths by specifying a tuple (path, service): path: The path the service resides on. service: The service class or service factory for creating new instances of the service. For more information about service factories, please see remote.Service.new_factory. If no tuple is provided, and therefore no path specified, a default path is calculated by using the fully qualified service name using a URL path separator for each of its components instead of a '.'. Args: services: Can be service type, service factory or string definition name of service being mapped or list of tuples (path, service): path: Path on server to map service to. service: Service type, service factory or string definition name of service being mapped. Can also be a dict. If so, the keys are treated as the path and values as the service. registry_path: Path to give to registry service. Use None to disable registry service. Returns: List of tuples defining a mapping of request handlers compatible with a webapp application. Raises: ServiceConfigurationError when duplicate paths are provided. """ if isinstance(services, dict): services = services.iteritems() mapping = [] registry_map = {} if registry_path is not None: registry_service = registry.RegistryService.new_factory(registry_map) services = list(services) + [(registry_path, registry_service)] mapping.append((registry_path + r'/form(?:/)?', forms.FormsHandler.new_factory(registry_path))) mapping.append((registry_path + r'/form/(.+)', forms.ResourceHandler)) paths = set() for service_item in services: infer_path = not isinstance(service_item, (list, tuple)) if infer_path: service = service_item else: service = service_item[1] service_class = getattr(service, 'service_class', service) if infer_path: path = '/' + service_class.definition_name().replace('.', '/') else: path = service_item[0] if path in paths: raise ServiceConfigurationError( 'Path %r is already defined in service mapping' % path) else: paths.add(path) # Create service mapping for webapp. new_mapping = ServiceHandlerFactory.default(service).mapping(path) mapping.append(new_mapping) # Update registry with service class. registry_map[path] = service_class return mapping def run_services(services, registry_path=DEFAULT_REGISTRY_PATH): """Handle CGI request using service mapping. Args: Same as service_mapping. """ mappings = service_mapping(services, registry_path=registry_path) application = webapp.WSGIApplication(mappings) webapp_util.run_wsgi_app(application)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Webapp forms interface to ProtoRPC services. This webapp application is automatically configured to work with ProtoRPCs that have a configured protorpc.RegistryService. This webapp is automatically added to the registry service URL at <registry-path>/forms (default is /protorpc/form) when configured using the service_handlers.service_mapping function. """ import logging import os from google.appengine.ext import webapp from google.appengine.ext.webapp import template __all__ = ['FormsHandler', 'ResourceHandler', 'DEFAULT_REGISTRY_PATH', ] _TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), 'static') _FORMS_TEMPLATE = os.path.join(_TEMPLATES_DIR, 'forms.html') _METHODS_TEMPLATE = os.path.join(_TEMPLATES_DIR, 'methods.html') DEFAULT_REGISTRY_PATH = '/protorpc' class ResourceHandler(webapp.RequestHandler): """Serves static resources without needing to add static files to app.yaml.""" __RESOURCE_MAP = { 'forms.js': 'text/javascript', } def get(self, relative): """Serve known static files. If static file is not known, will return 404 to client. Response items are cached for 300 seconds. Args: relative: Name of static file relative to main FormsHandler. """ content_type = self.__RESOURCE_MAP.get(relative, None) if not content_type: self.response.set_status(404) self.response.out.write('Resource not found.') return path = os.path.join(os.path.dirname(__file__), 'static', relative) self.response.headers['Content-Type'] = content_type static_file = open(path) try: contents = static_file.read() finally: static_file.close() self.response.out.write(contents) class FormsHandler(webapp.RequestHandler): """Handler for display HTML/javascript forms of ProtoRPC method calls. When accessed with no query parameters, will show a web page that displays all services and methods on the associated registry path. Links on this page fill in the service_path and method_name query parameters back to this same handler. When provided with service_path and method_name parameters will display a dynamic form representing the request message for that method. When sent, the form sends a JSON request to the ProtoRPC method and displays the response in the HTML page. Attribute: registry_path: Read-only registry path known by this handler. """ def __init__(self, registry_path=DEFAULT_REGISTRY_PATH): """Constructor. When configuring a FormsHandler to use with a webapp application do not pass the request handler class in directly. Instead use new_factory to ensure that the FormsHandler is created with the correct registry path for each request. Args: registry_path: Absolute path on server where the ProtoRPC RegsitryService is located. """ assert registry_path self.__registry_path = registry_path @property def registry_path(self): return self.__registry_path def get(self): """Send forms and method page to user. By default, displays a web page listing all services and methods registered on the server. Methods have links to display the actual method form. If both parameters are set, will display form for method. Query Parameters: service_path: Path to service to display method of. Optional. method_name: Name of method to display form for. Optional. """ params = {'forms_path': self.request.path.rstrip('/'), 'hostname': self.request.host, 'registry_path': self.__registry_path, } service_path = self.request.get('path', None) method_name = self.request.get('method', None) if service_path and method_name: form_template = _METHODS_TEMPLATE params['service_path'] = service_path params['method_name'] = method_name else: form_template = _FORMS_TEMPLATE self.response.out.write(template.render(form_template, params)) @classmethod def new_factory(cls, registry_path=DEFAULT_REGISTRY_PATH): """Construct a factory for use with WSGIApplication. This method is called automatically with the correct registry path when services are configured via service_handlers.service_mapping. Args: registry_path: Absolute path on server where the ProtoRPC RegsitryService is located. Returns: Factory function that creates a properly configured FormsHandler instance. """ def forms_factory(): return cls(registry_path) return forms_factory
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.stub.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import StringIO import sys import unittest from protorpc import definition from protorpc import descriptor from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import test_util import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = definition class DefineEnumTest(test_util.TestCase): """Test for define_enum.""" def testDefineEnum_Empty(self): """Test defining an empty enum.""" enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = 'Empty' enum_class = definition.define_enum(enum_descriptor, 'whatever') self.assertEquals('Empty', enum_class.__name__) self.assertEquals('whatever', enum_class.__module__) self.assertEquals(enum_descriptor, descriptor.describe_enum(enum_class)) def testDefineEnum(self): """Test defining an enum.""" red = descriptor.EnumValueDescriptor() green = descriptor.EnumValueDescriptor() blue = descriptor.EnumValueDescriptor() red.name = 'RED' red.number = 1 green.name = 'GREEN' green.number = 2 blue.name = 'BLUE' blue.number = 3 enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = 'Colors' enum_descriptor.values = [red, green, blue] enum_class = definition.define_enum(enum_descriptor, 'whatever') self.assertEquals('Colors', enum_class.__name__) self.assertEquals('whatever', enum_class.__module__) self.assertEquals(enum_descriptor, descriptor.describe_enum(enum_class)) class DefineFieldTest(test_util.TestCase): """Test for define_field.""" def testDefineField_Optional(self): """Test defining an optional field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.INT32 field_descriptor.label = descriptor.FieldDescriptor.Label.OPTIONAL field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.IntegerField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.INT32, field.variant) self.assertFalse(field.required) self.assertFalse(field.repeated) def testDefineField_Required(self): """Test defining a required field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.STRING field_descriptor.label = descriptor.FieldDescriptor.Label.REQUIRED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.StringField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.STRING, field.variant) self.assertTrue(field.required) self.assertFalse(field.repeated) def testDefineField_Repeated(self): """Test defining a repeated field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.DOUBLE field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.FloatField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.DOUBLE, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) def testDefineField_Message(self): """Test defining a message field.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.MESSAGE field_descriptor.type_name = 'something.yet.to.be.Defined' field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.MessageField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.MESSAGE, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError, 'Could not find definition for ' 'something.yet.to.be.Defined', getattr, field, 'type') def testDefineField_Enum(self): """Test defining an enum field.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.ENUM field_descriptor.type_name = 'something.yet.to.be.Defined' field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.EnumField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.ENUM, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError, 'Could not find definition for ' 'something.yet.to.be.Defined', getattr, field, 'type') class DefineMessageTest(test_util.TestCase): """Test for define_message.""" def testDefineMessageEmpty(self): """Test definition a message with no fields or enums.""" class AMessage(messages.Message): pass message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessageEnumOnly(self): """Test definition a message with only enums.""" class AMessage(messages.Message): class NestedEnum(messages.Enum): pass message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessageFieldsOnly(self): """Test definition a message with only fields.""" class AMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.StringField(2) message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessage(self): """Test defining Message class from descriptor.""" class AMessage(messages.Message): class NestedEnum(messages.Enum): pass field1 = messages.IntegerField(1) field2 = messages.StringField(2) message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) class DefineServiceTest(test_util.TestCase): """Test service proxy definition.""" def setUp(self): """Set up mock and request classes.""" self.module = new.module('stocks') class GetQuoteRequest(messages.Message): __module__ = 'stocks' symbols = messages.StringField(1, repeated=True) class GetQuoteResponse(messages.Message): __module__ = 'stocks' prices = messages.IntegerField(1, repeated=True) self.module.GetQuoteRequest = GetQuoteRequest self.module.GetQuoteResponse = GetQuoteResponse def testDefineService(self): """Test service definition from descriptor.""" method_descriptor = descriptor.MethodDescriptor() method_descriptor.name = 'get_quote' method_descriptor.request_type = 'GetQuoteRequest' method_descriptor.response_type = 'GetQuoteResponse' service_descriptor = descriptor.ServiceDescriptor() service_descriptor.name = 'Stocks' service_descriptor.methods = [method_descriptor] StockService = definition.define_service(service_descriptor, self.module) self.assertTrue(issubclass(StockService, remote.Service)) self.assertTrue(issubclass(StockService.Stub, remote.StubBase)) request = self.module.GetQuoteRequest() service = StockService() self.assertRaises(NotImplementedError, service.get_quote, request) self.assertEquals(self.module.GetQuoteRequest, service.get_quote.remote.request_type) self.assertEquals(self.module.GetQuoteResponse, service.get_quote.remote.response_type) class ModuleTest(test_util.TestCase): """Test for module creation and importation functions.""" def MakeFileDescriptor(self, package): """Helper method to construct FileDescriptors. Creates FileDescriptor with a MessageDescriptor and an EnumDescriptor. Args: package: Package name to give new file descriptors. Returns: New FileDescriptor instance. """ enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = u'MyEnum' message_descriptor = descriptor.MessageDescriptor() message_descriptor.name = u'MyMessage' service_descriptor = descriptor.ServiceDescriptor() service_descriptor.name = u'MyService' file_descriptor = descriptor.FileDescriptor() file_descriptor.package = package file_descriptor.enum_types = [enum_descriptor] file_descriptor.message_types = [message_descriptor] file_descriptor.service_types = [service_descriptor] return file_descriptor def testDefineModule(self): """Test define_module function.""" file_descriptor = self.MakeFileDescriptor('my.package') module = definition.define_file(file_descriptor) self.assertEquals('my.package', module.__name__) self.assertEquals('my.package', module.MyEnum.__module__) self.assertEquals('my.package', module.MyMessage.__module__) self.assertEquals('my.package', module.MyService.__module__) self.assertEquals(file_descriptor, descriptor.describe_file(module)) def testDefineModule_ReuseModule(self): """Test updating module with additional definitions.""" file_descriptor = self.MakeFileDescriptor('my.package') module = new.module('override') self.assertEquals(module, definition.define_file(file_descriptor, module)) self.assertEquals('override', module.MyEnum.__module__) self.assertEquals('override', module.MyMessage.__module__) self.assertEquals('override', module.MyService.__module__) # One thing is different between original descriptor and new. file_descriptor.package = 'override' self.assertEquals(file_descriptor, descriptor.describe_file(module)) def testImportFile(self): """Test importing FileDescriptor in to module space.""" modules = {} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone'])) def testImportFile_InToExisting(self): """Test importing FileDescriptor in to existing module.""" module = new.module('standalone') modules = {'standalone': module} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(module, modules['standalone']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone'])) def testImportFile_InToGlobalModules(self): """Test importing FileDescriptor in to global modules.""" original_modules = sys.modules try: sys.modules = dict(sys.modules) if 'standalone' in sys.modules: del sys.modules['standalone'] file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor) self.assertEquals(file_descriptor, descriptor.describe_file(sys.modules['standalone'])) finally: sys.modules = original_modules def testImportFile_Nested(self): """Test importing FileDescriptor in to existing nested module.""" modules = {} file_descriptor = self.MakeFileDescriptor('root.nested') definition.import_file(file_descriptor, modules=modules) self.assertEquals(modules['root'].nested, modules['root.nested']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['root.nested'])) def testImportFile_NoPackage(self): """Test importing FileDescriptor with no package.""" file_descriptor = self.MakeFileDescriptor('does not matter') file_descriptor.reset('package') self.assertRaisesWithRegexpMatch(ValueError, 'File descriptor must have package name', definition.import_file, file_descriptor) def testImportFileSet(self): """Test importing a whole file set.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] root = new.module('root') nested = new.module('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ])) def testImportFileSetFromFile(self): """Test importing a whole file set from a file.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] stream = StringIO.StringIO(protobuf.encode_message(file_set)) self.mox = mox.Mox() opener = self.mox.CreateMockAnything() opener('my-file.dat', 'rb').AndReturn(stream) self.mox.ReplayAll() modules = {} definition.import_file_set('my-file.dat', modules=modules, _open=opener) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ])) def testImportBuiltInProtorpcClasses(self): """Test that built in Protorpc classes are skipped.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), descriptor.describe_file(descriptor), ] root = new.module('root') nested = new.module('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, 'protorpc.descriptor': descriptor, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(descriptor, modules['protorpc.descriptor']) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], modules['protorpc.descriptor'], ])) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.forms.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import os import unittest from google.appengine.ext import webapp from google.appengine.ext.webapp import template from protorpc import forms from protorpc import test_util from protorpc import webapp_test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = forms def RenderTemplate(name, **params): """Load content from static file. Args: name: Name of static file to load from static directory. params: Passed in to webapp template generator. Returns: Contents of static file. """ path = os.path.join(os.path.dirname(__file__), 'static', name) return template.render(path, params) class ResourceHandlerTest(webapp_test_util.RequestHandlerTestBase): def CreateRequestHandler(self): return forms.ResourceHandler() def DoStaticContentTest(self, name, expected_type): """Run the static content test. Loads expected static content from source and compares with results in response. Checks content-type and cache header. Args: name: Name of file that should be served. expected_type: Expected content-type of served file. """ self.handler.get(name) content = RenderTemplate(name) self.CheckResponse('200 OK', {'content-type': expected_type, }, content) def testGet(self): self.DoStaticContentTest('forms.js', 'text/javascript') def testNoSuchFile(self): self.handler.get('unknown.txt') self.CheckResponse('404 Not Found', {}, 'Resource not found.') class FormsHandlerTest(webapp_test_util.RequestHandlerTestBase): def CreateRequestHandler(self): handler = forms.FormsHandler('/myreg') self.assertEquals('/myreg', handler.registry_path) return handler def testGetForm(self): self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGet_MissingPath(self): self.ResetHandler({'QUERY_STRING': 'method=my_method'}) self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGet_MissingMethod(self): self.ResetHandler({'QUERY_STRING': 'path=/my-path'}) self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGetMethod(self): self.ResetHandler({'QUERY_STRING': 'path=/my-path&method=my_method'}) self.handler.get() content = RenderTemplate( 'methods.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg', service_path='/my-path', method_name='my_method') self.CheckResponse('200 OK', {}, content) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Testing utilities for the webapp libraries. GetDefaultEnvironment: Method for easily setting up CGI environment. RequestHandlerTestBase: Base class for setting up handler tests. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import unittest from google.appengine.ext import webapp def GetDefaultEnvironment(): """Function for creating a default CGI environment.""" return { 'LC_NUMERIC': 'C', 'wsgi.multiprocess': True, 'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_SOFTWARE': 'Dev AppServer 0.1', 'SCRIPT_NAME': '', 'LOGNAME': 'nickjohnson', 'USER': 'nickjohnson', 'QUERY_STRING': 'foo=bar&foo=baz&foo2=123', 'PATH': '/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11', 'LANG': 'en_US', 'LANGUAGE': 'en', 'REMOTE_ADDR': '127.0.0.1', 'LC_MONETARY': 'C', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '8080', 'HOME': '/home/mruser', 'USERNAME': 'mruser', 'CONTENT_LENGTH': '', 'USER_IS_ADMIN': '1', 'PYTHONPATH': '/tmp/setup', 'LC_TIME': 'C', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; ' 'rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6', 'wsgi.multithread': False, 'wsgi.version': (1, 0), 'USER_EMAIL': 'test@example.com', 'USER_EMAIL': '112', 'wsgi.input': cStringIO.StringIO(), 'PATH_TRANSLATED': '/tmp/request.py', 'SERVER_NAME': 'localhost', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': True, 'LC_COLLATE': 'C', 'HOSTNAME': 'myhost', 'wsgi.errors': cStringIO.StringIO(), 'PWD': '/tmp', 'REQUEST_METHOD': 'GET', 'MAIL': '/dev/null', 'MAILCHECK': '0', 'USER_NICKNAME': 'test', 'HTTP_COOKIE': 'dev_appserver_login="test:test@example.com:True"', 'PATH_INFO': '/tmp/myhandler' } class RequestHandlerTestBase(unittest.TestCase): """Base class for writing RequestHandler tests. To test a specific request handler override CreateRequestHandler. To change the environment for that handler override GetEnvironment. """ def setUp(self): """Set up test for request handler.""" self.ResetHandler() def GetEnvironment(self): """Get environment. Override for more specific configurations. Returns: dict of CGI environment. """ return GetDefaultEnvironment() def CreateRequestHandler(self): """Create RequestHandler instances. Override to create more specific kinds of RequestHandler instances. Returns: RequestHandler instance used in test. """ return webapp.RequestHandler() def CheckResponse(self, expected_status, expected_headers, expected_content): """Check that the web response is as expected. Args: expected_status: Expected status message. expected_headers: Dictionary of expected headers. Will ignore unexpected headers and only check the value of those expected. expected_content: Expected body. """ def check_content(content): self.assertEquals(expected_content, content) def start_response(status, headers): self.assertEquals(expected_status, status) found_keys = set() for name, value in headers: name = name.lower() try: expected_value = expected_headers[name] except KeyError: pass else: found_keys.add(name) self.assertEquals(expected_value, value) missing_headers = set(expected_headers.iterkeys()) - found_keys if missing_headers: self.fail('Expected keys %r not found' % (list(missing_headers),)) return check_content self.handler.response.wsgi_write(start_response) def ResetHandler(self, change_environ=None): """Reset this tests environment with environment changes. Resets the entire test with a new handler which includes some changes to the default request environment. Args: change_environ: Dictionary of values that are added to default environment. """ environment = self.GetEnvironment() environment.update(change_environ or {}) self.request = webapp.Request(environment) self.response = webapp.Response() self.handler = self.CreateRequestHandler() self.handler.initialize(self.request, self.response)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import with_statement __author__ = 'rafek@google.com (Rafe Kaplan)' import logging from protorpc import descriptor from protorpc import generate from protorpc import messages from protorpc import util __all__ = ['format_proto_file'] @util.positional(2) def format_proto_file(file_descriptor, output, indent_space=2): out = generate.IndentWriter(output, indent_space=indent_space) if file_descriptor.package: out << 'package %s;' % file_descriptor.package def write_enums(enum_descriptors): """Write nested and non-nested Enum types. Args: enum_descriptors: List of EnumDescriptor objects from which to generate enums. """ # Write enums. for enum in enum_descriptors or []: out << '' out << '' out << 'enum %s {' % enum.name out << '' with out.indent(): if enum.values: for enum_value in enum.values: out << '%s = %s;' % (enum_value.name, enum_value.number) out << '}' write_enums(file_descriptor.enum_types) def write_fields(field_descriptors): """Write fields for Message types. Args: field_descriptors: List of FieldDescriptor objects from which to generate fields. """ for field in field_descriptors or []: default_format = '' if field.default_value is not None: if field.label == descriptor.FieldDescriptor.Label.REPEATED: logging.warning('Default value for repeated field %s is not being ' 'written to proto file' % field.name) else: # Convert default value to string. if field.variant == messages.Variant.MESSAGE: logging.warning( 'Message field %s should not have default values' % field.name) default = None elif field.variant == messages.Variant.STRING: default = repr(field.default_value.encode('utf-8')) elif field.variant == messages.Variant.BYTES: default = repr(field.default_value) else: default = str(field.default_value) if default is not None: default_format = ' [default=%s]' % default if field.variant in (messages.Variant.MESSAGE, messages.Variant.ENUM): field_type = field.type_name else: field_type = str(field.variant).lower() out << '%s %s %s = %s%s;' % (str(field.label).lower(), field_type, field.name, field.number, default_format) def write_messages(message_descriptors): """Write nested and non-nested Message types. Args: message_descriptors: List of MessageDescriptor objects from which to generate messages. """ for message in message_descriptors or []: out << '' out << '' out << 'message %s {' % message.name with out.indent(): if message.enum_types: write_enums(message.enum_types) if message.message_types: write_messages(message.message_types) if message.fields: write_fields(message.fields) out << '}' write_messages(file_descriptor.message_types)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate_python_test.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import os import shutil import sys import tempfile import unittest from protorpc import descriptor from protorpc import generate_python from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate_python class FormatPythonFileTest(test_util.TestCase): def setUp(self): self.original_path = list(sys.path) self.original_modules = dict(sys.modules) sys.path = list(sys.path) self.file_descriptor = descriptor.FileDescriptor() # Create temporary directory and add to Python path so that generated # Python code can be easily parsed, imported and executed. self.temp_dir = tempfile.mkdtemp() sys.path.append(self.temp_dir) def tearDown(self): # Reset path. sys.path[:] = [] sys.path.extend(self.original_path) # Reset modules. sys.modules.clear() sys.modules.update(self.original_modules) # Remove temporary directory. try: shutil.rmtree(self.temp_dir) except IOError: pass def DoPythonTest(self, file_descriptor): """Execute python test based on a FileDescriptor object. The full test of the Python code generation is to generate a Python source code file, import the module and regenerate the FileDescriptor from it. If the generated FileDescriptor is the same as the original, it means that the generated source code correctly implements the actual FileDescriptor. """ file_name = os.path.join(self.temp_dir, '%s.py' % (file_descriptor.package or 'blank',)) source_file = open(file_name, 'wt') try: generate_python.format_python_file(file_descriptor, source_file) finally: source_file.close() module_to_import = file_descriptor.package or 'blank' module = __import__(module_to_import) if not file_descriptor.package: self.assertFalse(hasattr(module, 'package')) module.package = '' # Create package name so that comparison will work. reloaded_descriptor = descriptor.describe_file(module) # Need to sort both message_types fields because document order is never # Ensured. # TODO(rafek): Ensure document order. if reloaded_descriptor.message_types: reloaded_descriptor.message_types = sorted( reloaded_descriptor.message_types, key=lambda v: v.name) if file_descriptor.message_types: file_descriptor.message_types = sorted( file_descriptor.message_types, key=lambda v: v.name) self.assertEquals(file_descriptor, reloaded_descriptor) @util.positional(2) def DoMessageTest(self, field_descriptors, message_types=None, enum_types=None): """Execute message generation test based on FieldDescriptor objects. Args: field_descriptor: List of FieldDescriptor object to generate and test. message_types: List of other MessageDescriptor objects that the new Message class depends on. enum_types: List of EnumDescriptor objects that the new Message class depends on. """ file_descriptor = descriptor.FileDescriptor() file_descriptor.package = 'my_package' message_descriptor = descriptor.MessageDescriptor() message_descriptor.name = 'MyMessage' message_descriptor.fields = list(field_descriptors) file_descriptor.message_types = message_types or [] file_descriptor.message_types.append(message_descriptor) if enum_types: file_descriptor.enum_types = list(enum_types) self.DoPythonTest(file_descriptor) def testBlankPackage(self): self.DoPythonTest(descriptor.FileDescriptor()) def testEmptyPackage(self): file_descriptor = descriptor.FileDescriptor() file_descriptor.package = 'mypackage' self.DoPythonTest(file_descriptor) def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testMessageField_InternalReference(self): other_message = descriptor.MessageDescriptor() other_message.name = 'OtherMessage' field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'my_package.OtherMessage' self.DoMessageTest([field], message_types=[other_message]) def testMessageField_ExternalReference(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'protorpc.registry.GetFileSetResponse' self.DoMessageTest([field]) def testEnumField_InternalReference(self): enum = descriptor.EnumDescriptor() enum.name = 'Color' field = descriptor.FieldDescriptor() field.name = 'color' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.Color' self.DoMessageTest([field], enum_types=[enum]) def testEnumField_ExternalReference(self): field = descriptor.FieldDescriptor() field.name = 'color' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'protorpc.descriptor.FieldDescriptor.Label' self.DoMessageTest([field]) def testNonDefaultVariant(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.UINT64 self.DoMessageTest([field]) def testRequiredField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REQUIRED field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testRepeatedField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testIntegerDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '10' self.DoMessageTest([field]) def testFloatDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'float_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.DOUBLE field.default_value = '10.1' self.DoMessageTest([field]) def testStringDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = u'a nice lovely string\'s "string"' self.DoMessageTest([field]) def testEnumDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'label' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'protorpc.descriptor.FieldDescriptor.Label' field.default_value = '2' self.DoMessageTest([field]) def testMultiFields(self): field1 = descriptor.FieldDescriptor() field1.name = 'integer_field' field1.number = 1 field1.label = descriptor.FieldDescriptor.Label.OPTIONAL field1.variant = descriptor.FieldDescriptor.Variant.INT64 field2 = descriptor.FieldDescriptor() field2.name = 'string_field' field2.number = 2 field2.label = descriptor.FieldDescriptor.Label.OPTIONAL field2.variant = descriptor.FieldDescriptor.Variant.STRING field3 = descriptor.FieldDescriptor() field3.name = 'unsigned_integer_field' field3.number = 3 field3.label = descriptor.FieldDescriptor.Label.OPTIONAL field3.variant = descriptor.FieldDescriptor.Variant.UINT64 self.DoMessageTest([field1, field2, field3]) def testNestedMessage(self): message = descriptor.MessageDescriptor() message.name = 'OuterMessage' inner_message = descriptor.MessageDescriptor() inner_message.name = 'InnerMessage' inner_inner_message = descriptor.MessageDescriptor() inner_inner_message.name = 'InnerInnerMessage' inner_message.message_types = [inner_inner_message] message.message_types = [inner_message] file_descriptor = descriptor.FileDescriptor() file_descriptor.message_types = [message] self.DoPythonTest(file_descriptor) def testNestedEnum(self): message = descriptor.MessageDescriptor() message.name = 'OuterMessage' inner_enum = descriptor.EnumDescriptor() inner_enum.name = 'InnerEnum' message.enum_types = [inner_enum] file_descriptor = descriptor.FileDescriptor() file_descriptor.message_types = [message] self.DoPythonTest(file_descriptor) def testService(self): service = descriptor.ServiceDescriptor() service.name = 'TheService' method1 = descriptor.MethodDescriptor() method1.name = 'method1' method1.request_type = 'protorpc.descriptor.FileDescriptor' method1.response_type = 'protorpc.descriptor.MethodDescriptor' service.methods = [method1] file_descriptor = descriptor.FileDescriptor() file_descriptor.service_types = [service] self.DoPythonTest(file_descriptor) # Test to make sure that implementation methods raise an exception. import blank service_instance = blank.TheService() self.assertRaisesWithRegexpMatch(NotImplementedError, 'Method method1 is not implemented', service_instance.method1, descriptor.FileDescriptor()) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protobuf.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import messages from protorpc import protobuf from protorpc import protorpc_test_pb2 from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protobuf class EncodeMessageTest(test_util.TestCase, test_util.ProtoConformanceTestBase): """Test message to protocol buffer encoding.""" PROTOLIB = protobuf def assertErrorIs(self, exception, message, function, *params, **kwargs): try: function(*params, **kwargs) self.fail('Expected to raise exception %s but did not.' % exception) except exception, err: self.assertEquals(message, str(err)) @property def encoded_partial(self): proto = protorpc_test_pb2.OptionalMessage() proto.double_value = 1.23 proto.int64_value = -100000000000 proto.int32_value = 1020 proto.string_value = u'a string' proto.enum_value = protorpc_test_pb2.OptionalMessage.VAL2 return proto.SerializeToString() @property def encoded_full(self): proto = protorpc_test_pb2.OptionalMessage() proto.double_value = 1.23 proto.float_value = -2.5 proto.int64_value = -100000000000 proto.uint64_value = 102020202020 proto.int32_value = 1020 proto.bool_value = True proto.string_value = u'a string\u044f' proto.bytes_value = 'a bytes\xff\xfe' proto.enum_value = protorpc_test_pb2.OptionalMessage.VAL2 return proto.SerializeToString() @property def encoded_repeated(self): proto = protorpc_test_pb2.RepeatedMessage() proto.double_value.append(1.23) proto.double_value.append(2.3) proto.float_value.append(-2.5) proto.float_value.append(0.5) proto.int64_value.append(-100000000000) proto.int64_value.append(20) proto.uint64_value.append(102020202020) proto.uint64_value.append(10) proto.int32_value.append(1020) proto.int32_value.append(718) proto.bool_value.append(True) proto.bool_value.append(False) proto.string_value.append(u'a string\u044f') proto.string_value.append(u'another string') proto.bytes_value.append('a bytes\xff\xfe') proto.bytes_value.append('another bytes') proto.enum_value.append(protorpc_test_pb2.RepeatedMessage.VAL2) proto.enum_value.append(protorpc_test_pb2.RepeatedMessage.VAL1) return proto.SerializeToString() @property def encoded_nested(self): proto = protorpc_test_pb2.HasNestedMessage() proto.nested.a_value = 'a string' return proto.SerializeToString() @property def encoded_repeated_nested(self): proto = protorpc_test_pb2.HasNestedMessage() proto.repeated_nested.add().a_value = 'a string' proto.repeated_nested.add().a_value = 'another string' return proto.SerializeToString() unexpected_tag_message = ( chr((15 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.NUMERIC) + chr(5)) @property def encoded_default_assigned(self): proto = protorpc_test_pb2.HasDefault() proto.a_value = test_util.HasDefault.a_value.default return proto.SerializeToString() @property def encoded_nested_empty(self): proto = protorpc_test_pb2.HasOptionalNestedMessage() proto.nested.Clear() return proto.SerializeToString() @property def encoded_repeated_nested_empty(self): proto = protorpc_test_pb2.HasOptionalNestedMessage() proto.repeated_nested.add() proto.repeated_nested.add() return proto.SerializeToString() @property def encoded_extend_message(self): proto = protorpc_test_pb2.RepeatedMessage() proto.add_int64_value(400) proto.add_int64_value(50) proto.add_int64_value(6000) return proto.SerializeToString() @property def encoded_string_types(self): proto = protorpc_test_pb2.OptionalMessage() proto.string_value = u'Latin' return proto.SerializeToString() def testDecodeWrongWireFormat(self): """Test what happens when wrong wire format found in protobuf.""" class ExpectedProto(messages.Message): value = messages.StringField(1) class WrongVariant(messages.Message): value = messages.IntegerField(1) original = WrongVariant() original.value = 10 self.assertErrorIs(messages.DecodeError, 'Expected wire type STRING but found NUMERIC', protobuf.decode_message, ExpectedProto, protobuf.encode_message(original)) def testDecodeBadWireType(self): """Test what happens when non-existant wire type found in protobuf.""" # Message has tag 1, type 3 which does not exist. bad_wire_type_message = chr((1 << protobuf._WIRE_TYPE_BITS) | 3) self.assertErrorIs(messages.DecodeError, 'No such wire type 3', protobuf.decode_message, test_util.OptionalMessage, bad_wire_type_message) def testUnexpectedTagBelowOne(self): """Test that completely invalid tags generate an error.""" # Message has tag 0, type NUMERIC. invalid_tag_message = chr(protobuf._Encoder.NUMERIC) self.assertErrorIs(messages.DecodeError, 'Invalid tag value 0', protobuf.decode_message, test_util.OptionalMessage, invalid_tag_message) def testProtocolBufferDecodeError(self): """Test what happens when there a ProtocolBufferDecodeError. This is what happens when the underlying ProtocolBuffer library raises it's own decode error. """ # Message has tag 1, type DOUBLE, missing value. truncated_message = ( chr((1 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.DOUBLE)) self.assertErrorIs(messages.DecodeError, 'Decoding error: truncated', protobuf.decode_message, test_util.OptionalMessage, truncated_message) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Common utility library.""" from __future__ import with_statement __author__ = ['rafek@google.com (Rafe Kaplan)', 'guido@google.com (Guido van Rossum)', ] import cgi import inspect import re __all__ = ['AcceptItem', 'AcceptError', 'Error', 'choose_content_type', 'parse_accept_header', 'positional', ] class Error(Exception): """Base class for protorpc exceptions.""" class AcceptError(Error): """Raised when there is an error parsing the accept header.""" def positional(max_positional_args): """A decorator to declare that only the first N arguments my be positional. This decorator makes it easy to support Python 3 style key-word only parameters. For example, in Python 3 it is possible to write: def fn(pos1, *, kwonly1=None, kwonly1=None): ... All named parameters after * must be a keyword: fn(10, 'kw1', 'kw2') # Raises exception. fn(10, kwonly1='kw1') # Ok. Example: To define a function like above, do: @positional(1) def fn(pos1, kwonly1=None, kwonly2=None): ... If no default value is provided to a keyword argument, it becomes a required keyword argument: @positional(0) def fn(required_kw): ... This must be called with the keyword parameter: fn() # Raises exception. fn(10) # Raises exception. fn(required_kw=10) # Ok. When defining instance or class methods always remember to account for 'self' and 'cls': class MyClass(object): @positional(2) def my_method(self, pos1, kwonly1=None): ... @classmethod @positional(2) def my_method(cls, pos1, kwonly1=None): ... Args: max_positional_arguments: Maximum number of positional arguments. All parameters after the this index must be keyword only. Returns: A decorator that prevents using arguments after max_positional_args from being used as positional parameters. Raises: TypeError if a key-word only argument is provided as a positional parameter. """ def positional_decorator(wrapped): def positional_wrapper(*args, **kwargs): if len(args) > max_positional_args: plural_s = '' if max_positional_args != 1: plural_s = 's' raise TypeError('%s() takes at most %d positional argument%s ' '(%d given)' % (wrapped.__name__, max_positional_args, plural_s, len(args))) return wrapped(*args, **kwargs) return positional_wrapper if isinstance(max_positional_args, (int, long)): return positional_decorator else: args, _, _, defaults = inspect.getargspec(max_positional_args) return positional(len(args) - len(defaults))(max_positional_args) # TODO(rafek): Support 'level' from the Accept header standard. class AcceptItem(object): """Encapsulate a single entry of an Accept header. Parses and extracts relevent values from an Accept header and implements a sort order based on the priority of each requested type as defined here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Accept headers are normally a list of comma separated items. Each item has the format of a normal HTTP header. For example: Accept: text/plain, text/html, text/*, */* This header means to prefer plain text over HTML, HTML over any other kind of text and text over any other kind of supported format. This class does not attempt to parse the list of items from the Accept header. The constructor expects the unparsed sub header and the index within the Accept header that the fragment was found. Properties: index: The index that this accept item was found in the Accept header. main_type: The main type of the content type. sub_type: The sub type of the content type. q: The q value extracted from the header as a float. If there is no q value, defaults to 1.0. values: All header attributes parsed form the sub-header. sort_key: A tuple (no_main_type, no_sub_type, q, no_values, index): no_main_type: */* has the least priority. no_sub_type: Items with no sub-type have less priority. q: Items with lower q value have less priority. no_values: Items with no values have less priority. index: Index of item in accept header is the last priority. """ __CONTENT_TYPE_REGEX = re.compile(r'^([^/]+)/([^/]+)$') def __init__(self, accept_header, index): """Parse component of an Accept header. Args: accept_header: Unparsed sub-expression of accept header. index: The index that this accept item was found in the Accept header. """ accept_header = accept_header.lower() content_type, values = cgi.parse_header(accept_header) match = self.__CONTENT_TYPE_REGEX.match(content_type) if not match: raise AcceptError('Not valid Accept header: %s' % accept_header) self.__index = index self.__main_type = match.group(1) self.__sub_type = match.group(2) self.__q = float(values.get('q', 1)) self.__values = values if self.__main_type == '*': self.__main_type = None if self.__sub_type == '*': self.__sub_type = None self.__sort_key = (not self.__main_type, not self.__sub_type, -self.__q, not self.__values, self.__index) @property def index(self): return self.__index @property def main_type(self): return self.__main_type @property def sub_type(self): return self.__sub_type @property def q(self): return self.__q @property def values(self): """Copy the dictionary of values parsed from the header fragment.""" return dict(self.__values) @property def sort_key(self): return self.__sort_key def match(self, content_type): """Determine if the given accept header matches content type. Args: content_type: Unparsed content type string. Returns: True if accept header matches content type, else False. """ content_type, _ = cgi.parse_header(content_type) match = self.__CONTENT_TYPE_REGEX.match(content_type.lower()) if not match: return False main_type, sub_type = match.group(1), match.group(2) if not(main_type and sub_type): return False return ((self.__main_type is None or self.__main_type == main_type) and (self.__sub_type is None or self.__sub_type == sub_type)) def __cmp__(self, other): """Comparison operator based on sort keys.""" if not isinstance(other, AcceptItem): return NotImplemented return cmp(self.sort_key, other.sort_key) def __str__(self): """Rebuilds Accept header.""" content_type = '%s/%s' % (self.__main_type or '*', self.__sub_type or '*') values = self.values if values: value_strings = ['%s=%s' % (i, v) for i, v in values.iteritems()] return '%s; %s' % (content_type, '; '.join(value_strings)) else: return content_type def __repr__(self): return 'AcceptItem(%r, %d)' % (str(self), self.__index) def parse_accept_header(accept_header): """Parse accept header. Args: accept_header: Unparsed accept header. Does not include name of header. Returns: List of AcceptItem instances sorted according to their priority. """ accept_items = [] for index, header in enumerate(accept_header.split(',')): accept_items.append(AcceptItem(header, index)) return sorted(accept_items) def choose_content_type(accept_header, supported_types): """Choose most appropriate supported type based on what client accepts. Args: accept_header: Unparsed accept header. Does not include name of header. supported_types: List of content-types supported by the server. The index of the supported types determines which supported type is prefered by the server should the accept header match more than one at the same priority. Returns: The preferred supported type if the accept header matches any, else None. """ for accept_item in parse_accept_header(accept_header): for supported_type in supported_types: if accept_item.match(supported_type): return supported_type return None
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Remote service library. This module contains classes that are useful for building remote services that conform to a standard request and response model. To conform to this model a service must be like the following class: # Each service instance only handles a single request and is then discarded. # Make these objects light weight. class Service(object): # It must be possible to construct service objects without any parameters. # If your constructor needs extra information you should provide a # no-argument factory function to create service instances. def __init__(self): ... # Each remote method must use the 'remote' decorator, passing the request # and response message types. The remote method itself must take a single # parameter which is an instance of RequestMessage and return an instance # of ResponseMessage. @method(RequestMessage, ResponseMessage) def remote_method(self, request): # Return an instance of ResponseMessage. # A service object may optionally implement a 'initialize_request_state' # method that takes as a parameter a single instance of a RequestState. If # a service does not implement this method it will not receive the request # state. def initialize_request_state(self, state): ... The 'Service' class is provided as a convenient base class that provides the above functionality. It implements all required and optional methods for a service. It also has convenience methods for creating factory functions that can pass persistent global state to a new service instance. The 'remote' decorator is used to declare which methods of a class are meant to service RPCs. While this decorator is not responsible for handling actual remote method invocations, such as handling sockets, handling various RPC protocols and checking messages for correctness, it does attach information to methods that responsible classes can examine and ensure the correctness of the RPC. When the remote decorator is used on a method, the wrapper method will have a 'remote' property associated with it. The 'remote' property contains the request_type and response_type expected by the methods implementation. On its own, the remote decorator does not provide any support for subclassing remote methods. In order to extend a service, one would need to redecorate the sub-classes methods. For example: class MyService(Service): @method(DoSomethingRequest, DoSomethingResponse) def do_something(self, request): ... implement do-something ... class MyBetterService(Service): @method(DoSomethingRequest, DoSomethingResponse) def do_something(self, request): response = super(MyService, self).do_something.remote.method(request) ... do something with response ... return response A Service subclass also has a Stub class that can be used with a transport for making RPCs. When a stub is created, it is capable of doing both synchronous and asynchronous RPCs if the underlying transport supports it. To make a stub using an HTTP transport do: my_service = MyService.Stub(HttpTransport('<my service URL>')) For synchronous calls, just call the expected methods on the service stub: request = DoSomethingRequest() ... response = my_service.do_something(request) Each stub instance has an async object that can be used for initiating asynchronous RPCs if the underlying protocol transport supports it. To make an asynchronous call, do: rpc = my_service.async.do_something(request) response = rpc.get_response() """ __author__ = 'rafek@google.com (Rafe Kaplan)' import logging import sys from protorpc import message_types from protorpc import messages from protorpc import descriptor from protorpc import util __all__ = [ 'ApplicationError', 'NetworkError', 'RequestError', 'RpcError', 'ServerError', 'ServerError', 'ServiceDefinitionError', 'RequestState', 'RpcState', 'RpcStatus', 'Service', 'StubBase', 'check_rpc_status', 'get_remote_method_info', 'is_error_status', 'method', 'remote', ] class ServiceDefinitionError(messages.Error): """Raised when a service is improperly defined.""" # TODO: Use error_name to map to specific exception message types. class RpcStatus(messages.Message): """Status of on-going or complete RPC. Fields: state: State of RPC. error_name: Error name set by application. Only set when status is APPLICATION_ERROR. For use by application to transmit specific reason for error. error_message: Error message associated with status. """ class State(messages.Enum): """Enumeration of possible RPC states. Values: OK: Completed successfully. RUNNING: Still running, not complete. REQUEST_ERROR: Request was malformed or incomplete. SERVER_ERROR: Server experienced an unexpected error. NETWORK_ERROR: An error occured on the network. APPLICATION_ERROR: The application is indicating an error. When in this state, RPC should also set application_error. """ OK = 0 RUNNING = 1 REQUEST_ERROR = 2 SERVER_ERROR = 3 NETWORK_ERROR = 4 APPLICATION_ERROR = 5 state = messages.EnumField(State, 1, required=True) error_message = messages.StringField(2) error_name = messages.StringField(3) RpcState = RpcStatus.State class RpcError(messages.Error): """Base class for RPC errors. Each sub-class of RpcError is associated with an error value from RpcState and has an attribute STATE that refers to that value. """ def __init__(self, message, cause=None): super(RpcError, self).__init__(message) self.cause = cause @classmethod def from_state(cls, state): """Get error class from RpcState. Args: state: RpcState value. Can be enum value itself, string or int. Returns: Exception class mapped to value if state is an error. Returns None if state is OK or RUNNING. """ return _RPC_STATE_TO_ERROR.get(RpcState(state)) class RequestError(RpcError): """Raised when wrong request objects received during method invocation.""" STATE = RpcState.REQUEST_ERROR class NetworkError(RpcError): """Raised when network error occurs during RPC.""" STATE = RpcState.NETWORK_ERROR class ServerError(RpcError): """Unexpected error occured on server.""" STATE = RpcState.SERVER_ERROR class ApplicationError(RpcError): """Raised for application specific errors. Attributes: error_name: Application specific error name for exception. """ STATE = RpcState.APPLICATION_ERROR def __init__(self, message, error_name=None): """Constructor. Args: message: Application specific error message. error_name: Application specific error name. Must be None, string or unicode string. """ super(ApplicationError, self).__init__(message) if error_name is None: self.error_name = None else: self.error_name = error_name def __str__(self): return self.args[0] def __repr__(self): if self.error_name is None: error_format = '' else: error_format = ', %r' % self.error_name return '%s(%r%s)' % (type(self).__name__, self.args[0], error_format) _RPC_STATE_TO_ERROR = { RpcState.REQUEST_ERROR: RequestError, RpcState.NETWORK_ERROR: NetworkError, RpcState.SERVER_ERROR: ServerError, RpcState.APPLICATION_ERROR: ApplicationError, } class _RemoteMethodInfo(object): """Object for encapsulating remote method information. An instance of this method is associated with the 'remote' attribute of the methods 'invoke_remote_method' instance. Instances of this class are created by the remote decorator and should not be created directly. """ def __init__(self, method, request_type, response_type): """Constructor. Args: method: The method which implements the remote method. This is a function that will act as an instance method of a class definition that is decorated by '@method'. It must always take 'self' as its first parameter. request_type: Expected request type for the remote method. response_type: Expected response type for the remote method. """ self.__method = method self.__request_type = request_type self.__response_type = response_type @property def method(self): """Original undecorated method.""" return self.__method @property def request_type(self): """Expected request type for remote method.""" if isinstance(self.__request_type, basestring): self.__request_type = messages.find_definition( self.__request_type, relative_to=sys.modules[self.__method.__module__]) return self.__request_type @property def response_type(self): """Expected response type for remote method.""" if isinstance(self.__response_type, basestring): self.__response_type = messages.find_definition( self.__response_type, relative_to=sys.modules[self.__method.__module__]) return self.__response_type def method(request_type, response_type): """Method decorator for creating remote methods. Args: request_type: Message type of expected request. response_type: Message type of expected response. Returns: 'remote_method_wrapper' function. Raises: TypeError: if the request_type or response_type parameters are not proper subclasses of messages.Message. """ if (not isinstance(request_type, basestring) and (not isinstance(request_type, type) or not issubclass(request_type, messages.Message) or request_type is messages.Message)): raise TypeError( 'Must provide message class for request-type. Found %s', request_type) if (not isinstance(response_type, basestring) and (not isinstance(response_type, type) or not issubclass(response_type, messages.Message) or response_type is messages.Message)): raise TypeError( 'Must provide message class for response-type. Found %s', response_type) def remote_method_wrapper(method): """Decorator used to wrap method. Args: method: Original method being wrapped. Returns: 'invoke_remote_method' function responsible for actual invocation. This invocation function instance is assigned an attribute 'remote' which contains information about the remote method: request_type: Expected request type for remote method. response_type: Response type returned from remote method. Raises: TypeError: If request_type or response_type is not a subclass of Message or is the Message class itself. """ def invoke_remote_method(service_instance, request): """Function used to replace original method. Invoke wrapped remote method. Checks to ensure that request and response objects are the correct types. Does not check whether messages are initialized. Args: service_instance: The service object whose method is being invoked. This is passed to 'self' during the invocation of the original method. request: Request message. Returns: Results of calling wrapped remote method. Raises: RequestError: Request object is not of the correct type. ServerError: Response object is not of the correct type. """ if not isinstance(request, remote_method_info.request_type): raise RequestError('Method %s.%s expected request type %s, ' 'received %s' % (type(service_instance).__name__, method.__name__, remote_method_info.request_type, type(request))) response = method(service_instance, request) if not isinstance(response, remote_method_info.response_type): raise ServerError('Method %s.%s expected response type %s, ' 'sent %s' % (type(service_instance).__name__, method.__name__, remote_method_info.response_type, type(response))) return response remote_method_info = _RemoteMethodInfo(method, request_type, response_type) invoke_remote_method.remote = remote_method_info invoke_remote_method.__name__ = method.__name__ return invoke_remote_method return remote_method_wrapper def remote(request_type, response_type): """Temporary backward compatibility alias for method.""" logging.warning('The remote decorator has been renamed method. It will be ' 'removed in very soon from future versions of ProtoRPC.') return method(request_type, response_type) def get_remote_method_info(method): """Get remote method info object from remote method. Returns: Remote method info object if method is a remote method, else None. """ if not callable(method): return None try: method_info = method.remote except AttributeError: return None if not isinstance(method_info, _RemoteMethodInfo): return None return method_info class StubBase(object): """Base class for client side service stubs. The remote method stubs are created by the _ServiceClass meta-class when a Service class is first created. The resulting stub will extend both this class and the service class it handles communications for. Assume that there is a service: class NewContactRequest(messages.Message): name = messages.StringField(1, required=True) phone = messages.StringField(2) email = messages.StringField(3) class AccountService(remote.Service): @remote.method(NewContact, message_types.VoidMessage): def new_contact(self, request): ... implementation ... A stub of this service can be called in two ways. The first is to pass in a correctly initialized NewContactRequest message: request = NewContactRequest() request.name = 'Bob Somebody' request.phone = '+1 415 555 1234' account_service_stub.new_contact(request) The second way is to pass in keyword parameters that correspond with the root request message type: account_service_stub.new_contact(name='Bob Somebody', phone='+1 415 555 1234') The second form will create a request message of the appropriate type. """ def __init__(self, transport): """Constructor. Args: transport: Underlying transport to communicate with remote service. """ self.__transport = transport @property def transport(self): """Transport used to communicate with remote service.""" return self.__transport @classmethod def definition_name(self): """Delegates to underlying service instance.""" return super(StubBase, self).definition_name() class _ServiceClass(type): """Meta-class for service class.""" def __new_async_method(cls, remote): """Create asynchronous method for Async handler. Args: remote: RemoteInfo to create method for. """ def async_method(self, *args, **kwargs): """Asynchronous remote method. Args: self: Instance of StubBase.Async subclass. Stub methods either take a single positional argument when a full request message is passed in, or keyword arguments, but not both. See docstring for StubBase for more information on how to use remote stub methods. Returns: Rpc instance used to represent asynchronous RPC. """ if args and kwargs: raise TypeError('May not provide both args and kwargs') if not args: # Construct request object from arguments. request = remote.request_type() for name, value in kwargs.iteritems(): setattr(request, name, value) else: # First argument is request object. request = args[0] return self.transport.send_rpc(remote, request) async_method.__name__ = remote.method.__name__ async_method = util.positional(2)(async_method) async_method.remote = remote return async_method def __new_sync_method(cls, async_method): """Create synchronous method for stub. Args: async_method: asynchronous method to delegate calls to. """ def sync_method(self, *args, **kwargs): """Synchronous remote method. Args: self: Instance of StubBase.Async subclass. args: Tuple (request,): request: Request object. kwargs: Field values for request. Must be empty if request object is provided. Returns: Response message from synchronized RPC. """ return async_method(self.async, *args, **kwargs).response sync_method.__name__ = async_method.__name__ sync_method.remote = async_method.remote return sync_method def __create_async_methods(cls, remote_methods): """Construct a dictionary of asynchronous methods based on remote methods. Args: remote_methods: Dictionary of methods with associated RemoteInfo objects. Returns: Dictionary of asynchronous methods with assocaited RemoteInfo objects. Results added to AsyncStub subclass. """ async_methods = {} for method_name, method in remote_methods.iteritems(): async_methods[method_name] = cls.__new_async_method(method.remote) return async_methods def __create_sync_methods(cls, async_methods): """Construct a dictionary of synchronous methods based on remote methods. Args: async_methods: Dictionary of async methods to delegate calls to. Returns: Dictionary of synchronous methods with assocaited RemoteInfo objects. Results added to Stub subclass. """ sync_methods = {} for method_name, async_method in async_methods.iteritems(): sync_methods[method_name] = cls.__new_sync_method(async_method) return sync_methods def __new__(cls, name, bases, dct): """Instantiate new service class instance.""" if StubBase not in bases: # Collect existing remote methods. base_methods = {} for base in bases: try: remote_methods = base.__remote_methods except AttributeError: pass else: base_methods.update(remote_methods) # Set this class private attribute so that base_methods do not have # to be recacluated in __init__. dct['_ServiceClass__base_methods'] = base_methods for attribute, value in dct.iteritems(): base_method = base_methods.get(attribute, None) if base_method: if not callable(value): raise ServiceDefinitionError( 'Must override %s in %s with a method.' % ( attribute, name)) if get_remote_method_info(value): raise ServiceDefinitionError( 'Do not use remote decorator when overloading remote method %s ' 'on service %s.' % (attribute, name)) base_remote_method_info = get_remote_method_info(base_method) remote_decorator = method( base_remote_method_info.request_type, base_remote_method_info.response_type) new_remote_method = remote_decorator(value) dct[attribute] = new_remote_method return type.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): """Create uninitialized state on new class.""" type.__init__(cls, name, bases, dct) # Only service implementation classes should have remote methods and stub # sub classes created. Stub implementations have their own methods passed # in to the type constructor. if StubBase not in bases: # Create list of remote methods. cls.__remote_methods = dict(cls.__base_methods) for attribute, value in dct.iteritems(): value = getattr(cls, attribute) remote_method_info = get_remote_method_info(value) if remote_method_info: cls.__remote_methods[attribute] = value # Build asynchronous stub class. stub_attributes = {'Service': cls} async_methods = cls.__create_async_methods(cls.__remote_methods) stub_attributes.update(async_methods) async_class = type('AsyncStub', (StubBase, cls), stub_attributes) cls.AsyncStub = async_class # Constructor for synchronous stub class. def __init__(self, transport): """Constructor. Args: transport: Underlying transport to communicate with remote service. """ super(cls.Stub, self).__init__(transport) self.async = cls.AsyncStub(transport) # Build synchronous stub class. stub_attributes = {'Service': cls, '__init__': __init__} stub_attributes.update(cls.__create_sync_methods(async_methods)) cls.Stub = type('Stub', (StubBase, cls), stub_attributes) @staticmethod def all_remote_methods(cls): """Get all remote methods of service. Returns: Dict from method name to unbound method. """ return dict(cls.__remote_methods) class RequestState(object): """Request state information. Attributes: remote_host: Remote host name where request originated. remote_address: IP address where request originated. server_host: Host of server within which service resides. server_port: Post which service has recevied request from. """ @util.positional(1) def __init__(self, remote_host=None, remote_address=None, server_host=None, server_port=None): """Constructor. Args: remote_host: Assigned to attribute. remote_address: Assigned to attribute. server_host: Assigned to attribute. server_port: Assigned to attribute. """ self.remote_host = remote_host self.remote_address = remote_address self.server_host = server_host self.server_port = server_port def __repr__(self): """String representation of state.""" state = [] if self.remote_host: state.append(('remote_host', self.remote_host)) if self.remote_address: state.append(('remote_address', self.remote_address)) if self.server_host: state.append(('server_host', self.server_host)) if self.server_port: state.append(('server_port', self.server_port)) if state: state_string = ' ' + ' '.join( '%s=%s' % (name, value) for (name, value) in state) else: state_string = '' return '<remote.RequestState%s>' % state_string class Service(object): """Service base class. Base class used for defining remote services. Contains reflection functions, useful helpers and built-in remote methods. Services are expected to be constructed via either a constructor or factory which takes no parameters. However, it might be required that some state or configuration is passed in to a service across multiple requests. To do this, define parameters to the constructor of the service and use the 'new_factory' class method to build a constructor that will transmit parameters to the constructor. For example: class MyService(Service): def __init__(self, configuration, state): self.configuration = configuration self.state = state configuration = MyServiceConfiguration() global_state = MyServiceState() my_service_factory = MyService.new_factory(configuration, state=global_state) The contract with any service handler is that a new service object is created to handle each user request, and that the construction does not take any parameters. The factory satisfies this condition: new_instance = my_service_factory() assert new_instance.state is global_state Attributes: request_state: RequestState set via initialize_request_state. """ __metaclass__ = _ServiceClass @classmethod def all_remote_methods(cls): """Get all remote methods for service class. Built-in methods do not appear in the dictionary of remote methods. Returns: Dictionary mapping method name to remote method. """ return _ServiceClass.all_remote_methods(cls) @classmethod def new_factory(cls, *args, **kwargs): """Create factory for service. Useful for passing configuration or state objects to the service. Accepts arbitrary parameters and keywords, however, underlying service must accept also accept not other parameters in its constructor. Args: args: Args to pass to service constructor. kwargs: Keyword arguments to pass to service constructor. Returns: Factory function that will create a new instance and forward args and keywords to the constructor. """ def service_factory(): return cls(*args, **kwargs) # Update docstring so that it is easier to debug. full_class_name = '%s.%s' % (cls.__module__, cls.__name__) service_factory.func_doc = ( 'Creates new instances of service %s.\n\n' 'Returns:\n' ' New instance of %s.' % (cls.__name__, full_class_name)) # Update name so that it is easier to debug the factory function. service_factory.func_name = '%s_service_factory' % cls.__name__ service_factory.service_class = cls return service_factory def initialize_request_state(self, request_state): """Save request state for use in remote method. Args: request_state: RequestState instance. """ self.__request_state = request_state @classmethod def definition_name(cls): """Get definition name for Service class. Package name is determined by the global 'package' attribute in the module that contains the Service definition. If no 'package' attribute is available, uses module name. If no module is found, just uses class name as name. Returns: Fully qualified service name. """ try: return cls.__definition_name except AttributeError: module = sys.modules.get(cls.__module__) if module: try: package = module.package except AttributeError: package = cls.__module__ cls.__definition_name = '%s.%s' % (package, cls.__name__) else: cls.__definition_name = cls.__name__ return cls.__definition_name @property def request_state(self): """Request state associated with this Service instance.""" return self.__request_state def is_error_status(status): """Function that determines whether the RPC status is an error. Args: status: Initialized RpcStatus message to check for errors. """ status.check_initialized() return RpcError.from_state(status.state) is not None def check_rpc_status(status): """Function converts an error status to a raised exception. Args: status: Initialized RpcStatus message to check for errors. Raises: RpcError according to state set on status, if it is an error state. """ status.check_initialized() error_class = RpcError.from_state(status.state) if error_class is not None: if error_class is ApplicationError: raise error_class(status.error_message, status.error_name) else: raise error_class(status.error_message)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Library for defining protocol messages in the Python language.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import itertools from google.protobuf import descriptor from google.protobuf import message from google.protobuf import reflection from protorpc import util __all__ = ['Error', 'EnumDefinitionError', 'MessageDefinitionError', 'BooleanField', 'BytesField', 'Enum', 'EnumField', 'FloatField', 'IntegerField', 'Message', 'StringField', ] class Error(Exception): """Base class for message exceptions.""" class EnumDefinitionError(Error): """Enumeration definition error.""" class MessageDefinitionError(Error): """Message definition error.""" _CPP_TYPE_MAP = { descriptor.FieldDescriptor.TYPE_DOUBLE: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_FLOAT: descriptor.FieldDescriptor.CPPTYPE_FLOAT, descriptor.FieldDescriptor.TYPE_INT64: descriptor.FieldDescriptor.CPPTYPE_INT64, descriptor.FieldDescriptor.TYPE_UINT64: descriptor.FieldDescriptor.CPPTYPE_UINT64, descriptor.FieldDescriptor.TYPE_INT32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_FIXED64: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_FIXED32: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_BOOL: descriptor.FieldDescriptor.CPPTYPE_BOOL, descriptor.FieldDescriptor.TYPE_STRING: descriptor.FieldDescriptor.CPPTYPE_STRING, descriptor.FieldDescriptor.TYPE_MESSAGE: descriptor.FieldDescriptor.CPPTYPE_MESSAGE, descriptor.FieldDescriptor.TYPE_BYTES: descriptor.FieldDescriptor.CPPTYPE_STRING, descriptor.FieldDescriptor.TYPE_UINT32: descriptor.FieldDescriptor.CPPTYPE_UINT32, descriptor.FieldDescriptor.TYPE_ENUM: descriptor.FieldDescriptor.CPPTYPE_ENUM, descriptor.FieldDescriptor.TYPE_SFIXED32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_SFIXED64: descriptor.FieldDescriptor.CPPTYPE_INT64, descriptor.FieldDescriptor.TYPE_SINT32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_SINT64: descriptor.FieldDescriptor.CPPTYPE_INT64, } class _EnumType(type): """Meta-class used for defining the Enum classes. Meta-class enables very specific behavior for any defined Enum class. All attributes defined on an Enum sub-class must be non-repeating integers. The meta-class ensures that only one level of Enum class hierarchy is possible. In other words it is not possible to delcare sub-classes of sub-classes of Enum. The class definition is used mainly for syntactic sugar. It is used by the _DynamicProtocolMessageType meta-class to initialize a descriptor object and then discarded. The class definition will NOT appear in the resulting class. The meta-class creates a class attribute _VALUES which is an ordered list of tuples (name, number) of the Enum definition in number order. """ __initialized = False __allowed_names = None def __new__(cls, name, bases, dictionary): if not _EnumType.__initialized: _EnumType.__initialized = True else: if bases != (Enum,): raise EnumDefinitionError('Enum classes may not be subclassed.') if not _EnumType.__allowed_names: _EnumType.__allowed_names = set(dir(Enum)) values = [] for attribute_name, value in dictionary.iteritems(): if attribute_name == '__module__': continue if not isinstance(value, (int, long)): raise EnumDefinitionError('Enum value %s must be an integer.' % value) values.append((attribute_name, value)) values.sort(key=lambda v: v[1]) dictionary['_VALUES'] = values return super(_EnumType, cls).__new__(cls, name, bases, dictionary) class Enum(object): """Base class for all enumerated types. Enumerated types are not meant to be instantiated. """ __metaclass__ = _EnumType def __init__(self): raise NotImplementedError() class _DynamicProtocolMessageType(reflection.GeneratedProtocolMessageType): """Meta-class used for defining the dynamic Message base class. For more details about Message classes, see the Message class docstring and protocol buffers: http://code.google.com/apis/protocolbuffers/docs/reference/python/index.html This meta-class enables very specific behavior for any defined Message class. All attributes defined on an Message sub-class must be field instances, Enum class definitions or other Message class definitions. Each field attribute defined on an Message sub-class is added to the set of field definitions and the attribute is translated in to FieldDescriptor. It also ensures that only one level of Message class hierarchy is possible. In other words it is not possible to declare sub-classes of sub-classes of Message. """ def __new__(cls, name, bases, dictionary): enums = [] enum_map = {} messages = [] field_definitions = [] fields = [] module = dictionary['__module__'] def update_nested_definitions(definition, root_name): """Update nested message, enum and field definitions When each message class is created, it cannot know what it's containing parent is. It is therefore necessary to recreate the full-name of nested messagse, enums and fields when every new message is created and to assign the definition's containing type. This method is recursive because any message definitions found within must also be updated. Args: definition: Definition that will be updated. root_name: The name of the module or definition containing this definition. """ # TODO(rafek): This is potentially an expensive process. Ideally the # descriptor should be able to generate a full name for a class based # on the containing types. definition.full_name = '%s.%s' % (root_name, definition.name) if isinstance(definition, descriptor.Descriptor): for sub_definition in itertools.chain(definition.nested_types, definition.enum_types, definition.fields): update_nested_definitions(sub_definition, definition.full_name) sub_definition.containing_type = definition # No additional intialization necessary for Message class defined in this # module. if bases != (message.Message,): # Do not subclass message classes. if bases != (Message,): raise MessageDefinitionError('May not subclass Message types.') # Configure nested definitions and fields. for attribute_name, value in dictionary.iteritems(): if attribute_name == '__module__': continue # Enumeration definitions. if isinstance(value, type) and issubclass(value, Enum): enum_numbers = [] for index, (enum_name, enum_number) in enumerate(value._VALUES): enum_numbers.append(descriptor.EnumValueDescriptor(name=enum_name, index=index, number=enum_number)) enum = descriptor.EnumDescriptor(name=attribute_name, full_name='', filename='', values=enum_numbers) enums.append(enum) enum_map[enum.name] = enum # Sub-message defintions. elif isinstance(value, type) and issubclass(value, message.Message): messages.append(value.DESCRIPTOR) # Field definitions. The fields are not configured here since they # must be processed in numeric order. elif isinstance(value, _Field): field_definitions.append((attribute_name, value)) else: raise MessageDefinitionError('Non-definition field %s.' % attribute_name) # Define fields in numeric order. field_definitions.sort(key=lambda v: v[1].number) for index, (attribute_name, field) in enumerate(field_definitions): if field.required and field.repeated: raise MessageDefinitionError('Field %s must be either required ' 'or repeated, not both' % attribute_name) default_value = field.default if field.required: label = descriptor.FieldDescriptor.LABEL_REQUIRED elif field.repeated: label = descriptor.FieldDescriptor.LABEL_REPEATED if default_value is None: default_value = [] else: label = descriptor.FieldDescriptor.LABEL_OPTIONAL if isinstance(field, EnumField): try: enum_type = enum_map[field.enum_type.__name__] except KeyError: raise MessageDefinitionError('Field %s may only use Enum type ' 'defined in same Message.' % attribute_name) else: enum_type = None fields.append(descriptor.FieldDescriptor( name=attribute_name, full_name='', index=index, number=field.number, type=field.variant, cpp_type=_CPP_TYPE_MAP[field.variant], label=label, default_value=default_value, message_type=None, enum_type=enum_type, containing_type=None, is_extension=False, extension_scope=None, has_default_value=field.default is not None)) # Throw away the Enum class definitions. for enum in enums: del dictionary[enum.name] # Define message descriptor. message_descriptor = descriptor.Descriptor(name=name, full_name='', filename='', containing_type=None, fields=fields, nested_types=messages, enum_types=enums, extensions=[]) update_nested_definitions(message_descriptor, module) dictionary[_DynamicProtocolMessageType._DESCRIPTOR_KEY] = message_descriptor if bases == (message.Message,): superclass = super(reflection.GeneratedProtocolMessageType, cls) else: superclass = super(_DynamicProtocolMessageType, cls) return superclass.__new__(cls, name, bases, dictionary) class Message(message.Message): """Base class for user defined message objects. Used to define messages for efficient transmission across network or process space. Messages are defined using the field classes (IntegerField, FloatField, EnumField, etc.). Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class is intentended to itself be transmitted across network or process space and used directly by clients or even other servers. As such methods and non-field attributes could not be transmitted with the structural information causing discrepancies between different languages and implementations. For more detail about how this message class works, please see: http://code.google.com/apis/protocolbuffers/docs/reference/python/index.html Field definitions are discarded by the meta-class and do not appear in the final class definition. In their place are a property instance defined by reflection.GeneratedProtocolMessageType. Example: class Lot(Message): price = IntegerField(1, required=True) quantity = IntegerField(2, required=True) class Order(Message): class TradeType(Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 symbol = StringProperty(1, required=True) total_quantity = IntegerProperty(2, required=True) trade_type = EnumProperty(TradeType, 3, required=True) limit = IntegerField(5) order = Order() assert not order.IsInitialized() order.symbol = 'GOOG' order.total_quantity = 10 order.trade_type = Order.BUY # Now object is initialized! assert order.IsInitialized() """ __metaclass__ = _DynamicProtocolMessageType __slots__ = [] class _Field(object): @util.positional(2) def __init__(self, number, required=False, repeated=False, variant=None, default=None): """Constructor. Store the attributes of a field so that the _DynamicProtocolMessageType meta-class can use it to populate field descriptors for the Message class. Instances of field are discarded after used by the meta-class. The required and repeated parameters are mutually exclusive. Setting both to True will raise a FieldDefinitionError. Repeated fields may not have default values. Sub-class Attributes: Each sub-class of _Field must define the following: VARIANTS: Set of variant types accepted by that field. DEFAULT_VARIANT: Default variant type if not specified in constructor. Args: number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive with 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive with 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: MessageDefinitionError when repeated fields are provided a default value or when an incompatible variant is provided. TypeError when an unexpected keyword argument is provided. """ self.number = number self.required = required self.repeated = repeated if self.repeated and default is not None: raise MessageDefinitionError( 'May not provide default for repeated fields.') self.default = default if variant is None: self.variant = self.DEFAULT_VARIANT else: self.variant = variant if self.variant not in self.VARIANTS: raise MessageDefinitionError('Bad variant.') class IntegerField(_Field): """Field definition for integer values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_INT64 VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_INT32, descriptor.FieldDescriptor.TYPE_INT64, descriptor.FieldDescriptor.TYPE_UINT32, descriptor.FieldDescriptor.TYPE_INT64, descriptor.FieldDescriptor.TYPE_SINT32, descriptor.FieldDescriptor.TYPE_SINT64, ]) class FloatField(_Field): """Field definition for float values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_DOUBLE VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_FLOAT, descriptor.FieldDescriptor.TYPE_DOUBLE, ]) class BooleanField(_Field): """Field definition for boolean values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_BOOL VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_BOOL]) class BytesField(_Field): """Field definition for byte (str) values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_BYTES VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_BYTES]) class StringField(_Field): """Field definition for unicode string values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_STRING VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_STRING]) class EnumField(_Field): """Field definition for enum values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_ENUM VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_ENUM]) def __init__(self, enum_type, number, **kwargs): """Constructor. Args: enum_type: Enum type for field. Must be subclass of Enum. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. default: Default value for field if not found in stream. Raises: TypeError when invalid enum_type is provided. """ # TODO(rafek): Support enumerated types outside of single message # definition scope. if isinstance(enum_type, type) and not issubclass(enum_type, Enum): raise TypeError('Enum field requires Enum class.') self.enum_type = enum_type super(EnumField, self).__init__(number, **kwargs)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.service_handlers.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import cStringIO import os import re import sys import unittest import urllib from google.appengine.ext import webapp from protorpc import forms from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import protourlencode from protorpc import message_types from protorpc import registry from protorpc import remote from protorpc import service_handlers from protorpc import test_util from protorpc import webapp_test_util import mox package = 'test_package' class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = service_handlers class Enum1(messages.Enum): """A test enum class.""" VAL1 = 1 VAL2 = 2 VAL3 = 3 class Request1(messages.Message): """A test request message type.""" integer_field = messages.IntegerField(1) string_field = messages.StringField(2) enum_field = messages.EnumField(Enum1, 3) class Response1(messages.Message): """A test response message type.""" integer_field = messages.IntegerField(1) string_field = messages.StringField(2) enum_field = messages.EnumField(Enum1, 3) class SuperMessage(messages.Message): """A test message with a nested message field.""" sub_message = messages.MessageField(Request1, 1) sub_messages = messages.MessageField(Request1, 2, repeated=True) class SuperSuperMessage(messages.Message): """A test message with two levels of nested.""" sub_message = messages.MessageField(SuperMessage, 1) sub_messages = messages.MessageField(Request1, 2, repeated=True) class RepeatedMessage(messages.Message): """A test message with a repeated field.""" ints = messages.IntegerField(1, repeated=True) strings = messages.StringField(2, repeated=True) enums = messages.EnumField(Enum1, 3, repeated=True) class Service(object): """A simple service that takes a Request1 and returns Request2.""" @remote.method(Request1, Response1) def method1(self, request): response = Response1() if hasattr(request, 'integer_field'): response.integer_field = request.integer_field if hasattr(request, 'string_field'): response.string_field = request.string_field if hasattr(request, 'enum_field'): response.enum_field = request.enum_field return response @remote.method(RepeatedMessage, RepeatedMessage) def repeated_method(self, request): response = RepeatedMessage() if hasattr(request, 'ints'): response = request.ints return response def not_remote(self): pass def VerifyResponse(test, response, expected_status, expected_status_message, expected_content): def write(content): if expected_content == '': test.assertEquals('', content) else: test.assertNotEquals(-1, content.find(expected_content)) def start_response(response, headers): status, message = response.split(' ', 1) test.assertEquals(expected_status, status) test.assertEquals(expected_status_message, message) return write response.wsgi_write(start_response) class ServiceHandlerFactoryTest(test_util.TestCase): """Tests for the service handler factory.""" def testAllRequestMappers(self): """Test all_request_mappers method.""" configuration = service_handlers.ServiceHandlerFactory(Service) mapper1 = service_handlers.RPCMapper(['whatever'], 'whatever', None) mapper2 = service_handlers.RPCMapper(['whatever'], 'whatever', None) configuration.add_request_mapper(mapper1) self.assertEquals([mapper1], list(configuration.all_request_mappers())) configuration.add_request_mapper(mapper2) self.assertEquals([mapper1, mapper2], list(configuration.all_request_mappers())) def testServiceFactory(self): """Test that service_factory attribute is set.""" handler_factory = service_handlers.ServiceHandlerFactory(Service) self.assertEquals(Service, handler_factory.service_factory) def testFactoryMethod(self): """Test that factory creates correct instance of class.""" factory = service_handlers.ServiceHandlerFactory(Service) handler = factory() self.assertTrue(isinstance(handler, service_handlers.ServiceHandler)) self.assertTrue(isinstance(handler.service, Service)) def testMapping(self): """Test the mapping method.""" factory = service_handlers.ServiceHandlerFactory(Service) path, mapped_factory = factory.mapping('/my_service') self.assertEquals(r'/my_service' + service_handlers._METHOD_PATTERN, path) self.assertEquals(id(factory), id(mapped_factory)) match = re.match(path, '/my_service.my_method') self.assertEquals('my_method', match.group(1)) path, mapped_factory = factory.mapping('/my_service/nested') self.assertEquals('/my_service/nested' + service_handlers._METHOD_PATTERN, path) match = re.match(path, '/my_service/nested.my_method') self.assertEquals('my_method', match.group(1)) def testRegexMapping(self): """Test the mapping method using a regex.""" factory = service_handlers.ServiceHandlerFactory(Service) path, mapped_factory = factory.mapping('.*/my_service') self.assertEquals(r'.*/my_service' + service_handlers._METHOD_PATTERN, path) self.assertEquals(id(factory), id(mapped_factory)) match = re.match(path, '/whatever_preceeds/my_service.my_method') self.assertEquals('my_method', match.group(1)) match = re.match(path, '/something_else/my_service.my_other_method') self.assertEquals('my_other_method', match.group(1)) def testMapping_BadPath(self): """Test bad parameterse to the mapping method.""" factory = service_handlers.ServiceHandlerFactory(Service) self.assertRaises(ValueError, factory.mapping, '/my_service/') def testDefault(self): """Test the default factory convenience method.""" handler_factory = service_handlers.ServiceHandlerFactory.default( Service, parameter_prefix='my_prefix.') self.assertEquals(Service, handler_factory.service_factory) mappers = handler_factory.all_request_mappers() # Verify URL encoded mapper. url_encoded_mapper = mappers.next() self.assertTrue(isinstance(url_encoded_mapper, service_handlers.URLEncodedRPCMapper)) self.assertEquals('my_prefix.', url_encoded_mapper.parameter_prefix) # Verify Protobuf encoded mapper. protobuf_mapper = mappers.next() self.assertTrue(isinstance(protobuf_mapper, service_handlers.ProtobufRPCMapper)) # Verify JSON encoded mapper. json_mapper = mappers.next() self.assertTrue(isinstance(json_mapper, service_handlers.JSONRPCMapper)) # Should have no more mappers. self.assertRaises(StopIteration, mappers.next) class ServiceHandlerTest(webapp_test_util.RequestHandlerTestBase): """Test the ServiceHandler class.""" def setUp(self): self.mox = mox.Mox() self.service_factory = Service self.remote_host = 'remote.host.com' self.server_host = 'server.host.com' self.ResetRequestHandler() self.request = Request1() self.request.integer_field = 1 self.request.string_field = 'a' self.request.enum_field = Enum1.VAL1 def ResetRequestHandler(self): super(ServiceHandlerTest, self).setUp() def CreateService(self): return self.service_factory() def CreateRequestHandler(self): self.rpc_mapper1 = self.mox.CreateMock(service_handlers.RPCMapper) self.rpc_mapper1.http_methods = set(['POST']) self.rpc_mapper1.content_types = set(['application/x-www-form-urlencoded']) self.rpc_mapper2 = self.mox.CreateMock(service_handlers.RPCMapper) self.rpc_mapper2.http_methods = set(['GET']) self.rpc_mapper2.content_types = set(['application/x-www-form-urlencoded']) self.factory = service_handlers.ServiceHandlerFactory( self.CreateService) self.factory.add_request_mapper(self.rpc_mapper1) self.factory.add_request_mapper(self.rpc_mapper2) return self.factory() def GetEnvironment(self): """Create handler to test.""" environ = super(ServiceHandlerTest, self).GetEnvironment() if self.remote_host: environ['REMOTE_HOST'] = self.remote_host if self.server_host: environ['SERVER_HOST'] = self.server_host return environ def VerifyResponse(self, expected_status, expected_status_message, expected_content): VerifyResponse(self, self.response, expected_status, expected_status_message, expected_content) def testRedirect(self): """Test that redirection is disabled.""" self.assertRaises(NotImplementedError, self.handler.redirect, '/') def testFirstMapper(self): """Make sure service attribute works when matches first RPCMapper.""" self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testSecondMapper(self): """Make sure service attribute works when matches first RPCMapper. Demonstrates the multiplicity of the RPCMapper configuration. """ self.rpc_mapper2.build_request( self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper2.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.handle('GET', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testCaseInsensitiveContentType(self): """Ensure that matching content-type is case insensitive.""" request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = ('ApPlIcAtIoN/' 'X-wWw-FoRm-UrLeNcOdEd') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testContentTypeWithParameters(self): """Test that content types have parameters parsed out.""" request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = ('application/' 'x-www-form-urlencoded' + '; a=b; c=d') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testContentFromHeaderOnly(self): """Test getting content-type from HTTP_CONTENT_TYPE directly. Some bad web server implementations might decide not to set CONTENT_TYPE for POST requests where there is an empty body. In these cases, need to get content-type directly from webob environ key HTTP_CONTENT_TYPE. """ request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = None self.handler.request.environ['HTTP_CONTENT_TYPE'] = ( 'application/x-www-form-urlencoded') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testRequestState(self): """Make sure request state is passed in to handler that supports it.""" class ServiceWithState(object): initialize_request_state = self.mox.CreateMockAnything() @remote.method(Request1, Response1) def method1(self, request): return Response1() self.service_factory = ServiceWithState # Reset handler with new service type. self.ResetRequestHandler() self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(Request1()) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)) def verify_state(state): return ('remote.host.com' == state.remote_host and '127.0.0.1' == state.remote_address and 'server.host.com' == state.server_host and 8080 == state.server_port) ServiceWithState.initialize_request_state(mox.Func(verify_state)) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '') self.mox.VerifyAll() def testRequestState_MissingHosts(self): """Make sure missing state environment values are handled gracefully.""" class ServiceWithState(object): initialize_request_state = self.mox.CreateMockAnything() @remote.method(Request1, Response1) def method1(self, request): return Response1() self.service_factory = ServiceWithState self.remote_host = None self.server_host = None # Reset handler with new service type. self.ResetRequestHandler() self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(Request1()) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)) def verify_state(state): return (None is state.remote_host and '127.0.0.1' == state.remote_address and None is state.server_host and 8080 == state.server_port) ServiceWithState.initialize_request_state(mox.Func(verify_state)) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '') self.mox.VerifyAll() def testNoMatch_UnknownHTTPMethod(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() self.handler.handle('UNKNOWN', 'does_not_matter') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoMatch_UnknownContentType(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = 'image/png' self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoMatch_NoContentType(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() del self.handler.request.headers['Content-Type'] self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoSuchMethod(self): """When service method not found.""" self.mox.ReplayAll() self.handler.handle('POST', 'no_such_method') self.VerifyResponse('400', 'Unrecognized RPC method: no_such_method', '') self.mox.VerifyAll() def testNoSuchRemoteMethod(self): """When service method exists but is not remote.""" self.mox.ReplayAll() self.handler.handle('POST', 'not_remote') self.VerifyResponse('400', 'Unrecognized RPC method: not_remote', '') self.mox.VerifyAll() def testRequestError(self): """RequestError handling.""" def build_request(handler, request): raise service_handlers.RequestError('This is a request error') self.rpc_mapper1.build_request( self.handler, Request1).WithSideEffects(build_request) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Invalid RPC request.', '') self.mox.VerifyAll() def testDecodeError(self): """DecodeError handling.""" def build_request(handler, request): raise messages.DecodeError('This is a decode error') self.rpc_mapper1.build_request( self.handler, Request1).WithSideEffects(build_request) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Invalid RPC request.', '') self.mox.VerifyAll() def testResponseException(self): """Test what happens when build_response raises ResponseError.""" self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(self.request) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).AndRaise( service_handlers.ResponseError) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('500', 'Invalid RPC response.', '') self.mox.VerifyAll() def testGet(self): """Test that GET goes to 'handle' properly.""" self.handler.handle = self.mox.CreateMockAnything() self.handler.handle('GET', 'alternate_method') self.mox.ReplayAll() self.handler.get('alternate_method') self.mox.VerifyAll() def testPost(self): """Test that POST goes to 'handle' properly.""" self.handler.handle = self.mox.CreateMockAnything() self.handler.handle('POST', 'alternate_method') self.mox.ReplayAll() self.handler.post('alternate_method') self.mox.VerifyAll() class RPCMapperTestBase(test_util.TestCase): def setUp(self): """Set up test framework.""" self.Reinitialize() def Reinitialize(self, input='', get=False, path_method='method1', content_type='text/plain'): """Allows reinitialization of test with custom input values and POST. Args: input: Query string or POST input. get: Use GET method if True. Use POST if False. """ self.factory = service_handlers.ServiceHandlerFactory(Service) self.service_handler = service_handlers.ServiceHandler(self.factory, Service()) self.service_handler.remote_method = path_method request_path = '/servicepath' if path_method: request_path += '/' + path_method if get: request_path += '?' + input if get: environ = {'wsgi.input': cStringIO.StringIO(''), 'CONTENT_LENGTH': '0', 'QUERY_STRING': input, 'REQUEST_METHOD': 'GET', 'PATH_INFO': request_path, } self.service_handler.method = 'GET' else: environ = {'wsgi.input': cStringIO.StringIO(input), 'CONTENT_LENGTH': str(len(input)), 'QUERY_STRING': '', 'REQUEST_METHOD': 'POST', 'PATH_INFO': request_path, } self.service_handler.method = 'POST' self.request = webapp.Request(environ) self.response = webapp.Response() self.service_handler.initialize(self.request, self.response) self.service_handler.request.headers['Content-Type'] = content_type class RPCMapperTest(RPCMapperTestBase, webapp_test_util.RequestHandlerTestBase): """Test the RPCMapper base class.""" def setUp(self): RPCMapperTestBase.setUp(self) webapp_test_util.RequestHandlerTestBase.setUp(self) self.mox = mox.Mox() self.protocol = self.mox.CreateMockAnything() def GetEnvironment(self): """Get environment. Return bogus content in body. Returns: dict of CGI environment. """ environment = super(RPCMapperTest, self).GetEnvironment() environment['wsgi.input'] = cStringIO.StringIO('my body') environment['CONTENT_LENGTH'] = len('my body') return environment def testContentTypes_JustDefault(self): """Test content type attributes.""" self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['GET', 'POST'], 'my-content-type', self.protocol) self.assertEquals(frozenset(['GET', 'POST']), mapper.http_methods) self.assertEquals('my-content-type', mapper.default_content_type) self.assertEquals(frozenset(['my-content-type']), mapper.content_types) self.mox.VerifyAll() def testContentTypes_Extended(self): """Test content type attributes.""" self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['GET', 'POST'], 'my-content-type', self.protocol, content_types=['a', 'b']) self.assertEquals(frozenset(['GET', 'POST']), mapper.http_methods) self.assertEquals('my-content-type', mapper.default_content_type) self.assertEquals(frozenset(['my-content-type', 'a', 'b']), mapper.content_types) self.mox.VerifyAll() def testBuildRequest(self): """Test building a request.""" expected_request = Request1() self.protocol.decode_message(Request1, 'my body').AndReturn(expected_request) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_request(self.handler, Request1) self.assertTrue(expected_request is request) def testBuildRequest_ValidationError(self): """Test building a request generating a validation error.""" expected_request = Request1() self.protocol.decode_message( Request1, 'my body').AndRaise(messages.ValidationError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch( service_handlers.RequestError, 'Unable to parse request content: xyz', mapper.build_request, self.handler, Request1) def testBuildRequest_DecodeError(self): """Test building a request generating a decode error.""" expected_request = Request1() self.protocol.decode_message( Request1, 'my body').AndRaise(messages.DecodeError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch( service_handlers.RequestError, 'Unable to parse request content: xyz', mapper.build_request, self.handler, Request1) def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndReturn('encoded') self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_response(self.handler, response) self.assertEquals('my-content-type', self.handler.response.headers['Content-Type']) self.assertEquals('encoded', self.handler.response.out.getvalue()) def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndRaise( messages.ValidationError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch(service_handlers.ResponseError, 'Unable to encode message: xyz', mapper.build_response, self.handler, response) class ProtocolMapperTestBase(object): """Base class for basic protocol mapper tests.""" def setUp(self): """Reinitialize test specifically for protocol buffer mapper.""" super(ProtocolMapperTestBase, self).setUp() self.Reinitialize(path_method='my_method', content_type='application/x-google-protobuf') self.request_message = Request1() self.request_message.integer_field = 1 self.request_message.string_field = u'something' self.request_message.enum_field = Enum1.VAL1 self.response_message = Response1() self.response_message.integer_field = 1 self.response_message.string_field = u'something' self.response_message.enum_field = Enum1.VAL1 def testBuildRequest(self): """Test request building.""" self.Reinitialize(self.protocol.encode_message(self.request_message), content_type=self.content_type) mapper = self.mapper() parsed_request = mapper.build_request(self.service_handler, Request1) self.assertEquals(self.request_message, parsed_request) def testBuildResponse(self): """Test response building.""" mapper = self.mapper() mapper.build_response(self.service_handler, self.response_message) self.assertEquals(self.protocol.encode_message(self.response_message), self.service_handler.response.out.getvalue()) def testWholeRequest(self): """Test the basic flow of a request with mapper class.""" body = self.protocol.encode_message(self.request_message) self.Reinitialize(input=body, content_type=self.content_type) self.factory.add_request_mapper(self.mapper()) self.service_handler.handle('POST', 'method1') VerifyResponse(self, self.service_handler.response, '200', 'OK', self.protocol.encode_message(self.response_message)) class URLEncodedRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the URL encoded RPC mapper.""" content_type = 'application/x-www-form-urlencoded' protocol = protourlencode mapper = service_handlers.URLEncodedRPCMapper def testBuildRequest_Prefix(self): """Test building request with parameter prefix.""" self.Reinitialize(urllib.urlencode([('prefix_integer_field', '10'), ('prefix_string_field', 'a string'), ('prefix_enum_field', 'VAL1'), ]), self.content_type) url_encoded_mapper = service_handlers.URLEncodedRPCMapper( parameter_prefix='prefix_') request = url_encoded_mapper.build_request(self.service_handler, Request1) self.assertEquals(10, request.integer_field) self.assertEquals('a string', request.string_field) self.assertEquals(Enum1.VAL1, request.enum_field) def testBuildRequest_DecodeError(self): """Test trying to build request that causes a decode error.""" self.Reinitialize(urllib.urlencode((('integer_field', '10'), ('integer_field', '20'), )), content_type=self.content_type) url_encoded_mapper = service_handlers.URLEncodedRPCMapper() self.assertRaises(service_handlers.RequestError, url_encoded_mapper.build_request, self.service_handler, Service.method1.remote.request_type) def testBuildResponse_Prefix(self): """Test building a response with parameter prefix.""" response = Response1() response.integer_field = 10 response.string_field = u'a string' response.enum_field = Enum1.VAL3 url_encoded_mapper = service_handlers.URLEncodedRPCMapper( parameter_prefix='prefix_') url_encoded_mapper.build_response(self.service_handler, response) self.assertEquals('application/x-www-form-urlencoded', self.response.headers['content-type']) self.assertEquals(cgi.parse_qs(self.response.out.getvalue(), True, True), {'prefix_integer_field': ['10'], 'prefix_string_field': [u'a string'], 'prefix_enum_field': ['VAL3'], }) class ProtobufRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the protobuf encoded RPC mapper.""" content_type = 'application/x-google-protobuf' protocol = protobuf mapper = service_handlers.ProtobufRPCMapper class JSONRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the URL encoded RPC mapper.""" content_type = 'application/json' protocol = protojson mapper = service_handlers.JSONRPCMapper class MyService(remote.Service): def __init__(self, value='default'): self.value = value class ServiceMappingTest(test_util.TestCase): def CheckFormMappings(self, mapping, registry_path='/protorpc'): """Check to make sure that form mapping is configured as expected. Args: mapping: Mapping that should contain forms handlers. """ pattern, factory = mapping[0] self.assertEquals('%s/form(?:/)?' % registry_path, pattern) handler = factory() self.assertTrue(isinstance(handler, forms.FormsHandler)) self.assertEquals(registry_path, handler.registry_path) pattern, factory = mapping[1] self.assertEquals('%s/form/(.+)' % registry_path, pattern) self.assertEquals(forms.ResourceHandler, factory) def DoMappingTest(self, services, registry_path='/myreg', expected_paths=None): mapped_services = mapping = service_handlers.service_mapping(services, registry_path) if registry_path: form_mapping = mapping[:2] mapped_registry_path, mapped_registry_factory = mapping[-1] mapped_services = mapping[2:-1] self.CheckFormMappings(form_mapping, registry_path=registry_path) self.assertEquals(registry_path + service_handlers._METHOD_PATTERN, mapped_registry_path) self.assertEquals(registry.RegistryService, mapped_registry_factory.service_factory.service_class) # Verify registry knows about other services. expected_registry = {registry_path: registry.RegistryService} for path, factory in dict(services).iteritems(): if isinstance(factory, type) and issubclass(factory, remote.Service): expected_registry[path] = factory else: expected_registry[path] = factory.service_class self.assertEquals(expected_registry, mapped_registry_factory().service.registry) # Verify that services are mapped to URL. self.assertEquals(len(services), len(mapped_services)) for path, service in dict(services).iteritems(): mapped_path = path + service_handlers._METHOD_PATTERN mapped_factory = dict(mapped_services)[mapped_path] self.assertEquals(service, mapped_factory.service_factory) def testServiceMapping_Empty(self): """Test an empty service mapping.""" self.DoMappingTest({}) def testServiceMapping_ByClass(self): """Test mapping a service by class.""" self.DoMappingTest({'/my-service': MyService}) def testServiceMapping_ByFactory(self): """Test mapping a service by factory.""" self.DoMappingTest({'/my-service': MyService.new_factory('new-value')}) def testServiceMapping_ByList(self): """Test mapping a service by factory.""" self.DoMappingTest( [('/my-service1', MyService.new_factory('service1')), ('/my-service2', MyService.new_factory('service2')), ]) def testServiceMapping_NoRegistry(self): """Test mapping a service by class.""" mapping = self.DoMappingTest({'/my-service': MyService}, None) def testDefaultMappingWithClass(self): """Test setting path just from the class. Path of the mapping will be the fully qualified ProtoRPC service name with '.' replaced with '/'. For example: com.nowhere.service.TheService -> /com/nowhere/service/TheService """ mapping = service_handlers.service_mapping([MyService]) mapped_services = mapping[2:-1] self.assertEquals(1, len(mapped_services)) path, factory = mapped_services[0] self.assertEquals( '/test_package/MyService' + service_handlers._METHOD_PATTERN, path) self.assertEquals(MyService, factory.service_factory) def testDefaultMappingWithFactory(self): mapping = service_handlers.service_mapping( [MyService.new_factory('service1')]) mapped_services = mapping[2:-1] self.assertEquals(1, len(mapped_services)) path, factory = mapped_services[0] self.assertEquals( '/test_package/MyService' + service_handlers._METHOD_PATTERN, path) self.assertEquals(MyService, factory.service_factory.service_class) def testMappingDuplicateExplicitServiceName(self): self.assertRaisesWithRegexpMatch( service_handlers.ServiceConfigurationError, "Path '/my_path' is already defined in service mapping", service_handlers.service_mapping, [('/my_path', MyService), ('/my_path', MyService), ]) def testMappingDuplicateServiceName(self): self.assertRaisesWithRegexpMatch( service_handlers.ServiceConfigurationError, "Path '/test_package/MyService' is already defined in service mapping", service_handlers.service_mapping, [MyService, MyService]) class GetCalled(remote.Service): def __init__(self, test): self.test = test @remote.method(Request1, Response1) def my_method(self, request): self.test.request = request return Response1(string_field='a response') class TestRunServices(test_util.TestCase): def DoRequest(self, path, request, response_type, reg_path='/protorpc'): stdin = sys.stdin stdout = sys.stdout environ = os.environ try: sys.stdin = cStringIO.StringIO(protojson.encode_message(request)) sys.stdout = cStringIO.StringIO() os.environ = webapp_test_util.GetDefaultEnvironment() os.environ['PATH_INFO'] = path os.environ['REQUEST_METHOD'] = 'POST' os.environ['CONTENT_TYPE'] = 'application/json' os.environ['wsgi.input'] = sys.stdin os.environ['wsgi.output'] = sys.stdout os.environ['CONTENT_LENGTH'] = len(sys.stdin.getvalue()) service_handlers.run_services( [('/my_service', GetCalled.new_factory(self))], reg_path) header, body = sys.stdout.getvalue().split('\n\n', 1) return (header.split('\n')[0], protojson.decode_message(response_type, body)) finally: sys.stdin = stdin sys.stdout = stdout os.environ = environ def testRequest(self): request = Request1(string_field='request value') status, response = self.DoRequest('/my_service.my_method', request, Response1) self.assertEquals('Status: 200 OK', status) self.assertEquals(request, self.request) self.assertEquals(Response1(string_field='a response'), response) def testRegistry(self): request = Request1(string_field='request value') status, response = self.DoRequest('/protorpc.services', message_types.VoidMessage(), registry.ServicesResponse) self.assertEquals('Status: 200 OK', status) self.assertEquals(registry.ServicesResponse( services=[ registry.ServiceMapping( name='/protorpc', definition='protorpc.registry.RegistryService'), registry.ServiceMapping( name='/my_service', definition='test_package.GetCalled'), ]), response) def testRunServicesWithOutRegistry(self): request = Request1(string_field='request value') status, response = self.DoRequest('/protorpc.services', message_types.VoidMessage(), registry.ServicesResponse, reg_path=None) self.assertEquals('Status: 404 Not Found', status) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import imp import inspect import new import re import sys import types import unittest from protorpc import test_util from protorpc import descriptor from protorpc import messages class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = messages class ValidationErrorTest(test_util.TestCase): def testStr_NoFieldName(self): """Test string version of ValidationError when no name provided.""" self.assertEquals('Validation error', str(messages.ValidationError('Validation error'))) def testStr_FieldName(self): """Test string version of ValidationError when no name provided.""" validation_error = messages.ValidationError('Validation error') validation_error.field_name = 'a_field' self.assertEquals('Field a_field: Validation error', str(validation_error)) class EnumTest(test_util.TestCase): def setUp(self): """Set up tests.""" # Redefine Color class in case so that changes to it (an error) in one test # does not affect other tests. global Color class Color(messages.Enum): RED = 20 ORANGE = 2 YELLOW = 40 GREEN = 4 BLUE = 50 INDIGO = 5 VIOLET = 80 def testNames(self): """Test that names iterates over enum names.""" self.assertEquals( set(['BLUE', 'GREEN', 'INDIGO', 'ORANGE', 'RED', 'VIOLET', 'YELLOW']), set(Color.names())) def testNumbers(self): """Tests that numbers iterates of enum numbers.""" self.assertEquals(set([2, 4, 5, 20, 40, 50, 80]), set(Color.numbers())) def testIterate(self): """Test that __iter__ iterates over all enum values.""" self.assertEquals(set(Color), set([Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.INDIGO, Color.VIOLET])) def testNaturalOrder(self): """Test that natural order enumeration is in numeric order.""" self.assertEquals([Color.ORANGE, Color.GREEN, Color.INDIGO, Color.RED, Color.YELLOW, Color.BLUE, Color.VIOLET], sorted(Color)) def testByName(self): """Test look-up by name.""" self.assertEquals(Color.RED, Color.lookup_by_name('RED')) self.assertRaises(KeyError, Color.lookup_by_name, 20) self.assertRaises(KeyError, Color.lookup_by_name, Color.RED) def testByNumber(self): """Test look-up by number.""" self.assertRaises(KeyError, Color.lookup_by_number, 'RED') self.assertEquals(Color.RED, Color.lookup_by_number(20)) self.assertRaises(KeyError, Color.lookup_by_number, Color.RED) def testConstructor(self): """Test that constructor look-up by name or number.""" self.assertEquals(Color.RED, Color('RED')) self.assertEquals(Color.RED, Color(u'RED')) self.assertEquals(Color.RED, Color(20)) self.assertEquals(Color.RED, Color(20L)) self.assertEquals(Color.RED, Color(Color.RED)) self.assertRaises(TypeError, Color, 'Not exists') self.assertRaises(TypeError, Color, 'Red') self.assertRaises(TypeError, Color, 100) self.assertRaises(TypeError, Color, 10.0) def testLen(self): """Test that len function works to count enums.""" self.assertEquals(7, len(Color)) def testNoSubclasses(self): """Test that it is not possible to sub-class enum classes.""" def declare_subclass(): class MoreColor(Color): pass self.assertRaises(messages.EnumDefinitionError, declare_subclass) def testClassNotMutable(self): """Test that enum classes themselves are not mutable.""" self.assertRaises(AttributeError, setattr, Color, 'something_new', 10) def testInstancesMutable(self): """Test that enum instances are not mutable.""" self.assertRaises(TypeError, setattr, Color.RED, 'something_new', 10) def testDefEnum(self): """Test def_enum works by building enum class from dict.""" WeekDay = messages.Enum.def_enum({'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 6, 'Saturday': 7, 'Sunday': 8}, 'WeekDay') self.assertEquals('Wednesday', WeekDay(3).name) self.assertEquals(6, WeekDay('Friday').number) self.assertEquals(WeekDay.Sunday, WeekDay('Sunday')) def testNonInt(self): """Test that non-integer values rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': '1'}, 'BadEnum') def testNegativeInt(self): """Test that negative numbers rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': -1}, 'BadEnum') def testLowerBound(self): """Test that zero is accepted by enum def.""" class NotImportant(messages.Enum): """Testing for value zero""" VALUE = 0 self.assertEquals(0, int(NotImportant.VALUE)) def testTooLargeInt(self): """Test that numbers too large are rejected.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': (2 ** 29)}, 'BadEnum') def testRepeatedInt(self): """Test duplicated numbers are forbidden.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Ok': 1, 'Repeated': 1}, 'BadEnum') def testStr(self): """Test converting to string.""" self.assertEquals('RED', str(Color.RED)) self.assertEquals('ORANGE', str(Color.ORANGE)) def testInt(self): """Test converting to int.""" self.assertEquals(20, int(Color.RED)) self.assertEquals(2, int(Color.ORANGE)) def testRepr(self): """Test enum representation.""" self.assertEquals('Color(RED, 20)', repr(Color.RED)) self.assertEquals('Color(YELLOW, 40)', repr(Color.YELLOW)) def testDocstring(self): """Test that docstring is supported ok.""" class NotImportant(messages.Enum): """I have a docstring.""" VALUE1 = 1 self.assertEquals('I have a docstring.', NotImportant.__doc__) def testDeleteEnumValue(self): """Test that enum values cannot be deleted.""" self.assertRaises(TypeError, delattr, Color, 'RED') def testEnumName(self): """Test enum name.""" self.assertEquals('messages_test.Color', Color.definition_name()) def testDefinitionName_OverrideModule(self): """Test enum module is overriden by module package name.""" global package try: package = 'my.package' self.assertEquals('my.package.Color', Color.definition_name()) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for enum.""" class Enum1(messages.Enum): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('Enum1', Enum1.definition_name()) self.assertEquals(unicode, type(Enum1.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested Enum names.""" class MyMessage(messages.Message): class NestedEnum(messages.Enum): pass class NestedMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals('messages_test.MyMessage.NestedEnum', MyMessage.NestedEnum.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedEnum', MyMessage.NestedMessage.NestedEnum.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterEnum(messages.Enum): pass self.assertEquals(None, OuterEnum.message_definition()) class OuterMessage(messages.Message): class InnerEnum(messages.Enum): pass self.assertEquals(OuterMessage, OuterMessage.InnerEnum.message_definition()) def testComparison(self): """Test comparing various enums to different types.""" class Enum1(messages.Enum): VAL1 = 1 VAL2 = 2 class Enum2(messages.Enum): VAL1 = 1 self.assertEquals(Enum1.VAL1, Enum1.VAL1) self.assertNotEquals(Enum1.VAL1, Enum1.VAL2) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertNotEquals(Enum1.VAL1, 'VAL1') self.assertNotEquals(Enum1.VAL1, 1) self.assertNotEquals(Enum1.VAL1, 2) self.assertNotEquals(Enum1.VAL1, None) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertTrue(Enum1.VAL1 < Enum1.VAL2) self.assertTrue(Enum1.VAL2 > Enum1.VAL1) self.assertNotEquals(1, Enum2.VAL1) class FieldListTest(test_util.TestCase): def setUp(self): self.integer_field = messages.IntegerField(1, repeated=True) def testConstructor(self): self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, [1, 2, 3])) self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, (1, 2, 3))) self.assertEquals([], messages.FieldList(self.integer_field, [])) def testNone(self): self.assertRaises(TypeError, messages.FieldList, self.integer_field, None) def testDoNotAutoConvertString(self): string_field = messages.StringField(1, repeated=True) self.assertRaises(messages.ValidationError, messages.FieldList, string_field, 'abc') def testConstructorCopies(self): a_list = [1, 3, 6] field_list = messages.FieldList(self.integer_field, a_list) self.assertFalse(a_list is field_list) self.assertFalse(field_list is messages.FieldList(self.integer_field, field_list)) def testNonRepeatedField(self): self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'FieldList may only accept repeated fields', messages.FieldList, messages.IntegerField(1), []) def testConstructor_InvalidValues(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 1 (type <type 'str'>)"), messages.FieldList, self.integer_field, ["1", "2", "3"]) def testConstructor_Scalars(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: 3", messages.FieldList, self.integer_field, 3) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <listiterator object", messages.FieldList, self.integer_field, iter([1, 2, 3])) def testSetSlice(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) field_list[1:3] = [10, 20] self.assertEquals([1, 10, 20, 4, 5], field_list) def testSetSlice_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) def setslice(): field_list[1:3] = ['10', '20'] self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setslice) def testSetItem(self): field_list = messages.FieldList(self.integer_field, [2]) field_list[0] = 10 self.assertEquals([10], field_list) def testSetItem_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def setitem(): field_list[0] = '10' self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setitem) def testAppend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.append(10) self.assertEquals([2, 10], field_list) def testAppend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def append(): field_list.append('10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), append) def testExtend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.extend([10]) self.assertEquals([2, 10], field_list) def testExtend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def extend(): field_list.extend(['10']) self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), extend) def testInsert(self): field_list = messages.FieldList(self.integer_field, [2, 3]) field_list.insert(1, 10) self.assertEquals([2, 10, 3], field_list) def testInsert_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2, 3]) def insert(): field_list.insert(1, '10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), insert) class FieldTest(test_util.TestCase): def ActionOnAllFieldClasses(self, action): """Test all field classes except Message and Enum. Message and Enum require separate tests. Args: action: Callable that takes the field class as a parameter. """ for field_class in (messages.IntegerField, messages.FloatField, messages.BooleanField, messages.BytesField, messages.StringField, ): action(field_class) def testNumberAttribute(self): """Test setting the number attribute.""" def action(field_class): # Check range. self.assertRaises(messages.InvalidNumberError, field_class, 0) self.assertRaises(messages.InvalidNumberError, field_class, -1) self.assertRaises(messages.InvalidNumberError, field_class, messages.MAX_FIELD_NUMBER + 1) # Check reserved. self.assertRaises(messages.InvalidNumberError, field_class, messages.FIRST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, messages.LAST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, '1') # This one should work. field_class(number=1) self.ActionOnAllFieldClasses(action) def testRequiredAndRepeated(self): """Test setting the required and repeated fields.""" def action(field_class): field_class(1, required=True) field_class(1, repeated=True) self.assertRaises(messages.FieldDefinitionError, field_class, 1, required=True, repeated=True) self.ActionOnAllFieldClasses(action) def testInvalidVariant(self): """Test field with invalid variants.""" def action(field_class): self.assertRaises(messages.InvalidVariantError, field_class, 1, variant=messages.Variant.ENUM) self.ActionOnAllFieldClasses(action) def testDefaultVariant(self): """Test that default variant is used when not set.""" def action(field_class): field = field_class(1) self.assertEquals(field_class.DEFAULT_VARIANT, field.variant) self.ActionOnAllFieldClasses(action) def testAlternateVariant(self): """Test that default variant is used when not set.""" field = messages.IntegerField(1, variant=messages.Variant.UINT32) self.assertEquals(messages.Variant.UINT32, field.variant) def testDefaultFields_Single(self): """Test default field is correct type.""" defaults = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): field_class(1, default=defaults[field_class]) self.ActionOnAllFieldClasses(action) # Run defaults test again checking for str/unicode compatiblity. defaults[messages.StringField] = 'abc' self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidSingle(self): """Test default field is correct type.""" def action(field_class): self.assertRaises(messages.InvalidDefaultError, field_class, 1, default=object()) self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidRepeated(self): """Test default field does not accept defaults.""" self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'Repeated fields may not have defaults', messages.StringField, 1, repeated=True, default=[1, 2, 3]) def testDefaultFields_None(self): """Test none is always acceptable.""" def action(field_class): field_class(1, default=None) field_class(1, required=True, default=None) field_class(1, repeated=True, default=None) self.ActionOnAllFieldClasses(action) def testDefaultFields_Enum(self): """Test the default for enum fields.""" class Symbol(messages.Enum): ALPHA = 1 BETA = 2 GAMMA = 3 field = messages.EnumField(Symbol, 1, default=Symbol.ALPHA) self.assertEquals(Symbol.ALPHA, field.default) def testDefaultFields_EnumStringDelayedResolution(self): """Test that enum fields resolve default strings.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default='OPTIONAL') self.assertEquals(descriptor.FieldDescriptor.Label.OPTIONAL, field.default) def testDefaultFields_EnumIntDelayedResolution(self): """Test that enum fields resolve default integers.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=2) self.assertEquals(descriptor.FieldDescriptor.Label.REQUIRED, field.default) def testDefaultFields_EnumOkIfTypeKnown(self): """Test that enum fields accept valid default values when type is known.""" field = messages.EnumField(descriptor.FieldDescriptor.Label, 1, default='REPEATED') self.assertEquals(descriptor.FieldDescriptor.Label.REPEATED, field.default) def testDefaultFields_EnumForceCheckIfTypeKnown(self): """Test that enum fields validate default values if type is known.""" self.assertRaisesWithRegexpMatch(TypeError, 'No such value for NOT_A_LABEL in ' 'Enum Label', messages.EnumField, descriptor.FieldDescriptor.Label, 1, default='NOT_A_LABEL') def testDefaultFields_EnumInvalidDelayedResolution(self): """Test that enum fields raise errors upon delayed resolution error.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=200) self.assertRaisesWithRegexpMatch(TypeError, 'No such value for 200 in Enum Label', getattr, field, 'default') def testValidate_Valid(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate(values[field_class]) # Required. field = field_class(1, required=True) field.validate(values[field_class]) # Repeated. field = field_class(1, repeated=True) field.validate([]) field.validate(()) field.validate([values[field_class]]) field.validate((values[field_class],)) # Right value, but not repeated. self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.ActionOnAllFieldClasses(action) def testValidate_Invalid(self): """Test validation of valid values.""" values = {messages.IntegerField: "10", messages.FloatField: 1, messages.BooleanField: 0, messages.BytesField: 10.20, messages.StringField: 42, } def action(field_class): # Optional. field = field_class(1) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Required. field = field_class(1, required=True) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(messages.ValidationError, field.validate, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate, (values[field_class],)) self.ActionOnAllFieldClasses(action) def testValidate_None(self): """Test that None is valid for non-required fields.""" def action(field_class): # Optional. field = field_class(1) field.validate(None) # Required. field = field_class(1, required=True) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Required field is missing', field.validate, None) # Repeated. field = field_class(1, repeated=True) field.validate(None) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Repeated values may not be None', field.validate, [None]) self.assertRaises(messages.ValidationError, field.validate, (None,)) self.ActionOnAllFieldClasses(action) def testValidateElement(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate_element(values[field_class]) # Required. field = field_class(1, required=True) field.validate_element(values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(message.VAlidationError, field.validate_element, []) self.assertRaises(message.VAlidationError, field.validate_element, ()) field.validate_element(values[field_class]) field.validate_element(values[field_class]) # Right value, but repeated. self.assertRaises(messages.ValidationError, field.validate_element, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate_element, (values[field_class],)) def testReadOnly(self): """Test that objects are all read-only.""" def action(field_class): field = field_class(10) self.assertRaises(AttributeError, setattr, field, 'number', 20) self.assertRaises(AttributeError, setattr, field, 'anything_else', 'whatever') self.ActionOnAllFieldClasses(action) def testMessageField(self): """Test the construction of message fields.""" self.assertRaises(messages.FieldDefinitionError, messages.MessageField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.MessageField, messages.Message, 10) class MyMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) self.assertEquals(MyMessage, field.type) def testMessageField_ForwardReference(self): """Test the construction of forward reference message fields.""" global MyMessage global ForwardMessage try: class MyMessage(messages.Message): self_reference = messages.MessageField('MyMessage', 1) forward = messages.MessageField('ForwardMessage', 2) nested = messages.MessageField('ForwardMessage.NestedMessage', 3) inner = messages.MessageField('Inner', 4) class Inner(messages.Message): sibling = messages.MessageField('Sibling', 1) class Sibling(messages.Message): pass class ForwardMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals(MyMessage, MyMessage.field_by_name('self_reference').type) self.assertEquals(ForwardMessage, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedMessage, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) self.assertEquals(MyMessage.Sibling, MyMessage.Inner.field_by_name('sibling').type) finally: try: del MyMessage del ForwardMessage except: pass def testMessageField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AnEnum try: class AnEnum(messages.Enum): pass class AnotherMessage(messages.Message): a_field = messages.MessageField('AnEnum', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AnEnum def testMessageFieldValidate(self): """Test validation on message field.""" class MyMessage(messages.Message): pass class AnotherMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) field.validate(MyMessage()) self.assertRaises(messages.ValidationError, field.validate, AnotherMessage()) def testIntegerField_AllowLong(self): """Test that the integer field allows for longs.""" messages.IntegerField(10, default=long(10)) def testMessageFieldValidate_Initialized(self): """Test validation on message field.""" class MyMessage(messages.Message): field1 = messages.IntegerField(1, required=True) field = messages.MessageField(MyMessage, 10) # Will validate messages where is_initialized() is False. message = MyMessage() field.validate(message) message.field1 = 20 field.validate(message) def testEnumField(self): """Test the construction of enum fields.""" self.assertRaises(messages.FieldDefinitionError, messages.EnumField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.EnumField, messages.Enum, 10) class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 field = messages.EnumField(Color, 10) self.assertEquals(Color, field.type) class Another(messages.Enum): VALUE = 1 self.assertRaises(messages.InvalidDefaultError, messages.EnumField, Color, 10, default=Another.VALUE) def testEnumField_ForwardReference(self): """Test the construction of forward reference enum fields.""" global MyMessage global ForwardEnum global ForwardMessage try: class MyMessage(messages.Message): forward = messages.EnumField('ForwardEnum', 1) nested = messages.EnumField('ForwardMessage.NestedEnum', 2) inner = messages.EnumField('Inner', 3) class Inner(messages.Enum): pass class ForwardEnum(messages.Enum): pass class ForwardMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals(ForwardEnum, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedEnum, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) finally: try: del MyMessage del ForwardEnum del ForwardMessage except: pass def testEnumField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AMessage try: class AMessage(messages.Message): pass class AnotherMessage(messages.Message): a_field = messages.EnumField('AMessage', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AMessage def testMessageDefinition(self): """Test that message definition is set on fields.""" class MyMessage(messages.Message): my_field = messages.StringField(1) self.assertEquals(MyMessage, MyMessage.field_by_name('my_field').message_definition()) def testNoneAssignment(self): """Test that assigning None does not change comparison.""" class MyMessage(messages.Message): my_field = messages.StringField(1) m1 = MyMessage() m2 = MyMessage() m2.my_field = None self.assertEquals(m1, m2) def testNonAsciiStr(self): """Test validation fails for non-ascii StringField values.""" class Thing(messages.Message): string_field = messages.StringField(2) thing = Thing() self.assertRaisesWithRegexpMatch( messages.ValidationError, 'Field string_field: Encountered non-ASCII string', setattr, thing, 'string_field', test_util.BINARY) class MessageTest(test_util.TestCase): """Tests for message class.""" def CreateMessageClass(self): """Creates a simple message class with 3 fields. Fields are defined in alphabetical order but with conflicting numeric order. """ class ComplexMessage(messages.Message): a3 = messages.IntegerField(3) b1 = messages.StringField(1) c2 = messages.StringField(2) return ComplexMessage def testSameNumbers(self): """Test that cannot assign two fields with same numbers.""" def action(): class BadMessage(messages.Message): f1 = messages.IntegerField(1) f2 = messages.IntegerField(1) self.assertRaises(messages.DuplicateNumberError, action) def testStrictAssignment(self): """Tests that cannot assign to unknown or non-reserved attributes.""" class SimpleMessage(messages.Message): field = messages.IntegerField(1) simple_message = SimpleMessage() self.assertRaises(AttributeError, setattr, simple_message, 'does_not_exist', 10) def testListAssignmentDoesNotCopy(self): class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message = SimpleMessage() original = message.repeated message.repeated = [] self.assertFalse(original is message.repeated) def testValidate_Optional(self): """Tests validation of optional fields.""" class SimpleMessage(messages.Message): non_required = messages.IntegerField(1) simple_message = SimpleMessage() simple_message.check_initialized() simple_message.non_required = 10 simple_message.check_initialized() def testValidate_Required(self): """Tests validation of required fields.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertRaises(messages.ValidationError, simple_message.check_initialized) simple_message.required = 10 simple_message.check_initialized() def testValidate_Repeated(self): """Tests validation of repeated fields.""" class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) simple_message = SimpleMessage() # Check valid values. for valid_value in [], [10], [10, 20], (), (10,), (10, 20): simple_message.repeated = valid_value simple_message.check_initialized() # Check cleared. simple_message.repeated = [] simple_message.check_initialized() # Check invalid values. for invalid_value in 10, ['10', '20'], [None], (None,): self.assertRaises(messages.ValidationError, setattr, simple_message, 'repeated', invalid_value) def testIsInitialized(self): """Tests is_initialized.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertFalse(simple_message.is_initialized()) simple_message.required = 10 self.assertTrue(simple_message.is_initialized()) def testNestedMethodsNotAllowed(self): """Test that method definitions on Message classes are not allowed.""" def action(): class WithMethods(messages.Message): def not_allowed(self): pass self.assertRaises(messages.MessageDefinitionError, action) def testNestedAttributesNotAllowed(self): """Test that attribute assignment on Message classes are not allowed.""" def int_attribute(): class WithMethods(messages.Message): not_allowed = 1 def string_attribute(): class WithMethods(messages.Message): not_allowed = 'not allowed' def enum_attribute(): class WithMethods(messages.Message): not_allowed = Color.RED for action in (int_attribute, string_attribute, enum_attribute): self.assertRaises(messages.MessageDefinitionError, action) def testNameIsSetOnFields(self): """Make sure name is set on fields after Message class init.""" class HasNamedFields(messages.Message): field = messages.StringField(1) self.assertEquals('field', HasNamedFields.field_by_number(1).name) def testSubclassingMessageDisallowed(self): """Not permitted to create sub-classes of message classes.""" class SuperClass(messages.Message): pass def action(): class SubClass(SuperClass): pass self.assertRaises(messages.MessageDefinitionError, action) def testAllFields(self): """Test all_fields method.""" ComplexMessage = self.CreateMessageClass() fields = list(ComplexMessage.all_fields()) # Order does not matter, so sort now. fields = sorted(fields, lambda f1, f2: cmp(f1.name, f2.name)) self.assertEquals(3, len(fields)) self.assertEquals('a3', fields[0].name) self.assertEquals('b1', fields[1].name) self.assertEquals('c2', fields[2].name) def testFieldByName(self): """Test getting field by name.""" ComplexMessage = self.CreateMessageClass() self.assertEquals(3, ComplexMessage.field_by_name('a3').number) self.assertEquals(1, ComplexMessage.field_by_name('b1').number) self.assertEquals(2, ComplexMessage.field_by_name('c2').number) self.assertRaises(KeyError, ComplexMessage.field_by_name, 'unknown') def testFieldByNumber(self): """Test getting field by number.""" ComplexMessage = self.CreateMessageClass() self.assertEquals('a3', ComplexMessage.field_by_number(3).name) self.assertEquals('b1', ComplexMessage.field_by_number(1).name) self.assertEquals('c2', ComplexMessage.field_by_number(2).name) self.assertRaises(KeyError, ComplexMessage.field_by_number, 4) def testGetAssignedValue(self): """Test getting the assigned value of a field.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertEquals(None, message.get_assigned_value('a_value')) message.a_value = u'a string' self.assertEquals(u'a string', message.get_assigned_value('a_value')) message.a_value = u'a default' self.assertEquals(u'a default', message.get_assigned_value('a_value')) self.assertRaisesWithRegexpMatch( AttributeError, 'Message SomeMessage has no field no_such_field', message.get_assigned_value, 'no_such_field') def testReset(self): """Test resetting a field value.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertRaises(AttributeError, message.reset, 'unknown') self.assertEquals(u'a default', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) message.a_value = u'a new value' self.assertEquals(u'a new value', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) def testAllowNestedEnums(self): """Test allowing nested enums in a message definition.""" class Trade(messages.Message): class Duration(messages.Enum): GTC = 1 DAY = 2 class Currency(messages.Enum): USD = 1 GBP = 2 INR = 3 # Sorted by name order seems to be the only feasible option. self.assertEquals(['Currency', 'Duration'], Trade.__enums__) # Message definition will now be set on Enumerated objects. self.assertEquals(Trade, Trade.Duration.message_definition()) def testAllowNestedMessages(self): """Test allowing nested messages in a message definition.""" class Trade(messages.Message): class Lot(messages.Message): pass class Agent(messages.Message): pass # Sorted by name order seems to be the only feasible option. self.assertEquals(['Agent', 'Lot'], Trade.__messages__) self.assertEquals(Trade, Trade.Agent.message_definition()) self.assertEquals(Trade, Trade.Lot.message_definition()) # But not Message itself. def action(): class Trade(messages.Message): NiceTry = messages.Message self.assertRaises(messages.MessageDefinitionError, action) def testDisallowClassAssignments(self): """Test setting class attributes may not happen.""" class MyMessage(messages.Message): pass self.assertRaises(AttributeError, setattr, MyMessage, 'x', 'do not assign') def testEquality(self): """Test message class equality.""" # Comparison against enums must work. class MyEnum(messages.Enum): val1 = 1 val2 = 2 # Comparisons against nested messages must work. class AnotherMessage(messages.Message): string = messages.StringField(1) class MyMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.EnumField(MyEnum, 2) field3 = messages.MessageField(AnotherMessage, 3) message1 = MyMessage() self.assertNotEquals('hi', message1) self.assertNotEquals(AnotherMessage(), message1) self.assertEquals(message1, message1) message2 = MyMessage() self.assertEquals(message1, message2) message1.field1 = 10 self.assertNotEquals(message1, message2) message2.field1 = 20 self.assertNotEquals(message1, message2) message2.field1 = 10 self.assertEquals(message1, message2) message1.field2 = MyEnum.val1 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val2 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val1 self.assertEquals(message1, message2) message1.field3 = AnotherMessage() message1.field3.string = 'value1' self.assertNotEquals(message1, message2) message2.field3 = AnotherMessage() message2.field3.string = 'value2' self.assertNotEquals(message1, message2) message2.field3.string = 'value1' self.assertEquals(message1, message2) def testRepr(self): """Test represtation of Message object.""" class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) unassigned = messages.StringField(3) unassigned_with_default = messages.StringField(4, default=u'a default') my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' self.assertEquals("<MyMessage\n integer_value: 42\n" " string_value: u'A string'>", repr(my_message)) def testValidation(self): """Test validation of message values.""" # Test optional. class SubMessage(messages.Message): pass class Message(messages.Message): val = messages.MessageField(SubMessage, 1) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test required. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, required=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test repeated. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, repeated=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <SubMessage>", setattr, message, 'val', SubMessage()) message.val = [SubMessage()] message_field.validate(message) def testDefinitionName(self): """Test message name.""" class MyMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) def testDefinitionName_OverrideModule(self): """Test message module is overriden by module package name.""" class MyMessage(messages.Message): pass global package package = 'my.package' try: self.assertEquals('my.package.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for message.""" class MyMessage(messages.Message): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested message names.""" class MyMessage(messages.Message): class NestedMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage.NestedMessage', MyMessage.NestedMessage.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedMessage', MyMessage.NestedMessage.NestedMessage.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterMessage(messages.Message): class InnerMessage(messages.Message): pass self.assertEquals(None, OuterMessage.message_definition()) self.assertEquals(OuterMessage, OuterMessage.InnerMessage.message_definition()) def testConstructorKwargs(self): """Test kwargs via constructor.""" class SomeMessage(messages.Message): name = messages.StringField(1) number = messages.IntegerField(2) expected = SomeMessage() expected.name = 'my name' expected.number = 200 self.assertEquals(expected, SomeMessage(name='my name', number=200)) def testConstructorNotAField(self): """Test kwargs via constructor with wrong names.""" class SomeMessage(messages.Message): pass self.assertRaisesWithRegexpMatch( AttributeError, 'May not assign arbitrary value does_not_exist to message SomeMessage', SomeMessage, does_not_exist=10) def testGetUnsetRepeatedValue(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) instance = SomeMessage() self.assertEquals([], instance.repeated) self.assertTrue(isinstance(instance.repeated, messages.FieldList)) def testCompareAutoInitializedRepeatedFields(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message1 = SomeMessage(repeated=[]) message2 = SomeMessage() self.assertEquals(message1, message2) class FindDefinitionTest(test_util.TestCase): """Test finding definitions relative to various definitions and modules.""" def setUp(self): """Set up module-space. Starts off empty.""" self.modules = {} def DefineModule(self, name): """Define a module and its parents in module space. Modules that are already defined in self.modules are not re-created. Args: name: Fully qualified name of modules to create. Returns: Deepest nested module. For example: DefineModule('a.b.c') # Returns c. """ name_path = name.split('.') full_path = [] for node in name_path: full_path.append(node) full_name = '.'.join(full_path) self.modules.setdefault(full_name, new.module(full_name)) return self.modules[name] def DefineMessage(self, module, name, children={}, add_to_module=True): """Define a new Message class in the context of a module. Used for easily describing complex Message hierarchy. Message is defined including all child definitions. Args: module: Fully qualified name of module to place Message class in. name: Name of Message to define within module. children: Define any level of nesting of children definitions. To define a message, map the name to another dictionary. The dictionary can itself contain additional definitions, and so on. To map to an Enum, define the Enum class separately and map it by name. add_to_module: If True, new Message class is added to module. If False, new Message is not added. """ # Make sure module exists. module_instance = self.DefineModule(module) # Recursively define all child messages. for attribute, value in children.items(): if isinstance(value, dict): children[attribute] = self.DefineMessage( module, attribute, value, False) # Override default __module__ variable. children['__module__'] = module # Instantiate and possibly add to module. message_class = new.classobj(name, (messages.Message,), dict(children)) if add_to_module: setattr(module_instance, name, message_class) return message_class def Importer(self, module, globals='', locals='', fromlist=None): """Importer function. Acts like __import__. Only loads modules from self.modules. Does not try to load real modules defined elsewhere. Does not try to handle relative imports. Args: module: Fully qualified name of module to load from self.modules. """ if fromlist is None: module = module.split('.')[0] try: return self.modules[module] except KeyError: raise ImportError() def testNoSuchModule(self): """Test searching for definitions that do no exist.""" self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'does.not.exist', importer=self.Importer) def testRefersToModule(self): """Test that referring to a module does not return that module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module', importer=self.Importer) def testNoDefinition(self): """Test not finding a definition in an existing module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.MyMessage', importer=self.Importer) def testNotADefinition(self): """Test trying to fetch something that is not a definition.""" module = self.DefineModule('i.am.a.module') setattr(module, 'A', 'a string') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.A', importer=self.Importer) def testGlobalFind(self): """Test finding definitions from fully qualified module names.""" A = self.DefineMessage('a.b.c', 'A', {}) self.assertEquals(A, messages.find_definition('a.b.c.A', importer=self.Importer)) B = self.DefineMessage('a.b.c', 'B', {'C':{}}) self.assertEquals(B.C, messages.find_definition('a.b.c.B.C', importer=self.Importer)) def testRelativeToModule(self): """Test finding definitions relative to modules.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.b') c = self.DefineModule('a.b.c') # Define messages. A = self.DefineMessage('a', 'A') B = self.DefineMessage('a.b', 'B') C = self.DefineMessage('a.b.c', 'C') D = self.DefineMessage('a.b.d', 'D') # Find A, B, C and D relative to a. self.assertEquals(A, messages.find_definition( 'A', a, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.B', a, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.c.C', a, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.d.D', a, importer=self.Importer)) # Find A, B, C and D relative to b. self.assertEquals(A, messages.find_definition( 'A', b, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', b, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'c.C', b, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', b, importer=self.Importer)) # Find A, B, C and D relative to c. Module d is the same case as c. self.assertEquals(A, messages.find_definition( 'A', c, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', c, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', c, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', c, importer=self.Importer)) def testRelativeToMessages(self): """Test finding definitions relative to Message definitions.""" A = self.DefineMessage('a.b', 'A', {'B': {'C': {}, 'D': {}}}) B = A.B C = A.B.C D = A.B.D # Find relative to A. self.assertEquals(A, messages.find_definition( 'A', A, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', A, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'B.C', A, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'B.D', A, importer=self.Importer)) # Find relative to B. self.assertEquals(A, messages.find_definition( 'A', B, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', B, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', B, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', B, importer=self.Importer)) # Find relative to C. self.assertEquals(A, messages.find_definition( 'A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', C, importer=self.Importer)) # Find relative to C searching from c. self.assertEquals(A, messages.find_definition( 'b.A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.A.B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.A.B.C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.A.B.D', C, importer=self.Importer)) def testAbsoluteReference(self): """Test finding absolute definition names.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.a') # Define messages. aA = self.DefineMessage('a', 'A') aaA = self.DefineMessage('a.a', 'A') # Always find a.A. self.assertEquals(aA, messages.find_definition('.a.A', None, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', a, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aA, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aaA, importer=self.Importer)) def testFindEnum(self): """Test that Enums are found.""" class Color(messages.Enum): pass A = self.DefineMessage('a', 'A', {'Color': Color}) self.assertEquals( Color, messages.find_definition('Color', A, importer=self.Importer)) def testFalseScope(self): """Test that Message definitions nested in strange objects are hidden.""" global X class X(object): class A(messages.Message): pass self.assertRaises(TypeError, messages.find_definition, 'A', X) self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'X.A', sys.modules[__name__]) def testSearchAttributeFirst(self): """Make sure not faked out by module, but continues searching.""" A = self.DefineMessage('a', 'A') module_A = self.DefineModule('a.A') self.assertEquals(A, messages.find_definition( 'a.A', None, importer=self.Importer)) class FindDefinitionUnicodeTests(test_util.TestCase): def testUnicodeString(self): """Test using unicode names.""" self.assertEquals('ServiceMapping', messages.find_definition( u'protorpc.registry.ServiceMapping', None).__name__) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate_proto_test.""" import os import shutil import cStringIO import sys import tempfile import unittest from protorpc import descriptor from protorpc import generate_proto from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate_proto class FormatProtoFileTest(test_util.TestCase): def setUp(self): self.file_descriptor = descriptor.FileDescriptor() self.output = cStringIO.StringIO() @property def result(self): return self.output.getvalue() def MakeMessage(self, name='MyMessage', fields=[]): message = descriptor.MessageDescriptor() message.name = name message.fields = fields messages_list = getattr(self.file_descriptor, 'fields', []) messages_list.append(message) self.file_descriptor.message_types = messages_list def testBlankPackage(self): self.file_descriptor.package = None generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('', self.result) def testEmptyPackage(self): self.file_descriptor.package = 'my_package' generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('package my_package;\n', self.result) def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '10' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1 [default=10];\n' '}\n', self.result) def testRepeatedFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '[10, 20]' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' repeated int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefaultString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = 'hello' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default='hello'];\n" '}\n', self.result) def testSingleFieldWithDefaultEmptyString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = '' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default=''];\n" '}\n', self.result) def testSingleFieldWithDefaultMessage(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'MyNestedMessage' field.default_value = 'not valid' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional MyNestedMessage message_field = 1;\n" '}\n', self.result) def testSingleFieldWithDefaultEnum(self): field = descriptor.FieldDescriptor() field.name = 'enum_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.MyEnum' field.default_value = '17' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional my_package.MyEnum enum_field = 1 " "[default=17];\n" '}\n', self.result) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protojson.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import base64 import sys import unittest from protorpc import messages from protorpc import protojson from protorpc import test_util from django.utils import simplejson class MyMessage(messages.Message): """Test message containing various types.""" class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 class Nested(messages.Message): nested_value = messages.StringField(1) a_string = messages.StringField(2) an_integer = messages.IntegerField(3) a_float = messages.FloatField(4) a_boolean = messages.BooleanField(5) an_enum = messages.EnumField(Color, 6) a_nested = messages.MessageField(Nested, 7) a_repeated = messages.IntegerField(8, repeated=True) class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protojson # TODO(rafek): Convert this test to the compliance test in test_util. class ProtojsonTest(test_util.TestCase, test_util.ProtoConformanceTestBase): """Test JSON encoding and decoding.""" PROTOLIB = protojson def CompareEncoded(self, expected_encoded, actual_encoded): """JSON encoding will be laundered to remove string differences.""" self.assertEquals(simplejson.loads(expected_encoded), simplejson.loads(actual_encoded)) encoded_empty_message = '{}' encoded_partial = """{ "double_value": 1.23, "int64_value": -100000000000, "int32_value": 1020, "string_value": "a string", "enum_value": "VAL2" } """ encoded_full = """{ "double_value": 1.23, "float_value": -2.5, "int64_value": -100000000000, "uint64_value": 102020202020, "int32_value": 1020, "bool_value": true, "string_value": "a string\u044f", "bytes_value": "YSBieXRlc//+", "enum_value": "VAL2" } """ encoded_repeated = """{ "double_value": [1.23, 2.3], "float_value": [-2.5, 0.5], "int64_value": [-100000000000, 20], "uint64_value": [102020202020, 10], "int32_value": [1020, 718], "bool_value": [true, false], "string_value": ["a string\u044f", "another string"], "bytes_value": ["YSBieXRlc//+", "YW5vdGhlciBieXRlcw=="], "enum_value": ["VAL2", "VAL1"] } """ encoded_nested = """{ "nested": { "a_value": "a string" } } """ encoded_repeated_nested = """{ "repeated_nested": [{"a_value": "a string"}, {"a_value": "another string"}] } """ unexpected_tag_message = '{"unknown": "value"}' encoded_default_assigned = '{"a_value": "a default"}' encoded_nested_empty = '{"nested": {}}' encoded_repeated_nested_empty = '{"repeated_nested": [{}, {}]}' encoded_extend_message = '{"int64_value": [400, 50, 6000]}' encoded_string_types = '{"string_value": "Latin"}' def testConvertIntegerToFloat(self): """Test that integers passed in to float fields are converted. This is necessary because JSON outputs integers for numbers with 0 decimals. """ message = protojson.decode_message(MyMessage, '{"a_float": 10}') self.assertTrue(isinstance(message.a_float, float)) self.assertEquals(10.0, message.a_float) def testWrongTypeAssignment(self): """Test when wrong type is assigned to a field.""" self.assertRaises(messages.ValidationError, protojson.decode_message, MyMessage, '{"a_string": 10}') def testNumericEnumeration(self): """Test that numbers work for enum values.""" message = protojson.decode_message(MyMessage, '{"an_enum": 2}') expected_message = MyMessage() expected_message.an_enum = MyMessage.Color.GREEN self.assertEquals(expected_message, message) def testNullValues(self): """Test that null values overwrite existing values.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, ('{"an_integer": null,' ' "a_nested": null' '}'))) def testEmptyList(self): """Test that empty lists are ignored.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, '{"a_repeated": []}')) def testNotJSON(self): """Test error when string is not valid JSON.""" self.assertRaises(ValueError, protojson.decode_message, MyMessage, '{this is not json}') def testDoNotEncodeStrangeObjects(self): """Test trying to encode a strange object. The main purpose of this test is to complete coverage. It ensures that the default behavior of the JSON encoder is preserved when someone tries to serialized an unexpected type. """ class BogusObject(object): def check_initialized(self): pass self.assertRaises(TypeError, protojson.encode_message, BogusObject()) def testMergeEmptyString(self): """Test merging the empty or space only string.""" message = protojson.decode_message(test_util.OptionalMessage, '') self.assertEquals(test_util.OptionalMessage(), message) message = protojson.decode_message(test_util.OptionalMessage, ' ') self.assertEquals(test_util.OptionalMessage(), message) class InvalidJsonModule(object): pass class ValidJsonModule(object): class JSONEncoder(object): pass class TestJsonDependencyLoading(test_util.TestCase): """Test loading various implementations of json.""" def setUp(self): """Save original import function.""" self.django_simplejson = sys.modules.pop('django.utils.simplejson', None) self.simplejson = sys.modules.pop('simplejson', None) self.json = sys.modules.pop('json', None) self.original_import = __builtins__.__import__ def block_all_jsons(name, *args, **kwargs): if 'json' in name: if name in sys.modules: module = sys.modules[name] module.name = name return module raise ImportError('Unable to find %s' % name) else: return self.original_import(name, *args, **kwargs) __builtins__.__import__ = block_all_jsons def tearDown(self): """Restore original import functions and any loaded modules.""" __builtins__.__import__ = self.original_import def reset_module(name, module): if module: sys.modules[name] = module else: sys.modules.pop(name, None) reset_module('django.utils.simplejson', self.django_simplejson) reset_module('simplejson', self.simplejson) reset_module('json', self.json) reload(protojson) def testLoadProtojsonWithValidJsonModule(self): """Test loading protojson module with a valid json dependency.""" sys.modules['json'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('json', protojson.json.name) def testLoadProtojsonWithSimplejsonModule(self): """Test loading protojson module with simplejson dependency.""" sys.modules['simplejson'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModule(self): """Loading protojson module with an invalid json defaults to simplejson.""" sys.modules['json'] = InvalidJsonModule sys.modules['simplejson'] = ValidJsonModule # Ignore bad module and default back to simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModuleAndNoSimplejson(self): """Loading protojson module with invalid json and no simplejson.""" sys.modules['json'] = InvalidJsonModule # Bad module without simplejson back raises errors. self.assertRaisesWithRegexpMatch( ImportError, 'json library "json" is not compatible with ProtoRPC', reload, protojson) def testLoadProtojsonWithNoJsonModules(self): """Loading protojson module with invalid json and no simplejson.""" # No json modules raise the first exception. self.assertRaisesWithRegexpMatch( ImportError, 'Unable to find json', reload, protojson) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import sys import unittest from protorpc import generate from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate class IndentWriterTest(test_util.TestCase): def setUp(self): self.out = cStringIO.StringIO() self.indent_writer = generate.IndentWriter(self.out) def testWriteLine(self): self.indent_writer.write_line('This is a line') self.indent_writer.write_line('This is another line') self.assertEquals('This is a line\n' 'This is another line\n', self.out.getvalue()) def testLeftShift(self): self.run_count = 0 def mock_write_line(line): self.run_count += 1 self.assertEquals('same as calling write_line', line) self.indent_writer.write_line = mock_write_line self.indent_writer << 'same as calling write_line' self.assertEquals(1, self.run_count) def testIndentation(self): self.indent_writer << 'indent 0' self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.indent_writer.end_indent() self.indent_writer << 'end 2' self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testAltIndentation(self): self.indent_writer = generate.IndentWriter(self.out, indent_space=3) self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 2' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertEquals(0, self.indent_writer.indent_level) self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals(0, self.indent_writer.indent_level) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testIndent(self): self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) def indent1(): self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) def indent2(): self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) test_util.do_with(self.indent_writer.indent(), indent2) self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer << 'end 2' test_util.do_with(self.indent_writer.indent(), indent1) self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer << 'end 1' self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """JSON support for message types. Public classes: MessageJSONEncoder: JSON encoder for message objects. Public functions: encode_message: Encodes a message in to a JSON string. decode_message: Merge from a JSON string in to a message. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import base64 import logging from protorpc import messages __all__ = [ 'CONTENT_TYPE', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/json' def _load_json_module(): """Try to load a valid json module. There are more than one json modules that might be installed. They are mostly compatible with one another but some versions may be different. This function attempts to load various json modules in a preferred order. It does a basic check to guess if a loaded version of json is compatible. Returns: Comptable json module. Raises: ImportError if there are no json modules or the loaded json module is not compatible with ProtoRPC. """ first_import_error = None for module_name in ['json', 'simplejson', 'django.utils.simplejson']: try: module = __import__(module_name, {}, {}, 'json') if not hasattr(module, 'JSONEncoder'): message = ('json library "%s" is not compatible with ProtoRPC' % module_name) logging.warning(message) raise ImportError(message) else: return module except ImportError, err: if not first_import_error: first_import_error = err logging.error('Must use valid json library (Python 2.6 json, simplejson or ' 'django.utils.simplejson)') raise first_import_error json = _load_json_module() class _MessageJSONEncoder(json.JSONEncoder): """Message JSON encoder class. Extension of JSONEncoder that can build JSON from a message object. """ def default(self, value): """Return dictionary instance from a message object. Args: value: Value to get dictionary for. If not encodable, will call superclasses default method. """ if isinstance(value, messages.Enum): return str(value) if isinstance(value, messages.Message): result = {} for field in value.all_fields(): item = value.get_assigned_value(field.name) if item not in (None, [], ()): if isinstance(field, messages.BytesField): if field.repeated: item = [base64.b64encode(i) for i in item] else: item = base64.b64encode(item) result[field.name] = item return result else: return super(_MessageJSONEncoder, self).default(value) def encode_message(message): """Encode Message instance to JSON string. Args: Message instance to encode in to JSON string. Returns: String encoding of Message instance in protocol JSON format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() return json.dumps(message, cls=_MessageJSONEncoder) def decode_message(message_type, encoded_message): """Merge JSON structure to Message instance. Args: message_type: Message to decode data to. encoded_message: JSON encoded version of message. Returns: Decoded instance of message_type. Raises: ValueError: If encoded_message is not valid JSON. messages.ValidationError if merged message is not initialized. """ if not encoded_message.strip(): return message_type() dictionary = json.loads(encoded_message) def decode_dictionary(message_type, dictionary): """Merge dictionary in to message. Args: message: Message to merge dictionary in to. dictionary: Dictionary to extract information from. Dictionary is as parsed from JSON. Nested objects will also be dictionaries. """ message = message_type() for key, value in dictionary.iteritems(): if value is None: message.reset(key) continue try: field = message.field_by_name(key) except KeyError: # TODO(rafek): Support saving unknown values. continue # Normalize values in to a list. if isinstance(value, list): if not value: continue else: value = [value] valid_value = [] for item in value: if isinstance(field, messages.EnumField): item = field.type(item) elif isinstance(field, messages.BytesField): item = base64.b64decode(item) elif isinstance(field, messages.MessageField): item = decode_dictionary(field.type, item) elif (isinstance(field, messages.FloatField) and isinstance(item, (int, long))): item = float(item) valid_value.append(item) if field.repeated: existing_value = getattr(message, field.name) setattr(message, field.name, valid_value) else: setattr(message, field.name, valid_value[-1]) return message message = decode_dictionary(message_type, dictionary) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Main module for ProtoRPC package.""" __author__ = 'rafek@google.com (Rafe Kaplan)'
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.descriptor.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import unittest from protorpc import descriptor from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util RUSSIA = u'\u0420\u043e\u0441\u0441\u0438\u044f' class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = descriptor class DescribeEnumValueTest(test_util.TestCase): def testDescribe(self): class MyEnum(messages.Enum): MY_NAME = 10 expected = descriptor.EnumValueDescriptor() expected.name = 'MY_NAME' expected.number = 10 described = descriptor.describe_enum_value(MyEnum.MY_NAME) described.check_initialized() self.assertEquals(expected, described) class DescribeEnumTest(test_util.TestCase): def testEmptyEnum(self): class EmptyEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'EmptyEnum' described = descriptor.describe_enum(EmptyEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MyScope(messages.Message): class NestedEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'NestedEnum' described = descriptor.describe_enum(MyScope.NestedEnum) described.check_initialized() self.assertEquals(expected, described) def testEnumWithItems(self): class EnumWithItems(messages.Enum): A = 3 B = 1 C = 2 expected = descriptor.EnumDescriptor() expected.name = 'EnumWithItems' a = descriptor.EnumValueDescriptor() a.name = 'A' a.number = 3 b = descriptor.EnumValueDescriptor() b.name = 'B' b.number = 1 c = descriptor.EnumValueDescriptor() c.name = 'C' c.number = 2 expected.values = [b, c, a] described = descriptor.describe_enum(EnumWithItems) described.check_initialized() self.assertEquals(expected, described) class DescribeFieldTest(test_util.TestCase): def testLabel(self): for repeated, required, expected_label in ( (True, False, descriptor.FieldDescriptor.Label.REPEATED), (False, True, descriptor.FieldDescriptor.Label.REQUIRED), (False, False, descriptor.FieldDescriptor.Label.OPTIONAL)): field = messages.IntegerField(10, required=required, repeated=repeated) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = expected_label expected.variant = descriptor.FieldDescriptor.Variant.INT64 described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault(self): for field_class, default, expected_default in ( (messages.IntegerField, 200, '200'), (messages.FloatField, 1.5, '1.5'), (messages.FloatField, 1e6, '1000000.0'), (messages.BooleanField, True, 'true'), (messages.BooleanField, False, 'false'), (messages.BytesField, 'ab\xF1', 'ab\\xf1'), (messages.StringField, RUSSIA, RUSSIA), ): field = field_class(10, default=default) field.name = u'a_field' expected = descriptor.FieldDescriptor() expected.name = u'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = field_class.DEFAULT_VARIANT expected.default_value = expected_default described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault_EnumField(self): class MyEnum(messages.Enum): VAL = 1 field = messages.EnumField(MyEnum, 10, default=MyEnum.VAL) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.EnumField.DEFAULT_VARIANT expected.type_name = 'descriptor_test.MyEnum' expected.default_value = '1' described = descriptor.describe_field(field) self.assertEquals(expected, described) def testMessageField(self): field = messages.MessageField(descriptor.FieldDescriptor, 10) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.MessageField.DEFAULT_VARIANT expected.type_name = ('protorpc.descriptor.FieldDescriptor') described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) class DescribeMessageTest(test_util.TestCase): def testEmptyDefinition(self): class MyMessage(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MyMessage' described = descriptor.describe_message(MyMessage) described.check_initialized() self.assertEquals(expected, described) def testDefinitionWithFields(self): class MessageWithFields(messages.Message): field1 = messages.IntegerField(10) field2 = messages.StringField(30) field3 = messages.IntegerField(20) expected = descriptor.MessageDescriptor() expected.name = 'MessageWithFields' expected.fields = [ descriptor.describe_field(MessageWithFields.field_by_name('field1')), descriptor.describe_field(MessageWithFields.field_by_name('field3')), descriptor.describe_field(MessageWithFields.field_by_name('field2')), ] described = descriptor.describe_message(MessageWithFields) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MessageWithEnum(messages.Message): class Mood(messages.Enum): GOOD = 1 BAD = 2 UGLY = 3 class Music(messages.Enum): CLASSIC = 1 JAZZ = 2 BLUES = 3 expected = descriptor.MessageDescriptor() expected.name = 'MessageWithEnum' expected.enum_types = [descriptor.describe_enum(MessageWithEnum.Mood), descriptor.describe_enum(MessageWithEnum.Music)] described = descriptor.describe_message(MessageWithEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedMessage(self): class MessageWithMessage(messages.Message): class Nesty(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MessageWithMessage' expected.message_types = [ descriptor.describe_message(MessageWithMessage.Nesty)] described = descriptor.describe_message(MessageWithMessage) described.check_initialized() self.assertEquals(expected, described) class DescribeMethodTest(test_util.TestCase): """Test describing remote methods.""" def testDescribe(self): class Request(messages.Message): pass class Response(messages.Message): pass @remote.method(Request, Response) def remote_method(request): pass expected = descriptor.MethodDescriptor() expected.name = 'remote_method' expected.request_type = 'descriptor_test.Request' expected.response_type = 'descriptor_test.Response' described = descriptor.describe_method(remote_method) described.check_initialized() self.assertEquals(expected, described) class DescribeServiceTest(test_util.TestCase): """Test describing service classes.""" def testDescribe(self): class Request1(messages.Message): pass class Response1(messages.Message): pass class Request2(messages.Message): pass class Response2(messages.Message): pass class MyService(remote.Service): @remote.method(Request1, Response1) def method1(self, request): pass @remote.method(Request2, Response2) def method2(self, request): pass expected = descriptor.ServiceDescriptor() expected.name = 'MyService' expected.methods = [] expected.methods.append(descriptor.describe_method(MyService.method1)) expected.methods.append(descriptor.describe_method(MyService.method2)) described = descriptor.describe_service(MyService) described.check_initialized() self.assertEquals(expected, described) class DescribeFileTest(test_util.TestCase): """Test describing modules.""" def LoadModule(self, module_name, source): result = {'__name__': module_name, 'messages': messages, 'remote': remote, } exec source in result module = new.module(module_name) for name, value in result.iteritems(): setattr(module, name, value) return module def testEmptyModule(self): """Test describing an empty file.""" module = new.module('my.package.name') expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testNoPackageName(self): """Test describing a module with no module name.""" module = new.module('') expected = descriptor.FileDescriptor() described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testPackageName(self): """Test using the 'package' module attribute.""" module = new.module('my.module.name') module.package = 'my.package.name' expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testMessages(self): """Test that messages are described.""" module = self.LoadModule('my.package', 'class Message1(messages.Message): pass\n' 'class Message2(messages.Message): pass\n') message1 = descriptor.MessageDescriptor() message1.name = 'Message1' message2 = descriptor.MessageDescriptor() message2.name = 'Message2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.message_types = [message1, message2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testEnums(self): """Test that enums are described.""" module = self.LoadModule('my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testServices(self): """Test that services are described.""" module = self.LoadModule('my.package', 'class Service1(remote.Service): pass\n' 'class Service2(remote.Service): pass\n') service1 = descriptor.ServiceDescriptor() service1.name = 'Service1' service2 = descriptor.ServiceDescriptor() service2.name = 'Service2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.service_types = [service1, service2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) class DescribeFileSetTest(test_util.TestCase): """Test describing multiple modules.""" def testNoModules(self): """Test what happens when no modules provided.""" described = descriptor.describe_file_set([]) described.check_initialized() # The described FileSet.files will be None. self.assertEquals(descriptor.FileSet(), described) def testWithModules(self): """Test what happens when no modules provided.""" modules = [new.module('package1'), new.module('package1')] file1 = descriptor.FileDescriptor() file1.package = 'package1' file2 = descriptor.FileDescriptor() file2.package = 'package2' expected = descriptor.FileSet() expected.files = [file1, file1] described = descriptor.describe_file_set(modules) described.check_initialized() self.assertEquals(expected, described) class DescribeTest(test_util.TestCase): def testModule(self): self.assertEquals(descriptor.describe_file(test_util), descriptor.describe(test_util)) def testMethod(self): class Param(messages.Message): pass class Service(remote.Service): @remote.method(Param, Param) def fn(self): return Param() self.assertEquals(descriptor.describe_method(Service.fn), descriptor.describe(Service.fn)) def testField(self): self.assertEquals( descriptor.describe_field(test_util.NestedMessage.a_value), descriptor.describe(test_util.NestedMessage.a_value)) def testEnumValue(self): self.assertEquals( descriptor.describe_enum_value( test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.describe(test_util.OptionalMessage.SimpleEnum.VAL1)) def testMessage(self): self.assertEquals(descriptor.describe_message(test_util.NestedMessage), descriptor.describe(test_util.NestedMessage)) def testEnum(self): self.assertEquals( descriptor.describe_enum(test_util.OptionalMessage.SimpleEnum), descriptor.describe(test_util.OptionalMessage.SimpleEnum)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testUndescribable(self): class NonService(object): def fn(self): pass for value in (NonService, NonService.fn, 1, 'string', 1.2, None): self.assertEquals(None, descriptor.describe(value)) class ModuleFinderTest(test_util.TestCase): def testFindModule(self): self.assertEquals(descriptor.describe_file(registry), descriptor.import_descriptor_loader('protorpc.registry')) def testFindMessage(self): self.assertEquals( descriptor.describe_message(descriptor.FileSet), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet')) def testFindField(self): self.assertEquals( descriptor.describe_field(descriptor.FileSet.files), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet.files')) def testFindEnumValue(self): self.assertEquals( descriptor.describe_enum_value(test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.import_descriptor_loader( 'protorpc.test_util.OptionalMessage.SimpleEnum.VAL1')) def testFindMethod(self): self.assertEquals( descriptor.describe_method(registry.RegistryService.services), descriptor.import_descriptor_loader( 'protorpc.registry.RegistryService.services')) def testFindService(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('protorpc.registry.RegistryService')) def testFindWithAbsoluteName(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('.protorpc.registry.RegistryService')) def testFindWrongThings(self): for name in ('a', 'protorpc.registry.RegistryService.__init__', '', ): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, descriptor.import_descriptor_loader, name) class DescriptorLibraryTest(test_util.TestCase): def setUp(self): self.packageless = descriptor.MessageDescriptor() self.packageless.name = 'Packageless' self.library = descriptor.DescriptorLibrary( descriptors={ 'not.real.Packageless': self.packageless, 'Packageless': self.packageless, }) def testLookupPackage(self): self.assertEquals('csv', self.library.lookup_package('csv')) self.assertEquals('protorpc', self.library.lookup_package('protorpc')) self.assertEquals('protorpc.registry', self.library.lookup_package('protorpc.registry')) self.assertEquals('protorpc.registry', self.library.lookup_package('.protorpc.registry')) self.assertEquals( 'protorpc.registry', self.library.lookup_package('protorpc.registry.RegistryService')) self.assertEquals( 'protorpc.registry', self.library.lookup_package( 'protorpc.registry.RegistryService.services')) def testLookupNonPackages(self): for name in ('', 'a', 'protorpc.descriptor.DescriptorLibrary'): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, self.library.lookup_package, name) def testNoPackage(self): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for not.real', self.library.lookup_package, 'not.real.Packageless') self.assertEquals(None, self.library.lookup_package('Packageless')) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stand-alone implementation of in memory protocol messages. Public Classes: Enum: Represents an enumerated type. Variant: Hint for wire format to determine how to serialize. Message: Base class for user defined messages. IntegerField: Field for integer values. FloatField: Field for float values. BooleanField: Field for boolean values. BytesField: Field for binary string values. StringField: Field for UTF-8 string values. MessageField: Field for other message type values. EnumField: Field for enumerated type values. Public Exceptions (indentation indications class hierarchy): EnumDefinitionError: Raised when enumeration is incorrectly defined. FieldDefinitionError: Raised when field is incorrectly defined. InvalidVariantError: Raised when variant is not compatible with field type. InvalidDefaultError: Raised when default is not compatiable with field. InvalidNumberError: Raised when field number is out of range or reserved. MessageDefinitionError: Raised when message is incorrectly defined. DuplicateNumberError: Raised when field has duplicate number with another. ValidationError: Raised when a message or field is not valid. DefinitionNotFoundError: Raised when definition not found. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import inspect import os import sys import traceback import types import weakref from protorpc import util __all__ = ['MAX_ENUM_VALUE', 'MAX_FIELD_NUMBER', 'FIRST_RESERVED_FIELD_NUMBER', 'LAST_RESERVED_FIELD_NUMBER', 'Enum', 'Field', 'FieldList', 'Variant', 'Message', 'IntegerField', 'FloatField', 'BooleanField', 'BytesField', 'StringField', 'MessageField', 'EnumField', 'find_definition', 'Error', 'DecodeError', 'EnumDefinitionError', 'FieldDefinitionError', 'InvalidVariantError', 'InvalidDefaultError', 'InvalidNumberError', 'MessageDefinitionError', 'DuplicateNumberError', 'ValidationError', 'DefinitionNotFoundError', ] # TODO(rafek): Add extended module test to ensure all exceptions # in services extends Error. Error = util.Error class EnumDefinitionError(Error): """Enumeration definition error.""" class FieldDefinitionError(Error): """Field definition error.""" class InvalidVariantError(FieldDefinitionError): """Invalid variant provided to field.""" class InvalidDefaultError(FieldDefinitionError): """Invalid default provided to field.""" class InvalidNumberError(FieldDefinitionError): """Invalid number provided to field.""" class MessageDefinitionError(Error): """Enumeration definition error.""" class DuplicateNumberError(Error): """Duplicate number assigned to field.""" class DefinitionNotFoundError(Error): """Raised when definition is not found.""" class DecodeError(Error): """Error found decoding message from encoded form.""" class ValidationError(Error): """Invalid value for message error.""" def __str__(self): """Prints string with field name if present on exception.""" message = Error.__str__(self) try: field_name = self.field_name except AttributeError: return message else: return 'Field %s: %s' % (field_name, message) # Attributes that are reserved by a class definition that # may not be used by either Enum or Message class definitions. _RESERVED_ATTRIBUTE_NAMES = frozenset( ['__module__', '__doc__']) _POST_INIT_FIELD_ATTRIBUTE_NAMES = frozenset( ['name', '_message_definition', '_MessageField__type', '_EnumField__type', '_EnumField__resolved_default']) _POST_INIT_ATTRIBUTE_NAMES = frozenset( ['_message_definition']) # Maximum enumeration value as defined by the protocol buffers standard. # All enum values must be less than or equal to this value. MAX_ENUM_VALUE = (2 ** 29) - 1 # Maximum field number as defined by the protocol buffers standard. # All field numbers must be less than or equal to this value. MAX_FIELD_NUMBER = (2 ** 29) - 1 # Field numbers between 19000 and 19999 inclusive are reserved by the # protobuf protocol and may not be used by fields. FIRST_RESERVED_FIELD_NUMBER = 19000 LAST_RESERVED_FIELD_NUMBER = 19999 class _DefinitionClass(type): """Base meta-class used for definition meta-classes. The Enum and Message definition classes share some basic functionality. Both of these classes may be contained by a Message definition. After initialization, neither class may have attributes changed except for the protected _message_definition attribute, and that attribute may change only once. """ __initialized = False def __init__(cls, name, bases, dct): """Constructor.""" type.__init__(cls, name, bases, dct) # Base classes may never be initialized. if cls.__bases__ != (object,): cls.__initialized = True def message_definition(cls): """Get outer Message definition that contains this definition. Returns: Containing Message definition if definition is contained within one, else None. """ try: return cls._message_definition() except AttributeError: return None def __setattr__(cls, name, value): """Overridden so that cannot set variables on definition classes after init. Setting attributes on a class must work during the period of initialization to set the enumation value class variables and build the name/number maps. Once __init__ has set the __initialized flag to True prohibits setting any more values on the class. The class is in effect frozen. Args: name: Name of value to set. value: Value to set. """ if cls.__initialized and name not in _POST_INIT_ATTRIBUTE_NAMES: raise AttributeError('May not change values: %s' % name) else: type.__setattr__(cls, name, value) def __delattr__(cls, name): """Overridden so that cannot delete varaibles on definition classes.""" raise TypeError('May not delete attributes on definition class') def definition_name(cls): """Helper method for creating definition name. Names will be generated to include the classes package name, scope (if the class is nested in another definition) and class name. By default, the package name for a definition is derived from its module name. However, this value can be overriden by placing a 'package' attribute in the module that contains the definition class. For example: package = 'some.alternate.package' class MyMessage(Message): ... >>> MyMessage.definition_name() some.alternate.package.MyMessage Returns: Dot-separated fully qualified name of definition. """ outer_definition = cls.message_definition() if not outer_definition: definition_module = sys.modules.get(cls.__module__, None) if definition_module: try: package = definition_module.package except AttributeError: package = definition_module.__name__ if package == '__main__': try: file_name = definition_module.__file__ except AttributeError: pass else: base_name = os.path.basename(file_name) package = '.'.join(base_name.split('.')[:-1]) else: return unicode(cls.__name__) else: return u'%s.%s' % (outer_definition.definition_name(), cls.__name__) return u'%s.%s' % (package, cls.__name__) class _EnumClass(_DefinitionClass): """Meta-class used for defining the Enum base class. Meta-class enables very specific behavior for any defined Enum class. All attributes defined on an Enum sub-class must be integers. Each attribute defined on an Enum sub-class is translated into an instance of that sub-class, with the name of the attribute as its name, and the number provided as its value. It also ensures that only one level of Enum class hierarchy is possible. In other words it is not possible to delcare sub-classes of sub-classes of Enum. This class also defines some functions in order to restrict the behavior of the Enum class and its sub-classes. It is not possible to change the behavior of the Enum class in later classes since any new classes may be defined with only integer values, and no methods. """ def __init__(cls, name, bases, dct): # Can only define one level of sub-classes below Enum. if not (bases == (object,) or bases == (Enum,)): raise EnumDefinitionError('Enum type %s may only inherit from Enum' % (name,)) cls.__by_number = {} cls.__by_name = {} # Enum base class does not need to be initialized or locked. if bases != (object,): # Replace integer with number. for attribute, value in dct.iteritems(): # Module will be in every enum class. if attribute in _RESERVED_ATTRIBUTE_NAMES: continue # Reject anything that is not an int. if not isinstance(value, (int, long)): raise EnumDefinitionError( 'May only use integers in Enum definitions. Found: %s = %s' % (attribute, value)) # Protocol buffer standard recommends non-negative values. # Reject negative values. if value < 0: raise EnumDefinitionError( 'Must use non-negative enum values. Found: %s = %d' % (attribute, value)) if value > MAX_ENUM_VALUE: raise EnumDefinitionError( 'Must use enum values less than or equal %d. Found: %s = %d' % (MAX_ENUM_VALUE, attribute, value)) if value in cls.__by_number: raise EnumDefinitionError( 'Value for %s = %d is already defined: %s' % (attribute, value, cls.__by_number[value].name)) # Create enum instance and list in new Enum type. instance = object.__new__(cls) cls.__init__(instance, attribute, value) cls.__by_name[instance.name] = instance cls.__by_number[instance.number] = instance setattr(cls, attribute, instance) _DefinitionClass.__init__(cls, name, bases, dct) def __iter__(cls): """Iterate over all values of enum. Yields: Enumeration instances of the Enum class in arbitrary order. """ return cls.__by_number.itervalues() def names(cls): """Get all names for Enum. Returns: An iterator for names of the enumeration in arbitrary order. """ return cls.__by_name.iterkeys() def numbers(cls): """Get all numbers for Enum. Returns: An iterator for all numbers of the enumeration in arbitrary order. """ return cls.__by_number.iterkeys() def lookup_by_name(cls, name): """Look up Enum by name. Args: name: Name of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_name[name] def lookup_by_number(cls, number): """Look up Enum by number. Args: number: Number of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_number[number] def __len__(cls): return len(cls.__by_name) class Enum(object): """Base class for all enumerated types.""" __metaclass__ = _EnumClass __slots__ = set(('name', 'number')) def __new__(cls, index): """Acts as look-up routine after class is initialized. The purpose of overriding __new__ is to provide a way to treat Enum subclasses as casting types, similar to how the int type functions. A program can pass a string or an integer and this method with "convert" that value in to an appropriate Enum instance. Args: index: Name or number to look up. During initialization this is always the name of the new enum value. Raises: TypeError: When an inappropriate index value is passed provided. """ # If is enum type of this class, return it. if isinstance(index, cls): return index # If number, look up by number. if isinstance(index, (int, long)): try: return cls.lookup_by_number(index) except KeyError: pass # If name, look up by name. if isinstance(index, basestring): try: return cls.lookup_by_name(index) except KeyError: pass raise TypeError('No such value for %s in Enum %s' % (index, cls.__name__)) def __init__(self, name, number=None): """Initialize new Enum instance. Since this should only be called during class initialization any calls that happen after the class is frozen raises an exception. """ # Immediately return if __init__ was called after _Enum.__init__(). # It means that casting operator version of the class constructor # is being used. if getattr(type(self), '_DefinitionClass__initialized'): return object.__setattr__(self, 'name', name) object.__setattr__(self, 'number', number) def __setattr__(self, name, value): raise TypeError('May not change enum values') def __str__(self): return self.name def __int__(self): return self.number def __repr__(self): return '%s(%s, %d)' % (type(self).__name__, self.name, self.number) def __cmp__(self, other): """Order is by number.""" if isinstance(other, type(self)): return cmp(self.number, other.number) return NotImplemented @classmethod def to_dict(cls): """Make dictionary version of enumerated class. Dictionary created this way can be used with def_num and import_enum. Returns: A dict (name) -> number """ return dict((item.name, item.number) for item in iter(cls)) @staticmethod def def_enum(dct, name): """Define enum class from dictionary. Args: dct: Dictionary of enumerated values for type. name: Name of enum. """ return type(name, (Enum,), dct) # TODO(rafek): Determine to what degree this enumeration should be compatible # with FieldDescriptor.Type in: # # http://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto class Variant(Enum): """Wire format variant. Used by the 'protobuf' wire format to determine how to transmit a single piece of data. May be used by other formats. See: http://code.google.com/apis/protocolbuffers/docs/encoding.html Values: DOUBLE: 64-bit floating point number. FLOAT: 32-bit floating point number. INT64: 64-bit signed integer. UINT64: 64-bit unsigned integer. INT32: 32-bit signed integer. BOOL: Boolean value (True or False). STRING: String of UTF-8 encoded text. MESSAGE: Embedded message as byte string. BYTES: String of 8-bit bytes. UINT32: 32-bit unsigned integer. ENUM: Enum value as integer. SINT32: 32-bit signed integer. Uses "zig-zag" encoding. SINT64: 64-bit signed integer. Uses "zig-zag" encoding. """ DOUBLE = 1 FLOAT = 2 INT64 = 3 UINT64 = 4 INT32 = 5 BOOL = 8 STRING = 9 MESSAGE = 11 BYTES = 12 UINT32 = 13 ENUM = 14 SINT32 = 17 SINT64 = 18 class _MessageClass(_DefinitionClass): """Meta-class used for defining the Message base class. For more details about Message classes, see the Message class docstring. Information contained there may help understanding this class. Meta-class enables very specific behavior for any defined Message class. All attributes defined on an Message sub-class must be field instances, Enum class definitions or other Message class definitions. Each field attribute defined on an Message sub-class is added to the set of field definitions and the attribute is translated in to a slot. It also ensures that only one level of Message class hierarchy is possible. In other words it is not possible to declare sub-classes of sub-classes of Message. This class also defines some functions in order to restrict the behavior of the Message class and its sub-classes. It is not possible to change the behavior of the Message class in later classes since any new classes may be defined with only field, Enums and Messages, and no methods. """ def __new__(cls, name, bases, dct): """Create new Message class instance. The __new__ method of the _MessageClass type is overridden so as to allow the translation of Field instances to slots. """ by_number = {} by_name = {} variant_map = {} if bases != (object,): # Can only define one level of sub-classes below Message. if bases != (Message,): raise MessageDefinitionError( 'Message types may only inherit from Message') enums = [] messages = [] # Must not use iteritems because this loop will change the state of dct. for key, field in dct.items(): if key in _RESERVED_ATTRIBUTE_NAMES: continue if isinstance(field, type) and issubclass(field, Enum): enums.append(key) continue if (isinstance(field, type) and issubclass(field, Message) and field is not Message): messages.append(key) continue # Reject anything that is not a field. if type(field) is Field or not isinstance(field, Field): raise MessageDefinitionError( 'May only use fields in message definitions. Found: %s = %s' % (key, field)) if field.number in by_number: raise DuplicateNumberError( 'Field with number %d declared more than once in %s' % (field.number, name)) field.name = key # Place in name and number maps. by_name[key] = field by_number[field.number] = field # Add enums if any exist. if enums: dct['__enums__'] = sorted(enums) # Add messages if any exist. if messages: dct['__messages__'] = sorted(messages) dct['_Message__by_number'] = by_number dct['_Message__by_name'] = by_name return _DefinitionClass.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): """Initializer required to assign references to new class.""" if bases != (object,): for value in dct.itervalues(): if isinstance(value, _DefinitionClass) and not value is Message: value._message_definition = weakref.ref(cls) for field in cls.all_fields(): field._message_definition = weakref.ref(cls) _DefinitionClass.__init__(cls, name, bases, dct) class Message(object): """Base class for user defined message objects. Used to define messages for efficient transmission across network or process space. Messages are defined using the field classes (IntegerField, FloatField, EnumField, etc.). Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class is intentended to itself be transmitted across network or process space and used directly by clients or even other servers. As such methods and non-field attributes could not be transmitted with the structural information causing discrepancies between different languages and implementations. Initialization and validation: A Message object is considered to be initialized if it has all required fields and any nested messages are also initialized. Calling 'check_initialized' will raise a ValidationException if it is not initialized; 'is_initialized' returns a boolean value indicating if it is valid. Validation automatically occurs when Message objects are created and populated. Validation that a given value will be compatible with a field that it is assigned to can be done through the Field instances validate() method. The validate method used on a message will check that all values of a message and its sub-messages are valid. Assingning an invalid value to a field will raise a ValidationException. Example: # Trade type. class TradeType(Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 class Lot(Message): price = IntegerField(1, required=True) quantity = IntegerField(2, required=True) class Order(Message): symbol = StringProperty(1, required=True) total_quantity = IntegerProperty(2, required=True) trade_type = EnumProperty(TradeType, 3, required=True) lots = MessageProperty(Lot, 4, repeated=True) limit = IntegerField(5) order = Order(symbol='GOOG', total_quantity=10, trade_type=TradeType.BUY) lot1 = Lot(price=304, quantity=7) lot2 = Lot(price = 305, quantity=3) order.lots = [lot1, lot2] # Now object is initialized! order.check_initialized() """ __metaclass__ = _MessageClass def __init__(self, **kwargs): """Initialize internal messages state. Args: A message can be initialized via the constructor by passing in keyword arguments corresponding to fields. For example: class Date(Message): day = IntegerField(1) month = IntegerField(2) year = IntegerField(3) Invoking: date = Date(day=6, month=6, year=1911) is the same as doing: date = Date() date.day = 6 date.month = 6 date.year = 1911 """ # Tag being an essential implementation detail must be private. self.__tags = {} assigned = set() for name, value in kwargs.iteritems(): setattr(self, name, value) assigned.add(name) # initialize repeated fields. for field in self.all_fields(): if field.repeated and field.name not in assigned: setattr(self, field.name, []) def check_initialized(self): """Check class for initialization status. Check that all required fields are initialized Raises: ValidationError: If message is not initialized. """ for name, field in self.__by_name.iteritems(): value = getattr(self, name) if value is None: if field.required: raise ValidationError("Message %s is missing required field %s" % (type(self).__name__, name)) else: try: if isinstance(field, MessageField): if field.repeated: for item in value: item.check_initialized() else: value.check_initialized() except ValidationError, err: if not hasattr(err, 'message_name'): err.message_name = type(self).__name__ raise def is_initialized(self): """Get initialization status. Returns: True if message is valid, else False. """ try: self.check_initialized() except ValidationError: return False else: return True @classmethod def all_fields(cls): """Get all field definition objects. Ordering is arbitrary. Returns: Iterator over all values in arbitrary order. """ return cls.__by_name.itervalues() @classmethod def field_by_name(cls, name): """Get field by name. Returns: Field object associated with name. Raises: KeyError if no field found by that name. """ return cls.__by_name[name] @classmethod def field_by_number(cls, number): """Get field by number. Returns: Field object associated with number. Raises: KeyError if no field found by that number. """ return cls.__by_number[number] def get_assigned_value(self, name): """Get the assigned value of an attribute. Get the underlying value of an attribute. If value has not been set, will not return the default for the field. Args: name: Name of attribute to get. Returns: Value of attribute, None if it has not been set. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) return self.__tags.get(field.number) def reset(self, name): """Reset assigned value for field. Resetting a field will return it to its default value or None. Args: name: Name of field to reset. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: if name not in message_type.__by_name: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) self.__tags.pop(field.number, None) def __setattr__(self, name, value): """Change set behavior for messages. Messages may only be assigned values that are fields. Does not try to validate field when set. Args: name: Name of field to assign to. vlaue: Value to assign to field. Raises: AttributeError when trying to assign value that is not a field. """ if name in self.__by_name or name.startswith('_Message__'): object.__setattr__(self, name, value) else: raise AttributeError("May not assign arbitrary value %s " "to message %s" % (name, type(self).__name__)) def __repr__(self): """Make string representation of message. Example: class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' print my_message >>> <MyMessage ... integer_value: 42 ... string_value: u'A string'> Returns: String representation of message, including the values of all fields and repr of all sub-messages. """ body = ['<', type(self).__name__] for field in sorted(self.all_fields(), key=lambda f: f.number): attribute = field.name value = self.get_assigned_value(field.name) if value is not None: body.append('\n %s: %s' % (attribute, repr(value))) body.append('>') return ''.join(body) def __eq__(self, other): """Equality operator. Does field by field comparison with other message. For equality, must be same type and values of all fields must be equal. Messages not required to be initialized for comparison. Does not attempt to determine equality for values that have default values that are not set. In other words: class HasDefault(Message): attr1 = StringField(1, default='default value') message1 = HasDefault() message2 = HasDefault() message2.attr1 = 'default value' message1 != message2 Args: other: Other message to compare with. """ # TODO(rafek): Implement "equivalent" which does comparisons # taking default values in to consideration. if self is other: return True if type(self) is not type(other): return False return self.__tags == other.__tags def __ne__(self, other): """Not equals operator. Does field by field comparison with other message. For non-equality, must be different type or any value of a field must be non-equal to the same field in the other instance. Messages not required to be initialized for comparison. Args: other: Other message to compare with. """ return not self.__eq__(other) class FieldList(list): """List implementation that validates field values. This list implementation overrides all methods that add values in to a list in order to validate those new elements. Attempting to add or set list values that are not of the correct type will raise ValidationError. """ def __init__(self, field_instance, sequence): """Constructor. Args: field_instance: Instance of field that validates the list. sequence: List or tuple to construct list from. """ if not field_instance.repeated: raise FieldDefinitionError('FieldList may only accept repeated fields') self.__field = field_instance self.__field.validate(sequence) list.__init__(self, sequence) @property def field(self): """Field that validates list.""" return self.__field def __setslice__(self, i, j, sequence): """Validate slice assignment to list.""" self.__field.validate(sequence) list.__setslice__(self, i, j, sequence) def __setitem__(self, index, value): """Validate item assignment to list.""" self.__field.validate_element(value) list.__setitem__(self, index, value) def append(self, value): """Validate item appending to list.""" self.__field.validate_element(value) return list.append(self, value) def extend(self, sequence): """Validate extension of list.""" self.__field.validate(sequence) return list.extend(self, sequence) def insert(self, index, value): """Validate item insertion to list.""" self.__field.validate_element(value) return list.insert(self, index, value) # TODO(rafek): Prevent additional field subclasses. class Field(object): __variant_to_type = {} class __metaclass__(type): def __init__(cls, name, bases, dct): getattr(cls, '_Field__variant_to_type').update( (variant, cls) for variant in getattr(cls, 'VARIANTS', [])) type.__init__(cls, name, bases, dct) __initialized = False @util.positional(2) def __init__(self, number, required=False, repeated=False, variant=None, default=None): """Constructor. The required and repeated parameters are mutually exclusive. Setting both to True will raise a FieldDefinitionError. Sub-class Attributes: Each sub-class of Field must define the following: VARIANTS: Set of variant types accepted by that field. DEFAULT_VARIANT: Default variant type if not specified in constructor. Args: number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive with 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive with 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: InvalidVariantError when invalid variant for field is provided. InvalidDefaultError when invalid default for field is provided. FieldDefinitionError when invalid number provided or mutually exclusive fields are used. InvalidNumberError when the field number is out of range or reserved. """ if not isinstance(number, int) or not 1 <= number <= MAX_FIELD_NUMBER: raise InvalidNumberError('Invalid number for field: %s\n' 'Number must be 1 or greater and %d or less' % (number, MAX_FIELD_NUMBER)) if FIRST_RESERVED_FIELD_NUMBER <= number <= LAST_RESERVED_FIELD_NUMBER: raise InvalidNumberError('Tag number %d is a reserved number.\n' 'Numbers %d to %d are reserved' % (number, FIRST_RESERVED_FIELD_NUMBER, LAST_RESERVED_FIELD_NUMBER)) if repeated and required: raise FieldDefinitionError('Cannot set both repeated and required') if variant is None: variant = self.DEFAULT_VARIANT if repeated and default is not None: raise FieldDefinitionError('Repeated fields may not have defaults') if variant not in self.VARIANTS: raise InvalidVariantError( 'Invalid variant: %s\nValid variants for %s are %r' % (variant, type(self).__name__, sorted(self.VARIANTS))) self.number = number self.required = required self.repeated = repeated self.variant = variant if default is not None: try: self.validate_default(default) except ValidationError, err: raise InvalidDefaultError('Invalid default value for field: %s: %s' % (default, err)) self.__default = default self.__initialized = True def __setattr__(self, name, value): """Setter overidden to prevent assignment to fields after creation. Args: name: Name of attribute to set. value: Value to assign. """ # Special case post-init names. They need to be set after constructor. if name in _POST_INIT_FIELD_ATTRIBUTE_NAMES: object.__setattr__(self, name, value) return # All other attributes must be set before __initialized. if not self.__initialized: # Not initialized yet, allow assignment. object.__setattr__(self, name, value) else: raise AttributeError('Field objects are read-only') def __set__(self, message_instance, value): """Set value on message. Args: message_instance: Message instance to set value on. value: Value to set on message. """ # Reaches in to message instance directly to assign to private tags. if value is None: if self.repeated: raise ValidationError( 'May not assign None to repeated field %s' % self.name) else: message_instance._Message__tags.pop(self.number, None) else: if self.repeated: value = FieldList(self, value) else: self.validate(value) message_instance._Message__tags[self.number] = value def __get__(self, message_instance, message_class): if message_instance is None: return self result = message_instance._Message__tags.get(self.number) if result is None: return self.default else: return result def validate_element(self, value): """Validate single element of field. This is different from validate in that it is used on individual values of repeated fields. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ if not isinstance(value, self.type): if value is None: if self.required: raise ValidationError('Required field is missing') else: raise ValidationError('Expected type %s, found %s (type %s)' % (self.type, value, type(value))) def __validate(self, value, validate_element): """Internal validation function. Validate an internal value using a function to validate individual elements. Args: value: Value to validate. validate_element: Function to use to validate individual elements. Raises: ValidationError if value is not expected type. """ if not self.repeated: validate_element(value) else: # Must be a list or tuple, may not be a string. if isinstance(value, (list, tuple)): for element in value: if element is None: raise ValidationError('Repeated values may not be None') validate_element(element) elif value is not None: raise ValidationError('Field is repeated. Found: %s' % value) def validate(self, value): """Validate value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_element) def validate_default_element(self, value): """Validate value as assigned to field default field. Some fields may allow for delayed resolution of default types necessary in the case of circular definition references. In this case, the default value might be a place holder that is resolved when needed after all the message classes are defined. Args: value: Default value to validate. Raises: ValidationError if value is not expected type. """ self.validate_element(value) def validate_default(self, value): """Validate default value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_default_element) def message_definition(self): """Get Message definition that contains this Field definition. Returns: Containing Message definition for Field. Will return None if for some reason Field is defined outside of a Message class. """ try: return self._message_definition() except AttributeError: return None @property def default(self): """Get default value for field.""" return self.__default @classmethod def lookup_field_type_by_variant(cls, variant): return cls.__variant_to_type[variant] class IntegerField(Field): """Field definition for integer values.""" VARIANTS = frozenset([Variant.INT32, Variant.INT64, Variant.UINT32, Variant.UINT64, Variant.SINT32, Variant.SINT64, ]) DEFAULT_VARIANT = Variant.INT64 type = (int, long) class FloatField(Field): """Field definition for float values.""" VARIANTS = frozenset([Variant.FLOAT, Variant.DOUBLE, ]) DEFAULT_VARIANT = Variant.DOUBLE type = float class BooleanField(Field): """Field definition for boolean values.""" VARIANTS = frozenset([Variant.BOOL]) DEFAULT_VARIANT = Variant.BOOL type = bool class BytesField(Field): """Field definition for byte string values.""" VARIANTS = frozenset([Variant.BYTES]) DEFAULT_VARIANT = Variant.BYTES type = str class StringField(Field): """Field definition for unicode string values.""" VARIANTS = frozenset([Variant.STRING]) DEFAULT_VARIANT = Variant.STRING type = unicode def validate_element(self, value): """Validate StringField allowing for str and unicode. Raises: ValidationError if a str value is not 7-bit ascii. """ # If value is str is it considered valid. Satisfies "required=True". if isinstance(value, str): try: unicode(value) except UnicodeDecodeError, err: validation_error = ValidationError( 'Encountered non-ASCII string %s: %s' % (value, err)) validation_error.field_name = self.name raise validation_error else: super(StringField, self).validate_element(value) class MessageField(Field): """Field definition for sub-message values. Message fields contain instance of other messages. Instances stored on messages stored on message fields are considered to be owned by the containing message instance and should not be shared between owning instances. Message fields must be defined to reference a single type of message. Normally message field are defined by passing the referenced message class in to the constructor. It is possible to define a message field for a type that does not yet exist by passing the name of the message in to the constructor instead of a message class. Resolution of the actual type of the message is deferred until it is needed, for example, during message verification. Names provided to the constructor must refer to a class within the same python module as the class that is using it. Names refer to messages relative to the containing messages scope. For example, the two fields of OuterMessage refer to the same message type: class Outer(Message): inner_relative = MessageField('Inner', 1) inner_absolute = MessageField('Outer.Inner', 2) class Inner(Message): ... When resolving an actual type, MessageField will traverse the entire scope of nested messages to match a message name. This makes it easy for siblings to reference siblings: class Outer(Message): class Inner(Message): sibling = MessageField('Sibling', 1) class Sibling(Message): ... """ VARIANTS = frozenset([Variant.MESSAGE]) DEFAULT_VARIANT = Variant.MESSAGE @util.positional(3) def __init__(self, message_type, number, required=False, repeated=False, variant=None): """Constructor. Args: message_type: Message type for field. Must be subclass of Message. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. Raises: FieldDefinitionError when invalid message_type is provided. """ valid_type = (isinstance(message_type, basestring) or (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message))) if not valid_type: raise FieldDefinitionError('Invalid message class: %s' % message_type) if isinstance(message_type, basestring): self.__type_name = message_type self.__type = None else: self.__type = message_type super(MessageField, self).__init__(number, required=required, repeated=repeated, variant=variant) @property def type(self): """Message type used for field.""" if self.__type is None: message_type = find_definition(self.__type_name, self.message_definition()) if not (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message)): raise FieldDefinitionError('Invalid message class: %s' % message_type) self.__type = message_type return self.__type class EnumField(Field): """Field definition for enum values. Enum fields may have default values that are delayed until the associated enum type is resolved. This is necessary to support certain circular references. For example: class Message1(Message): class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 # This field default value will be validated when default is accessed. animal = EnumField('Message2.Animal', 1, default='HORSE') class Message2(Message): class Animal(Enum): DOG = 1 CAT = 2 HORSE = 3 # This fields default value will be validated right away since Color is # already fully resolved. color = EnumField(Message1.Color, 1, default='RED') """ VARIANTS = frozenset([Variant.ENUM]) DEFAULT_VARIANT = Variant.ENUM def __init__(self, enum_type, number, **kwargs): """Constructor. Args: enum_type: Enum type for field. Must be subclass of Enum. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: FieldDefinitionError when invalid enum_type is provided. """ valid_type = (isinstance(enum_type, basestring) or (enum_type is not Enum and isinstance(enum_type, type) and issubclass(enum_type, Enum))) if not valid_type: raise FieldDefinitionError('Invalid enum type: %s' % enum_type) if isinstance(enum_type, basestring): self.__type_name = enum_type self.__type = None else: self.__type = enum_type super(EnumField, self).__init__(number, **kwargs) def validate_default_element(self, value): """Validate default element of Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is validated against that type. Args: value: Value to validate. Raises: ValidationError if value is not expected message type. """ if isinstance(value, (basestring, int, long)): # Validation of the value does not happen for delayed resolution # enumerated types. Ignore if type is not yet resolved. if self.__type: self.__type(value) return super(EnumField, self).validate_default_element(value) @property def type(self): """Enum type used for field.""" if self.__type is None: found_type = find_definition(self.__type_name, self.message_definition()) if not (found_type is not Enum and isinstance(found_type, type) and issubclass(found_type, Enum)): raise FieldDefinitionError('Invalid enum type: %s' % found_type) self.__type = found_type return self.__type @property def default(self): """Default for enum field. Will cause resolution of Enum type and unresolved default value. """ try: return self.__resolved_default except AttributeError: resolved_default = super(EnumField, self).default if isinstance(resolved_default, (basestring, int, long)): resolved_default = self.type(resolved_default) self.__resolved_default = resolved_default return self.__resolved_default @util.positional(2) def find_definition(name, relative_to=None, importer=__import__): """Find definition by name in module-space. The find algorthm will look for definitions by name relative to a message definition or by fully qualfied name. If no definition is found relative to the relative_to parameter it will do the same search against the container of relative_to. If relative_to is a nested Message, it will search its message_definition(). If that message has no message_definition() it will search its module. If relative_to is a module, it will attempt to look for the containing module and search relative to it. If the module is a top-level module, it will look for the a message using a fully qualified name. If no message is found then, the search fails and DefinitionNotFoundError is raised. For example, when looking for any definition 'foo.bar.ADefinition' relative to an actual message definition abc.xyz.SomeMessage: find_definition('foo.bar.ADefinition', SomeMessage) It is like looking for the following fully qualified names: abc.xyz.SomeMessage. foo.bar.ADefinition abc.xyz. foo.bar.ADefinition abc. foo.bar.ADefinition foo.bar.ADefinition When resolving the name relative to Message definitions and modules, the algorithm searches any Messages or sub-modules found in its path. Non-Message values are not searched. A name that begins with '.' is considered to be a fully qualified name. The name is always searched for from the topmost package. For example, assume two message types: abc.xyz.SomeMessage xyz.SomeMessage Searching for '.xyz.SomeMessage' relative to 'abc' will resolve to 'xyz.SomeMessage' and not 'abc.xyz.SomeMessage'. For this kind of name, the relative_to parameter is effectively ignored and always set to None. For more information about package name resolution, please see: http://code.google.com/apis/protocolbuffers/docs/proto.html#packages Args: name: Name of definition to find. May be fully qualified or relative name. relative_to: Search for definition relative to message definition or module. None will cause a fully qualified name search. importer: Import function to use for resolving modules. Returns: Enum or Message class definition associated with name. Raises: DefinitionNotFoundError if no definition is found in any search path. """ # Check parameters. if not (relative_to is None or isinstance(relative_to, types.ModuleType) or isinstance(relative_to, type) and issubclass(relative_to, Message)): raise TypeError('relative_to must be None, Message definition or module. ' 'Found: %s' % relative_to) name_path = name.split('.') # Handle absolute path reference. if not name_path[0]: relative_to = None name_path = name_path[1:] def search_path(): """Performs a single iteration searching the path from relative_to. This is the function that searches up the path from a relative object. fully.qualified.object . relative.or.nested.Definition ----------------------------> ^ | this part of search --+ Returns: Message or Enum at the end of name_path, else None. """ next = relative_to for node in name_path: # Look for attribute first. attribute = getattr(next, node, None) if attribute is not None: next = attribute else: # If module, look for sub-module. if next is None or isinstance(next, types.ModuleType): if next is None: module_name = node else: module_name = '%s.%s' % (next.__name__, node) try: fromitem = module_name.split('.')[-1] next = importer(module_name, '', '', [str(fromitem)]) except ImportError: return None else: return None if (not isinstance(next, types.ModuleType) and not (isinstance(next, type) and issubclass(next, (Message, Enum)))): return None return next while True: found = search_path() if isinstance(found, type) and issubclass(found, (Enum, Message)): return found else: # Find next relative_to to search against. # # fully.qualified.object . relative.or.nested.Definition # <--------------------- # ^ # | # does this part of search if relative_to is None: # Fully qualified search was done. Nothing found. Fail. raise DefinitionNotFoundError('Could not find definition for %s' % (name,)) else: if isinstance(relative_to, types.ModuleType): # Find parent module. module_path = relative_to.__name__.split('.')[:-1] if not module_path: relative_to = None else: # Should not raise ImportError. If it does... weird and # unexepected. Propagate. relative_to = importer( '.'.join(module_path), '', '', [module_path[-1]]) elif (isinstance(relative_to, type) and issubclass(relative_to, Message)): parent = relative_to.message_definition() if parent is None: last_module_name = relative_to.__module__.split('.')[-1] relative_to = importer( relative_to.__module__, '', '', [last_module_name]) else: relative_to = parent
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protourlencode.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import logging import unittest import urllib from protorpc import messages from protorpc import protourlencode from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protourlencode class SuperMessage(messages.Message): """A test message with a nested message field.""" sub_message = messages.MessageField(test_util.OptionalMessage, 1) sub_messages = messages.MessageField(test_util.OptionalMessage, 2, repeated=True) class SuperSuperMessage(messages.Message): """A test message with two levels of nested.""" sub_message = messages.MessageField(SuperMessage, 1) sub_messages = messages.MessageField(SuperMessage, 2, repeated=True) class URLEncodedRequestBuilderTest(test_util.TestCase): """Test the URL Encoded request builder.""" def testMakePath(self): builder = protourlencode.URLEncodedRequestBuilder(SuperSuperMessage(), prefix='pre.') self.assertEquals(None, builder.make_path('')) self.assertEquals(None, builder.make_path('no_such_field')) self.assertEquals(None, builder.make_path('pre.no_such_field')) # Missing prefix. self.assertEquals(None, builder.make_path('sub_message')) # Valid parameters. self.assertEquals((('sub_message', None),), builder.make_path('pre.sub_message')) self.assertEquals((('sub_message', None), ('sub_messages', 1)), builder.make_path('pre.sub_message.sub_messages-1')) self.assertEquals( (('sub_message', None), ('sub_messages', 1), ('int64_value', None)), builder.make_path('pre.sub_message.sub_messages-1.int64_value')) # Missing index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_messages.integer_field')) # Has unexpected index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_message-1.integer_field')) def testAddParameter_SimpleAttributes(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value', ['10'])) self.assertTrue(builder.add_parameter('pre.string_value', ['a string'])) self.assertTrue(builder.add_parameter('pre.enum_value', ['VAL1'])) self.assertEquals(10, message.int64_value) self.assertEquals('a string', message.string_value) self.assertEquals(test_util.OptionalMessage.SimpleEnum.VAL1, message.enum_value) def testAddParameter_InvalidAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') def assert_empty(): self.assertEquals(None, getattr(message, 'sub_message')) self.assertEquals([], getattr(message, 'sub_messages')) self.assertFalse(builder.add_parameter('pre.nothing', ['x'])) assert_empty() self.assertFalse(builder.add_parameter('pre.sub_messages', ['x'])) self.assertFalse(builder.add_parameter('pre.sub_messages-1.nothing', ['x'])) assert_empty() def testAddParameter_NestedAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Set an empty message fields. self.assertTrue(builder.add_parameter('pre.sub_message', [''])) self.assertTrue(isinstance(message.sub_message, SuperMessage)) # Add a basic attribute. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.int64_value', ['10'])) self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.string_value', ['hello'])) self.assertTrue(10, message.sub_message.sub_message.int64_value) self.assertTrue('hello', message.sub_message.sub_message.string_value) def testAddParameter_NestedMessages(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Add a repeated empty message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-0', [''])) sub_message = message.sub_message.sub_messages[0] self.assertTrue(1, len(message.sub_message.sub_messages)) self.assertTrue(isinstance(sub_message, test_util.OptionalMessage)) self.assertEquals(None, getattr(sub_message, 'int64_value')) self.assertEquals(None, getattr(sub_message, 'string_value')) self.assertEquals(None, getattr(sub_message, 'enum_value')) # Add a repeated message with value. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.int64_value', ['10'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertTrue(10, message.sub_message.sub_messages[1].int64_value) # Add another value to the same nested message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.string_value', ['a string'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertEquals(10, message.sub_message.sub_messages[1].int64_value) self.assertEquals('a string', message.sub_message.sub_messages[1].string_value) def testAddParameter_RepeatedValues(self): message = test_util.RepeatedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value-0', ['20'])) self.assertTrue(builder.add_parameter('pre.int64_value-1', ['30'])) self.assertEquals([20, 30], message.int64_value) self.assertTrue(builder.add_parameter('pre.string_value-0', ['hi'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['lo'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['dups overwrite'])) self.assertEquals(['hi', 'dups overwrite'], message.string_value) def testAddParameter_InvalidValuesMayRepeat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('nothing', [1, 2, 3])) def testAddParameter_RepeatedParameters(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', [1, 2, 3]) self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', []) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field.""" message = test_util.HasNestedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, 'pre.') self.assertFalse(builder.add_parameter('pre.nested.a_value.whatever', ['1'])) def testInvalidFieldFormat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('pre.illegal%20', ['1'])) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field There is an odd corner case where if trying to insert a repeated value on an nested repeated message that would normally succeed in being created should fail. This case can only be tested when the first message of the nested messages already exists. Another case is trying to access an indexed value nested within a non-message field. """ class HasRepeated(messages.Message): values = messages.IntegerField(1, repeated=True) class HasNestedRepeated(messages.Message): nested = messages.MessageField(HasRepeated, 1, repeated=True) message = HasNestedRepeated() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.nested-0.values-0', ['1'])) # Try to create an indexed value on a non-message field. self.assertFalse(builder.add_parameter('pre.nested-0.values-0.unknown-0', ['1'])) # Try to create an out of range indexed field on an otherwise valid # repeated message field. self.assertFalse(builder.add_parameter('pre.nested-1.values-1', ['1'])) class ProtourlencodeConformanceTest(test_util.TestCase, test_util.ProtoConformanceTestBase): PROTOLIB = protourlencode encoded_partial = urllib.urlencode([('double_value', 1.23), ('int64_value', -100000000000), ('int32_value', 1020), ('string_value', u'a string'), ('enum_value', 'VAL2'), ]) encoded_full = urllib.urlencode([('double_value', 1.23), ('float_value', -2.5), ('int64_value', -100000000000), ('uint64_value', 102020202020), ('int32_value', 1020), ('bool_value', 'true'), ('string_value', u'a string\u044f'.encode('utf-8')), ('bytes_value', 'a bytes\xff\xfe'), ('enum_value', 'VAL2'), ]) encoded_repeated = urllib.urlencode([('double_value-0', 1.23), ('double_value-1', 2.3), ('float_value-0', -2.5), ('float_value-1', 0.5), ('int64_value-0', -100000000000), ('int64_value-1', 20), ('uint64_value-0', 102020202020), ('uint64_value-1', 10), ('int32_value-0', 1020), ('int32_value-1', 718), ('bool_value-0', 'true'), ('bool_value-1', 'false'), ('string_value-0', u'a string\u044f'.encode('utf-8')), ('string_value-1', u'another string'.encode('utf-8')), ('bytes_value-0', 'a bytes\xff\xfe'), ('bytes_value-1', 'another bytes'), ('enum_value-0', 'VAL2'), ('enum_value-1', 'VAL1'), ]) encoded_nested = urllib.urlencode([('nested.a_value', 'a string'), ]) encoded_repeated_nested = urllib.urlencode( [('repeated_nested-0.a_value', 'a string'), ('repeated_nested-1.a_value', 'another string'), ]) unexpected_tag_message = 'unexpected=whatever' encoded_default_assigned = urllib.urlencode([('a_value', 'a default'), ]) encoded_nested_empty = urllib.urlencode([('nested', '')]) encoded_repeated_nested_empty = urllib.urlencode([('repeated_nested-0', ''), ('repeated_nested-1', '')]) encoded_extend_message = urllib.urlencode([('int64_value-0', 400), ('int64_value-1', 50), ('int64_value-2', 6000)]) encoded_string_types = urllib.urlencode( [('string_value', 'Latin')]) def testParameterPrefix(self): """Test using the 'prefix' parameter to encode_message.""" class MyMessage(messages.Message): number = messages.IntegerField(1) names = messages.StringField(2, repeated=True) message = MyMessage() message.number = 10 message.names = [u'Fred', u'Lisa'] encoded_message = protourlencode.encode_message(message, prefix='prefix-') self.assertEquals({'prefix-number': ['10'], 'prefix-names-0': ['Fred'], 'prefix-names-1': ['Lisa'], }, cgi.parse_qs(encoded_message)) self.assertEquals(message, protourlencode.decode_message(MyMessage, encoded_message, prefix='prefix-')) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Service regsitry for service discovery. The registry service can be deployed on a server in order to provide a central place where remote clients can discover available. On the server side, each service is registered by their name which is unique to the registry. Typically this name provides enough information to identify the service and locate it within a server. For example, for an HTTP based registry the name is the URL path on the host where the service is invocable. The registry is also able to resolve the full descriptor.FileSet necessary to describe the service and all required data-types (messages and enums). A configured registry is itself a remote service and should reference itself. """ import sys import weakref from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import util __all__ = [ 'ServiceMapping', 'ServicesResponse', 'GetFileSetRequest', 'GetFileSetResponse', 'RegistryService', ] class ServiceMapping(messages.Message): """Description of registered service. Fields: name: Name of service. On HTTP based services this will be the URL path used for invocation. definition: Fully qualified name of the service definition. Useful for clients that can look up service definitions based on an existing repository of definitions. """ name = messages.StringField(1, required=True) definition = messages.StringField(2, required=True) class ServicesResponse(messages.Message): """Response containing all registered services. May also contain complete descriptor file-set for all services known by the registry. Fields: services: Service mappings for all registered services in registry. file_set: Descriptor file-set describing all services, messages and enum types needed for use with all requested services if asked for in the request. """ services = messages.MessageField(ServiceMapping, 1, repeated=True) class GetFileSetRequest(messages.Message): """Request for service descriptor file-set. Request to retrieve file sets for specific services. Fields: names: Names of services to retrieve file-set for. """ names = messages.StringField(1, repeated=True) class GetFileSetResponse(messages.Message): """Descriptor file-set for all names in GetFileSetRequest. Fields: file_set: Descriptor file-set containing all descriptors for services, messages and enum types needed for listed names in request. """ file_set = messages.MessageField(descriptor.FileSet, 1, required=True) class RegistryService(remote.Service): """Registry service. Maps names to services and is able to describe all descriptor file-sets necessary to use contined services. On an HTTP based server, the name is the URL path to the service. """ @util.positional(2) def __init__(self, registry, modules=None): """Constructor. Args: registry: Map of name to service class. This map is not copied and may be modified after the reigstry service has been configured. modules: Module dict to draw descriptors from. Defaults to sys.modules. """ # Private Attributes: # __registry: Map of name to service class. Refers to same instance as # registry parameter. # __modules: Mapping of module name to module. # __definition_to_modules: Mapping of definition types to set of modules # that they refer to. This cache is used to make repeated look-ups # faster and to prevent circular references from causing endless loops. self.__registry = registry if modules is None: modules = sys.modules self.__modules = modules # This cache will only last for a single request. self.__definition_to_modules = {} def __find_modules_for_message(self, message_type): """Find modules referred to by a message type. Determines the entire list of modules ultimately referred to by message_type by iterating over all of its message and enum fields. Includes modules referred to fields within its referred messages. Args: message_type: Message type to find all referring modules for. Returns: Set of modules referred to by message_types by traversing all its message and enum fields. """ # TODO(rafek): Maybe this should be a method on Message and Service? def get_dependencies(message_type, seen=None): """Get all dependency definitions of a message type. This function works by collecting the types of all enumeration and message fields defined within the message type. When encountering a message field, it will recursivly find all of the associated message's dependencies. It will terminate on circular dependencies by keeping track of what definitions it already via the seen set. Args: message_type: Message type to get dependencies for. seen: Set of definitions that have already been visited. Returns: All dependency message and enumerated types associated with this message including the message itself. """ if seen is None: seen = set() seen.add(message_type) for field in message_type.all_fields(): if isinstance(field, messages.MessageField): if field.type not in seen: get_dependencies(field.type, seen) elif isinstance(field, messages.EnumField): seen.add(field.type) return seen found_modules = self.__definition_to_modules.setdefault(message_type, set()) if not found_modules: dependencies = get_dependencies(message_type) found_modules.update(self.__modules[definition.__module__] for definition in dependencies) return found_modules def __describe_file_set(self, names): """Get file-set for named services. Args: names: List of names to get file-set for. Returns: descriptor.FileSet containing all the descriptors for all modules ultimately referred to by all service types request by names parameter. """ service_modules = set() if names: for service in (self.__registry[name] for name in names): found_modules = self.__definition_to_modules.setdefault(service, set()) if not found_modules: found_modules.add(self.__modules[service.__module__]) for method_name in service.all_remote_methods(): method = getattr(service, method_name) for message_type in (method.remote.request_type, method.remote.response_type): found_modules.update( self.__find_modules_for_message(message_type)) service_modules.update(found_modules) return descriptor.describe_file_set(service_modules) @property def registry(self): """Get service registry associated with this service instance.""" return self.__registry @remote.method(message_types.VoidMessage, ServicesResponse) def services(self, request): """Get all registered services.""" response = ServicesResponse() response.services = [] for name, service_class in self.__registry.iteritems(): mapping = ServiceMapping() mapping.name = name.decode('utf-8') mapping.definition = service_class.definition_name().decode('utf-8') response.services.append(mapping) return response @remote.method(GetFileSetRequest, GetFileSetResponse) def get_file_set(self, request): """Get file-set for registered servies.""" response = GetFileSetResponse() response.file_set = self.__describe_file_set(request.names) return response
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import with_statement __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import descriptor from protorpc import generate from protorpc import messages from protorpc import util __all__ = ['format_python_file'] def _write_enums(enum_descriptors, out): """Write nested and non-nested Enum types. Args: enum_descriptors: List of EnumDescriptor objects from which to generate enums. out: Indent writer used for generating text. """ # Write enums. for enum in enum_descriptors or []: out << '' out << '' out << 'class %s(messages.Enum):' % enum.name out << '' with out.indent(): if not enum.values: out << 'pass' else: for enum_value in enum.values: out << '%s = %s' % (enum_value.name, enum_value.number) def _write_fields(field_descriptors, out): """Write fields for Message types. Args: field_descriptors: List of FieldDescriptor objects from which to generate fields. out: Indent writer used for generating text. """ out << '' for field in field_descriptors or []: field_type = messages.Field.lookup_field_type_by_variant(field.variant) type_format = '' label_format = '' if issubclass(field_type, (messages.EnumField, messages.MessageField)): type_format = '\'%s\', ' % field.type_name if field.label == descriptor.FieldDescriptor.Label.REQUIRED: label_format = ', required=True' elif field.label == descriptor.FieldDescriptor.Label.REPEATED: label_format = ', repeated=True' if field_type.DEFAULT_VARIANT != field.variant: variant_format = ', variant=messages.Variant.%s' % field.variant else: variant_format = '' if field.default_value: if field_type in [messages.BytesField, messages.StringField, ]: default_value = repr(field.default_value) elif field_type is messages.EnumField: try: default_value = str(int(field.default_value)) except ValueError: default_value = repr(field.default_value) else: default_value = field.default_value default_format = ', default=%s' % (default_value,) else: default_format = '' out << '%s = messages.%s(%s%s%s%s%s)' % (field.name, field_type.__name__, type_format, field.number, label_format, variant_format, default_format) def _write_messages(message_descriptors, out): """Write nested and non-nested Message types. Args: message_descriptors: List of MessageDescriptor objects from which to generate messages. out: Indent writer used for generating text. """ for message in message_descriptors or []: out << '' out << '' out << 'class %s(messages.Message):' % message.name with out.indent(): if not (message.enum_types or message.message_types or message.fields): out << '' out << 'pass' else: _write_enums(message.enum_types, out) _write_messages(message.message_types, out) _write_fields(message.fields, out) def _write_methods(method_descriptors, out): """Write methods of Service types. All service method implementations raise NotImplementedError. Args: method_descriptors: List of MethodDescriptor objects from which to generate methods. out: Indent writer used for generating text. """ for method in method_descriptors: out << '' out << "@remote.method('%s', '%s')" % (method.request_type, method.response_type) out << 'def %s(self, request):' % (method.name,) with out.indent(): out << ('raise NotImplementedError' "('Method %s is not implemented')" % (method.name)) def _write_services(service_descriptors, out): """Write Service types. Args: service_descriptors: List of ServiceDescriptor instances from which to generate services. out: Indent writer used for generating text. """ for service in service_descriptors or []: out << '' out << '' out << 'class %s(remote.Service):' % service.name with out.indent(): if service.methods: _write_methods(service.methods, out) else: out << '' out << 'pass' @util.positional(2) def format_python_file(file_descriptor, output, indent_space=2): """Format FileDescriptor object as a single Python module. Services generated by this function will raise NotImplementedError. All Python classes generated by this function use delayed binding for all message fields, enum fields and method parameter types. For example a service method might be generated like so: class MyService(remote.Service): @remote.method('my_package.MyRequestType', 'my_package.MyResponseType') def my_method(self, request): raise NotImplementedError('Method my_method is not implemented') Args: file_descriptor: FileDescriptor instance to format as python module. output: File-like object to write module source code to. indent_space: Number of spaces for each level of Python indentation. """ out = generate.IndentWriter(output, indent_space=indent_space) out << 'from protorpc import messages' if file_descriptor.service_types: out << 'from protorpc import remote' if file_descriptor.package: out << "package = '%s'" % file_descriptor.package _write_enums(file_descriptor.enum_types, out) _write_messages(file_descriptor.message_types, out) _write_services(file_descriptor.service_types, out)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = registry class MyService1(remote.Service): """Test service that refers to messages in another module.""" @remote.method(test_util.NestedMessage, test_util.NestedMessage) def a_method(self, request): pass class MyService2(remote.Service): """Test service that does not refer to messages in another module.""" class RegistryServiceTest(test_util.TestCase): def setUp(self): self.registry = { 'my-service1': MyService1, 'my-service2': MyService2, } self.modules = { __name__: sys.modules[__name__], test_util.__name__: test_util, } self.registry_service = registry.RegistryService(self.registry, modules=self.modules) def CheckServiceMappings(self, mappings): service1_mapping = registry.ServiceMapping() service1_mapping.name = 'my-service1' service1_mapping.definition = '__main__.MyService1' service2_mapping = registry.ServiceMapping() service2_mapping.name = 'my-service2' service2_mapping.definition = '__main__.MyService2' mappings.sort(key=lambda mapping: mapping.name) self.assertEquals(mappings[0], service1_mapping) self.assertEquals(mappings[1], service2_mapping) def testServices(self): response = self.registry_service.services(message_types.VoidMessage()) self.CheckServiceMappings(response.services) def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_None(self): request = registry.GetFileSetRequest() response = self.registry_service.get_file_set(request) self.assertEquals(descriptor.FileSet(), response.file_set) def testGetFileSet_ReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service1'] response = self.registry_service.get_file_set(request) # Will suck in and describe the test_util module. expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_DoNotReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service2'] response = self.registry_service.get_file_set(request) # Service does not reference test_util, so will only describe this module. expected_file_set = descriptor.describe_file_set([self.modules[__name__]]) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.remote.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import types import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import test_util from protorpc import transport import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = remote class Request(messages.Message): """Test request message.""" value = messages.StringField(1) class Response(messages.Message): """Test response message.""" value = messages.StringField(1) class MyService(remote.Service): @remote.method(Request, Response) def remote_method(self, request): response = Response() response.value = request.value return response class SimpleRequest(messages.Message): """Simple request message type used for tests.""" param1 = messages.StringField(1) param2 = messages.StringField(2) class SimpleResponse(messages.Message): """Simple response message type used for tests.""" class BasicService(remote.Service): """A basic service with decorated remote method.""" def __init__(self): self.request_ids = [] @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, request): self.request_ids.append(id(request)) return SimpleResponse() class RpcErrorTest(test_util.TestCase): def testFromStatus(self): for state in remote.RpcState: exception = remote.RpcError.from_state self.assertEquals(remote.ServerError, remote.RpcError.from_state('SERVER_ERROR')) class ApplicationErrorTest(test_util.TestCase): def testErrorCode(self): self.assertEquals('blam', remote.ApplicationError('an error', 'blam').error_name) def testStr(self): self.assertEquals('an error', str(remote.ApplicationError('an error', 1))) def testRepr(self): self.assertEquals("ApplicationError('an error', 1)", repr(remote.ApplicationError('an error', 1))) self.assertEquals("ApplicationError('an error')", repr(remote.ApplicationError('an error'))) class RemoteTest(test_util.TestCase): """Test remote method decorator.""" def testRemote(self): """Test use of remote decorator.""" self.assertEquals(SimpleRequest, BasicService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, BasicService.remote_method.remote.response_type) self.assertTrue(isinstance(BasicService.remote_method.remote.method, types.FunctionType)) def testRemoteMessageResolution(self): """Test use of remote decorator to resolve message types by name.""" class OtherService(remote.Service): @remote.method('SimpleRequest', 'SimpleResponse') def remote_method(self, request): pass self.assertEquals(SimpleRequest, OtherService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, OtherService.remote_method.remote.response_type) def testRemoteMessageResolution_NotFound(self): """Test failure to find message types.""" class OtherService(remote.Service): @remote.method('NoSuchRequest', 'NoSuchResponse') def remote_method(self, request): pass self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchRequest', getattr, OtherService.remote_method.remote, 'request_type') self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchResponse', getattr, OtherService.remote_method.remote, 'response_type') def testInvocation(self): """Test that invocation passes request through properly.""" service = BasicService() request = SimpleRequest() self.assertEquals(SimpleResponse(), service.remote_method(request)) self.assertEquals([id(request)], service.request_ids) def testInvocation_WrongRequestType(self): """Wrong request type passed to remote method.""" service = BasicService() self.assertRaises(remote.RequestError, service.remote_method, 'wrong') self.assertRaises(remote.RequestError, service.remote_method, None) self.assertRaises(remote.RequestError, service.remote_method, SimpleResponse()) def testInvocation_WrongResponseType(self): """Wrong response type returned from remote method.""" class AnotherService(object): @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, unused_request): return self.return_this service = AnotherService() service.return_this = 'wrong' self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = None self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = SimpleRequest() self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) def testBadRequestType(self): """Test bad request types used in remote definition.""" for request_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(request_type, SimpleResponse) def remote_method(self, request): pass self.assertRaises(TypeError, declare) def testBadResponseType(self): """Test bad response types used in remote definition.""" for response_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(SimpleRequest, response_type) def remote_method(self, request): pass self.assertRaises(TypeError, declare) class GetRemoteMethodTest(test_util.TestCase): """Test for is_remote_method.""" def testGetRemoteMethod(self): """Test valid remote method detection.""" class Service(object): @remote.method(Request, Response) def remote_method(self, request): pass self.assertEquals(Service.remote_method.remote, remote.get_remote_method_info(Service.remote_method)) self.assertTrue(Service.remote_method.remote, remote.get_remote_method_info(Service().remote_method)) def testGetNotRemoteMethod(self): """Test positive result on a remote method.""" class NotService(object): def not_remote_method(self, request): pass def fn(self): pass class NotReallyRemote(object): """Test negative result on many bad values for remote methods.""" def not_really(self, request): pass not_really.remote = 'something else' for not_remote in [NotService.not_remote_method, NotService().not_remote_method, NotReallyRemote.not_really, NotReallyRemote().not_really, None, 1, 'a string', fn]: self.assertEquals(None, remote.get_remote_method_info(not_remote)) class RequestStateTest(test_util.TestCase): """Test request state.""" def testConstructor(self): """Test constructor.""" state = remote.RequestState(remote_host='remote-host', remote_address='remote-address', server_host='server-host', server_port=10) self.assertEquals('remote-host', state.remote_host) self.assertEquals('remote-address', state.remote_address) self.assertEquals('server-host', state.server_host) self.assertEquals(10, state.server_port) state = remote.RequestState() self.assertEquals(None, state.remote_host) self.assertEquals(None, state.remote_address) self.assertEquals(None, state.server_host) self.assertEquals(None, state.server_port) def testConstructorError(self): """Test unexpected keyword argument.""" self.assertRaises(TypeError, remote.RequestState, x=10) def testRepr(self): """Test string representation.""" self.assertEquals('<remote.RequestState>', repr(remote.RequestState())) self.assertEquals('<remote.RequestState remote_host=abc>', repr(remote.RequestState(remote_host='abc'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def>', repr(remote.RequestState(remote_host='abc', remote_address='def'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi ' 'server_port=102>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi', server_port=102))) class ServiceTest(test_util.TestCase): """Test Service class.""" def testServiceBase_AllRemoteMethods(self): """Test that service base class has no remote methods.""" self.assertEquals({}, remote.Service.all_remote_methods()) def testAllRemoteMethods(self): """Test all_remote_methods with properly Service subclass.""" self.assertEquals({'remote_method': MyService.remote_method}, MyService.all_remote_methods()) def testAllRemoteMethods_SubClass(self): """Test all_remote_methods on a sub-class of a service.""" class SubClass(MyService): @remote.method(Request, Response) def sub_class_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, 'sub_class_method': SubClass.sub_class_method, }, SubClass.all_remote_methods()) def testOverrideMethod(self): """Test that trying to override a remote method with remote decorator.""" class SubClass(MyService): def remote_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, }, SubClass.all_remote_methods()) def testOverrideMethodWithRemote(self): """Test trying to override a remote method with remote decorator.""" def do_override(): class SubClass(MyService): @remote.method(Request, Response) def remote_method(self, request): pass self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Do not use remote decorator when ' 'overloading remote method remote_method ' 'on service SubClass', do_override) def testOverrideMethodWithInvalidValue(self): """Test trying to override a remote method with remote decorator.""" def do_override(bad_value): class SubClass(MyService): remote_method = bad_value for bad_value in [None, 1, 'string', {}]: self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Must override remote_method in ' 'SubClass with a method', do_override, bad_value) def testCallingRemoteMethod(self): """Test invoking a remote method.""" expected = Response() expected.value = 'what was passed in' request = Request() request.value = 'what was passed in' service = MyService() self.assertEquals(expected, service.remote_method(request)) def testFactory(self): """Test using factory to pass in state.""" class StatefulService(remote.Service): def __init__(self, a, b, c=None): self.a = a self.b = b self.c = c state = [1, 2, 3] factory = StatefulService.new_factory(1, state) self.assertEquals('Creates new instances of service StatefulService.\n\n' 'Returns:\n' ' New instance of __main__.StatefulService.', factory.func_doc) self.assertEquals('StatefulService_service_factory', factory.func_name) self.assertEquals(StatefulService, factory.service_class) service = factory() self.assertEquals(1, service.a) self.assertEquals(id(state), id(service.b)) self.assertEquals(None, service.c) factory = StatefulService.new_factory(2, b=3, c=4) service = factory() self.assertEquals(2, service.a) self.assertEquals(3, service.b) self.assertEquals(4, service.c) def testFactoryError(self): """Test misusing a factory.""" # Passing positional argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(1)) # Passing keyword argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(x=1)) class StatefulService(remote.Service): def __init__(self, a): pass # Missing required parameter. self.assertRaises(TypeError, StatefulService.new_factory()) def testDefinitionName(self): """Test getting service definition name.""" class TheService(remote.Service): pass self.assertEquals('__main__.TheService', TheService.definition_name()) def testDefinitionNameWithPackage(self): """Test getting service definition name when package defined.""" global package package = 'my.package' try: class TheService(remote.Service): pass self.assertEquals('my.package.TheService', TheService.definition_name()) finally: del package def testDefinitionNameWithNoModule(self): """Test getting service definition name when package defined.""" module = sys.modules[__name__] try: del sys.modules[__name__] class TheService(remote.Service): pass self.assertEquals('TheService', TheService.definition_name()) finally: sys.modules[__name__] = module class StubTest(test_util.TestCase): def setUp(self): self.mox = mox.Mox() self.transport = self.mox.CreateMockAnything() def testDefinitionName(self): self.assertEquals(BasicService.definition_name(), BasicService.Stub.definition_name()) def testRemoteMethods(self): self.assertEquals(BasicService.all_remote_methods(), BasicService.Stub.all_remote_methods()) def testSync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(request)) self.mox.VerifyAll() def testSync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(request)) self.mox.VerifyAll() def testAsync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequestAndKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'May not provide both args and kwargs', stub.async.remote_method, request, param1='val1', param2='val2') self.mox.VerifyAll() def testAsync_WithTooManyPositionals(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'remote_method\(\) takes at most 2 positional arguments \(3 given\)', stub.async.remote_method, request, 'another value') self.mox.VerifyAll() class IsErrorStatusTest(test_util.TestCase): def testIsError(self): for state in (s for s in remote.RpcState if s > remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertTrue(remote.is_error_status(status)) def testIsNotError(self): for state in (s for s in remote.RpcState if s <= remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertFalse(remote.is_error_status(status)) def testStateNone(self): self.assertRaises(messages.ValidationError, remote.is_error_status, remote.RpcStatus()) class CheckRpcStatusTest(test_util.TestCase): def testStateNone(self): self.assertRaises(messages.ValidationError, remote.check_rpc_status, remote.RpcStatus()) def testNoError(self): for state in (remote.RpcState.OK, remote.RpcState.RUNNING): remote.check_rpc_status(remote.RpcStatus(state=state)) def testErrorState(self): status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='a request error') self.assertRaisesWithRegexpMatch(remote.RequestError, 'a request error', remote.check_rpc_status, status) def testApplicationErrorState(self): status = remote.RpcStatus(state=remote.RpcState.APPLICATION_ERROR, error_message='an application error', error_name='blam') try: remote.check_rpc_status(status) self.fail('Should have raised application error.') except remote.ApplicationError, err: self.assertEquals('an application error', str(err)) self.assertEquals('blam', err.error_name) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stub library.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import sys import urllib2 from protorpc import descriptor from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import util __all__ = [ 'define_enum', 'define_field', 'define_file', 'define_message', 'define_service', 'import_file', 'import_file_set', ] # Map variant back to message field classes. def _build_variant_map(): """Map variants to fields. Returns: Dictionary mapping field variant to its associated field type. """ result = {} for name in dir(messages): value = getattr(messages, name) if isinstance(value, type) and issubclass(value, messages.Field): for variant in getattr(value, 'VARIANTS', []): result[variant] = value return result _VARIANT_MAP = _build_variant_map() def _get_or_define_module(full_name, modules): """Helper method for defining new modules. Args: full_name: Fully qualified name of module to create or return. modules: Dictionary of all modules. Defaults to sys.modules. Returns: Named module if found in 'modules', else creates new module and inserts in 'modules'. Will also construct parent modules if necessary. """ module = modules.get(full_name) if not module: module = new.module(full_name) modules[full_name] = module split_name = full_name.rsplit('.', 1) if len(split_name) > 1: parent_module_name, sub_module_name = split_name parent_module = _get_or_define_module(parent_module_name, modules) setattr(parent_module, sub_module_name, module) return module def define_enum(enum_descriptor, module_name): """Define Enum class from descriptor. Args: enum_descriptor: EnumDescriptor to build Enum class from. module_name: Module name to give new descriptor class. Returns: New messages.Enum sub-class as described by enum_descriptor. """ enum_values = enum_descriptor.values or [] class_dict = dict((value.name, value.number) for value in enum_values) class_dict['__module__'] = module_name return type(str(enum_descriptor.name), (messages.Enum,), class_dict) def define_field(field_descriptor): """Define Field instance from descriptor. Args: field_descriptor: FieldDescriptor class to build field instance from. Returns: New field instance as described by enum_descriptor. """ field_class = _VARIANT_MAP[field_descriptor.variant] params = {'number': field_descriptor.number, 'variant': field_descriptor.variant, } if field_descriptor.label == descriptor.FieldDescriptor.Label.REQUIRED: params['required'] = True elif field_descriptor.label == descriptor.FieldDescriptor.Label.REPEATED: params['repeated'] = True if field_class in (messages.EnumField, messages.MessageField): return field_class(field_descriptor.type_name, **params) else: return field_class(**params) def define_message(message_descriptor, module_name): """Define Message class from descriptor. Args: message_descriptor: MessageDescriptor to describe message class from. module_name: Module name to give to new descriptor class. Returns: New messages.Message sub-class as described by message_descriptor. """ class_dict = {'__module__': module_name} for enum in message_descriptor.enum_types or []: enum_instance = define_enum(enum, module_name) class_dict[enum.name] = enum_instance # TODO(rafek): support nested messages when supported by descriptor. for field in message_descriptor.fields or []: field_instance = define_field(field) class_dict[field.name] = field_instance class_name = message_descriptor.name.encode('utf-8') return type(class_name, (messages.Message,), class_dict) def define_service(service_descriptor, module): """Define a new service proxy. Args: service_descriptor: ServiceDescriptor class that describes the service. module: Module to add service to. Request and response types are found relative to this module. Returns: Service class proxy capable of communicating with a remote server. """ class_dict = {'__module__': module.__name__} class_name = service_descriptor.name.encode('utf-8') for method_descriptor in service_descriptor.methods or []: request_definition = messages.find_definition( method_descriptor.request_type, module) response_definition = messages.find_definition( method_descriptor.response_type, module) method_name = method_descriptor.name.encode('utf-8') def remote_method(self, request): """Actual service method.""" raise NotImplementedError('Method is not implemented') remote_method.__name__ = method_name remote_method_decorator = remote.method(request_definition, response_definition) class_dict[method_name] = remote_method_decorator(remote_method) service_class = type(class_name, (remote.Service,), class_dict) return service_class def define_file(file_descriptor, module=None): """Define module from FileDescriptor. Args: file_descriptor: FileDescriptor instance to describe module from. module: Module to add contained objects to. Module name overrides value in file_descriptor.package. Definitions are added to existing module if provided. Returns: If no module provided, will create a new module with its name set to the file descriptor's package. If a module is provided, returns the same module. """ if module is None: module = new.module(file_descriptor.package) for enum_descriptor in file_descriptor.enum_types or []: enum_class = define_enum(enum_descriptor, module.__name__) setattr(module, enum_descriptor.name, enum_class) for message_descriptor in file_descriptor.message_types or []: message_class = define_message(message_descriptor, module.__name__) setattr(module, message_descriptor.name, message_class) for service_descriptor in file_descriptor.service_types or []: service_class = define_service(service_descriptor, module) setattr(module, service_descriptor.name, service_class) return module @util.positional(1) def import_file(file_descriptor, modules=None): """Import FileDescriptor in to module space. This is like define_file except that a new module and any required parent modules are created and added to the modules parameter or sys.modules if not provided. Args: file_descriptor: FileDescriptor instance to describe module from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. Returns: Module found in modules, else a new module. """ if not file_descriptor.package: raise ValueError('File descriptor must have package name') if modules is None: modules = sys.modules module = _get_or_define_module(file_descriptor.package.encode('utf-8'), modules) return define_file(file_descriptor, module) @util.positional(1) def import_file_set(file_set, modules=None, _open=open): """Import FileSet in to module space. Args: file_set: If string, open file and read serialized FileSet. Otherwise, a FileSet instance to import definitions from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. _open: Used for dependency injection during tests. """ if isinstance(file_set, basestring): encoded_file = _open(file_set, 'rb') try: encoded_file_set = encoded_file.read() finally: encoded_file.close() file_set = protobuf.decode_message(descriptor.FileSet, encoded_file_set) for file_descriptor in file_set.files: # Do not reload built in protorpc classes. if not file_descriptor.package.startswith('protorpc.'): import_file(file_descriptor, modules=modules)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.dynamic.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import dynamic from protorpc import test_util from google.protobuf import descriptor class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = dynamic class Color(dynamic.Enum): RED = 1 GREEN = 2L BLUE = 4 class EnumTest(test_util.TestCase): """Tests for Enum class.""" def testDefinition(self): """Test correct Enum definition.""" self.assertEquals(1, Color.RED) self.assertEquals(2, Color.GREEN) self.assertEquals(4, Color.BLUE) self.assertEquals([('RED', 1), ('GREEN', 2), ('BLUE', 4)], Color._VALUES) def testIntegerOnly(self): """Test that only integers are permitted values for class.""" def do_test(): class Stuff(dynamic.Enum): A_STRING = 'a string' self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoEnumSubclass(self): """Test that it is not possible to sub-class Enum types.""" def do_test(): class MoreColor(Color): MAGENTA = 4 self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoConstructor(self): """Test that it is not possible to construct Enum values.""" self.assertRaises(NotImplementedError, Color) class MessageTest(test_util.TestCase): """Tests for Message class.""" def testEmptyDefinition(self): """Test the creation of an empty message definition.""" class MyMessage(dynamic.Message): pass self.assertEquals([], MyMessage.DESCRIPTOR.enum_types) self.assertEquals([], MyMessage.DESCRIPTOR.nested_types) self.assertEquals([], MyMessage.DESCRIPTOR.fields) self.assertEquals('MyMessage', MyMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage', MyMessage.DESCRIPTOR.full_name) def testNestedDefinition(self): """Test nesting message definitions in another.""" class MyMessage(dynamic.Message): class NestedMessage(dynamic.Message): pass self.assertEquals('NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage.NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.full_name) self.assertEquals(MyMessage.DESCRIPTOR, MyMessage.NestedMessage.DESCRIPTOR.containing_type) self.assertEquals([MyMessage.NestedMessage.DESCRIPTOR], MyMessage.DESCRIPTOR.nested_types) def testNestedEnum(self): """Test nesting an Enum type within a definition.""" class MyMessage(dynamic.Message): class Greek(dynamic.Enum): ALPHA = 1 BETA = 2 GAMMA = 4 self.assertFalse(hasattr(MyMessage, 'Greek')) self.assertEquals(1, len(MyMessage.DESCRIPTOR.enum_types)) Greek = MyMessage.DESCRIPTOR.enum_types[0] self.assertEquals('Greek', Greek.name) self.assertEquals('__main__.MyMessage.Greek', Greek.full_name) self.assertEquals(MyMessage.DESCRIPTOR, Greek.containing_type) self.assertEquals(3, len(Greek.values)) ALPHA = Greek.values[0] BETA = Greek.values[1] GAMMA = Greek.values[2] self.assertEquals(Greek, ALPHA.type) self.assertEquals(Greek, BETA.type) self.assertEquals(Greek, GAMMA.type) self.assertEquals(1, MyMessage.ALPHA) self.assertEquals(2, MyMessage.BETA) self.assertEquals(4, MyMessage.GAMMA) def testOptionalFields(self): """Test optional fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.StringField(2) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1 = 102 self.assertTrue(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRequiredFields(self): """Test required fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True) f2 = dynamic.StringField(2, required=True) m1 = MyMessage() self.assertFalse(m1.IsInitialized()) m1.f1 = 102 self.assertFalse(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRepeatedFields(self): """Test repeated fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True) f2 = dynamic.StringField(2, repeated=True) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1.append(102) self.assertTrue(m1.IsInitialized()) m1.f2.append('a string') self.assertTrue(m1.IsInitialized()) def testFieldDescriptor(self): """Test field descriptors after message class creation.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) class Nested(dynamic.Message): f2 = dynamic.IntegerField(1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) self.assertEquals(1, len(MyMessage.Nested.DESCRIPTOR.fields)) f1 = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('f1', f1.name) self.assertEquals('__main__.MyMessage.f1', f1.full_name) self.assertEquals(MyMessage.DESCRIPTOR, f1.containing_type) f2 = MyMessage.Nested.DESCRIPTOR.fields[0] self.assertEquals('f2', f2.name) self.assertEquals('__main__.MyMessage.Nested.f2', f2.full_name) self.assertEquals(MyMessage.Nested.DESCRIPTOR, f2.containing_type) def testRequiredAndRepeated(self): """Test using required and repeated flags together.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True, repeated=True) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDefaults(self): """Test using default values.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, default=10) m = MyMessage() self.assertEquals(10, m.f1) def testNoDefaultList(self): """Test that default does not work for repeated fields.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True, default=[1, 2, 3]) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testUnexpectedFieldArgument(self): """Test that unknown keyword arguments may not be used.""" self.assertRaises(TypeError, dynamic.IntegerField, 1, whatever=10) def testOverrideVariant(self): """Test overriding the variant of a field.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.IntegerField(2, variant=descriptor.FieldDescriptor.TYPE_UINT32) self.assertEquals(descriptor.FieldDescriptor.TYPE_INT64, MyMessage.DESCRIPTOR.fields[0].type) self.assertEquals(descriptor.FieldDescriptor.TYPE_UINT32, MyMessage.DESCRIPTOR.fields[1].type) def testOverrideWrongVariant(self): """Test assigning an incompatible variant.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField( 1, variant=descriptor.FieldDescriptor.TYPE_STRING) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowNonDefinitionValues(self): """Test that non-definitions may not be assigned to class.""" def do_test(): class MyMessage(dynamic.Message): f1 = 'A non-field value.' self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowMethods(self): """Test that methods may not be defined on class.""" def do_test(): class MyMessage(dynamic.Message): def i_dont_think_so(self): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotSubclassing(self): """Test that messages may not be sub-classed.""" class MyMessage(dynamic.Message): pass def do_test(): class SubClass(MyMessage): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testEnumField(self): """Test a basic enum field.""" class MyMessage(dynamic.Message): class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) color = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('color', color.name) self.assertEquals('__main__.MyMessage.color', color.full_name) Color = color.enum_type self.assertEquals(MyMessage.DESCRIPTOR.enum_types[0], Color) def testEnumFieldWithNonEnum(self): """Test enum field with a non-enum class.""" def do_test(): class MyMessage(dynamic.Message): class Color(object): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertRaises(TypeError, do_test) def testEnumFieldWithNonNestedEnum(self): """Test enum field with a non-peer Enum class.""" class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 def do_test(): class MyMessage(dynamic.Message): color = dynamic.EnumField(Color, 1) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testNoAbitraryAssignment(self): """Test that not possible to assing non-field attributes.""" class MyMessage(dynamic.Message): pass self.assertRaises(AttributeError, setattr, MyMessage(), 'a', 10) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Protocol buffer support for message types. For more details about protocol buffer encoding and decoding please see: http://code.google.com/apis/protocolbuffers/docs/encoding.html Public Exceptions: DecodeError: Raised when a decode error occurs from incorrect protobuf format. Public Functions: encode_message: Encodes a message in to a protocol buffer string. decode_message: Decode from a protocol buffer string to a message. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import array import cStringIO from protorpc import messages # TODO(rafek): Do something about this dependency maybe. from google.net.proto import ProtocolBuffer __all__ = ['CONTENT_TYPE', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/x-google-protobuf' class _Encoder(ProtocolBuffer.Encoder): """Extension of protocol buffer encoder. Original protocol buffer encoder does not have complete set of methods for handling required encoding. This class adds them. """ # TODO(rafek): Implement the missing encoding types. def no_encoding(self, value): """No encoding available for type. Args: value: Value to encode. Raises: NotImplementedError at all times. """ raise NotImplementedError() def encode_enum(self, value): """Encode an enum value. Args: value: Enum to encode. """ self.putVarInt32(value.number) def encode_message(self, value): """Encode a Message in to an embedded message. Args: value: Message instance to encode. """ self.putPrefixedString(encode_message(value)) def encode_unicode_string(self, value): """Helper to properly pb encode unicode strings to UTF-8. Args: value: String value to encode. """ if isinstance(value, unicode): value = value.encode('utf-8') self.putPrefixedString(value) class _Decoder(ProtocolBuffer.Decoder): """Extension of protocol buffer decoder. Original protocol buffer decoder does not have complete set of methods for handling required decoding. This class adds them. """ # TODO(rafek): Implement the missing encoding types. def no_decoding(self): """No decoding available for type. Raises: NotImplementedError at all times. """ raise NotImplementedError() def decode_string(self): """Decode a unicode string. Returns: Next value in stream as a unicode string. """ return self.getPrefixedString().decode('UTF-8') def decode_boolean(self): """Decode a boolean value. Returns: Next value in stream as a boolean. """ return bool(self.getBoolean()) # Number of bits used to describe a protocol buffer bits used for the variant. _WIRE_TYPE_BITS = 3 _WIRE_TYPE_MASK = 7 # Maps variant to underlying wire type. Many variants map to same type. _VARIANT_TO_WIRE_TYPE = { messages.Variant.DOUBLE: _Encoder.DOUBLE, messages.Variant.FLOAT: _Encoder.FLOAT, messages.Variant.INT64: _Encoder.NUMERIC, messages.Variant.UINT64: _Encoder.NUMERIC, messages.Variant.INT32: _Encoder.NUMERIC, messages.Variant.BOOL: _Encoder.NUMERIC, messages.Variant.STRING: _Encoder.STRING, messages.Variant.MESSAGE: _Encoder.STRING, messages.Variant.BYTES: _Encoder.STRING, messages.Variant.UINT32: _Encoder.NUMERIC, messages.Variant.ENUM: _Encoder.NUMERIC, messages.Variant.SINT32: _Encoder.NUMERIC, messages.Variant.SINT64: _Encoder.NUMERIC, } # Maps variant to encoder method. _VARIANT_TO_ENCODER_MAP = { messages.Variant.DOUBLE: _Encoder.putDouble, messages.Variant.FLOAT: _Encoder.putFloat, messages.Variant.INT64: _Encoder.putVarInt64, messages.Variant.UINT64: _Encoder.putVarUint64, messages.Variant.INT32: _Encoder.putVarInt32, messages.Variant.BOOL: _Encoder.putBoolean, messages.Variant.STRING: _Encoder.encode_unicode_string, messages.Variant.MESSAGE: _Encoder.encode_message, messages.Variant.BYTES: _Encoder.encode_unicode_string, messages.Variant.UINT32: _Encoder.no_encoding, messages.Variant.ENUM: _Encoder.encode_enum, messages.Variant.SINT32: _Encoder.no_encoding, messages.Variant.SINT64: _Encoder.no_encoding, } # Basic wire format decoders. Used for skipping unknown values. _WIRE_TYPE_TO_DECODER_MAP = { _Encoder.NUMERIC: _Decoder.getVarInt32, _Encoder.DOUBLE: _Decoder.getDouble, _Encoder.STRING: _Decoder.getPrefixedString, _Encoder.FLOAT: _Decoder.getFloat, } # Wire type to name mapping for error messages. _WIRE_TYPE_NAME = { _Encoder.NUMERIC: 'NUMERIC', _Encoder.DOUBLE: 'DOUBLE', _Encoder.STRING: 'STRING', _Encoder.FLOAT: 'FLOAT', } # Maps variant to decoder method. _VARIANT_TO_DECODER_MAP = { messages.Variant.DOUBLE: _Decoder.getDouble, messages.Variant.FLOAT: _Decoder.getFloat, messages.Variant.INT64: _Decoder.getVarInt64, messages.Variant.UINT64: _Decoder.getVarUint64, messages.Variant.INT32: _Decoder.getVarInt32, messages.Variant.BOOL: _Decoder.decode_boolean, messages.Variant.STRING: _Decoder.decode_string, messages.Variant.MESSAGE: _Decoder.getPrefixedString, messages.Variant.BYTES: _Decoder.getPrefixedString, messages.Variant.UINT32: _Decoder.no_decoding, messages.Variant.ENUM: _Decoder.getVarInt32, messages.Variant.SINT32: _Decoder.no_decoding, messages.Variant.SINT64: _Decoder.no_decoding, } def encode_message(message): """Encode Message instance to protocol buffer. Args: Message instance to encode in to protocol buffer. Returns: String encoding of Message instance in protocol buffer format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() encoder = _Encoder() for field in sorted(message.all_fields(), key=lambda field: field.number): value = message.get_assigned_value(field.name) if value is not None: # Encode tag. tag = ((field.number << _WIRE_TYPE_BITS) | _VARIANT_TO_WIRE_TYPE[field.variant]) # Write value to wire. if field.repeated: values = value else: values = [value] for next in values: encoder.putVarInt32(tag) field_encoder = _VARIANT_TO_ENCODER_MAP[field.variant] field_encoder(encoder, next) return encoder.buffer().tostring() def decode_message(message_type, encoded_message): """Decode protocol buffer to Message instance. Args: message_type: Message type to decode data to. encoded_message: Encoded version of message as string. Returns: Decoded instance of message_type. Raises: DecodeError if an error occurs during decoding, such as incompatible wire format for a field. messages.ValidationError if merged message is not initialized. """ message = message_type() message_array = array.array('B') message_array.fromstring(encoded_message) try: decoder = _Decoder(message_array, 0, len(message_array)) while decoder.avail() > 0: # Decode tag and variant information. encoded_tag = decoder.getVarInt32() tag = encoded_tag >> _WIRE_TYPE_BITS wire_type = encoded_tag & _WIRE_TYPE_MASK try: found_wire_type_decoder = _WIRE_TYPE_TO_DECODER_MAP[wire_type] except: raise messages.DecodeError('No such wire type %d' % wire_type) if tag < 1: raise messages.DecodeError('Invalid tag value %d' % tag) try: field = message.field_by_number(tag) except KeyError: # Unexpected tags are ok, just ignored unless below 0. field = None wire_type_decoder = found_wire_type_decoder else: expected_wire_type = _VARIANT_TO_WIRE_TYPE[field.variant] if expected_wire_type != wire_type: raise messages.DecodeError('Expected wire type %s but found %s' % ( _WIRE_TYPE_NAME[expected_wire_type], _WIRE_TYPE_NAME[wire_type])) wire_type_decoder = _VARIANT_TO_DECODER_MAP[field.variant] value = wire_type_decoder(decoder) # Skip additional processing if unknown field. if not field: continue # Special case Enum and Message types. if isinstance(field, messages.EnumField): value = field.type(value) elif isinstance(field, messages.MessageField): nested_message = decode_message(field.type, value) value = nested_message # Merge value in to message. if field.repeated: values = getattr(message, field.name) if values is None: setattr(message, field.name, [value]) else: values.append(value) else: setattr(message, field.name, value) except ProtocolBuffer.ProtocolBufferDecodeError, err: raise messages.DecodeError('Decoding error: %s' % str(err)) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # indexes: - kind: Info properties: - name: class - name: encoded_name - kind: Info properties: - name: artist - name: class - name: encoded_name - name: name - kind: Info properties: - name: class - name: encoded_name - name: name # AUTOGENERATED # This index.yaml is automatically updated whenever the dev_appserver # detects that a new type of query is run. If you want to manage the # index.yaml file manually, remove the above marker line (the line # saying "# AUTOGENERATED"). If you want to manage some indexes # manually, move them above the marker line. The index.yaml file is # automatically uploaded to the admin console when you next deploy # your application using appcfg.py.
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for model.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from google.appengine.ext import db import datastore_test_util import model class InfoTest(datastore_test_util.DatastoreTest): """Test the info base class. This test uses the ArtistInfo sub-class, but the functionality defined there will work for all sub-classes. """ def testEncodedName(self): """Test the encoded_name derived property.""" def get_encoded_name(name): """Helper to get encoded name for an provided name. Args: name: Encoded name to convert to encoded_name. """ return db.get(model.ArtistInfo(name=name).put()).encoded_name # Normal strings. self.assertEquals('stereo total', get_encoded_name('Stereo Total')) # Not alphabetic characters. self.assertEquals('the go team', get_encoded_name('The Go! Team')) # Unecessary spaces. self.assertEquals('ananda shankar', get_encoded_name(' Ananda Shankar ')) # Non-ascii unicode. self.assertEquals('vive la f\xc3\xaate', get_encoded_name(u'Vive la f\xeate')) # Numerics. self.assertEquals('delta5', get_encoded_name(u'Delta5')) # The pesky '_'. self.assertEquals('wendy carlos', get_encoded_name('Wendy__Carlos')) def testSearch(self): """Test searching by name prefix.""" # Defined out of order to make sure search is in order. model.ArtistInfo(name='The Bee__Gees').put() model.ArtistInfo(name=' The-DooRs ').put() model.ArtistInfo(name='Wendy Carlos').put() model.ArtistInfo(name='Amadeus Mozart').put() model.ArtistInfo(name='The Beatles').put() names = [artist.name for artist in model.ArtistInfo.search(' ')] self.assertEquals(['Amadeus Mozart', 'The Beatles', 'The Bee__Gees', ' The-DooRs ', 'Wendy Carlos'], names) names = [artist.name for artist in model.ArtistInfo.search(' !tHe} ')] self.assertEquals(['The Beatles', 'The Bee__Gees', ' The-DooRs '], names) names = [artist.name for artist in model.ArtistInfo.search('the bee gees')] self.assertEquals(['The Bee__Gees'], names) names = [artist.name for artist in model.ArtistInfo.search('the doors')] self.assertEquals([' The-DooRs '], names) if __name__ == '__main__': unittest.main()
Python
def webapp_add_wsgi_middleware(app): """Configure additional middlewares for webapp. This function is called automatically by webapp.util.run_wsgi_app to give the opportunity for an application to register additional wsgi middleware components. See http://http://code.google.com/appengine/docs/python/tools/appstats.html for more information about configuring and running appstats. """ from google.appengine.ext.appstats import recording app = recording.appstats_wsgi_middleware(app) return app
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Web services demo. Implements a simple music database. The music database is served off the /music URL. It supports all built in protocols (url-encoded and protocol buffers) using the default RPC mapping scheme. For details about the Tunes service itself, please see tunes_db.py. For details about the datastore representation of the Tunes db, please see model.py. """ __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import service_handlers import tunes_db def main(): service_handlers.run_services( [('/music', tunes_db.MusicLibraryService), ]) if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # application: tunes-db version: 1 api_version: 1 runtime: python handlers: - url: /forms/(.*)\.(png|css|js) static_files: forms_static/\1.\2 upload: forms_static/(.*)\.(png|css|js) - url: /music.* script: services.py - url: /protorpc.* script: services.py - url: /_ah/stats/service.* script: protorpc_appstats/main.py - url: /_ah/stats.* script: $PYTHON_LIB/google/appengine/ext/appstats/ui.py - url: / script: main.py
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Internal Datastore model for the Tunes DB. The Tunes DB is a simple polymophic structure composed of polymorphic Info entities. Artists and Albums are represented. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import base64 import re from google.appengine.ext import db from google.appengine.ext.db import polymodel _SEARCH_NAME_REGEX = re.compile('\w+', re.UNICODE) def _normalize_name(name): """Helper used to convert a user entered name in to search compatible string. In order to make search as error free as possible, names of info records are converted to a simplified utf-8 encoded string that makes prefix searches easy. to make searching simpler, it removes all extra punctuation and spaces. Examples: _normalize_name('Duke Ellington') == 'duke ellington' _normalize_name(' Duke Ellington ') == 'duke ellington' _normalize_name('Duke-Ellington!') == 'duke ellington' _normalize_name('Duke_Ellington') == 'duke ellington' _normalize_name(u'Duk\xea Ellington') == 'Duk\xc3\xaa Ellington' Args: name: Name to convert to search string. Returns: Lower case, single space separated ByteString of name with punctuation removed. Unicode values are converted to UTF-8 encoded string. """ if name is None: return None elif isinstance(name, str): name = name.decode('utf-8') # Must explicitly replace '_' because the \w re part does not name = name.replace(u'_', u' ') names = _SEARCH_NAME_REGEX.findall(name) name = ' '.join(names) return db.ByteString(name.lower().encode('utf-8')) class Info(polymodel.PolyModel): """Base class for all Info records in Tunes DB. Properties: name: User friendly name for record. encoded_name: Derived from name to allow easy prefix searching. Name is transformed using _normalize_name. """ name = db.StringProperty() @db.ComputedProperty def encoded_name(self): return _normalize_name(self.name) @classmethod def search(cls, name_prefix=None): """Create search query based on info record name prefix. Args: name_prefix: User input name-prefix to search for. If name_prefix is empty string or None returns all records of Info sub-class. Records are sorted by their encoded name. Returns: Datastore query pointing to search results. """ name_prefix = _normalize_name(name_prefix) query = cls.all().order('encoded_name') if name_prefix: query.filter('encoded_name >=', db.ByteString(name_prefix)) # Do not need to worry about name_prefix + '\xff\xff' because not # a unicode character. query.filter('encoded_name <=', db.ByteString(name_prefix + '\xff')) return query class ArtistInfo(Info): """Musician or music group responsible for recording. Properties: album_count: Number of albums produced by artist. albums: Implicit collection of albums produced by artist. """ album_count = db.IntegerProperty(default=0) class AlbumInfo(Info): """Album produced by a musician or music group. Properties: artist: Artist that produced album. released: Year that album was released. """ artist = db.ReferenceProperty(ArtistInfo, collection_name='albums', required=True) released = db.IntegerProperty()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from google.appengine.ext import webapp from google.appengine.ext.webapp import util from protorpc import messages from protorpc import service_handlers from protorpc import remote class HelloRequest(messages.Message): my_name = messages.StringField(1, required=True) class HelloResponse(messages.Message): hello = messages.StringField(1, required=True) class HelloService(remote.Service): @remote.method(HelloRequest, HelloResponse) def hello(self, request): return HelloResponse(hello='Hello there, %s!' % request.my_name) service_mappings = service_handlers.service_mapping( [('/hello', HelloService), ]) application = webapp.WSGIApplication(service_mappings) def main(): util.run_wsgi_app(application) if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Simple protocol message types.""" __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import messages class VoidMessage(messages.Message): """Empty message."""
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.util.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import random import unittest from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = util class UtilTest(test_util.TestCase): def testDecoratedFunction_LengthZero(self): @util.positional(0) def fn(kwonly=1): return [kwonly] self.assertEquals([1], fn()) self.assertEquals([2], fn(kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 0 positional ' r'arguments \(1 given\)', fn, 1) def testDecoratedFunction_LengthOne(self): @util.positional(1) def fn(pos, kwonly=1): return [pos, kwonly] self.assertEquals([1, 1], fn(1)) self.assertEquals([2, 2], fn(2, kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 1 positional ' r'argument \(2 given\)', fn, 2, 3) def testDecoratedFunction_LengthTwoWithDefault(self): @util.positional(2) def fn(pos1, pos2=1, kwonly=1): return [pos1, pos2, kwonly] self.assertEquals([1, 1, 1], fn(1)) self.assertEquals([2, 2, 1], fn(2, 2)) self.assertEquals([2, 3, 4], fn(2, 3, kwonly=4)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 2 positional ' r'arguments \(3 given\)', fn, 2, 3, 4) def testDecoratedMethod(self): class MyClass(object): @util.positional(2) def meth(self, pos1, kwonly=1): return [pos1, kwonly] self.assertEquals([1, 1], MyClass().meth(1)) self.assertEquals([2, 2], MyClass().meth(2, kwonly=2)) self.assertRaisesWithRegexpMatch(TypeError, r'meth\(\) takes at most 2 positional ' r'arguments \(3 given\)', MyClass().meth, 2, 3) def testDefaultDecoration(self): @util.positional def fn(a, b, c=None): return a, b, c self.assertEquals((1, 2, 3), fn(1, 2, c=3)) self.assertEquals((3, 4, None), fn(3, b=4)) self.assertRaisesWithRegexpMatch(TypeError, r'fn\(\) takes at most 2 positional ' r'arguments \(3 given\)', fn, 2, 3, 4) class AcceptItemTest(test_util.TestCase): def CheckAttributes(self, item, main_type, sub_type, q=1, values={}, index=1): self.assertEquals(index, item.index) self.assertEquals(main_type, item.main_type) self.assertEquals(sub_type, item.sub_type) self.assertEquals(q, item.q) self.assertEquals(values, item.values) def testParse(self): self.CheckAttributes(util.AcceptItem('*/*', 1), None, None) self.CheckAttributes(util.AcceptItem('text/*', 1), 'text', None) self.CheckAttributes(util.AcceptItem('text/plain', 1), 'text', 'plain') self.CheckAttributes( util.AcceptItem('text/plain; q=0.3', 1), 'text', 'plain', 0.3, values={'q': '0.3'}) self.CheckAttributes( util.AcceptItem('text/plain; level=2', 1), 'text', 'plain', values={'level': '2'}) self.CheckAttributes( util.AcceptItem('text/plain', 10), 'text', 'plain', index=10) def testCaseInsensitive(self): self.CheckAttributes(util.AcceptItem('Text/Plain', 1), 'text', 'plain') def testBadValue(self): self.assertRaises(util.AcceptError, util.AcceptItem, 'bad value', 1) self.assertRaises(util.AcceptError, util.AcceptItem, 'bad value/', 1) self.assertRaises(util.AcceptError, util.AcceptItem, '/bad value', 1) def testSortKey(self): item = util.AcceptItem('main/sub; q=0.2; level=3', 11) self.assertEquals((False, False, -0.2, False, 11), item.sort_key) item = util.AcceptItem('main/*', 12) self.assertEquals((False, True, -1, True, 12), item.sort_key) item = util.AcceptItem('*/*', 1) self.assertEquals((True, True, -1, True, 1), item.sort_key) def testSort(self): i1 = util.AcceptItem('text/*', 1) i2 = util.AcceptItem('text/html', 2) i3 = util.AcceptItem('text/html; q=0.9', 3) i4 = util.AcceptItem('text/html; q=0.3', 4) i5 = util.AcceptItem('text/xml', 5) i6 = util.AcceptItem('text/html; level=1', 6) i7 = util.AcceptItem('*/*', 7) items = [i1, i2 ,i3 ,i4 ,i5 ,i6, i7] random.shuffle(items) self.assertEquals([i6, i2, i5, i3, i4, i1, i7], sorted(items)) def testMatchAll(self): item = util.AcceptItem('*/*', 1) self.assertTrue(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertTrue(item.match('image/png')) self.assertTrue(item.match('image/png; q=0.3')) def testMatchMainType(self): item = util.AcceptItem('text/*', 1) self.assertTrue(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertFalse(item.match('image/png')) self.assertFalse(item.match('image/png; q=0.3')) def testMatchFullType(self): item = util.AcceptItem('text/plain', 1) self.assertFalse(item.match('text/html')) self.assertTrue(item.match('text/plain; level=1')) self.assertFalse(item.match('image/png')) self.assertFalse(item.match('image/png; q=0.3')) def testMatchCaseInsensitive(self): item = util.AcceptItem('text/plain', 1) self.assertTrue(item.match('tExt/pLain')) def testStr(self): self.assertEquals('*/*', str(util.AcceptItem('*/*', 1))) self.assertEquals('text/*', str(util.AcceptItem('text/*', 1))) self.assertEquals('text/plain', str(util.AcceptItem('text/plain', 1))) self.assertEquals('text/plain; q=0.2', str(util.AcceptItem('text/plain; q=0.2', 1))) self.assertEquals('text/plain; q=0.2; level=1', str(util.AcceptItem('text/plain; level=1; q=0.2', 1))) def testRepr(self): self.assertEquals("AcceptItem('*/*', 1)", repr(util.AcceptItem('*/*', 1))) self.assertEquals("AcceptItem('text/plain', 11)", repr(util.AcceptItem('text/plain', 11))) def testValues(self): item = util.AcceptItem('text/plain; a=1; b=2; c=3;', 1) values = item.values self.assertEquals(dict(a="1", b="2", c="3"), values) values['a'] = "7" self.assertNotEquals(values, item.values) class ParseAcceptHeaderTest(test_util.TestCase): def testIndex(self): accept_header = """text/*, text/html, text/html; q=0.9, text/xml, text/html; level=1, */*""" accepts = util.parse_accept_header(accept_header) self.assertEquals(6, len(accepts)) self.assertEquals([4, 1, 3, 2, 0, 5], [a.index for a in accepts]) class ChooseContentTypeTest(test_util.TestCase): def testIgnoreUnrequested(self): self.assertEquals('application/json', util.choose_content_type( 'text/plain, application/json, */*', ['application/X-Google-protobuf', 'application/json' ])) def testUseCorrectPreferenceIndex(self): self.assertEquals('application/json', util.choose_content_type( '*/*, text/plain, application/json', ['application/X-Google-protobuf', 'application/json' ])) def testPreferFirstInList(self): self.assertEquals('application/X-Google-protobuf', util.choose_content_type( '*/*', ['application/X-Google-protobuf', 'application/json' ])) def testCaseInsensitive(self): self.assertEquals('application/X-Google-protobuf', util.choose_content_type( 'application/x-google-protobuf', ['application/X-Google-protobuf', 'application/json' ])) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Transport library for ProtoRPC. Contains underlying infrastructure used for communicating RPCs over low level transports such as HTTP. Includes HTTP transport built over urllib2. """ import logging import sys import urllib2 from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import util __all__ = [ 'RpcStateError', 'HttpTransport', 'Rpc', 'Transport', ] class RpcStateError(messages.Error): """Raised when trying to put RPC in to an invalid state.""" class Rpc(object): """Represents a client side RPC. An RPC is created by the transport class and is used with a single RPC. While an RPC is still in process, the response is set to None. When it is complete the response will contain the response message. """ def __init__(self, request): """Constructor. Args: request: Request associated with this RPC. """ self.__request = request self.__response = None self.__state = remote.RpcState.RUNNING self.__error_message = None self.__error_name = None @property def request(self): """Request associated with RPC.""" return self.__request @property def response(self): """Response associated with RPC.""" return self.__response @property def state(self): return self.__state @property def error_message(self): return self.__error_message @property def error_name(self): return self.__error_name def __set_state(self, state, error_message=None, error_name=None): if self.__state != remote.RpcState.RUNNING: raise RpcStateError( 'RPC must be in RUNNING state to change to %s' % state) if state == remote.RpcState.RUNNING: raise RpcStateError('RPC is already in RUNNING state') self.__state = state self.__error_message = error_message self.__error_name = error_name def set_response(self, response): # TODO: Even more specific type checking. if not isinstance(response, messages.Message): raise TypeError('Expected Message type, received %r' % (response)) self.__response = response self.__set_state(remote.RpcState.OK) def set_status(self, status): status.check_initialized() self.__set_state(status.state, status.error_message, status.error_name) class Transport(object): """Transport base class. Provides basic support for implementing a ProtoRPC transport such as one that can send and receive messages over HTTP. Implementations override _transport_rpc. This method receives an encoded response as determined by the transports configured protocol. The transport is expected to set the rpc response or raise an exception before termination. Asynchronous transports are not supported. """ @util.positional(1) def __init__(self, protocol=protobuf): """Constructor. Args: protocol: The protocol implementation. Must implement encode_message and decode_message. """ self.__protocol = protocol @property def protocol(self): """Protocol associated with this transport.""" return self.__protocol def send_rpc(self, remote_info, request): """Initiate sending an RPC over the transport. Args: remote_info: RemoteInfo instance describing remote method. request: Request message to send to service. Returns: An Rpc instance intialized with request and response. """ request.check_initialized() encoded_request = self.__protocol.encode_message(request) rpc = Rpc(request) self._transport_rpc(remote_info, encoded_request, rpc) return rpc def _transport_rpc(self, remote_info, encoded_request, rpc): """Transport RPC method. Args: remote_info: RemoteInfo instance describing remote method. encoded_request: Request message as encoded by transport protocol. rpc: Rpc instance associated with a single request. """ raise NotImplementedError() class HttpTransport(Transport): """Transport for communicating with HTTP servers.""" @util.positional(2) def __init__(self, service_url, protocol=protobuf): """Constructor. Args: service_url: URL where the service is located. All communication via the transport will go to this URL. protocol: The protocol implementation. Must implement encode_message and decode_message. """ super(HttpTransport, self).__init__(protocol=protocol) self.__service_url = service_url def __http_error_to_exception(self, http_error): error_code = http_error.code content_type = http_error.hdrs.get('content-type') if error_code == 500 and content_type == self.protocol.CONTENT_TYPE: try: rpc_status = self.protocol.decode_message(remote.RpcStatus, http_error.msg) except Exception, decode_err: logging.warning( 'An error occurred trying to parse status: %s\n%s', str(decode_err), http_error.msg) else: # TODO: Move the check_rpc_status to the Rpc.response property. # Will raise exception if rpc_status is in an error state. remote.check_rpc_status(rpc_status) def _transport_rpc(self, remote_info, encoded_request, rpc): """HTTP transport rpc method. Uses urllib2 as underlying HTTP transport. """ method_url = '%s.%s' % (self.__service_url, remote_info.method.func_name) http_request = urllib2.Request(method_url, encoded_request) http_request.add_header('content-type', self.protocol.CONTENT_TYPE) try: http_response = urllib2.urlopen(http_request) except urllib2.HTTPError, err: self.__http_error_to_exception(err) # TODO: Map other types of errors to appropriate exceptions. _, _, trace_back = sys.exc_info() raise remote.ServerError, (str(err), err), trace_back except urllib2.URLError, err: _, _, trace_back = sys.exc_info() if isinstance(err, basestring): error_message = err else: error_message = err.args[0] raise remote.NetworkError, (error_message, err), trace_back encoded_response = http_response.read() response = self.protocol.decode_message(remote_info.response_type, encoded_response) rpc.set_response(response)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # __author__ = 'rafek@google.com (Rafe Kaplan)' import contextlib from protorpc import messages from protorpc import util __all__ = ['IndentationError', 'IndentWriter', ] class IndentationError(messages.Error): """Raised when end_indent is called too many times.""" class IndentWriter(object): """Utility class to make it easy to write formatted indented text. IndentWriter delegates to a file-like object and is able to keep track of the level of indentation. Each call to write_line will write a line terminated by a new line proceeded by a number of spaces indicated by the current level of indentation. IndexWriter overloads the << operator to make line writing operations clearer. The indent method returns a context manager that can be used by the Python with statement that makes generating python code easier to use. For example: index_writer << 'def factorial(n):' with index_writer.indent(): index_writer << 'if n <= 1:' with index_writer.indent(): index_writer << 'return 1' index_writer << 'else:' with index_writer.indent(): index_writer << 'return factorial(n - 1)' This would generate: def factorial(n): if n <= 1: return 1 else: return factorial(n - 1) """ @util.positional(2) def __init__(self, output, indent_space=2): """Constructor. Args: output: File-like object to wrap. indent_space: Number of spaces each level of indentation will be. """ # Private attributes: # # __output: The wrapped file-like object. # __indent_space: String to append for each level of indentation. # __indentation: The current full indentation string. self.__output = output self.__indent_space = indent_space * ' ' self.__indentation = 0 @property def indent_level(self): """Current level of indentation for IndentWriter.""" return self.__indentation def write_line(self, line): """Write line to wrapped file-like object using correct indentation. The line is written with the current level of indentation printed before it and terminated by a new line. Args: line: Line to write to wrapped file-like object. """ self.__output.write(self.__indentation * self.__indent_space) self.__output.write(line) self.__output.write('\n') def begin_indent(self): """Begin a level of indentation.""" self.__indentation += 1 def end_indent(self): """Undo the most recent level of indentation. Raises: IndentationError when called with no indentation levels. """ if not self.__indentation: raise IndentationError('Unable to un-indent further') self.__indentation -= 1 @contextlib.contextmanager def indent(self): """Create indentation level compatible with the Python 'with' keyword.""" self.begin_indent() yield self.end_indent() def __lshift__(self, line): """Syntactic sugar for write_line method. Args: line: Line to write to wrapped file=like object. """ self.write_line(line)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """URL encoding support for messages types. Protocol support for URL encoded form parameters. Nested Fields: Nested fields are repesented by dot separated names. For example, consider the following messages: class WebPage(Message): title = StringField(1) tags = StringField(2, repeated=True) class WebSite(Message): name = StringField(1) home = MessageField(WebPage, 2) pages = MessageField(WebPage, 3, repeated=True) And consider the object: page = WebPage() page.title = 'Welcome to NewSite 2010' site = WebSite() site.name = 'NewSite 2010' site.home = page The URL encoded representation of this constellation of objects is. name=NewSite+2010&home.title=Welcome+to+NewSite+2010 An object that exists but does not have any state can be represented with a reference to its name alone with no value assigned to it. For example: page = WebSite() page.name = 'My Empty Site' page.home = WebPage() is represented as: name=My+Empty+Site&home= This represents a site with an empty uninitialized home page. Repeated Fields: Repeated fields are represented by the name of and the index of each value separated by a dash. For example, consider the following message: home = Page() home.title = 'Nome' news = Page() news.title = 'News' news.tags = ['news', 'articles'] instance = WebSite() instance.name = 'Super fun site' instance.pages = [home, news, preferences] An instance of this message can be represented as: name=Super+fun+site&page-0.title=Home&pages-1.title=News&... pages-1.tags-0=new&pages-1.tags-1=articles Helper classes: URLEncodedRequestBuilder: Used for encapsulating the logic used for building a request message from a URL encoded RPC. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import re import urllib from protorpc import messages from protorpc import util __all__ = ['CONTENT_TYPE', 'URLEncodedRequestBuilder', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/x-www-form-urlencoded' _FIELD_NAME_REGEX = re.compile(r'^([a-zA-Z_][a-zA-Z_0-9]*)(?:-([0-9]+))?$') class URLEncodedRequestBuilder(object): """Helper that encapsulates the logic used for building URL encoded messages. This helper is used to map query parameters from a URL encoded RPC to a message instance. """ @util.positional(2) def __init__(self, message, prefix=''): """Constructor. Args: message: Message instance to build from parameters. prefix: Prefix expected at the start of valid parameters. """ self.__parameter_prefix = prefix # The empty tuple indicates the root message, which has no path. # __messages is a full cache that makes it very easy to look up message # instances by their paths. See make_path for details about what a path # is. self.__messages = {(): message} # This is a cache that stores paths which have been checked for # correctness. Correctness means that an index is present for repeated # fields on the path and absent for non-repeated fields. The cache is # also used to check that indexes are added in the right order so that # dicontiguous ranges of indexes are ignored. self.__checked_indexes = set([()]) def make_path(self, parameter_name): """Parse a parameter name and build a full path to a message value. The path of a method is a tuple of 2-tuples describing the names and indexes within repeated fields from the root message (the message being constructed by the builder) to an arbitrarily nested message within it. Each 2-tuple node of a path (name, index) is: name: The name of the field that refers to the message instance. index: The index within a repeated field that refers to the message instance, None if not a repeated field. For example, consider: class VeryInner(messages.Message): ... class Inner(messages.Message): very_inner = messages.MessageField(VeryInner, 1, repeated=True) class Outer(messages.Message): inner = messages.MessageField(Inner, 1) If this builder is building an instance of Outer, that instance is referred to in the URL encoded parameters without a path. Therefore its path is (). The child 'inner' is referred to by its path (('inner', None)). The first child of repeated field 'very_inner' on the Inner instance is referred to by (('inner', None), ('very_inner', 0)). Examples: # Correct reference to model where nation is a Message, district is # repeated Message and county is any not repeated field type. >>> make_path('nation.district-2.county') (('nation', None), ('district', 2), ('county', None)) # Field is not part of model. >>> make_path('nation.made_up_field') None # nation field is not repeated and index provided. >>> make_path('nation-1') None # district field is repeated and no index provided. >>> make_path('nation.district') None Args: parameter_name: Name of query parameter as passed in from the request. in order to make a path, this parameter_name must point to a valid field within the message structure. Nodes of the path that refer to repeated fields must be indexed with a number, non repeated nodes must not have an index. Returns: Parsed version of the parameter_name as a tuple of tuples: attribute: Name of attribute associated with path. index: Postitive integer index when it is a repeated field, else None. Will return None if the parameter_name does not have the right prefix, does not point to a field within the message structure, does not have an index if it is a repeated field or has an index but is not a repeated field. """ if parameter_name.startswith(self.__parameter_prefix): parameter_name = parameter_name[len(self.__parameter_prefix):] else: return None path = [] name = [] message_type = type(self.__messages[()]) # Get root message. for item in parameter_name.split('.'): # This will catch sub_message.real_message_field.not_real_field if not message_type: return None item_match = _FIELD_NAME_REGEX.match(item) if not item_match: return None attribute = item_match.group(1) index = item_match.group(2) if index: index = int(index) try: field = message_type.field_by_name(attribute) except KeyError: return None if field.repeated != (index is not None): return None if isinstance(field, messages.MessageField): message_type = field.type else: message_type = None # Path is valid so far. Append node and continue. path.append((attribute, index)) return tuple(path) def __check_index(self, parent_path, name, index): """Check correct index use and value relative to a given path. Check that for a given path the index is present for repeated fields and that it is in range for the existing list that it will be inserted in to or appended to. Args: parent_path: Path to check against name and index. name: Name of field to check for existance. index: Index to check. If field is repeated, should be a number within range of the length of the field, or point to the next item for appending. """ # Don't worry about non-repeated fields. # It's also ok if index is 0 because that means next insert will append. if not index: return True parent = self.__messages.get(parent_path, None) value_list = getattr(parent, name, None) # If the list does not exist then the index should be 0. Since it is # not, path is not valid. if not value_list: return False # The index must either point to an element of the list or to the tail. return len(value_list) >= index def __check_indexes(self, path): """Check that all indexes are valid and in the right order. This method must iterate over the path and check that all references to indexes point to an existing message or to the end of the list, meaning the next value should be appended to the repeated field. Args: path: Path to check indexes for. Tuple of 2-tuples (name, index). See make_path for more information. Returns: True if all the indexes of the path are within range, else False. """ if path in self.__checked_indexes: return True # Start with the root message. parent_path = () for name, index in path: next_path = parent_path + ((name, index),) # First look in the checked indexes cache. if next_path not in self.__checked_indexes: if not self.__check_index(parent_path, name, index): return False self.__checked_indexes.add(next_path) parent_path = next_path return True def __get_or_create_path(self, path): """Get a message from the messages cache or create it and add it. This method will also create any parent messages based on the path. When a new instance of a given message is created, it is stored in __message by its path. Args: path: Path of message to get. Path must be valid, in other words __check_index(path) returns true. Tuple of 2-tuples (name, index). See make_path for more information. Returns: Message instance if the field being pointed to by the path is a message, else will return None for non-message fields. """ message = self.__messages.get(path, None) if message: return message parent_path = () parent = self.__messages[()] # Get the root object for name, index in path: field = parent.field_by_name(name) next_path = parent_path + ((name, index),) next_message = self.__messages.get(next_path, None) if next_message is None: message_type = field.type next_message = message_type() self.__messages[next_path] = next_message if not field.repeated: setattr(parent, field.name, next_message) else: list_value = getattr(parent, field.name, None) if list_value is None: setattr(parent, field.name, [next_message]) else: list_value.append(next_message) parent_path = next_path parent = next_message return parent def add_parameter(self, parameter, values): """Add a single parameter. Adds a single parameter and its value to the request message. Args: parameter: Query string parameter to map to request. values: List of values to assign to request message. Returns: True if parameter was valid and added to the message, else False. Raises: DecodeError if the parameter refers to a valid field, and the values parameter does not have one and only one value. Non-valid query parameters may have multiple values and should not cause an error. """ path = self.make_path(parameter) if not path: return False # Must check that all indexes of all items in the path are correct before # instantiating any of them. For example, consider: # # class Repeated(object): # ... # # class Inner(object): # # repeated = messages.MessageField(Repeated, 1, repeated=True) # # class Outer(object): # # inner = messages.MessageField(Inner, 1) # # instance = Outer() # builder = URLEncodedRequestBuilder(instance) # builder.add_parameter('inner.repeated') # # assert not hasattr(instance, 'inner') # # The check is done relative to the instance of Outer pass in to the # constructor of the builder. This instance is not referred to at all # because all names are assumed to be relative to it. # # The 'repeated' part of the path is not correct because it is missing an # index. Because it is missing an index, it should not create an instance # of Repeated. In this case add_parameter will return False and have no # side effects. # # A correct path that would cause a new Inner instance to be inserted at # instance.inner and a new Repeated instance to be appended to the # instance.inner.repeated list would be 'inner.repeated-0'. if not self.__check_indexes(path): return False # Ok to build objects. parent_path = path[:-1] parent = self.__get_or_create_path(parent_path) name, index = path[-1] field = parent.field_by_name(name) if len(values) != 1: raise messages.DecodeError( 'Found repeated values for field %s.' % field.name) value = values[0] if isinstance(field, messages.IntegerField): converted_value = int(value) elif isinstance(field, messages.MessageField): # Just make sure it's instantiated. Assignment to field or # appending to list is done in __get_or_create_path. self.__get_or_create_path(path) return True elif isinstance(field, messages.StringField): converted_value = value.decode('utf-8') elif isinstance(field, messages.BooleanField): converted_value = value.lower() == 'true' and True or False else: converted_value = field.type(value) if field.repeated: value_list = getattr(parent, field.name, None) if value_list is None: setattr(parent, field.name, [converted_value]) else: if index == len(value_list): value_list.append(converted_value) else: # Index should never be above len(value_list) because it was # verified during the index check above. value_list[index] = converted_value else: setattr(parent, field.name, converted_value) return True @util.positional(1) def encode_message(message, prefix=''): """Encode Message instance to url-encoded string. Args: message: Message instance to encode in to url-encoded string. prefix: Prefix to append to field names of contained values. Returns: String encoding of Message in URL encoded format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() parameters = [] def build_message(parent, prefix): """Recursively build parameter list for URL response. Args: parent: Message to build parameters for. prefix: Prefix to append to field names of contained values. Returns: True if some value of parent was added to the parameters list, else False, meaning the object contained no values. """ has_any_values = False for field in sorted(parent.all_fields(), key=lambda f: f.number): next_value = parent.get_assigned_value(field.name) if next_value is None: continue # Found a value. Ultimate return value should be True. has_any_values = True # Normalize all values in to a list. if not field.repeated: next_value = [next_value] for index, item in enumerate(next_value): # Create a name with an index if it is a repeated field. if field.repeated: field_name = '%s%s-%s' % (prefix, field.name, index) else: field_name = prefix + field.name if isinstance(field, messages.MessageField): # Message fields must be recursed in to in order to construct # their component parameter values. if not build_message(item, field_name + '.'): # The nested message is empty. Append an empty value to # represent it. parameters.append((field_name, '')) elif isinstance(field, messages.BooleanField): parameters.append((field_name, item and 'true' or 'false')) else: if isinstance(item, unicode): item = item.encode('utf-8') parameters.append((field_name, str(item))) return has_any_values build_message(message, prefix) return urllib.urlencode(parameters) def decode_message(message_type, encoded_message, **kwargs): """Decode urlencoded content to message. Args: message_type: Message instance to merge URL encoded content into. encoded_message: URL encoded message. prefix: Prefix to append to field names of contained values. Returns: Decoded instance of message_type. """ message = message_type() builder = URLEncodedRequestBuilder(message, **kwargs) arguments = cgi.parse_qs(encoded_message, keep_blank_values=True) for argument, values in sorted(arguments.iteritems()): builder.add_parameter(argument, values) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Services descriptor definitions. Contains message definitions and functions for converting service classes into transmittable message format. Describing an Enum instance, Enum class, Field class or Message class will generate an appropriate descriptor object that describes that class. This message can itself be used to transmit information to clients wishing to know the description of an enum value, enum, field or message without needing to download the source code. This format is also compatible with other, non-Python languages. The descriptors are modeled to be binary compatible with: http://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto NOTE: The names of types and fields are not always the same between these descriptors and the ones defined in descriptor.proto. This was done in order to make source code files that use these descriptors easier to read. For example, it is not necessary to prefix TYPE to all the values in FieldDescriptor.Variant as is done in descriptor.proto FieldDescriptorProto.Type. Example: class Pixel(messages.Message): x = messages.IntegerField(1, required=True) y = messages.IntegerField(2, required=True) color = messages.BytesField(3) # Describe Pixel class using message descriptor. fields = [] field = FieldDescriptor() field.name = 'x' field.number = 1 field.label = FieldDescriptor.Label.REQUIRED field.variant = FieldDescriptor.Variant.INT64 field = FieldDescriptor() field.name = 'y' field.number = 2 field.label = FieldDescriptor.Label.REQUIRED field.variant = FieldDescriptor.Variant.INT64 field = FieldDescriptor() field.name = 'color' field.number = 3 field.label = FieldDescriptor.Label.OPTIONAL field.variant = FieldDescriptor.Variant.BYTES message = MessageDescriptor() message.name = 'Pixel' message.fields = fields # Describing is the equivalent of building the above message. message == describe_message(Pixel) Public Classes: EnumValueDescriptor: Describes Enum values. EnumDescriptor: Describes Enum classes. FieldDescriptor: Describes field instances. FileDescriptor: Describes a single 'file' unit. FileSet: Describes a collection of file descriptors. MessageDescriptor: Describes Message classes. MethodDescriptor: Describes a method of a service. ServiceDescriptor: Describes a services. Public Functions: describe_enum_value: Describe an individual enum-value. describe_enum: Describe an Enum class. describe_field: Describe a Field definition. describe_file: Describe a 'file' unit from a Python module or object. describe_file_set: Describe a file set from a list of modules or objects. describe_message: Describe a Message definition. describe_method: Describe a Method definition. describe_service: Describe a Service definition. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import codecs import types from protorpc import messages from protorpc import util __all__ = ['EnumDescriptor', 'EnumValueDescriptor', 'FieldDescriptor', 'MessageDescriptor', 'MethodDescriptor', 'FileDescriptor', 'FileSet', 'ServiceDescriptor', 'DescriptorLibrary', 'describe_enum', 'describe_enum_value', 'describe_field', 'describe_message', 'describe_method', 'describe_file', 'describe_file_set', 'describe_service', 'describe', 'import_descriptor_loader', ] # NOTE: MessageField is missing because message fields cannot have # a default value at this time. # TODO(rafek): Support default message values. # # Map to functions that convert default values of fields of a given type # to a string. The function must return a value that is compatible with # FieldDescriptor.default_value and therefore a unicode string. _DEFAULT_TO_STRING_MAP = { messages.IntegerField: unicode, messages.FloatField: unicode, messages.BooleanField: lambda value: value and u'true' or u'false', messages.BytesField: lambda value: codecs.escape_encode(value)[0], messages.StringField: lambda value: value, messages.EnumField: lambda value: unicode(value.number), } class EnumValueDescriptor(messages.Message): """Enum value descriptor. Fields: name: Name of enumeration value. number: Number of enumeration value. """ # TODO(rafek): Why are these listed as optional in descriptor.proto. # Harmonize? name = messages.StringField(1, required=True) number = messages.IntegerField(2, required=True, variant=messages.Variant.INT32) class EnumDescriptor(messages.Message): """Enum class descriptor. Fields: name: Name of Enum without any qualification. values: Values defined by Enum class. """ name = messages.StringField(1) values = messages.MessageField(EnumValueDescriptor, 2, repeated=True) class FieldDescriptor(messages.Message): """Field definition descriptor. Enums: Variant: Wire format hint sub-types for field. Label: Values for optional, required and repeated fields. Fields: name: Name of field. number: Number of field. variant: Variant of field. type_name: Type name for message and enum fields. default_value: String representation of default value. """ Variant = messages.Variant class Label(messages.Enum): """Field label.""" OPTIONAL = 1 REQUIRED = 2 REPEATED = 3 name = messages.StringField(1, required=True) number = messages.IntegerField(3, required=True, variant=messages.Variant.INT32) label = messages.EnumField(Label, 4, default=Label.OPTIONAL) variant = messages.EnumField(Variant, 5) type_name = messages.StringField(6) # For numeric types, contains the original text representation of the value. # For booleans, "true" or "false". # For strings, contains the default text contents (not escaped in any way). # For bytes, contains the C escaped value. All bytes < 128 are that are # traditionally considered unprintable are also escaped. default_value = messages.StringField(7) class MessageDescriptor(messages.Message): """Message definition descriptor. Fields: name: Name of Message without any qualification. fields: Fields defined for message. message_types: Nested Message classes defined on message. enum_types: Nested Enum classes defined on message. """ name = messages.StringField(1) fields = messages.MessageField(FieldDescriptor, 2, repeated=True) message_types = messages.MessageField( 'protorpc.descriptor.MessageDescriptor', 3, repeated=True) enum_types = messages.MessageField(EnumDescriptor, 4, repeated=True) class MethodDescriptor(messages.Message): """Service method definition descriptor. Fields: name: Name of service method. request_type: Fully qualified or relative name of request message type. response_type: Fully qualified or relative name of response message type. """ name = messages.StringField(1) request_type = messages.StringField(2) response_type = messages.StringField(3) class ServiceDescriptor(messages.Message): """Service definition descriptor. Fields: name: Name of Service without any qualification. methods: Remote methods of Service. """ name = messages.StringField(1) methods = messages.MessageField(MethodDescriptor, 2, repeated=True) class FileDescriptor(messages.Message): """Description of file containing protobuf definitions. Fields: package: Fully qualified name of package that definitions belong to. message_types: Message definitions contained in file. enum_types: Enum definitions contained in file. service_types: Service definitions contained in file. """ package = messages.StringField(2) # TODO(rafek): Add dependency field message_types = messages.MessageField(MessageDescriptor, 4, repeated=True) enum_types = messages.MessageField(EnumDescriptor, 5, repeated=True) service_types = messages.MessageField(ServiceDescriptor, 6, repeated=True) class FileSet(messages.Message): """A collection of FileDescriptors. Fields: files: Files in file-set. """ files = messages.MessageField(FileDescriptor, 1, repeated=True) def describe_enum_value(enum_value): """Build descriptor for Enum instance. Args: enum_value: Enum value to provide descriptor for. Returns: Initialized EnumValueDescriptor instance describing the Enum instance. """ enum_value_descriptor = EnumValueDescriptor() enum_value_descriptor.name = unicode(enum_value.name) enum_value_descriptor.number = enum_value.number return enum_value_descriptor def describe_enum(enum_definition): """Build descriptor for Enum class. Args: enum_definition: Enum class to provide descriptor for. Returns: Initialized EnumDescriptor instance describing the Enum class. """ enum_descriptor = EnumDescriptor() enum_descriptor.name = enum_definition.definition_name().split('.')[-1] values = [] for number in enum_definition.numbers(): value = enum_definition.lookup_by_number(number) values.append(describe_enum_value(value)) if values: enum_descriptor.values = values return enum_descriptor def describe_field(field_definition): """Build descriptor for Field instance. Args: field_definition: Field instance to provide descriptor for. Returns: Initialized FieldDescriptor instance describing the Field instance. """ field_descriptor = FieldDescriptor() field_descriptor.name = field_definition.name field_descriptor.number = field_definition.number field_descriptor.variant = field_definition.variant if isinstance(field_definition, (messages.EnumField, messages.MessageField)): field_descriptor.type_name = field_definition.type.definition_name() if field_definition.default is not None: field_descriptor.default_value = _DEFAULT_TO_STRING_MAP[ type(field_definition)](field_definition.default) # Set label. if field_definition.repeated: field_descriptor.label = FieldDescriptor.Label.REPEATED elif field_definition.required: field_descriptor.label = FieldDescriptor.Label.REQUIRED else: field_descriptor.label = FieldDescriptor.Label.OPTIONAL return field_descriptor def describe_message(message_definition): """Build descriptor for Message class. Args: message_definition: Message class to provide descriptor for. Returns: Initialized MessageDescriptor instance describing the Message class. """ message_descriptor = MessageDescriptor() message_descriptor.name = message_definition.definition_name().split('.')[-1] fields = sorted(message_definition.all_fields(), key=lambda v: v.number) if fields: message_descriptor.fields = [describe_field(field) for field in fields] try: nested_messages = message_definition.__messages__ except AttributeError: pass else: message_descriptors = [] for name in nested_messages: value = getattr(message_definition, name) message_descriptors.append(describe_message(value)) message_descriptor.message_types = message_descriptors try: nested_enums = message_definition.__enums__ except AttributeError: pass else: enum_descriptors = [] for name in nested_enums: value = getattr(message_definition, name) enum_descriptors.append(describe_enum(value)) message_descriptor.enum_types = enum_descriptors return message_descriptor def describe_method(method): """Build descriptor for service method. Args: method: Remote service method to describe. Returns: Initialized MethodDescriptor instance describing the service method. """ method_info = method.remote descriptor = MethodDescriptor() descriptor.name = method_info.method.func_name descriptor.request_type = method_info.request_type.definition_name() descriptor.response_type = method_info.response_type.definition_name() return descriptor def describe_service(service_class): """Build descriptor for service. Args: service_class: Service class to describe. Returns: Initialized ServiceDescriptor instance describing the service. """ descriptor = ServiceDescriptor() descriptor.name = service_class.__name__ methods = [] remote_methods = service_class.all_remote_methods() for name in sorted(remote_methods.iterkeys()): if name == 'get_descriptor': continue method = remote_methods[name] methods.append(describe_method(method)) if methods: descriptor.methods = methods return descriptor def describe_file(module): """Build a file from a specified Python module. Args: module: Python module to describe. Returns: Initialized FileDescriptor instance describing the module. """ # May not import remote at top of file because remote depends on this # file # TODO(rafek): Straighten out this dependency. Possibly move these functions # from descriptor to their own module. from protorpc import remote descriptor = FileDescriptor() try: descriptor.package = module.package except AttributeError: descriptor.package = module.__name__ if not descriptor.package: descriptor.package = None message_descriptors = [] enum_descriptors = [] service_descriptors = [] # Need to iterate over all top level attributes of the module looking for # message, enum and service definitions. Each definition must be itself # described. for name in sorted(dir(module)): value = getattr(module, name) if isinstance(value, type): if issubclass(value, messages.Message): message_descriptors.append(describe_message(value)) elif issubclass(value, messages.Enum): enum_descriptors.append(describe_enum(value)) elif issubclass(value, remote.Service): service_descriptors.append(describe_service(value)) if message_descriptors: descriptor.message_types = message_descriptors if enum_descriptors: descriptor.enum_types = enum_descriptors if service_descriptors: descriptor.service_types = service_descriptors return descriptor def describe_file_set(modules): """Build a file set from a specified Python modules. Args: modules: Iterable of Python module to describe. Returns: Initialized FileSet instance describing the modules. """ descriptor = FileSet() file_descriptors = [] for module in modules: file_descriptors.append(describe_file(module)) if file_descriptors: descriptor.files = file_descriptors return descriptor def describe(value): """Describe any value as a descriptor. Helper function for describing any object with an appropriate descriptor object. Args: value: Value to describe as a descriptor. Returns: Descriptor message class if object is describable as a descriptor, else None. """ from protorpc import remote if isinstance(value, types.ModuleType): return describe_file(value) elif callable(value) and hasattr(value, 'remote'): return describe_method(value) elif isinstance(value, messages.Field): return describe_field(value) elif isinstance(value, messages.Enum): return describe_enum_value(value) elif isinstance(value, type): if issubclass(value, messages.Message): return describe_message(value) elif issubclass(value, messages.Enum): return describe_enum(value) elif issubclass(value, remote.Service): return describe_service(value) return None @util.positional(1) def import_descriptor_loader(definition_name, importer=__import__): """Find objects by importing modules as needed. A definition loader is a function that resolves a definition name to a descriptor. The import finder resolves definitions to their names by importing modules when necessary. Args: definition_name: Name of definition to find. importer: Import function used for importing new modules. Returns: Appropriate descriptor for any describable type located by name. Raises: DefinitionNotFoundError when a name does not refer to either a definition or a module. """ # Attempt to import descriptor as a module. if definition_name.startswith('.'): definition_name = definition_name[1:] if not definition_name.startswith('.'): leaf = definition_name.split('.')[-1] if definition_name: try: module = importer(definition_name, '', '', [leaf]) except ImportError: pass else: return describe(module) try: # Attempt to use messages.find_definition to find item. return describe(messages.find_definition(definition_name, importer=__import__)) except messages.DefinitionNotFoundError, err: # There are things that find_definition will not find, but if the parent # is loaded, its children can be searched for a match. split_name = definition_name.rsplit('.', 1) if len(split_name) > 1: parent, child = split_name try: parent_definition = import_descriptor_loader(parent, importer=importer) except messages.DefinitionNotFoundError: # Fall through to original error. pass else: # Check the parent definition for a matching descriptor. if isinstance(parent_definition, FileDescriptor): search_list = parent_definition.service_types or [] elif isinstance(parent_definition, ServiceDescriptor): search_list = parent_definition.methods or [] elif isinstance(parent_definition, EnumDescriptor): search_list = parent_definition.values or [] elif isinstance(parent_definition, MessageDescriptor): search_list = parent_definition.fields or [] else: search_list = [] for definition in search_list: if definition.name == child: return definition # Still didn't find. Reraise original exception. raise err class DescriptorLibrary(object): """A descriptor library is an object that contains known definitions. A descriptor library contains a cache of descriptor objects mapped by definition name. It contains all types of descriptors except for file sets. When a definition name is requested that the library does not know about it can be provided with a descriptor loader which attempt to resolve the missing descriptor. """ @util.positional(1) def __init__(self, descriptors=None, descriptor_loader=import_descriptor_loader): """Constructor. Args: descriptors: A dictionary or dictionary-like object that can be used to store and cache descriptors by definition name. definition_loader: A function used for resolving missing descriptors. The function takes a definition name as its parameter and returns an appropriate descriptor. It may raise DefinitionNotFoundError. """ self.__descriptor_loader = descriptor_loader self.__descriptors = descriptors or {} def lookup_descriptor(self, definition_name): """Lookup descriptor by name. Get descriptor from library by name. If descriptor is not found will attempt to find via descriptor loader if provided. Args: definition_name: Definition name to find. Returns: Descriptor that describes definition name. Raises: DefinitionNotFoundError if not descriptor exists for definition name. """ try: return self.__descriptors[definition_name] except KeyError: pass if self.__descriptor_loader: definition = self.__descriptor_loader(definition_name) self.__descriptors[definition_name] = definition return definition else: raise messages.DefinitionNotFoundError( 'Could not find definition for %s' % definition_name) def lookup_package(self, definition_name): """Determines the package name for any definition. Determine the package that any definition name belongs to. May check parent for package name and will resolve missing descriptors if provided descriptor loader. Args: definition_name: Definition name to find package for. """ while True: descriptor = self.lookup_descriptor(definition_name) if isinstance(descriptor, FileDescriptor): return descriptor.package else: index = definition_name.rfind('.') if index < 0: return None definition_name = definition_name[:index]
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import StringIO import types import unittest import urllib2 from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import test_util from protorpc import remote from protorpc import transport import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = transport class Message(messages.Message): value = messages.StringField(1) class Service(remote.Service): @remote.method(Message, Message) def method(self, request): pass class RpcTest(test_util.TestCase): def setUp(self): self.request = Message(value=u'request') self.response = Message(value=u'response') self.status = remote.RpcStatus(state=remote.RpcState.APPLICATION_ERROR, error_message='an error', error_name='blam') self.rpc = transport.Rpc(self.request) def testConstructor(self): self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.RUNNING, self.rpc.state) self.assertEquals(None, self.rpc.response) self.assertEquals(None, self.rpc.error_message) self.assertEquals(None, self.rpc.error_name) def testSetResponse(self): self.rpc.set_response(self.response) self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.OK, self.rpc.state) self.assertEquals(self.response, self.rpc.response) self.assertEquals(None, self.rpc.error_message) self.assertEquals(None, self.rpc.error_name) def testSetResponseAlreadySet(self): self.rpc.set_response(self.response) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetResponseAlreadyError(self): self.rpc.set_status(self.status) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetStatus(self): self.rpc.set_status(self.status) self.assertEquals(self.request, self.rpc.request) self.assertEquals(remote.RpcState.APPLICATION_ERROR, self.rpc.state) self.assertEquals(None, self.rpc.response) self.assertEquals('an error', self.rpc.error_message) self.assertEquals('blam', self.rpc.error_name) def testSetStatusAlreadySet(self): self.rpc.set_response(self.response) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetNonMessage(self): self.assertRaisesWithRegexpMatch( TypeError, 'Expected Message type, received 10', self.rpc.set_response, 10) def testSetStatusAlreadyError(self): self.rpc.set_status(self.status) self.assertRaisesWithRegexpMatch( transport.RpcStateError, 'RPC must be in RUNNING state to change to OK', self.rpc.set_response, self.response) def testSetUninitializedStatus(self): self.assertRaises(messages.ValidationError, self.rpc.set_status, remote.RpcStatus()) class TransportTest(test_util.TestCase): def do_test(self, protocol, trans): request = Message() request.value = u'request' response = Message() response.value = u'response' encoded_request = protocol.encode_message(request) encoded_response = protocol.encode_message(response) self.assertEquals(protocol, trans.protocol) received_rpc = [None] def transport_rpc(remote, data, rpc): received_rpc[0] = rpc self.assertEquals(remote, Service.method.remote) self.assertEquals(encoded_request, data) self.assertTrue(isinstance(rpc, transport.Rpc)) self.assertEquals(request, rpc.request) self.assertEquals(None, rpc.response) rpc.set_response(response) trans._transport_rpc = transport_rpc rpc = trans.send_rpc(Service.method.remote, request) self.assertEquals(received_rpc[0], rpc) def testDefaultProtocol(self): self.do_test(protobuf, transport.Transport()) def testAlternateProtocol(self): self.do_test(protojson, transport.Transport(protocol=protojson)) class HttpTransportTest(test_util.TestCase): def setUp(self): self.mox = mox.Mox() self.mox.StubOutWithMock(urllib2, 'urlopen') def tearDown(self): self.mox.UnsetStubs() self.mox.VerifyAll() @remote.method(Message, Message) def my_method(self, request): self.fail('self.my_method should not be directly invoked.') def do_test_send_rpc(self, protocol): trans = transport.HttpTransport('http://myserver/myservice', protocol=protocol) request = Message(value=u'The request value') encoded_request = protocol.encode_message(request) response = Message(value=u'The response value') encoded_response = protocol.encode_message(response) def verify_request(urllib2_request): self.assertEquals('http://myserver/myservice.my_method', urllib2_request.get_full_url()) self.assertEquals(urllib2_request.get_data(), encoded_request) self.assertEquals(protocol.CONTENT_TYPE, urllib2_request.headers['Content-type']) return True # First call succeeds. urllib2.urlopen(mox.Func(verify_request)).AndReturn( StringIO.StringIO(encoded_response)) # Second call raises a normal HTTP error. urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, 'a server error', {}, StringIO.StringIO(''))) # Third call raises a 500 error with message. status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='an error') urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, protocol.encode_message(status), {'content-type': protocol.CONTENT_TYPE}, StringIO.StringIO(''))) # Fourth call is not parsable. status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='an error') urllib2.urlopen(mox.Func(verify_request)).AndRaise( urllib2.HTTPError('http://whatever', 500, 'a text message is here anyway', {'content-type': protocol.CONTENT_TYPE}, StringIO.StringIO(''))) self.mox.ReplayAll() actual_rpc = trans.send_rpc(self.my_method.remote, request) self.assertEquals(response, actual_rpc.response) try: trans.send_rpc(self.my_method.remote, request) except remote.ServerError, err: self.assertEquals('HTTP Error 500: a server error', str(err)) self.assertTrue(isinstance(err.cause, urllib2.HTTPError)) else: self.fail('ServerError expected') try: trans.send_rpc(self.my_method.remote, request) except remote.RequestError, err: self.assertEquals('an error', str(err)) self.assertEquals(None, err.cause) else: self.fail('RequestError expected') try: trans.send_rpc(self.my_method.remote, request) except remote.ServerError, err: self.assertEquals('HTTP Error 500: a text message is here anyway', str(err)) self.assertTrue(isinstance(err.cause, urllib2.HTTPError)) else: self.fail('ServerError expected') def testSendProtobuf(self): self.do_test_send_rpc(protobuf) def testSendProtojson(self): self.do_test_send_rpc(protojson) def testURLError(self): trans = transport.HttpTransport('http://myserver/myservice', protocol=protojson) urllib2.urlopen(mox.IsA(urllib2.Request)).AndRaise( urllib2.URLError('a bad connection')) self.mox.ReplayAll() request = Message(value=u'The request value') try: trans.send_rpc(self.my_method.remote, request) except remote.NetworkError, err: self.assertEquals('a bad connection', str(err)) self.assertTrue(isinstance(err.cause, urllib2.URLError)) else: self.fail('Network error expected') def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Handlers for remote services. This module contains classes that may be used to build a service on top of the App Engine Webapp framework. The services request handler can be configured to handle requests in a number of different request formats. All different request formats must have a way to map the request to the service handlers defined request message.Message class. The handler can also send a response in any format that can be mapped from the response message.Message class. Participants in an RPC: There are four classes involved with the life cycle of an RPC. Service factory: A user-defined service factory that is responsible for instantiating an RPC service. The methods intended for use as RPC methods must be decorated by the 'remote' decorator. RPCMapper: Responsible for determining whether or not a specific request matches a particular RPC format and translating between the actual request/response and the underlying message types. A single instance of an RPCMapper sub-class is required per service configuration. Each mapper must be usable across multiple requests. ServiceHandler: A webapp.RequestHandler sub-class that responds to the webapp framework. It mediates between the RPCMapper and service implementation class during a request. As determined by the Webapp framework, a new ServiceHandler instance is created to handle each user request. A handler is never used to handle more than one request. ServiceHandlerFactory: A class that is responsible for creating new, properly configured ServiceHandler instance for each request. The factory is configured by providing it with a set of RPCMapper instances. When the Webapp framework invokes the service handler, the handler creates a new service class instance. The service class instance is provided with a reference to the handler. A single instance of an RPCMapper sub-class is required to configure each service. Each mapper instance must be usable across multiple requests. RPC mappers: RPC mappers translate between a single HTTP based RPC protocol and the underlying service implementation. Each RPC mapper must configured with the following information to determine if it is an appropriate mapper for a given request: http_methods: Set of HTTP methods supported by handler. content_types: Set of supported content types. default_content_type: Default content type for handler responses. Built-in mapper implementations: URLEncodedRPCMapper: Matches requests that are compatible with post forms with the 'application/x-www-form-urlencoded' content-type (this content type is the default if none is specified. It translates post parameters into request parameters. ProtobufRPCMapper: Matches requests that are compatible with post forms with the 'application/x-google-protobuf' content-type. It reads the contents of a binary post request. Public Exceptions: Error: Base class for service handler errors. ServiceConfigurationError: Raised when a service not correctly configured. RequestError: Raised by RPC mappers when there is an error in its request or request format. ResponseError: Raised by RPC mappers when there is an error in its response. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import array import cgi import itertools import logging import re import sys import urllib import weakref from google.appengine.ext import webapp from google.appengine.ext.webapp import util as webapp_util from protorpc import forms from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import protourlencode from protorpc import registry from protorpc import remote from protorpc import util __all__ = [ 'Error', 'RequestError', 'ResponseError', 'ServiceConfigurationError', 'DEFAULT_REGISTRY_PATH', 'ProtobufRPCMapper', 'RPCMapper', 'ServiceHandler', 'ServiceHandlerFactory', 'URLEncodedRPCMapper', 'JSONRPCMapper', 'service_mapping', 'run_services', ] class Error(Exception): """Base class for all errors in service handlers module.""" class ServiceConfigurationError(Error): """When service configuration is incorrect.""" class RequestError(Error): """Error occurred when building request.""" class ResponseError(Error): """Error occurred when building response.""" _URLENCODED_CONTENT_TYPE = protourlencode.CONTENT_TYPE _PROTOBUF_CONTENT_TYPE = protobuf.CONTENT_TYPE _JSON_CONTENT_TYPE = protojson.CONTENT_TYPE _EXTRA_JSON_CONTENT_TYPES = ['application/x-javascript', 'text/javascript', 'text/x-javascript', 'text/x-json', 'text/json', ] # The whole method pattern is an optional regex. It contains a single # group used for mapping to the query parameter. This is passed to the # parameters of 'get' and 'post' on the ServiceHandler. _METHOD_PATTERN = r'(?:\.([^?]*))?' DEFAULT_REGISTRY_PATH = forms.DEFAULT_REGISTRY_PATH class RPCMapper(object): """Interface to mediate between request and service object. Request mappers are implemented to support various types of RPC protocols. It is responsible for identifying whether a given request matches a particular protocol, resolve the remote method to invoke and mediate between the request and appropriate protocol messages for the remote method. """ @util.positional(4) def __init__(self, http_methods, default_content_type, protocol, content_types=None): """Constructor. Args: http_methods: Set of HTTP methods supported by mapper. default_content_type: Default content type supported by mapper. protocol: The protocol implementation. Must implement encode_message and decode_message. content_types: Set of additionally supported content types. """ self.__http_methods = frozenset(http_methods) self.__default_content_type = default_content_type self.__protocol = protocol if content_types is None: content_types = [] self.__content_types = frozenset([self.__default_content_type] + content_types) @property def http_methods(self): return self.__http_methods @property def default_content_type(self): return self.__default_content_type @property def content_types(self): return self.__content_types def build_request(self, handler, request_type): """Build request message based on request. Each request mapper implementation is responsible for converting a request to an appropriate message instance. Args: handler: RequestHandler instance that is servicing request. Must be initialized with request object and been previously determined to matching the protocol of the RPCMapper. request_type: Message type to build. Returns: Instance of request_type populated by protocol buffer in request body. Raises: RequestError if the mapper implementation is not able to correctly convert the request to the appropriate message. """ try: return self.__protocol.decode_message(request_type, handler.request.body) except (messages.ValidationError, messages.DecodeError), err: raise RequestError('Unable to parse request content: %s' % err) def build_response(self, handler, response): """Build response based on service object response message. Each request mapper implementation is responsible for converting a response message to an appropriate handler response. Args: handler: RequestHandler instance that is servicing request. Must be initialized with request object and been previously determined to matching the protocol of the RPCMapper. response: Response message as returned from the service object. Raises: ResponseError if the mapper implementation is not able to correctly convert the message to an appropriate response. """ try: encoded_message = self.__protocol.encode_message(response) except messages.ValidationError, err: raise ResponseError('Unable to encode message: %s' % err) else: handler.response.headers['Content-Type'] = self.default_content_type handler.response.out.write(encoded_message) class ServiceHandlerFactory(object): """Factory class used for instantiating new service handlers. Normally a handler class is passed directly to the webapp framework so that it can be simply instantiated to handle a single request. The service handler, however, must be configured with additional information so that it knows how to instantiate a service object. This class acts the same as a normal RequestHandler class by overriding the __call__ method to correctly configures a ServiceHandler instance with a new service object. The factory must also provide a set of RPCMapper instances which examine a request to determine what protocol is being used and mediates between the request and the service object. The mapping of a service handler must have a single group indicating the part of the URL path that maps to the request method. This group must exist but can be optional for the request (the group may be followed by '?' in the regular expression matching the request). Usage: stock_factory = ServiceHandlerFactory(StockService) ... configure stock_factory by adding RPCMapper instances ... application = webapp.WSGIApplication( [stock_factory.mapping('/stocks')]) Default usage: application = webapp.WSGIApplication( [ServiceHandlerFactory.default(StockService).mapping('/stocks')]) """ def __init__(self, service_factory): """Constructor. Args: service_factory: Service factory to instantiate and provide to service handler. """ self.__service_factory = service_factory self.__request_mappers = [] def all_request_mappers(self): """Get all request mappers. Returns: Iterator of all request mappers used by this service factory. """ return iter(self.__request_mappers) def add_request_mapper(self, mapper): """Add request mapper to end of request mapper list.""" self.__request_mappers.append(mapper) def __call__(self): """Construct a new service handler instance.""" return ServiceHandler(self, self.__service_factory()) @property def service_factory(self): """Service factory associated with this factory.""" return self.__service_factory @staticmethod def __check_path(path): """Check a path parameter. Make sure a provided path parameter is compatible with the webapp URL mapping. Args: path: Path to check. This is a plain path, not a regular expression. Raises: ValueError if path does not start with /, path ends with /. """ if path.endswith('/'): raise ValueError('Path %s must not end with /.' % path) def mapping(self, path): """Convenience method to map service to application. Args: path: Path to map service to. It must be a simple path with a leading / and no trailing /. Returns: Mapping from service URL to service handler factory. """ self.__check_path(path) service_url_pattern = path + _METHOD_PATTERN return service_url_pattern, self @classmethod def default(cls, service_factory, parameter_prefix=''): """Convenience method to map default factory configuration to application. Creates a standardized default service factory configuration that pre-maps the URL encoded protocol handler to the factory. Args: service_factory: Service factory to instantiate and provide to service handler. method_parameter: The name of the form parameter used to determine the method to invoke used by the URLEncodedRPCMapper. If None, no parameter is used and the mapper will only match against the form path-name. Defaults to 'method'. parameter_prefix: If provided, all the parameters in the form are expected to begin with that prefix by the URLEncodedRPCMapper. Returns: Mapping from service URL to service handler factory. """ factory = cls(service_factory) factory.add_request_mapper(URLEncodedRPCMapper(parameter_prefix)) factory.add_request_mapper(ProtobufRPCMapper()) factory.add_request_mapper(JSONRPCMapper()) return factory class ServiceHandler(webapp.RequestHandler): """Web handler for RPC service. Overridden methods: get: All requests handled by 'handle' method. HTTP method stored in attribute. Takes remote_method parameter as derived from the URL mapping. post: All requests handled by 'handle' method. HTTP method stored in attribute. Takes remote_method parameter as derived from the URL mapping. redirect: Not implemented for this service handler. New methods: handle: Handle request for both GET and POST. Attributes (in addition to attributes in RequestHandler): service: Service instance associated with request being handled. method: Method of request. Used by RPCMapper to determine match. remote_method: Sub-path as provided to the 'get' and 'post' methods. """ def __init__(self, factory, service): """Constructor. Args: factory: Instance of ServiceFactory used for constructing new service instances used for handling requests. """ self.__factory = factory self.service = service def get(self, remote_method): """Handler method for GET requests. Delegates to handle. Sets new class attributes: Args: remote_method: Sub-path after service mapping has been matched. """ self.handle('GET', remote_method) def post(self, remote_method): """Handler method for POST requests. Delegates to handle. Sets new class attributes: Args: remote_method: Sub-path after service mapping has been matched. """ self.handle('POST', remote_method) def redirect(self, uri, permanent=False): """Not supported for services.""" raise NotImplementedError('Services do not currently support redirection.') def __match_request(self, mapper, http_method, remote_method): content_type = self.request.headers.get('content-type', None) if not content_type: content_type = self.request.environ.get('HTTP_CONTENT_TYPE', None) if not content_type: return False # Lop off parameters from the end (for example content-encoding) content_type = content_type.split(';', 1)[0] return bool(http_method in mapper.http_methods and # Must have correct content type. content_type and content_type.lower() in mapper.content_types and # Must have remote method name. remote_method) def handle(self, http_method, remote_method): """Handle a service request. The handle method will handle either a GET or POST response. It is up to the individual mappers from the handler factory to determine which request methods they can service. If the protocol is not recognized, the request does not provide a correct request for that protocol or the service object does not support the requested RPC method, will return error code 400 in the response. """ # Provide server state to the service. If the service object does not have # an "initialize_request_state" method, will not attempt to assign state. try: state_initializer = self.service.initialize_request_state except AttributeError: pass else: server_port = self.request.environ.get('SERVER_PORT', None) if server_port: server_port = int(server_port) request_state = remote.RequestState( remote_host=self.request.environ.get('REMOTE_HOST', None), remote_address=self.request.environ.get('REMOTE_ADDR', None), server_host=self.request.environ.get('SERVER_HOST', None), server_port=server_port) state_initializer(request_state) # Search for mapper to mediate request. for mapper in self.__factory.all_request_mappers(): if self.__match_request(mapper, http_method, remote_method): break else: message = 'Unrecognized RPC format.' logging.error(message) self.response.set_status(400, message) return try: try: method = getattr(self.service, remote_method) method_info = method.remote except AttributeError, err: message = 'Unrecognized RPC method: %s' % remote_method logging.error(message) self.response.set_status(400, message) return request = mapper.build_request(self, method_info.request_type) except (RequestError, messages.DecodeError), err: logging.error('Error building request: %s', err) self.response.set_status(400, 'Invalid RPC request.') return response = method(request) try: mapper.build_response(self, response) except ResponseError, err: logging.error('Error building response: %s', err) self.response.set_status(500, 'Invalid RPC response.') # TODO(rafek): Support tag-id only forms. class URLEncodedRPCMapper(RPCMapper): """Request mapper for application/x-www-form-urlencoded forms. This mapper is useful for building forms that can invoke RPC. Many services are also configured to work using URL encoded request information because of its perceived ease of programming and debugging. The mapper must be provided with at least method_parameter or remote_method_pattern so that it is possible to determine how to determine the requests RPC method. If both are provided, the service will respond to both method request types, however, only one may be present in a given request. If both types are detected, the request will not match. """ def __init__(self, parameter_prefix=''): """Constructor. Args: parameter_prefix: If provided, all the parameters in the form are expected to begin with that prefix. """ # Private attributes: # __parameter_prefix: parameter prefix as provided by constructor # parameter. super(URLEncodedRPCMapper, self).__init__(['POST'], _URLENCODED_CONTENT_TYPE, self) self.__parameter_prefix = parameter_prefix def encode_message(self, message): """Encode a message using parameter prefix. Args: message: Message to URL Encode. Returns: URL encoded message. """ return protourlencode.encode_message(message, prefix=self.__parameter_prefix) @property def parameter_prefix(self): """Prefix all form parameters are expected to begin with.""" return self.__parameter_prefix def build_request(self, handler, request_type): """Build request from URL encoded HTTP request. Constructs message from names of URL encoded parameters. If this service handler has a parameter prefix, parameters must begin with it or are ignored. Args: handler: RequestHandler instance that is servicing request. request_type: Message type to build. Returns: Instance of request_type populated by protocol buffer in request parameters. Raises: RequestError if message type contains nested message field or repeated message field. Will raise RequestError if there are any repeated parameters. """ request = request_type() builder = protourlencode.URLEncodedRequestBuilder( request, prefix=self.__parameter_prefix) for argument in sorted(handler.request.arguments()): values = handler.request.get_all(argument) try: builder.add_parameter(argument, values) except messages.DecodeError, err: raise RequestError(str(err)) return request class ProtobufRPCMapper(RPCMapper): """Request mapper for application/x-protobuf service requests. This mapper will parse protocol buffer from a POST body and return the request as a protocol buffer. """ def __init__(self): super(ProtobufRPCMapper, self).__init__(['POST'], _PROTOBUF_CONTENT_TYPE, protobuf) class JSONRPCMapper(RPCMapper): """Request mapper for application/x-protobuf service requests. This mapper will parse protocol buffer from a POST body and return the request as a protocol buffer. """ def __init__(self): super(JSONRPCMapper, self).__init__( ['POST'], _JSON_CONTENT_TYPE, protojson, content_types=_EXTRA_JSON_CONTENT_TYPES) def service_mapping(services, registry_path=DEFAULT_REGISTRY_PATH): """Create a services mapping for use with webapp. Creates basic default configuration and registration for ProtoRPC services. Each service listed in the service mapping has a standard service handler factory created for it. The list of mappings can either be an explicit path to service mapping or just services. If mappings are just services, they will automatically be mapped to their default name. For exampel: package = 'my_package' class MyService(remote.Service): ... server_mapping([('/my_path', MyService), # Maps to /my_path MyService, # Maps to /my_package/MyService ]) Specifying a service mapping: Normally services are mapped to URL paths by specifying a tuple (path, service): path: The path the service resides on. service: The service class or service factory for creating new instances of the service. For more information about service factories, please see remote.Service.new_factory. If no tuple is provided, and therefore no path specified, a default path is calculated by using the fully qualified service name using a URL path separator for each of its components instead of a '.'. Args: services: Can be service type, service factory or string definition name of service being mapped or list of tuples (path, service): path: Path on server to map service to. service: Service type, service factory or string definition name of service being mapped. Can also be a dict. If so, the keys are treated as the path and values as the service. registry_path: Path to give to registry service. Use None to disable registry service. Returns: List of tuples defining a mapping of request handlers compatible with a webapp application. Raises: ServiceConfigurationError when duplicate paths are provided. """ if isinstance(services, dict): services = services.iteritems() mapping = [] registry_map = {} if registry_path is not None: registry_service = registry.RegistryService.new_factory(registry_map) services = list(services) + [(registry_path, registry_service)] mapping.append((registry_path + r'/form(?:/)?', forms.FormsHandler.new_factory(registry_path))) mapping.append((registry_path + r'/form/(.+)', forms.ResourceHandler)) paths = set() for service_item in services: infer_path = not isinstance(service_item, (list, tuple)) if infer_path: service = service_item else: service = service_item[1] service_class = getattr(service, 'service_class', service) if infer_path: path = '/' + service_class.definition_name().replace('.', '/') else: path = service_item[0] if path in paths: raise ServiceConfigurationError( 'Path %r is already defined in service mapping' % path) else: paths.add(path) # Create service mapping for webapp. new_mapping = ServiceHandlerFactory.default(service).mapping(path) mapping.append(new_mapping) # Update registry with service class. registry_map[path] = service_class return mapping def run_services(services, registry_path=DEFAULT_REGISTRY_PATH): """Handle CGI request using service mapping. Args: Same as service_mapping. """ mappings = service_mapping(services, registry_path=registry_path) application = webapp.WSGIApplication(mappings) webapp_util.run_wsgi_app(application)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Webapp forms interface to ProtoRPC services. This webapp application is automatically configured to work with ProtoRPCs that have a configured protorpc.RegistryService. This webapp is automatically added to the registry service URL at <registry-path>/forms (default is /protorpc/form) when configured using the service_handlers.service_mapping function. """ import logging import os from google.appengine.ext import webapp from google.appengine.ext.webapp import template __all__ = ['FormsHandler', 'ResourceHandler', 'DEFAULT_REGISTRY_PATH', ] _TEMPLATES_DIR = os.path.join(os.path.dirname(__file__), 'static') _FORMS_TEMPLATE = os.path.join(_TEMPLATES_DIR, 'forms.html') _METHODS_TEMPLATE = os.path.join(_TEMPLATES_DIR, 'methods.html') DEFAULT_REGISTRY_PATH = '/protorpc' class ResourceHandler(webapp.RequestHandler): """Serves static resources without needing to add static files to app.yaml.""" __RESOURCE_MAP = { 'forms.js': 'text/javascript', } def get(self, relative): """Serve known static files. If static file is not known, will return 404 to client. Response items are cached for 300 seconds. Args: relative: Name of static file relative to main FormsHandler. """ content_type = self.__RESOURCE_MAP.get(relative, None) if not content_type: self.response.set_status(404) self.response.out.write('Resource not found.') return path = os.path.join(os.path.dirname(__file__), 'static', relative) self.response.headers['Content-Type'] = content_type static_file = open(path) try: contents = static_file.read() finally: static_file.close() self.response.out.write(contents) class FormsHandler(webapp.RequestHandler): """Handler for display HTML/javascript forms of ProtoRPC method calls. When accessed with no query parameters, will show a web page that displays all services and methods on the associated registry path. Links on this page fill in the service_path and method_name query parameters back to this same handler. When provided with service_path and method_name parameters will display a dynamic form representing the request message for that method. When sent, the form sends a JSON request to the ProtoRPC method and displays the response in the HTML page. Attribute: registry_path: Read-only registry path known by this handler. """ def __init__(self, registry_path=DEFAULT_REGISTRY_PATH): """Constructor. When configuring a FormsHandler to use with a webapp application do not pass the request handler class in directly. Instead use new_factory to ensure that the FormsHandler is created with the correct registry path for each request. Args: registry_path: Absolute path on server where the ProtoRPC RegsitryService is located. """ assert registry_path self.__registry_path = registry_path @property def registry_path(self): return self.__registry_path def get(self): """Send forms and method page to user. By default, displays a web page listing all services and methods registered on the server. Methods have links to display the actual method form. If both parameters are set, will display form for method. Query Parameters: service_path: Path to service to display method of. Optional. method_name: Name of method to display form for. Optional. """ params = {'forms_path': self.request.path.rstrip('/'), 'hostname': self.request.host, 'registry_path': self.__registry_path, } service_path = self.request.get('path', None) method_name = self.request.get('method', None) if service_path and method_name: form_template = _METHODS_TEMPLATE params['service_path'] = service_path params['method_name'] = method_name else: form_template = _FORMS_TEMPLATE self.response.out.write(template.render(form_template, params)) @classmethod def new_factory(cls, registry_path=DEFAULT_REGISTRY_PATH): """Construct a factory for use with WSGIApplication. This method is called automatically with the correct registry path when services are configured via service_handlers.service_mapping. Args: registry_path: Absolute path on server where the ProtoRPC RegsitryService is located. Returns: Factory function that creates a properly configured FormsHandler instance. """ def forms_factory(): return cls(registry_path) return forms_factory
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.stub.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import StringIO import sys import unittest from protorpc import definition from protorpc import descriptor from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import test_util import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = definition class DefineEnumTest(test_util.TestCase): """Test for define_enum.""" def testDefineEnum_Empty(self): """Test defining an empty enum.""" enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = 'Empty' enum_class = definition.define_enum(enum_descriptor, 'whatever') self.assertEquals('Empty', enum_class.__name__) self.assertEquals('whatever', enum_class.__module__) self.assertEquals(enum_descriptor, descriptor.describe_enum(enum_class)) def testDefineEnum(self): """Test defining an enum.""" red = descriptor.EnumValueDescriptor() green = descriptor.EnumValueDescriptor() blue = descriptor.EnumValueDescriptor() red.name = 'RED' red.number = 1 green.name = 'GREEN' green.number = 2 blue.name = 'BLUE' blue.number = 3 enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = 'Colors' enum_descriptor.values = [red, green, blue] enum_class = definition.define_enum(enum_descriptor, 'whatever') self.assertEquals('Colors', enum_class.__name__) self.assertEquals('whatever', enum_class.__module__) self.assertEquals(enum_descriptor, descriptor.describe_enum(enum_class)) class DefineFieldTest(test_util.TestCase): """Test for define_field.""" def testDefineField_Optional(self): """Test defining an optional field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.INT32 field_descriptor.label = descriptor.FieldDescriptor.Label.OPTIONAL field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.IntegerField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.INT32, field.variant) self.assertFalse(field.required) self.assertFalse(field.repeated) def testDefineField_Required(self): """Test defining a required field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.STRING field_descriptor.label = descriptor.FieldDescriptor.Label.REQUIRED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.StringField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.STRING, field.variant) self.assertTrue(field.required) self.assertFalse(field.repeated) def testDefineField_Repeated(self): """Test defining a repeated field instance from a method descriptor.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.DOUBLE field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.FloatField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.DOUBLE, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) def testDefineField_Message(self): """Test defining a message field.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.MESSAGE field_descriptor.type_name = 'something.yet.to.be.Defined' field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.MessageField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.MESSAGE, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError, 'Could not find definition for ' 'something.yet.to.be.Defined', getattr, field, 'type') def testDefineField_Enum(self): """Test defining an enum field.""" field_descriptor = descriptor.FieldDescriptor() field_descriptor.name = 'a_field' field_descriptor.number = 1 field_descriptor.variant = descriptor.FieldDescriptor.Variant.ENUM field_descriptor.type_name = 'something.yet.to.be.Defined' field_descriptor.label = descriptor.FieldDescriptor.Label.REPEATED field = definition.define_field(field_descriptor) # Name will not be set from the original descriptor. self.assertFalse(hasattr(field, 'name')) self.assertTrue(isinstance(field, messages.EnumField)) self.assertEquals(1, field.number) self.assertEquals(descriptor.FieldDescriptor.Variant.ENUM, field.variant) self.assertFalse(field.required) self.assertTrue(field.repeated) self.assertRaisesWithRegexpMatch(messages.DefinitionNotFoundError, 'Could not find definition for ' 'something.yet.to.be.Defined', getattr, field, 'type') class DefineMessageTest(test_util.TestCase): """Test for define_message.""" def testDefineMessageEmpty(self): """Test definition a message with no fields or enums.""" class AMessage(messages.Message): pass message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessageEnumOnly(self): """Test definition a message with only enums.""" class AMessage(messages.Message): class NestedEnum(messages.Enum): pass message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessageFieldsOnly(self): """Test definition a message with only fields.""" class AMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.StringField(2) message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) def testDefineMessage(self): """Test defining Message class from descriptor.""" class AMessage(messages.Message): class NestedEnum(messages.Enum): pass field1 = messages.IntegerField(1) field2 = messages.StringField(2) message_descriptor = descriptor.describe_message(AMessage) message_class = definition.define_message(message_descriptor, '__main__') self.assertEquals('AMessage', message_class.__name__) self.assertEquals('__main__', message_class.__module__) self.assertEquals(message_descriptor, descriptor.describe_message(message_class)) class DefineServiceTest(test_util.TestCase): """Test service proxy definition.""" def setUp(self): """Set up mock and request classes.""" self.module = new.module('stocks') class GetQuoteRequest(messages.Message): __module__ = 'stocks' symbols = messages.StringField(1, repeated=True) class GetQuoteResponse(messages.Message): __module__ = 'stocks' prices = messages.IntegerField(1, repeated=True) self.module.GetQuoteRequest = GetQuoteRequest self.module.GetQuoteResponse = GetQuoteResponse def testDefineService(self): """Test service definition from descriptor.""" method_descriptor = descriptor.MethodDescriptor() method_descriptor.name = 'get_quote' method_descriptor.request_type = 'GetQuoteRequest' method_descriptor.response_type = 'GetQuoteResponse' service_descriptor = descriptor.ServiceDescriptor() service_descriptor.name = 'Stocks' service_descriptor.methods = [method_descriptor] StockService = definition.define_service(service_descriptor, self.module) self.assertTrue(issubclass(StockService, remote.Service)) self.assertTrue(issubclass(StockService.Stub, remote.StubBase)) request = self.module.GetQuoteRequest() service = StockService() self.assertRaises(NotImplementedError, service.get_quote, request) self.assertEquals(self.module.GetQuoteRequest, service.get_quote.remote.request_type) self.assertEquals(self.module.GetQuoteResponse, service.get_quote.remote.response_type) class ModuleTest(test_util.TestCase): """Test for module creation and importation functions.""" def MakeFileDescriptor(self, package): """Helper method to construct FileDescriptors. Creates FileDescriptor with a MessageDescriptor and an EnumDescriptor. Args: package: Package name to give new file descriptors. Returns: New FileDescriptor instance. """ enum_descriptor = descriptor.EnumDescriptor() enum_descriptor.name = u'MyEnum' message_descriptor = descriptor.MessageDescriptor() message_descriptor.name = u'MyMessage' service_descriptor = descriptor.ServiceDescriptor() service_descriptor.name = u'MyService' file_descriptor = descriptor.FileDescriptor() file_descriptor.package = package file_descriptor.enum_types = [enum_descriptor] file_descriptor.message_types = [message_descriptor] file_descriptor.service_types = [service_descriptor] return file_descriptor def testDefineModule(self): """Test define_module function.""" file_descriptor = self.MakeFileDescriptor('my.package') module = definition.define_file(file_descriptor) self.assertEquals('my.package', module.__name__) self.assertEquals('my.package', module.MyEnum.__module__) self.assertEquals('my.package', module.MyMessage.__module__) self.assertEquals('my.package', module.MyService.__module__) self.assertEquals(file_descriptor, descriptor.describe_file(module)) def testDefineModule_ReuseModule(self): """Test updating module with additional definitions.""" file_descriptor = self.MakeFileDescriptor('my.package') module = new.module('override') self.assertEquals(module, definition.define_file(file_descriptor, module)) self.assertEquals('override', module.MyEnum.__module__) self.assertEquals('override', module.MyMessage.__module__) self.assertEquals('override', module.MyService.__module__) # One thing is different between original descriptor and new. file_descriptor.package = 'override' self.assertEquals(file_descriptor, descriptor.describe_file(module)) def testImportFile(self): """Test importing FileDescriptor in to module space.""" modules = {} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone'])) def testImportFile_InToExisting(self): """Test importing FileDescriptor in to existing module.""" module = new.module('standalone') modules = {'standalone': module} file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor, modules=modules) self.assertEquals(module, modules['standalone']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['standalone'])) def testImportFile_InToGlobalModules(self): """Test importing FileDescriptor in to global modules.""" original_modules = sys.modules try: sys.modules = dict(sys.modules) if 'standalone' in sys.modules: del sys.modules['standalone'] file_descriptor = self.MakeFileDescriptor('standalone') definition.import_file(file_descriptor) self.assertEquals(file_descriptor, descriptor.describe_file(sys.modules['standalone'])) finally: sys.modules = original_modules def testImportFile_Nested(self): """Test importing FileDescriptor in to existing nested module.""" modules = {} file_descriptor = self.MakeFileDescriptor('root.nested') definition.import_file(file_descriptor, modules=modules) self.assertEquals(modules['root'].nested, modules['root.nested']) self.assertEquals(file_descriptor, descriptor.describe_file(modules['root.nested'])) def testImportFile_NoPackage(self): """Test importing FileDescriptor with no package.""" file_descriptor = self.MakeFileDescriptor('does not matter') file_descriptor.reset('package') self.assertRaisesWithRegexpMatch(ValueError, 'File descriptor must have package name', definition.import_file, file_descriptor) def testImportFileSet(self): """Test importing a whole file set.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] root = new.module('root') nested = new.module('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ])) def testImportFileSetFromFile(self): """Test importing a whole file set from a file.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), ] stream = StringIO.StringIO(protobuf.encode_message(file_set)) self.mox = mox.Mox() opener = self.mox.CreateMockAnything() opener('my-file.dat', 'rb').AndReturn(stream) self.mox.ReplayAll() modules = {} definition.import_file_set('my-file.dat', modules=modules, _open=opener) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], ])) def testImportBuiltInProtorpcClasses(self): """Test that built in Protorpc classes are skipped.""" file_set = descriptor.FileSet() file_set.files = [self.MakeFileDescriptor(u'standalone'), self.MakeFileDescriptor(u'root.nested'), self.MakeFileDescriptor(u'root.nested.nested'), descriptor.describe_file(descriptor), ] root = new.module('root') nested = new.module('root.nested') root.nested = nested modules = { 'root': root, 'root.nested': nested, 'protorpc.descriptor': descriptor, } definition.import_file_set(file_set, modules=modules) self.assertEquals(root, modules['root']) self.assertEquals(nested, modules['root.nested']) self.assertEquals(nested.nested, modules['root.nested.nested']) self.assertEquals(descriptor, modules['protorpc.descriptor']) self.assertEquals(file_set, descriptor.describe_file_set( [modules['standalone'], modules['root.nested'], modules['root.nested.nested'], modules['protorpc.descriptor'], ])) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.forms.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import os import unittest from google.appengine.ext import webapp from google.appengine.ext.webapp import template from protorpc import forms from protorpc import test_util from protorpc import webapp_test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = forms def RenderTemplate(name, **params): """Load content from static file. Args: name: Name of static file to load from static directory. params: Passed in to webapp template generator. Returns: Contents of static file. """ path = os.path.join(os.path.dirname(__file__), 'static', name) return template.render(path, params) class ResourceHandlerTest(webapp_test_util.RequestHandlerTestBase): def CreateRequestHandler(self): return forms.ResourceHandler() def DoStaticContentTest(self, name, expected_type): """Run the static content test. Loads expected static content from source and compares with results in response. Checks content-type and cache header. Args: name: Name of file that should be served. expected_type: Expected content-type of served file. """ self.handler.get(name) content = RenderTemplate(name) self.CheckResponse('200 OK', {'content-type': expected_type, }, content) def testGet(self): self.DoStaticContentTest('forms.js', 'text/javascript') def testNoSuchFile(self): self.handler.get('unknown.txt') self.CheckResponse('404 Not Found', {}, 'Resource not found.') class FormsHandlerTest(webapp_test_util.RequestHandlerTestBase): def CreateRequestHandler(self): handler = forms.FormsHandler('/myreg') self.assertEquals('/myreg', handler.registry_path) return handler def testGetForm(self): self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGet_MissingPath(self): self.ResetHandler({'QUERY_STRING': 'method=my_method'}) self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGet_MissingMethod(self): self.ResetHandler({'QUERY_STRING': 'path=/my-path'}) self.handler.get() content = RenderTemplate( 'forms.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg') self.CheckResponse('200 OK', {}, content) def testGetMethod(self): self.ResetHandler({'QUERY_STRING': 'path=/my-path&method=my_method'}) self.handler.get() content = RenderTemplate( 'methods.html', forms_path='/tmp/myhandler', hostname=self.request.host, registry_path='/myreg', service_path='/my-path', method_name='my_method') self.CheckResponse('200 OK', {}, content) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Testing utilities for the webapp libraries. GetDefaultEnvironment: Method for easily setting up CGI environment. RequestHandlerTestBase: Base class for setting up handler tests. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import unittest from google.appengine.ext import webapp def GetDefaultEnvironment(): """Function for creating a default CGI environment.""" return { 'LC_NUMERIC': 'C', 'wsgi.multiprocess': True, 'SERVER_PROTOCOL': 'HTTP/1.0', 'SERVER_SOFTWARE': 'Dev AppServer 0.1', 'SCRIPT_NAME': '', 'LOGNAME': 'nickjohnson', 'USER': 'nickjohnson', 'QUERY_STRING': 'foo=bar&foo=baz&foo2=123', 'PATH': '/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11', 'LANG': 'en_US', 'LANGUAGE': 'en', 'REMOTE_ADDR': '127.0.0.1', 'LC_MONETARY': 'C', 'CONTENT_TYPE': 'application/x-www-form-urlencoded', 'wsgi.url_scheme': 'http', 'SERVER_PORT': '8080', 'HOME': '/home/mruser', 'USERNAME': 'mruser', 'CONTENT_LENGTH': '', 'USER_IS_ADMIN': '1', 'PYTHONPATH': '/tmp/setup', 'LC_TIME': 'C', 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; ' 'rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6', 'wsgi.multithread': False, 'wsgi.version': (1, 0), 'USER_EMAIL': 'test@example.com', 'USER_EMAIL': '112', 'wsgi.input': cStringIO.StringIO(), 'PATH_TRANSLATED': '/tmp/request.py', 'SERVER_NAME': 'localhost', 'GATEWAY_INTERFACE': 'CGI/1.1', 'wsgi.run_once': True, 'LC_COLLATE': 'C', 'HOSTNAME': 'myhost', 'wsgi.errors': cStringIO.StringIO(), 'PWD': '/tmp', 'REQUEST_METHOD': 'GET', 'MAIL': '/dev/null', 'MAILCHECK': '0', 'USER_NICKNAME': 'test', 'HTTP_COOKIE': 'dev_appserver_login="test:test@example.com:True"', 'PATH_INFO': '/tmp/myhandler' } class RequestHandlerTestBase(unittest.TestCase): """Base class for writing RequestHandler tests. To test a specific request handler override CreateRequestHandler. To change the environment for that handler override GetEnvironment. """ def setUp(self): """Set up test for request handler.""" self.ResetHandler() def GetEnvironment(self): """Get environment. Override for more specific configurations. Returns: dict of CGI environment. """ return GetDefaultEnvironment() def CreateRequestHandler(self): """Create RequestHandler instances. Override to create more specific kinds of RequestHandler instances. Returns: RequestHandler instance used in test. """ return webapp.RequestHandler() def CheckResponse(self, expected_status, expected_headers, expected_content): """Check that the web response is as expected. Args: expected_status: Expected status message. expected_headers: Dictionary of expected headers. Will ignore unexpected headers and only check the value of those expected. expected_content: Expected body. """ def check_content(content): self.assertEquals(expected_content, content) def start_response(status, headers): self.assertEquals(expected_status, status) found_keys = set() for name, value in headers: name = name.lower() try: expected_value = expected_headers[name] except KeyError: pass else: found_keys.add(name) self.assertEquals(expected_value, value) missing_headers = set(expected_headers.iterkeys()) - found_keys if missing_headers: self.fail('Expected keys %r not found' % (list(missing_headers),)) return check_content self.handler.response.wsgi_write(start_response) def ResetHandler(self, change_environ=None): """Reset this tests environment with environment changes. Resets the entire test with a new handler which includes some changes to the default request environment. Args: change_environ: Dictionary of values that are added to default environment. """ environment = self.GetEnvironment() environment.update(change_environ or {}) self.request = webapp.Request(environment) self.response = webapp.Response() self.handler = self.CreateRequestHandler() self.handler.initialize(self.request, self.response)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import with_statement __author__ = 'rafek@google.com (Rafe Kaplan)' import logging from protorpc import descriptor from protorpc import generate from protorpc import messages from protorpc import util __all__ = ['format_proto_file'] @util.positional(2) def format_proto_file(file_descriptor, output, indent_space=2): out = generate.IndentWriter(output, indent_space=indent_space) if file_descriptor.package: out << 'package %s;' % file_descriptor.package def write_enums(enum_descriptors): """Write nested and non-nested Enum types. Args: enum_descriptors: List of EnumDescriptor objects from which to generate enums. """ # Write enums. for enum in enum_descriptors or []: out << '' out << '' out << 'enum %s {' % enum.name out << '' with out.indent(): if enum.values: for enum_value in enum.values: out << '%s = %s;' % (enum_value.name, enum_value.number) out << '}' write_enums(file_descriptor.enum_types) def write_fields(field_descriptors): """Write fields for Message types. Args: field_descriptors: List of FieldDescriptor objects from which to generate fields. """ for field in field_descriptors or []: default_format = '' if field.default_value is not None: if field.label == descriptor.FieldDescriptor.Label.REPEATED: logging.warning('Default value for repeated field %s is not being ' 'written to proto file' % field.name) else: # Convert default value to string. if field.variant == messages.Variant.MESSAGE: logging.warning( 'Message field %s should not have default values' % field.name) default = None elif field.variant == messages.Variant.STRING: default = repr(field.default_value.encode('utf-8')) elif field.variant == messages.Variant.BYTES: default = repr(field.default_value) else: default = str(field.default_value) if default is not None: default_format = ' [default=%s]' % default if field.variant in (messages.Variant.MESSAGE, messages.Variant.ENUM): field_type = field.type_name else: field_type = str(field.variant).lower() out << '%s %s %s = %s%s;' % (str(field.label).lower(), field_type, field.name, field.number, default_format) def write_messages(message_descriptors): """Write nested and non-nested Message types. Args: message_descriptors: List of MessageDescriptor objects from which to generate messages. """ for message in message_descriptors or []: out << '' out << '' out << 'message %s {' % message.name with out.indent(): if message.enum_types: write_enums(message.enum_types) if message.message_types: write_messages(message.message_types) if message.fields: write_fields(message.fields) out << '}' write_messages(file_descriptor.message_types)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate_python_test.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import os import shutil import sys import tempfile import unittest from protorpc import descriptor from protorpc import generate_python from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate_python class FormatPythonFileTest(test_util.TestCase): def setUp(self): self.original_path = list(sys.path) self.original_modules = dict(sys.modules) sys.path = list(sys.path) self.file_descriptor = descriptor.FileDescriptor() # Create temporary directory and add to Python path so that generated # Python code can be easily parsed, imported and executed. self.temp_dir = tempfile.mkdtemp() sys.path.append(self.temp_dir) def tearDown(self): # Reset path. sys.path[:] = [] sys.path.extend(self.original_path) # Reset modules. sys.modules.clear() sys.modules.update(self.original_modules) # Remove temporary directory. try: shutil.rmtree(self.temp_dir) except IOError: pass def DoPythonTest(self, file_descriptor): """Execute python test based on a FileDescriptor object. The full test of the Python code generation is to generate a Python source code file, import the module and regenerate the FileDescriptor from it. If the generated FileDescriptor is the same as the original, it means that the generated source code correctly implements the actual FileDescriptor. """ file_name = os.path.join(self.temp_dir, '%s.py' % (file_descriptor.package or 'blank',)) source_file = open(file_name, 'wt') try: generate_python.format_python_file(file_descriptor, source_file) finally: source_file.close() module_to_import = file_descriptor.package or 'blank' module = __import__(module_to_import) if not file_descriptor.package: self.assertFalse(hasattr(module, 'package')) module.package = '' # Create package name so that comparison will work. reloaded_descriptor = descriptor.describe_file(module) # Need to sort both message_types fields because document order is never # Ensured. # TODO(rafek): Ensure document order. if reloaded_descriptor.message_types: reloaded_descriptor.message_types = sorted( reloaded_descriptor.message_types, key=lambda v: v.name) if file_descriptor.message_types: file_descriptor.message_types = sorted( file_descriptor.message_types, key=lambda v: v.name) self.assertEquals(file_descriptor, reloaded_descriptor) @util.positional(2) def DoMessageTest(self, field_descriptors, message_types=None, enum_types=None): """Execute message generation test based on FieldDescriptor objects. Args: field_descriptor: List of FieldDescriptor object to generate and test. message_types: List of other MessageDescriptor objects that the new Message class depends on. enum_types: List of EnumDescriptor objects that the new Message class depends on. """ file_descriptor = descriptor.FileDescriptor() file_descriptor.package = 'my_package' message_descriptor = descriptor.MessageDescriptor() message_descriptor.name = 'MyMessage' message_descriptor.fields = list(field_descriptors) file_descriptor.message_types = message_types or [] file_descriptor.message_types.append(message_descriptor) if enum_types: file_descriptor.enum_types = list(enum_types) self.DoPythonTest(file_descriptor) def testBlankPackage(self): self.DoPythonTest(descriptor.FileDescriptor()) def testEmptyPackage(self): file_descriptor = descriptor.FileDescriptor() file_descriptor.package = 'mypackage' self.DoPythonTest(file_descriptor) def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testMessageField_InternalReference(self): other_message = descriptor.MessageDescriptor() other_message.name = 'OtherMessage' field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'my_package.OtherMessage' self.DoMessageTest([field], message_types=[other_message]) def testMessageField_ExternalReference(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'protorpc.registry.GetFileSetResponse' self.DoMessageTest([field]) def testEnumField_InternalReference(self): enum = descriptor.EnumDescriptor() enum.name = 'Color' field = descriptor.FieldDescriptor() field.name = 'color' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.Color' self.DoMessageTest([field], enum_types=[enum]) def testEnumField_ExternalReference(self): field = descriptor.FieldDescriptor() field.name = 'color' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'protorpc.descriptor.FieldDescriptor.Label' self.DoMessageTest([field]) def testNonDefaultVariant(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.UINT64 self.DoMessageTest([field]) def testRequiredField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REQUIRED field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testRepeatedField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 self.DoMessageTest([field]) def testIntegerDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '10' self.DoMessageTest([field]) def testFloatDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'float_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.DOUBLE field.default_value = '10.1' self.DoMessageTest([field]) def testStringDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = u'a nice lovely string\'s "string"' self.DoMessageTest([field]) def testEnumDefaultValue(self): field = descriptor.FieldDescriptor() field.name = 'label' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'protorpc.descriptor.FieldDescriptor.Label' field.default_value = '2' self.DoMessageTest([field]) def testMultiFields(self): field1 = descriptor.FieldDescriptor() field1.name = 'integer_field' field1.number = 1 field1.label = descriptor.FieldDescriptor.Label.OPTIONAL field1.variant = descriptor.FieldDescriptor.Variant.INT64 field2 = descriptor.FieldDescriptor() field2.name = 'string_field' field2.number = 2 field2.label = descriptor.FieldDescriptor.Label.OPTIONAL field2.variant = descriptor.FieldDescriptor.Variant.STRING field3 = descriptor.FieldDescriptor() field3.name = 'unsigned_integer_field' field3.number = 3 field3.label = descriptor.FieldDescriptor.Label.OPTIONAL field3.variant = descriptor.FieldDescriptor.Variant.UINT64 self.DoMessageTest([field1, field2, field3]) def testNestedMessage(self): message = descriptor.MessageDescriptor() message.name = 'OuterMessage' inner_message = descriptor.MessageDescriptor() inner_message.name = 'InnerMessage' inner_inner_message = descriptor.MessageDescriptor() inner_inner_message.name = 'InnerInnerMessage' inner_message.message_types = [inner_inner_message] message.message_types = [inner_message] file_descriptor = descriptor.FileDescriptor() file_descriptor.message_types = [message] self.DoPythonTest(file_descriptor) def testNestedEnum(self): message = descriptor.MessageDescriptor() message.name = 'OuterMessage' inner_enum = descriptor.EnumDescriptor() inner_enum.name = 'InnerEnum' message.enum_types = [inner_enum] file_descriptor = descriptor.FileDescriptor() file_descriptor.message_types = [message] self.DoPythonTest(file_descriptor) def testService(self): service = descriptor.ServiceDescriptor() service.name = 'TheService' method1 = descriptor.MethodDescriptor() method1.name = 'method1' method1.request_type = 'protorpc.descriptor.FileDescriptor' method1.response_type = 'protorpc.descriptor.MethodDescriptor' service.methods = [method1] file_descriptor = descriptor.FileDescriptor() file_descriptor.service_types = [service] self.DoPythonTest(file_descriptor) # Test to make sure that implementation methods raise an exception. import blank service_instance = blank.TheService() self.assertRaisesWithRegexpMatch(NotImplementedError, 'Method method1 is not implemented', service_instance.method1, descriptor.FileDescriptor()) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protobuf.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import messages from protorpc import protobuf from protorpc import protorpc_test_pb2 from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protobuf class EncodeMessageTest(test_util.TestCase, test_util.ProtoConformanceTestBase): """Test message to protocol buffer encoding.""" PROTOLIB = protobuf def assertErrorIs(self, exception, message, function, *params, **kwargs): try: function(*params, **kwargs) self.fail('Expected to raise exception %s but did not.' % exception) except exception, err: self.assertEquals(message, str(err)) @property def encoded_partial(self): proto = protorpc_test_pb2.OptionalMessage() proto.double_value = 1.23 proto.int64_value = -100000000000 proto.int32_value = 1020 proto.string_value = u'a string' proto.enum_value = protorpc_test_pb2.OptionalMessage.VAL2 return proto.SerializeToString() @property def encoded_full(self): proto = protorpc_test_pb2.OptionalMessage() proto.double_value = 1.23 proto.float_value = -2.5 proto.int64_value = -100000000000 proto.uint64_value = 102020202020 proto.int32_value = 1020 proto.bool_value = True proto.string_value = u'a string\u044f' proto.bytes_value = 'a bytes\xff\xfe' proto.enum_value = protorpc_test_pb2.OptionalMessage.VAL2 return proto.SerializeToString() @property def encoded_repeated(self): proto = protorpc_test_pb2.RepeatedMessage() proto.double_value.append(1.23) proto.double_value.append(2.3) proto.float_value.append(-2.5) proto.float_value.append(0.5) proto.int64_value.append(-100000000000) proto.int64_value.append(20) proto.uint64_value.append(102020202020) proto.uint64_value.append(10) proto.int32_value.append(1020) proto.int32_value.append(718) proto.bool_value.append(True) proto.bool_value.append(False) proto.string_value.append(u'a string\u044f') proto.string_value.append(u'another string') proto.bytes_value.append('a bytes\xff\xfe') proto.bytes_value.append('another bytes') proto.enum_value.append(protorpc_test_pb2.RepeatedMessage.VAL2) proto.enum_value.append(protorpc_test_pb2.RepeatedMessage.VAL1) return proto.SerializeToString() @property def encoded_nested(self): proto = protorpc_test_pb2.HasNestedMessage() proto.nested.a_value = 'a string' return proto.SerializeToString() @property def encoded_repeated_nested(self): proto = protorpc_test_pb2.HasNestedMessage() proto.repeated_nested.add().a_value = 'a string' proto.repeated_nested.add().a_value = 'another string' return proto.SerializeToString() unexpected_tag_message = ( chr((15 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.NUMERIC) + chr(5)) @property def encoded_default_assigned(self): proto = protorpc_test_pb2.HasDefault() proto.a_value = test_util.HasDefault.a_value.default return proto.SerializeToString() @property def encoded_nested_empty(self): proto = protorpc_test_pb2.HasOptionalNestedMessage() proto.nested.Clear() return proto.SerializeToString() @property def encoded_repeated_nested_empty(self): proto = protorpc_test_pb2.HasOptionalNestedMessage() proto.repeated_nested.add() proto.repeated_nested.add() return proto.SerializeToString() @property def encoded_extend_message(self): proto = protorpc_test_pb2.RepeatedMessage() proto.add_int64_value(400) proto.add_int64_value(50) proto.add_int64_value(6000) return proto.SerializeToString() @property def encoded_string_types(self): proto = protorpc_test_pb2.OptionalMessage() proto.string_value = u'Latin' return proto.SerializeToString() def testDecodeWrongWireFormat(self): """Test what happens when wrong wire format found in protobuf.""" class ExpectedProto(messages.Message): value = messages.StringField(1) class WrongVariant(messages.Message): value = messages.IntegerField(1) original = WrongVariant() original.value = 10 self.assertErrorIs(messages.DecodeError, 'Expected wire type STRING but found NUMERIC', protobuf.decode_message, ExpectedProto, protobuf.encode_message(original)) def testDecodeBadWireType(self): """Test what happens when non-existant wire type found in protobuf.""" # Message has tag 1, type 3 which does not exist. bad_wire_type_message = chr((1 << protobuf._WIRE_TYPE_BITS) | 3) self.assertErrorIs(messages.DecodeError, 'No such wire type 3', protobuf.decode_message, test_util.OptionalMessage, bad_wire_type_message) def testUnexpectedTagBelowOne(self): """Test that completely invalid tags generate an error.""" # Message has tag 0, type NUMERIC. invalid_tag_message = chr(protobuf._Encoder.NUMERIC) self.assertErrorIs(messages.DecodeError, 'Invalid tag value 0', protobuf.decode_message, test_util.OptionalMessage, invalid_tag_message) def testProtocolBufferDecodeError(self): """Test what happens when there a ProtocolBufferDecodeError. This is what happens when the underlying ProtocolBuffer library raises it's own decode error. """ # Message has tag 1, type DOUBLE, missing value. truncated_message = ( chr((1 << protobuf._WIRE_TYPE_BITS) | protobuf._Encoder.DOUBLE)) self.assertErrorIs(messages.DecodeError, 'Decoding error: truncated', protobuf.decode_message, test_util.OptionalMessage, truncated_message) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Common utility library.""" from __future__ import with_statement __author__ = ['rafek@google.com (Rafe Kaplan)', 'guido@google.com (Guido van Rossum)', ] import cgi import inspect import re __all__ = ['AcceptItem', 'AcceptError', 'Error', 'choose_content_type', 'parse_accept_header', 'positional', ] class Error(Exception): """Base class for protorpc exceptions.""" class AcceptError(Error): """Raised when there is an error parsing the accept header.""" def positional(max_positional_args): """A decorator to declare that only the first N arguments my be positional. This decorator makes it easy to support Python 3 style key-word only parameters. For example, in Python 3 it is possible to write: def fn(pos1, *, kwonly1=None, kwonly1=None): ... All named parameters after * must be a keyword: fn(10, 'kw1', 'kw2') # Raises exception. fn(10, kwonly1='kw1') # Ok. Example: To define a function like above, do: @positional(1) def fn(pos1, kwonly1=None, kwonly2=None): ... If no default value is provided to a keyword argument, it becomes a required keyword argument: @positional(0) def fn(required_kw): ... This must be called with the keyword parameter: fn() # Raises exception. fn(10) # Raises exception. fn(required_kw=10) # Ok. When defining instance or class methods always remember to account for 'self' and 'cls': class MyClass(object): @positional(2) def my_method(self, pos1, kwonly1=None): ... @classmethod @positional(2) def my_method(cls, pos1, kwonly1=None): ... Args: max_positional_arguments: Maximum number of positional arguments. All parameters after the this index must be keyword only. Returns: A decorator that prevents using arguments after max_positional_args from being used as positional parameters. Raises: TypeError if a key-word only argument is provided as a positional parameter. """ def positional_decorator(wrapped): def positional_wrapper(*args, **kwargs): if len(args) > max_positional_args: plural_s = '' if max_positional_args != 1: plural_s = 's' raise TypeError('%s() takes at most %d positional argument%s ' '(%d given)' % (wrapped.__name__, max_positional_args, plural_s, len(args))) return wrapped(*args, **kwargs) return positional_wrapper if isinstance(max_positional_args, (int, long)): return positional_decorator else: args, _, _, defaults = inspect.getargspec(max_positional_args) return positional(len(args) - len(defaults))(max_positional_args) # TODO(rafek): Support 'level' from the Accept header standard. class AcceptItem(object): """Encapsulate a single entry of an Accept header. Parses and extracts relevent values from an Accept header and implements a sort order based on the priority of each requested type as defined here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Accept headers are normally a list of comma separated items. Each item has the format of a normal HTTP header. For example: Accept: text/plain, text/html, text/*, */* This header means to prefer plain text over HTML, HTML over any other kind of text and text over any other kind of supported format. This class does not attempt to parse the list of items from the Accept header. The constructor expects the unparsed sub header and the index within the Accept header that the fragment was found. Properties: index: The index that this accept item was found in the Accept header. main_type: The main type of the content type. sub_type: The sub type of the content type. q: The q value extracted from the header as a float. If there is no q value, defaults to 1.0. values: All header attributes parsed form the sub-header. sort_key: A tuple (no_main_type, no_sub_type, q, no_values, index): no_main_type: */* has the least priority. no_sub_type: Items with no sub-type have less priority. q: Items with lower q value have less priority. no_values: Items with no values have less priority. index: Index of item in accept header is the last priority. """ __CONTENT_TYPE_REGEX = re.compile(r'^([^/]+)/([^/]+)$') def __init__(self, accept_header, index): """Parse component of an Accept header. Args: accept_header: Unparsed sub-expression of accept header. index: The index that this accept item was found in the Accept header. """ accept_header = accept_header.lower() content_type, values = cgi.parse_header(accept_header) match = self.__CONTENT_TYPE_REGEX.match(content_type) if not match: raise AcceptError('Not valid Accept header: %s' % accept_header) self.__index = index self.__main_type = match.group(1) self.__sub_type = match.group(2) self.__q = float(values.get('q', 1)) self.__values = values if self.__main_type == '*': self.__main_type = None if self.__sub_type == '*': self.__sub_type = None self.__sort_key = (not self.__main_type, not self.__sub_type, -self.__q, not self.__values, self.__index) @property def index(self): return self.__index @property def main_type(self): return self.__main_type @property def sub_type(self): return self.__sub_type @property def q(self): return self.__q @property def values(self): """Copy the dictionary of values parsed from the header fragment.""" return dict(self.__values) @property def sort_key(self): return self.__sort_key def match(self, content_type): """Determine if the given accept header matches content type. Args: content_type: Unparsed content type string. Returns: True if accept header matches content type, else False. """ content_type, _ = cgi.parse_header(content_type) match = self.__CONTENT_TYPE_REGEX.match(content_type.lower()) if not match: return False main_type, sub_type = match.group(1), match.group(2) if not(main_type and sub_type): return False return ((self.__main_type is None or self.__main_type == main_type) and (self.__sub_type is None or self.__sub_type == sub_type)) def __cmp__(self, other): """Comparison operator based on sort keys.""" if not isinstance(other, AcceptItem): return NotImplemented return cmp(self.sort_key, other.sort_key) def __str__(self): """Rebuilds Accept header.""" content_type = '%s/%s' % (self.__main_type or '*', self.__sub_type or '*') values = self.values if values: value_strings = ['%s=%s' % (i, v) for i, v in values.iteritems()] return '%s; %s' % (content_type, '; '.join(value_strings)) else: return content_type def __repr__(self): return 'AcceptItem(%r, %d)' % (str(self), self.__index) def parse_accept_header(accept_header): """Parse accept header. Args: accept_header: Unparsed accept header. Does not include name of header. Returns: List of AcceptItem instances sorted according to their priority. """ accept_items = [] for index, header in enumerate(accept_header.split(',')): accept_items.append(AcceptItem(header, index)) return sorted(accept_items) def choose_content_type(accept_header, supported_types): """Choose most appropriate supported type based on what client accepts. Args: accept_header: Unparsed accept header. Does not include name of header. supported_types: List of content-types supported by the server. The index of the supported types determines which supported type is prefered by the server should the accept header match more than one at the same priority. Returns: The preferred supported type if the accept header matches any, else None. """ for accept_item in parse_accept_header(accept_header): for supported_type in supported_types: if accept_item.match(supported_type): return supported_type return None
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Remote service library. This module contains classes that are useful for building remote services that conform to a standard request and response model. To conform to this model a service must be like the following class: # Each service instance only handles a single request and is then discarded. # Make these objects light weight. class Service(object): # It must be possible to construct service objects without any parameters. # If your constructor needs extra information you should provide a # no-argument factory function to create service instances. def __init__(self): ... # Each remote method must use the 'remote' decorator, passing the request # and response message types. The remote method itself must take a single # parameter which is an instance of RequestMessage and return an instance # of ResponseMessage. @method(RequestMessage, ResponseMessage) def remote_method(self, request): # Return an instance of ResponseMessage. # A service object may optionally implement a 'initialize_request_state' # method that takes as a parameter a single instance of a RequestState. If # a service does not implement this method it will not receive the request # state. def initialize_request_state(self, state): ... The 'Service' class is provided as a convenient base class that provides the above functionality. It implements all required and optional methods for a service. It also has convenience methods for creating factory functions that can pass persistent global state to a new service instance. The 'remote' decorator is used to declare which methods of a class are meant to service RPCs. While this decorator is not responsible for handling actual remote method invocations, such as handling sockets, handling various RPC protocols and checking messages for correctness, it does attach information to methods that responsible classes can examine and ensure the correctness of the RPC. When the remote decorator is used on a method, the wrapper method will have a 'remote' property associated with it. The 'remote' property contains the request_type and response_type expected by the methods implementation. On its own, the remote decorator does not provide any support for subclassing remote methods. In order to extend a service, one would need to redecorate the sub-classes methods. For example: class MyService(Service): @method(DoSomethingRequest, DoSomethingResponse) def do_something(self, request): ... implement do-something ... class MyBetterService(Service): @method(DoSomethingRequest, DoSomethingResponse) def do_something(self, request): response = super(MyService, self).do_something.remote.method(request) ... do something with response ... return response A Service subclass also has a Stub class that can be used with a transport for making RPCs. When a stub is created, it is capable of doing both synchronous and asynchronous RPCs if the underlying transport supports it. To make a stub using an HTTP transport do: my_service = MyService.Stub(HttpTransport('<my service URL>')) For synchronous calls, just call the expected methods on the service stub: request = DoSomethingRequest() ... response = my_service.do_something(request) Each stub instance has an async object that can be used for initiating asynchronous RPCs if the underlying protocol transport supports it. To make an asynchronous call, do: rpc = my_service.async.do_something(request) response = rpc.get_response() """ __author__ = 'rafek@google.com (Rafe Kaplan)' import logging import sys from protorpc import message_types from protorpc import messages from protorpc import descriptor from protorpc import util __all__ = [ 'ApplicationError', 'NetworkError', 'RequestError', 'RpcError', 'ServerError', 'ServerError', 'ServiceDefinitionError', 'RequestState', 'RpcState', 'RpcStatus', 'Service', 'StubBase', 'check_rpc_status', 'get_remote_method_info', 'is_error_status', 'method', 'remote', ] class ServiceDefinitionError(messages.Error): """Raised when a service is improperly defined.""" # TODO: Use error_name to map to specific exception message types. class RpcStatus(messages.Message): """Status of on-going or complete RPC. Fields: state: State of RPC. error_name: Error name set by application. Only set when status is APPLICATION_ERROR. For use by application to transmit specific reason for error. error_message: Error message associated with status. """ class State(messages.Enum): """Enumeration of possible RPC states. Values: OK: Completed successfully. RUNNING: Still running, not complete. REQUEST_ERROR: Request was malformed or incomplete. SERVER_ERROR: Server experienced an unexpected error. NETWORK_ERROR: An error occured on the network. APPLICATION_ERROR: The application is indicating an error. When in this state, RPC should also set application_error. """ OK = 0 RUNNING = 1 REQUEST_ERROR = 2 SERVER_ERROR = 3 NETWORK_ERROR = 4 APPLICATION_ERROR = 5 state = messages.EnumField(State, 1, required=True) error_message = messages.StringField(2) error_name = messages.StringField(3) RpcState = RpcStatus.State class RpcError(messages.Error): """Base class for RPC errors. Each sub-class of RpcError is associated with an error value from RpcState and has an attribute STATE that refers to that value. """ def __init__(self, message, cause=None): super(RpcError, self).__init__(message) self.cause = cause @classmethod def from_state(cls, state): """Get error class from RpcState. Args: state: RpcState value. Can be enum value itself, string or int. Returns: Exception class mapped to value if state is an error. Returns None if state is OK or RUNNING. """ return _RPC_STATE_TO_ERROR.get(RpcState(state)) class RequestError(RpcError): """Raised when wrong request objects received during method invocation.""" STATE = RpcState.REQUEST_ERROR class NetworkError(RpcError): """Raised when network error occurs during RPC.""" STATE = RpcState.NETWORK_ERROR class ServerError(RpcError): """Unexpected error occured on server.""" STATE = RpcState.SERVER_ERROR class ApplicationError(RpcError): """Raised for application specific errors. Attributes: error_name: Application specific error name for exception. """ STATE = RpcState.APPLICATION_ERROR def __init__(self, message, error_name=None): """Constructor. Args: message: Application specific error message. error_name: Application specific error name. Must be None, string or unicode string. """ super(ApplicationError, self).__init__(message) if error_name is None: self.error_name = None else: self.error_name = error_name def __str__(self): return self.args[0] def __repr__(self): if self.error_name is None: error_format = '' else: error_format = ', %r' % self.error_name return '%s(%r%s)' % (type(self).__name__, self.args[0], error_format) _RPC_STATE_TO_ERROR = { RpcState.REQUEST_ERROR: RequestError, RpcState.NETWORK_ERROR: NetworkError, RpcState.SERVER_ERROR: ServerError, RpcState.APPLICATION_ERROR: ApplicationError, } class _RemoteMethodInfo(object): """Object for encapsulating remote method information. An instance of this method is associated with the 'remote' attribute of the methods 'invoke_remote_method' instance. Instances of this class are created by the remote decorator and should not be created directly. """ def __init__(self, method, request_type, response_type): """Constructor. Args: method: The method which implements the remote method. This is a function that will act as an instance method of a class definition that is decorated by '@method'. It must always take 'self' as its first parameter. request_type: Expected request type for the remote method. response_type: Expected response type for the remote method. """ self.__method = method self.__request_type = request_type self.__response_type = response_type @property def method(self): """Original undecorated method.""" return self.__method @property def request_type(self): """Expected request type for remote method.""" if isinstance(self.__request_type, basestring): self.__request_type = messages.find_definition( self.__request_type, relative_to=sys.modules[self.__method.__module__]) return self.__request_type @property def response_type(self): """Expected response type for remote method.""" if isinstance(self.__response_type, basestring): self.__response_type = messages.find_definition( self.__response_type, relative_to=sys.modules[self.__method.__module__]) return self.__response_type def method(request_type, response_type): """Method decorator for creating remote methods. Args: request_type: Message type of expected request. response_type: Message type of expected response. Returns: 'remote_method_wrapper' function. Raises: TypeError: if the request_type or response_type parameters are not proper subclasses of messages.Message. """ if (not isinstance(request_type, basestring) and (not isinstance(request_type, type) or not issubclass(request_type, messages.Message) or request_type is messages.Message)): raise TypeError( 'Must provide message class for request-type. Found %s', request_type) if (not isinstance(response_type, basestring) and (not isinstance(response_type, type) or not issubclass(response_type, messages.Message) or response_type is messages.Message)): raise TypeError( 'Must provide message class for response-type. Found %s', response_type) def remote_method_wrapper(method): """Decorator used to wrap method. Args: method: Original method being wrapped. Returns: 'invoke_remote_method' function responsible for actual invocation. This invocation function instance is assigned an attribute 'remote' which contains information about the remote method: request_type: Expected request type for remote method. response_type: Response type returned from remote method. Raises: TypeError: If request_type or response_type is not a subclass of Message or is the Message class itself. """ def invoke_remote_method(service_instance, request): """Function used to replace original method. Invoke wrapped remote method. Checks to ensure that request and response objects are the correct types. Does not check whether messages are initialized. Args: service_instance: The service object whose method is being invoked. This is passed to 'self' during the invocation of the original method. request: Request message. Returns: Results of calling wrapped remote method. Raises: RequestError: Request object is not of the correct type. ServerError: Response object is not of the correct type. """ if not isinstance(request, remote_method_info.request_type): raise RequestError('Method %s.%s expected request type %s, ' 'received %s' % (type(service_instance).__name__, method.__name__, remote_method_info.request_type, type(request))) response = method(service_instance, request) if not isinstance(response, remote_method_info.response_type): raise ServerError('Method %s.%s expected response type %s, ' 'sent %s' % (type(service_instance).__name__, method.__name__, remote_method_info.response_type, type(response))) return response remote_method_info = _RemoteMethodInfo(method, request_type, response_type) invoke_remote_method.remote = remote_method_info invoke_remote_method.__name__ = method.__name__ return invoke_remote_method return remote_method_wrapper def remote(request_type, response_type): """Temporary backward compatibility alias for method.""" logging.warning('The remote decorator has been renamed method. It will be ' 'removed in very soon from future versions of ProtoRPC.') return method(request_type, response_type) def get_remote_method_info(method): """Get remote method info object from remote method. Returns: Remote method info object if method is a remote method, else None. """ if not callable(method): return None try: method_info = method.remote except AttributeError: return None if not isinstance(method_info, _RemoteMethodInfo): return None return method_info class StubBase(object): """Base class for client side service stubs. The remote method stubs are created by the _ServiceClass meta-class when a Service class is first created. The resulting stub will extend both this class and the service class it handles communications for. Assume that there is a service: class NewContactRequest(messages.Message): name = messages.StringField(1, required=True) phone = messages.StringField(2) email = messages.StringField(3) class AccountService(remote.Service): @remote.method(NewContact, message_types.VoidMessage): def new_contact(self, request): ... implementation ... A stub of this service can be called in two ways. The first is to pass in a correctly initialized NewContactRequest message: request = NewContactRequest() request.name = 'Bob Somebody' request.phone = '+1 415 555 1234' account_service_stub.new_contact(request) The second way is to pass in keyword parameters that correspond with the root request message type: account_service_stub.new_contact(name='Bob Somebody', phone='+1 415 555 1234') The second form will create a request message of the appropriate type. """ def __init__(self, transport): """Constructor. Args: transport: Underlying transport to communicate with remote service. """ self.__transport = transport @property def transport(self): """Transport used to communicate with remote service.""" return self.__transport @classmethod def definition_name(self): """Delegates to underlying service instance.""" return super(StubBase, self).definition_name() class _ServiceClass(type): """Meta-class for service class.""" def __new_async_method(cls, remote): """Create asynchronous method for Async handler. Args: remote: RemoteInfo to create method for. """ def async_method(self, *args, **kwargs): """Asynchronous remote method. Args: self: Instance of StubBase.Async subclass. Stub methods either take a single positional argument when a full request message is passed in, or keyword arguments, but not both. See docstring for StubBase for more information on how to use remote stub methods. Returns: Rpc instance used to represent asynchronous RPC. """ if args and kwargs: raise TypeError('May not provide both args and kwargs') if not args: # Construct request object from arguments. request = remote.request_type() for name, value in kwargs.iteritems(): setattr(request, name, value) else: # First argument is request object. request = args[0] return self.transport.send_rpc(remote, request) async_method.__name__ = remote.method.__name__ async_method = util.positional(2)(async_method) async_method.remote = remote return async_method def __new_sync_method(cls, async_method): """Create synchronous method for stub. Args: async_method: asynchronous method to delegate calls to. """ def sync_method(self, *args, **kwargs): """Synchronous remote method. Args: self: Instance of StubBase.Async subclass. args: Tuple (request,): request: Request object. kwargs: Field values for request. Must be empty if request object is provided. Returns: Response message from synchronized RPC. """ return async_method(self.async, *args, **kwargs).response sync_method.__name__ = async_method.__name__ sync_method.remote = async_method.remote return sync_method def __create_async_methods(cls, remote_methods): """Construct a dictionary of asynchronous methods based on remote methods. Args: remote_methods: Dictionary of methods with associated RemoteInfo objects. Returns: Dictionary of asynchronous methods with assocaited RemoteInfo objects. Results added to AsyncStub subclass. """ async_methods = {} for method_name, method in remote_methods.iteritems(): async_methods[method_name] = cls.__new_async_method(method.remote) return async_methods def __create_sync_methods(cls, async_methods): """Construct a dictionary of synchronous methods based on remote methods. Args: async_methods: Dictionary of async methods to delegate calls to. Returns: Dictionary of synchronous methods with assocaited RemoteInfo objects. Results added to Stub subclass. """ sync_methods = {} for method_name, async_method in async_methods.iteritems(): sync_methods[method_name] = cls.__new_sync_method(async_method) return sync_methods def __new__(cls, name, bases, dct): """Instantiate new service class instance.""" if StubBase not in bases: # Collect existing remote methods. base_methods = {} for base in bases: try: remote_methods = base.__remote_methods except AttributeError: pass else: base_methods.update(remote_methods) # Set this class private attribute so that base_methods do not have # to be recacluated in __init__. dct['_ServiceClass__base_methods'] = base_methods for attribute, value in dct.iteritems(): base_method = base_methods.get(attribute, None) if base_method: if not callable(value): raise ServiceDefinitionError( 'Must override %s in %s with a method.' % ( attribute, name)) if get_remote_method_info(value): raise ServiceDefinitionError( 'Do not use remote decorator when overloading remote method %s ' 'on service %s.' % (attribute, name)) base_remote_method_info = get_remote_method_info(base_method) remote_decorator = method( base_remote_method_info.request_type, base_remote_method_info.response_type) new_remote_method = remote_decorator(value) dct[attribute] = new_remote_method return type.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): """Create uninitialized state on new class.""" type.__init__(cls, name, bases, dct) # Only service implementation classes should have remote methods and stub # sub classes created. Stub implementations have their own methods passed # in to the type constructor. if StubBase not in bases: # Create list of remote methods. cls.__remote_methods = dict(cls.__base_methods) for attribute, value in dct.iteritems(): value = getattr(cls, attribute) remote_method_info = get_remote_method_info(value) if remote_method_info: cls.__remote_methods[attribute] = value # Build asynchronous stub class. stub_attributes = {'Service': cls} async_methods = cls.__create_async_methods(cls.__remote_methods) stub_attributes.update(async_methods) async_class = type('AsyncStub', (StubBase, cls), stub_attributes) cls.AsyncStub = async_class # Constructor for synchronous stub class. def __init__(self, transport): """Constructor. Args: transport: Underlying transport to communicate with remote service. """ super(cls.Stub, self).__init__(transport) self.async = cls.AsyncStub(transport) # Build synchronous stub class. stub_attributes = {'Service': cls, '__init__': __init__} stub_attributes.update(cls.__create_sync_methods(async_methods)) cls.Stub = type('Stub', (StubBase, cls), stub_attributes) @staticmethod def all_remote_methods(cls): """Get all remote methods of service. Returns: Dict from method name to unbound method. """ return dict(cls.__remote_methods) class RequestState(object): """Request state information. Attributes: remote_host: Remote host name where request originated. remote_address: IP address where request originated. server_host: Host of server within which service resides. server_port: Post which service has recevied request from. """ @util.positional(1) def __init__(self, remote_host=None, remote_address=None, server_host=None, server_port=None): """Constructor. Args: remote_host: Assigned to attribute. remote_address: Assigned to attribute. server_host: Assigned to attribute. server_port: Assigned to attribute. """ self.remote_host = remote_host self.remote_address = remote_address self.server_host = server_host self.server_port = server_port def __repr__(self): """String representation of state.""" state = [] if self.remote_host: state.append(('remote_host', self.remote_host)) if self.remote_address: state.append(('remote_address', self.remote_address)) if self.server_host: state.append(('server_host', self.server_host)) if self.server_port: state.append(('server_port', self.server_port)) if state: state_string = ' ' + ' '.join( '%s=%s' % (name, value) for (name, value) in state) else: state_string = '' return '<remote.RequestState%s>' % state_string class Service(object): """Service base class. Base class used for defining remote services. Contains reflection functions, useful helpers and built-in remote methods. Services are expected to be constructed via either a constructor or factory which takes no parameters. However, it might be required that some state or configuration is passed in to a service across multiple requests. To do this, define parameters to the constructor of the service and use the 'new_factory' class method to build a constructor that will transmit parameters to the constructor. For example: class MyService(Service): def __init__(self, configuration, state): self.configuration = configuration self.state = state configuration = MyServiceConfiguration() global_state = MyServiceState() my_service_factory = MyService.new_factory(configuration, state=global_state) The contract with any service handler is that a new service object is created to handle each user request, and that the construction does not take any parameters. The factory satisfies this condition: new_instance = my_service_factory() assert new_instance.state is global_state Attributes: request_state: RequestState set via initialize_request_state. """ __metaclass__ = _ServiceClass @classmethod def all_remote_methods(cls): """Get all remote methods for service class. Built-in methods do not appear in the dictionary of remote methods. Returns: Dictionary mapping method name to remote method. """ return _ServiceClass.all_remote_methods(cls) @classmethod def new_factory(cls, *args, **kwargs): """Create factory for service. Useful for passing configuration or state objects to the service. Accepts arbitrary parameters and keywords, however, underlying service must accept also accept not other parameters in its constructor. Args: args: Args to pass to service constructor. kwargs: Keyword arguments to pass to service constructor. Returns: Factory function that will create a new instance and forward args and keywords to the constructor. """ def service_factory(): return cls(*args, **kwargs) # Update docstring so that it is easier to debug. full_class_name = '%s.%s' % (cls.__module__, cls.__name__) service_factory.func_doc = ( 'Creates new instances of service %s.\n\n' 'Returns:\n' ' New instance of %s.' % (cls.__name__, full_class_name)) # Update name so that it is easier to debug the factory function. service_factory.func_name = '%s_service_factory' % cls.__name__ service_factory.service_class = cls return service_factory def initialize_request_state(self, request_state): """Save request state for use in remote method. Args: request_state: RequestState instance. """ self.__request_state = request_state @classmethod def definition_name(cls): """Get definition name for Service class. Package name is determined by the global 'package' attribute in the module that contains the Service definition. If no 'package' attribute is available, uses module name. If no module is found, just uses class name as name. Returns: Fully qualified service name. """ try: return cls.__definition_name except AttributeError: module = sys.modules.get(cls.__module__) if module: try: package = module.package except AttributeError: package = cls.__module__ cls.__definition_name = '%s.%s' % (package, cls.__name__) else: cls.__definition_name = cls.__name__ return cls.__definition_name @property def request_state(self): """Request state associated with this Service instance.""" return self.__request_state def is_error_status(status): """Function that determines whether the RPC status is an error. Args: status: Initialized RpcStatus message to check for errors. """ status.check_initialized() return RpcError.from_state(status.state) is not None def check_rpc_status(status): """Function converts an error status to a raised exception. Args: status: Initialized RpcStatus message to check for errors. Raises: RpcError according to state set on status, if it is an error state. """ status.check_initialized() error_class = RpcError.from_state(status.state) if error_class is not None: if error_class is ApplicationError: raise error_class(status.error_message, status.error_name) else: raise error_class(status.error_message)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Library for defining protocol messages in the Python language.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import itertools from google.protobuf import descriptor from google.protobuf import message from google.protobuf import reflection from protorpc import util __all__ = ['Error', 'EnumDefinitionError', 'MessageDefinitionError', 'BooleanField', 'BytesField', 'Enum', 'EnumField', 'FloatField', 'IntegerField', 'Message', 'StringField', ] class Error(Exception): """Base class for message exceptions.""" class EnumDefinitionError(Error): """Enumeration definition error.""" class MessageDefinitionError(Error): """Message definition error.""" _CPP_TYPE_MAP = { descriptor.FieldDescriptor.TYPE_DOUBLE: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_FLOAT: descriptor.FieldDescriptor.CPPTYPE_FLOAT, descriptor.FieldDescriptor.TYPE_INT64: descriptor.FieldDescriptor.CPPTYPE_INT64, descriptor.FieldDescriptor.TYPE_UINT64: descriptor.FieldDescriptor.CPPTYPE_UINT64, descriptor.FieldDescriptor.TYPE_INT32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_FIXED64: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_FIXED32: descriptor.FieldDescriptor.CPPTYPE_DOUBLE, descriptor.FieldDescriptor.TYPE_BOOL: descriptor.FieldDescriptor.CPPTYPE_BOOL, descriptor.FieldDescriptor.TYPE_STRING: descriptor.FieldDescriptor.CPPTYPE_STRING, descriptor.FieldDescriptor.TYPE_MESSAGE: descriptor.FieldDescriptor.CPPTYPE_MESSAGE, descriptor.FieldDescriptor.TYPE_BYTES: descriptor.FieldDescriptor.CPPTYPE_STRING, descriptor.FieldDescriptor.TYPE_UINT32: descriptor.FieldDescriptor.CPPTYPE_UINT32, descriptor.FieldDescriptor.TYPE_ENUM: descriptor.FieldDescriptor.CPPTYPE_ENUM, descriptor.FieldDescriptor.TYPE_SFIXED32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_SFIXED64: descriptor.FieldDescriptor.CPPTYPE_INT64, descriptor.FieldDescriptor.TYPE_SINT32: descriptor.FieldDescriptor.CPPTYPE_INT32, descriptor.FieldDescriptor.TYPE_SINT64: descriptor.FieldDescriptor.CPPTYPE_INT64, } class _EnumType(type): """Meta-class used for defining the Enum classes. Meta-class enables very specific behavior for any defined Enum class. All attributes defined on an Enum sub-class must be non-repeating integers. The meta-class ensures that only one level of Enum class hierarchy is possible. In other words it is not possible to delcare sub-classes of sub-classes of Enum. The class definition is used mainly for syntactic sugar. It is used by the _DynamicProtocolMessageType meta-class to initialize a descriptor object and then discarded. The class definition will NOT appear in the resulting class. The meta-class creates a class attribute _VALUES which is an ordered list of tuples (name, number) of the Enum definition in number order. """ __initialized = False __allowed_names = None def __new__(cls, name, bases, dictionary): if not _EnumType.__initialized: _EnumType.__initialized = True else: if bases != (Enum,): raise EnumDefinitionError('Enum classes may not be subclassed.') if not _EnumType.__allowed_names: _EnumType.__allowed_names = set(dir(Enum)) values = [] for attribute_name, value in dictionary.iteritems(): if attribute_name == '__module__': continue if not isinstance(value, (int, long)): raise EnumDefinitionError('Enum value %s must be an integer.' % value) values.append((attribute_name, value)) values.sort(key=lambda v: v[1]) dictionary['_VALUES'] = values return super(_EnumType, cls).__new__(cls, name, bases, dictionary) class Enum(object): """Base class for all enumerated types. Enumerated types are not meant to be instantiated. """ __metaclass__ = _EnumType def __init__(self): raise NotImplementedError() class _DynamicProtocolMessageType(reflection.GeneratedProtocolMessageType): """Meta-class used for defining the dynamic Message base class. For more details about Message classes, see the Message class docstring and protocol buffers: http://code.google.com/apis/protocolbuffers/docs/reference/python/index.html This meta-class enables very specific behavior for any defined Message class. All attributes defined on an Message sub-class must be field instances, Enum class definitions or other Message class definitions. Each field attribute defined on an Message sub-class is added to the set of field definitions and the attribute is translated in to FieldDescriptor. It also ensures that only one level of Message class hierarchy is possible. In other words it is not possible to declare sub-classes of sub-classes of Message. """ def __new__(cls, name, bases, dictionary): enums = [] enum_map = {} messages = [] field_definitions = [] fields = [] module = dictionary['__module__'] def update_nested_definitions(definition, root_name): """Update nested message, enum and field definitions When each message class is created, it cannot know what it's containing parent is. It is therefore necessary to recreate the full-name of nested messagse, enums and fields when every new message is created and to assign the definition's containing type. This method is recursive because any message definitions found within must also be updated. Args: definition: Definition that will be updated. root_name: The name of the module or definition containing this definition. """ # TODO(rafek): This is potentially an expensive process. Ideally the # descriptor should be able to generate a full name for a class based # on the containing types. definition.full_name = '%s.%s' % (root_name, definition.name) if isinstance(definition, descriptor.Descriptor): for sub_definition in itertools.chain(definition.nested_types, definition.enum_types, definition.fields): update_nested_definitions(sub_definition, definition.full_name) sub_definition.containing_type = definition # No additional intialization necessary for Message class defined in this # module. if bases != (message.Message,): # Do not subclass message classes. if bases != (Message,): raise MessageDefinitionError('May not subclass Message types.') # Configure nested definitions and fields. for attribute_name, value in dictionary.iteritems(): if attribute_name == '__module__': continue # Enumeration definitions. if isinstance(value, type) and issubclass(value, Enum): enum_numbers = [] for index, (enum_name, enum_number) in enumerate(value._VALUES): enum_numbers.append(descriptor.EnumValueDescriptor(name=enum_name, index=index, number=enum_number)) enum = descriptor.EnumDescriptor(name=attribute_name, full_name='', filename='', values=enum_numbers) enums.append(enum) enum_map[enum.name] = enum # Sub-message defintions. elif isinstance(value, type) and issubclass(value, message.Message): messages.append(value.DESCRIPTOR) # Field definitions. The fields are not configured here since they # must be processed in numeric order. elif isinstance(value, _Field): field_definitions.append((attribute_name, value)) else: raise MessageDefinitionError('Non-definition field %s.' % attribute_name) # Define fields in numeric order. field_definitions.sort(key=lambda v: v[1].number) for index, (attribute_name, field) in enumerate(field_definitions): if field.required and field.repeated: raise MessageDefinitionError('Field %s must be either required ' 'or repeated, not both' % attribute_name) default_value = field.default if field.required: label = descriptor.FieldDescriptor.LABEL_REQUIRED elif field.repeated: label = descriptor.FieldDescriptor.LABEL_REPEATED if default_value is None: default_value = [] else: label = descriptor.FieldDescriptor.LABEL_OPTIONAL if isinstance(field, EnumField): try: enum_type = enum_map[field.enum_type.__name__] except KeyError: raise MessageDefinitionError('Field %s may only use Enum type ' 'defined in same Message.' % attribute_name) else: enum_type = None fields.append(descriptor.FieldDescriptor( name=attribute_name, full_name='', index=index, number=field.number, type=field.variant, cpp_type=_CPP_TYPE_MAP[field.variant], label=label, default_value=default_value, message_type=None, enum_type=enum_type, containing_type=None, is_extension=False, extension_scope=None, has_default_value=field.default is not None)) # Throw away the Enum class definitions. for enum in enums: del dictionary[enum.name] # Define message descriptor. message_descriptor = descriptor.Descriptor(name=name, full_name='', filename='', containing_type=None, fields=fields, nested_types=messages, enum_types=enums, extensions=[]) update_nested_definitions(message_descriptor, module) dictionary[_DynamicProtocolMessageType._DESCRIPTOR_KEY] = message_descriptor if bases == (message.Message,): superclass = super(reflection.GeneratedProtocolMessageType, cls) else: superclass = super(_DynamicProtocolMessageType, cls) return superclass.__new__(cls, name, bases, dictionary) class Message(message.Message): """Base class for user defined message objects. Used to define messages for efficient transmission across network or process space. Messages are defined using the field classes (IntegerField, FloatField, EnumField, etc.). Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class is intentended to itself be transmitted across network or process space and used directly by clients or even other servers. As such methods and non-field attributes could not be transmitted with the structural information causing discrepancies between different languages and implementations. For more detail about how this message class works, please see: http://code.google.com/apis/protocolbuffers/docs/reference/python/index.html Field definitions are discarded by the meta-class and do not appear in the final class definition. In their place are a property instance defined by reflection.GeneratedProtocolMessageType. Example: class Lot(Message): price = IntegerField(1, required=True) quantity = IntegerField(2, required=True) class Order(Message): class TradeType(Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 symbol = StringProperty(1, required=True) total_quantity = IntegerProperty(2, required=True) trade_type = EnumProperty(TradeType, 3, required=True) limit = IntegerField(5) order = Order() assert not order.IsInitialized() order.symbol = 'GOOG' order.total_quantity = 10 order.trade_type = Order.BUY # Now object is initialized! assert order.IsInitialized() """ __metaclass__ = _DynamicProtocolMessageType __slots__ = [] class _Field(object): @util.positional(2) def __init__(self, number, required=False, repeated=False, variant=None, default=None): """Constructor. Store the attributes of a field so that the _DynamicProtocolMessageType meta-class can use it to populate field descriptors for the Message class. Instances of field are discarded after used by the meta-class. The required and repeated parameters are mutually exclusive. Setting both to True will raise a FieldDefinitionError. Repeated fields may not have default values. Sub-class Attributes: Each sub-class of _Field must define the following: VARIANTS: Set of variant types accepted by that field. DEFAULT_VARIANT: Default variant type if not specified in constructor. Args: number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive with 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive with 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: MessageDefinitionError when repeated fields are provided a default value or when an incompatible variant is provided. TypeError when an unexpected keyword argument is provided. """ self.number = number self.required = required self.repeated = repeated if self.repeated and default is not None: raise MessageDefinitionError( 'May not provide default for repeated fields.') self.default = default if variant is None: self.variant = self.DEFAULT_VARIANT else: self.variant = variant if self.variant not in self.VARIANTS: raise MessageDefinitionError('Bad variant.') class IntegerField(_Field): """Field definition for integer values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_INT64 VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_INT32, descriptor.FieldDescriptor.TYPE_INT64, descriptor.FieldDescriptor.TYPE_UINT32, descriptor.FieldDescriptor.TYPE_INT64, descriptor.FieldDescriptor.TYPE_SINT32, descriptor.FieldDescriptor.TYPE_SINT64, ]) class FloatField(_Field): """Field definition for float values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_DOUBLE VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_FLOAT, descriptor.FieldDescriptor.TYPE_DOUBLE, ]) class BooleanField(_Field): """Field definition for boolean values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_BOOL VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_BOOL]) class BytesField(_Field): """Field definition for byte (str) values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_BYTES VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_BYTES]) class StringField(_Field): """Field definition for unicode string values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_STRING VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_STRING]) class EnumField(_Field): """Field definition for enum values.""" DEFAULT_VARIANT = descriptor.FieldDescriptor.TYPE_ENUM VARIANTS = frozenset([descriptor.FieldDescriptor.TYPE_ENUM]) def __init__(self, enum_type, number, **kwargs): """Constructor. Args: enum_type: Enum type for field. Must be subclass of Enum. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. default: Default value for field if not found in stream. Raises: TypeError when invalid enum_type is provided. """ # TODO(rafek): Support enumerated types outside of single message # definition scope. if isinstance(enum_type, type) and not issubclass(enum_type, Enum): raise TypeError('Enum field requires Enum class.') self.enum_type = enum_type super(EnumField, self).__init__(number, **kwargs)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.service_handlers.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import cStringIO import os import re import sys import unittest import urllib from google.appengine.ext import webapp from protorpc import forms from protorpc import messages from protorpc import protobuf from protorpc import protojson from protorpc import protourlencode from protorpc import message_types from protorpc import registry from protorpc import remote from protorpc import service_handlers from protorpc import test_util from protorpc import webapp_test_util import mox package = 'test_package' class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = service_handlers class Enum1(messages.Enum): """A test enum class.""" VAL1 = 1 VAL2 = 2 VAL3 = 3 class Request1(messages.Message): """A test request message type.""" integer_field = messages.IntegerField(1) string_field = messages.StringField(2) enum_field = messages.EnumField(Enum1, 3) class Response1(messages.Message): """A test response message type.""" integer_field = messages.IntegerField(1) string_field = messages.StringField(2) enum_field = messages.EnumField(Enum1, 3) class SuperMessage(messages.Message): """A test message with a nested message field.""" sub_message = messages.MessageField(Request1, 1) sub_messages = messages.MessageField(Request1, 2, repeated=True) class SuperSuperMessage(messages.Message): """A test message with two levels of nested.""" sub_message = messages.MessageField(SuperMessage, 1) sub_messages = messages.MessageField(Request1, 2, repeated=True) class RepeatedMessage(messages.Message): """A test message with a repeated field.""" ints = messages.IntegerField(1, repeated=True) strings = messages.StringField(2, repeated=True) enums = messages.EnumField(Enum1, 3, repeated=True) class Service(object): """A simple service that takes a Request1 and returns Request2.""" @remote.method(Request1, Response1) def method1(self, request): response = Response1() if hasattr(request, 'integer_field'): response.integer_field = request.integer_field if hasattr(request, 'string_field'): response.string_field = request.string_field if hasattr(request, 'enum_field'): response.enum_field = request.enum_field return response @remote.method(RepeatedMessage, RepeatedMessage) def repeated_method(self, request): response = RepeatedMessage() if hasattr(request, 'ints'): response = request.ints return response def not_remote(self): pass def VerifyResponse(test, response, expected_status, expected_status_message, expected_content): def write(content): if expected_content == '': test.assertEquals('', content) else: test.assertNotEquals(-1, content.find(expected_content)) def start_response(response, headers): status, message = response.split(' ', 1) test.assertEquals(expected_status, status) test.assertEquals(expected_status_message, message) return write response.wsgi_write(start_response) class ServiceHandlerFactoryTest(test_util.TestCase): """Tests for the service handler factory.""" def testAllRequestMappers(self): """Test all_request_mappers method.""" configuration = service_handlers.ServiceHandlerFactory(Service) mapper1 = service_handlers.RPCMapper(['whatever'], 'whatever', None) mapper2 = service_handlers.RPCMapper(['whatever'], 'whatever', None) configuration.add_request_mapper(mapper1) self.assertEquals([mapper1], list(configuration.all_request_mappers())) configuration.add_request_mapper(mapper2) self.assertEquals([mapper1, mapper2], list(configuration.all_request_mappers())) def testServiceFactory(self): """Test that service_factory attribute is set.""" handler_factory = service_handlers.ServiceHandlerFactory(Service) self.assertEquals(Service, handler_factory.service_factory) def testFactoryMethod(self): """Test that factory creates correct instance of class.""" factory = service_handlers.ServiceHandlerFactory(Service) handler = factory() self.assertTrue(isinstance(handler, service_handlers.ServiceHandler)) self.assertTrue(isinstance(handler.service, Service)) def testMapping(self): """Test the mapping method.""" factory = service_handlers.ServiceHandlerFactory(Service) path, mapped_factory = factory.mapping('/my_service') self.assertEquals(r'/my_service' + service_handlers._METHOD_PATTERN, path) self.assertEquals(id(factory), id(mapped_factory)) match = re.match(path, '/my_service.my_method') self.assertEquals('my_method', match.group(1)) path, mapped_factory = factory.mapping('/my_service/nested') self.assertEquals('/my_service/nested' + service_handlers._METHOD_PATTERN, path) match = re.match(path, '/my_service/nested.my_method') self.assertEquals('my_method', match.group(1)) def testRegexMapping(self): """Test the mapping method using a regex.""" factory = service_handlers.ServiceHandlerFactory(Service) path, mapped_factory = factory.mapping('.*/my_service') self.assertEquals(r'.*/my_service' + service_handlers._METHOD_PATTERN, path) self.assertEquals(id(factory), id(mapped_factory)) match = re.match(path, '/whatever_preceeds/my_service.my_method') self.assertEquals('my_method', match.group(1)) match = re.match(path, '/something_else/my_service.my_other_method') self.assertEquals('my_other_method', match.group(1)) def testMapping_BadPath(self): """Test bad parameterse to the mapping method.""" factory = service_handlers.ServiceHandlerFactory(Service) self.assertRaises(ValueError, factory.mapping, '/my_service/') def testDefault(self): """Test the default factory convenience method.""" handler_factory = service_handlers.ServiceHandlerFactory.default( Service, parameter_prefix='my_prefix.') self.assertEquals(Service, handler_factory.service_factory) mappers = handler_factory.all_request_mappers() # Verify URL encoded mapper. url_encoded_mapper = mappers.next() self.assertTrue(isinstance(url_encoded_mapper, service_handlers.URLEncodedRPCMapper)) self.assertEquals('my_prefix.', url_encoded_mapper.parameter_prefix) # Verify Protobuf encoded mapper. protobuf_mapper = mappers.next() self.assertTrue(isinstance(protobuf_mapper, service_handlers.ProtobufRPCMapper)) # Verify JSON encoded mapper. json_mapper = mappers.next() self.assertTrue(isinstance(json_mapper, service_handlers.JSONRPCMapper)) # Should have no more mappers. self.assertRaises(StopIteration, mappers.next) class ServiceHandlerTest(webapp_test_util.RequestHandlerTestBase): """Test the ServiceHandler class.""" def setUp(self): self.mox = mox.Mox() self.service_factory = Service self.remote_host = 'remote.host.com' self.server_host = 'server.host.com' self.ResetRequestHandler() self.request = Request1() self.request.integer_field = 1 self.request.string_field = 'a' self.request.enum_field = Enum1.VAL1 def ResetRequestHandler(self): super(ServiceHandlerTest, self).setUp() def CreateService(self): return self.service_factory() def CreateRequestHandler(self): self.rpc_mapper1 = self.mox.CreateMock(service_handlers.RPCMapper) self.rpc_mapper1.http_methods = set(['POST']) self.rpc_mapper1.content_types = set(['application/x-www-form-urlencoded']) self.rpc_mapper2 = self.mox.CreateMock(service_handlers.RPCMapper) self.rpc_mapper2.http_methods = set(['GET']) self.rpc_mapper2.content_types = set(['application/x-www-form-urlencoded']) self.factory = service_handlers.ServiceHandlerFactory( self.CreateService) self.factory.add_request_mapper(self.rpc_mapper1) self.factory.add_request_mapper(self.rpc_mapper2) return self.factory() def GetEnvironment(self): """Create handler to test.""" environ = super(ServiceHandlerTest, self).GetEnvironment() if self.remote_host: environ['REMOTE_HOST'] = self.remote_host if self.server_host: environ['SERVER_HOST'] = self.server_host return environ def VerifyResponse(self, expected_status, expected_status_message, expected_content): VerifyResponse(self, self.response, expected_status, expected_status_message, expected_content) def testRedirect(self): """Test that redirection is disabled.""" self.assertRaises(NotImplementedError, self.handler.redirect, '/') def testFirstMapper(self): """Make sure service attribute works when matches first RPCMapper.""" self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testSecondMapper(self): """Make sure service attribute works when matches first RPCMapper. Demonstrates the multiplicity of the RPCMapper configuration. """ self.rpc_mapper2.build_request( self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper2.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.handle('GET', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testCaseInsensitiveContentType(self): """Ensure that matching content-type is case insensitive.""" request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = ('ApPlIcAtIoN/' 'X-wWw-FoRm-UrLeNcOdEd') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testContentTypeWithParameters(self): """Test that content types have parameters parsed out.""" request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = ('application/' 'x-www-form-urlencoded' + '; a=b; c=d') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testContentFromHeaderOnly(self): """Test getting content-type from HTTP_CONTENT_TYPE directly. Some bad web server implementations might decide not to set CONTENT_TYPE for POST requests where there is an empty body. In these cases, need to get content-type directly from webob environ key HTTP_CONTENT_TYPE. """ request = Request1() request.integer_field = 1 request.string_field = 'a' request.enum_field = Enum1.VAL1 self.rpc_mapper1.build_request(self.handler, Request1).AndReturn(self.request) def build_response(handler, response): output = '%s %s %s' % (response.integer_field, response.string_field, response.enum_field) handler.response.out.write(output) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).WithSideEffects(build_response) self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = None self.handler.request.environ['HTTP_CONTENT_TYPE'] = ( 'application/x-www-form-urlencoded') self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '1 a VAL1') self.mox.VerifyAll() def testRequestState(self): """Make sure request state is passed in to handler that supports it.""" class ServiceWithState(object): initialize_request_state = self.mox.CreateMockAnything() @remote.method(Request1, Response1) def method1(self, request): return Response1() self.service_factory = ServiceWithState # Reset handler with new service type. self.ResetRequestHandler() self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(Request1()) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)) def verify_state(state): return ('remote.host.com' == state.remote_host and '127.0.0.1' == state.remote_address and 'server.host.com' == state.server_host and 8080 == state.server_port) ServiceWithState.initialize_request_state(mox.Func(verify_state)) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '') self.mox.VerifyAll() def testRequestState_MissingHosts(self): """Make sure missing state environment values are handled gracefully.""" class ServiceWithState(object): initialize_request_state = self.mox.CreateMockAnything() @remote.method(Request1, Response1) def method1(self, request): return Response1() self.service_factory = ServiceWithState self.remote_host = None self.server_host = None # Reset handler with new service type. self.ResetRequestHandler() self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(Request1()) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)) def verify_state(state): return (None is state.remote_host and '127.0.0.1' == state.remote_address and None is state.server_host and 8080 == state.server_port) ServiceWithState.initialize_request_state(mox.Func(verify_state)) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('200', 'OK', '') self.mox.VerifyAll() def testNoMatch_UnknownHTTPMethod(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() self.handler.handle('UNKNOWN', 'does_not_matter') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoMatch_UnknownContentType(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() self.handler.request.headers['Content-Type'] = 'image/png' self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoMatch_NoContentType(self): """Test what happens when no RPCMapper matches..""" self.mox.ReplayAll() del self.handler.request.headers['Content-Type'] self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Unrecognized RPC format.', '') self.mox.VerifyAll() def testNoSuchMethod(self): """When service method not found.""" self.mox.ReplayAll() self.handler.handle('POST', 'no_such_method') self.VerifyResponse('400', 'Unrecognized RPC method: no_such_method', '') self.mox.VerifyAll() def testNoSuchRemoteMethod(self): """When service method exists but is not remote.""" self.mox.ReplayAll() self.handler.handle('POST', 'not_remote') self.VerifyResponse('400', 'Unrecognized RPC method: not_remote', '') self.mox.VerifyAll() def testRequestError(self): """RequestError handling.""" def build_request(handler, request): raise service_handlers.RequestError('This is a request error') self.rpc_mapper1.build_request( self.handler, Request1).WithSideEffects(build_request) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Invalid RPC request.', '') self.mox.VerifyAll() def testDecodeError(self): """DecodeError handling.""" def build_request(handler, request): raise messages.DecodeError('This is a decode error') self.rpc_mapper1.build_request( self.handler, Request1).WithSideEffects(build_request) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('400', 'Invalid RPC request.', '') self.mox.VerifyAll() def testResponseException(self): """Test what happens when build_response raises ResponseError.""" self.rpc_mapper1.build_request( self.handler, Request1).AndReturn(self.request) self.rpc_mapper1.build_response( self.handler, mox.IsA(Response1)).AndRaise( service_handlers.ResponseError) self.mox.ReplayAll() self.handler.handle('POST', 'method1') self.VerifyResponse('500', 'Invalid RPC response.', '') self.mox.VerifyAll() def testGet(self): """Test that GET goes to 'handle' properly.""" self.handler.handle = self.mox.CreateMockAnything() self.handler.handle('GET', 'alternate_method') self.mox.ReplayAll() self.handler.get('alternate_method') self.mox.VerifyAll() def testPost(self): """Test that POST goes to 'handle' properly.""" self.handler.handle = self.mox.CreateMockAnything() self.handler.handle('POST', 'alternate_method') self.mox.ReplayAll() self.handler.post('alternate_method') self.mox.VerifyAll() class RPCMapperTestBase(test_util.TestCase): def setUp(self): """Set up test framework.""" self.Reinitialize() def Reinitialize(self, input='', get=False, path_method='method1', content_type='text/plain'): """Allows reinitialization of test with custom input values and POST. Args: input: Query string or POST input. get: Use GET method if True. Use POST if False. """ self.factory = service_handlers.ServiceHandlerFactory(Service) self.service_handler = service_handlers.ServiceHandler(self.factory, Service()) self.service_handler.remote_method = path_method request_path = '/servicepath' if path_method: request_path += '/' + path_method if get: request_path += '?' + input if get: environ = {'wsgi.input': cStringIO.StringIO(''), 'CONTENT_LENGTH': '0', 'QUERY_STRING': input, 'REQUEST_METHOD': 'GET', 'PATH_INFO': request_path, } self.service_handler.method = 'GET' else: environ = {'wsgi.input': cStringIO.StringIO(input), 'CONTENT_LENGTH': str(len(input)), 'QUERY_STRING': '', 'REQUEST_METHOD': 'POST', 'PATH_INFO': request_path, } self.service_handler.method = 'POST' self.request = webapp.Request(environ) self.response = webapp.Response() self.service_handler.initialize(self.request, self.response) self.service_handler.request.headers['Content-Type'] = content_type class RPCMapperTest(RPCMapperTestBase, webapp_test_util.RequestHandlerTestBase): """Test the RPCMapper base class.""" def setUp(self): RPCMapperTestBase.setUp(self) webapp_test_util.RequestHandlerTestBase.setUp(self) self.mox = mox.Mox() self.protocol = self.mox.CreateMockAnything() def GetEnvironment(self): """Get environment. Return bogus content in body. Returns: dict of CGI environment. """ environment = super(RPCMapperTest, self).GetEnvironment() environment['wsgi.input'] = cStringIO.StringIO('my body') environment['CONTENT_LENGTH'] = len('my body') return environment def testContentTypes_JustDefault(self): """Test content type attributes.""" self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['GET', 'POST'], 'my-content-type', self.protocol) self.assertEquals(frozenset(['GET', 'POST']), mapper.http_methods) self.assertEquals('my-content-type', mapper.default_content_type) self.assertEquals(frozenset(['my-content-type']), mapper.content_types) self.mox.VerifyAll() def testContentTypes_Extended(self): """Test content type attributes.""" self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['GET', 'POST'], 'my-content-type', self.protocol, content_types=['a', 'b']) self.assertEquals(frozenset(['GET', 'POST']), mapper.http_methods) self.assertEquals('my-content-type', mapper.default_content_type) self.assertEquals(frozenset(['my-content-type', 'a', 'b']), mapper.content_types) self.mox.VerifyAll() def testBuildRequest(self): """Test building a request.""" expected_request = Request1() self.protocol.decode_message(Request1, 'my body').AndReturn(expected_request) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_request(self.handler, Request1) self.assertTrue(expected_request is request) def testBuildRequest_ValidationError(self): """Test building a request generating a validation error.""" expected_request = Request1() self.protocol.decode_message( Request1, 'my body').AndRaise(messages.ValidationError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch( service_handlers.RequestError, 'Unable to parse request content: xyz', mapper.build_request, self.handler, Request1) def testBuildRequest_DecodeError(self): """Test building a request generating a decode error.""" expected_request = Request1() self.protocol.decode_message( Request1, 'my body').AndRaise(messages.DecodeError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch( service_handlers.RequestError, 'Unable to parse request content: xyz', mapper.build_request, self.handler, Request1) def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndReturn('encoded') self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) request = mapper.build_response(self.handler, response) self.assertEquals('my-content-type', self.handler.response.headers['Content-Type']) self.assertEquals('encoded', self.handler.response.out.getvalue()) def testBuildResponse(self): """Test building a response.""" response = Response1() self.protocol.encode_message(response).AndRaise( messages.ValidationError('xyz')) self.mox.ReplayAll() mapper = service_handlers.RPCMapper(['POST'], 'my-content-type', self.protocol) self.assertRaisesWithRegexpMatch(service_handlers.ResponseError, 'Unable to encode message: xyz', mapper.build_response, self.handler, response) class ProtocolMapperTestBase(object): """Base class for basic protocol mapper tests.""" def setUp(self): """Reinitialize test specifically for protocol buffer mapper.""" super(ProtocolMapperTestBase, self).setUp() self.Reinitialize(path_method='my_method', content_type='application/x-google-protobuf') self.request_message = Request1() self.request_message.integer_field = 1 self.request_message.string_field = u'something' self.request_message.enum_field = Enum1.VAL1 self.response_message = Response1() self.response_message.integer_field = 1 self.response_message.string_field = u'something' self.response_message.enum_field = Enum1.VAL1 def testBuildRequest(self): """Test request building.""" self.Reinitialize(self.protocol.encode_message(self.request_message), content_type=self.content_type) mapper = self.mapper() parsed_request = mapper.build_request(self.service_handler, Request1) self.assertEquals(self.request_message, parsed_request) def testBuildResponse(self): """Test response building.""" mapper = self.mapper() mapper.build_response(self.service_handler, self.response_message) self.assertEquals(self.protocol.encode_message(self.response_message), self.service_handler.response.out.getvalue()) def testWholeRequest(self): """Test the basic flow of a request with mapper class.""" body = self.protocol.encode_message(self.request_message) self.Reinitialize(input=body, content_type=self.content_type) self.factory.add_request_mapper(self.mapper()) self.service_handler.handle('POST', 'method1') VerifyResponse(self, self.service_handler.response, '200', 'OK', self.protocol.encode_message(self.response_message)) class URLEncodedRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the URL encoded RPC mapper.""" content_type = 'application/x-www-form-urlencoded' protocol = protourlencode mapper = service_handlers.URLEncodedRPCMapper def testBuildRequest_Prefix(self): """Test building request with parameter prefix.""" self.Reinitialize(urllib.urlencode([('prefix_integer_field', '10'), ('prefix_string_field', 'a string'), ('prefix_enum_field', 'VAL1'), ]), self.content_type) url_encoded_mapper = service_handlers.URLEncodedRPCMapper( parameter_prefix='prefix_') request = url_encoded_mapper.build_request(self.service_handler, Request1) self.assertEquals(10, request.integer_field) self.assertEquals('a string', request.string_field) self.assertEquals(Enum1.VAL1, request.enum_field) def testBuildRequest_DecodeError(self): """Test trying to build request that causes a decode error.""" self.Reinitialize(urllib.urlencode((('integer_field', '10'), ('integer_field', '20'), )), content_type=self.content_type) url_encoded_mapper = service_handlers.URLEncodedRPCMapper() self.assertRaises(service_handlers.RequestError, url_encoded_mapper.build_request, self.service_handler, Service.method1.remote.request_type) def testBuildResponse_Prefix(self): """Test building a response with parameter prefix.""" response = Response1() response.integer_field = 10 response.string_field = u'a string' response.enum_field = Enum1.VAL3 url_encoded_mapper = service_handlers.URLEncodedRPCMapper( parameter_prefix='prefix_') url_encoded_mapper.build_response(self.service_handler, response) self.assertEquals('application/x-www-form-urlencoded', self.response.headers['content-type']) self.assertEquals(cgi.parse_qs(self.response.out.getvalue(), True, True), {'prefix_integer_field': ['10'], 'prefix_string_field': [u'a string'], 'prefix_enum_field': ['VAL3'], }) class ProtobufRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the protobuf encoded RPC mapper.""" content_type = 'application/x-google-protobuf' protocol = protobuf mapper = service_handlers.ProtobufRPCMapper class JSONRPCMapperTest(ProtocolMapperTestBase, RPCMapperTestBase): """Test the URL encoded RPC mapper.""" content_type = 'application/json' protocol = protojson mapper = service_handlers.JSONRPCMapper class MyService(remote.Service): def __init__(self, value='default'): self.value = value class ServiceMappingTest(test_util.TestCase): def CheckFormMappings(self, mapping, registry_path='/protorpc'): """Check to make sure that form mapping is configured as expected. Args: mapping: Mapping that should contain forms handlers. """ pattern, factory = mapping[0] self.assertEquals('%s/form(?:/)?' % registry_path, pattern) handler = factory() self.assertTrue(isinstance(handler, forms.FormsHandler)) self.assertEquals(registry_path, handler.registry_path) pattern, factory = mapping[1] self.assertEquals('%s/form/(.+)' % registry_path, pattern) self.assertEquals(forms.ResourceHandler, factory) def DoMappingTest(self, services, registry_path='/myreg', expected_paths=None): mapped_services = mapping = service_handlers.service_mapping(services, registry_path) if registry_path: form_mapping = mapping[:2] mapped_registry_path, mapped_registry_factory = mapping[-1] mapped_services = mapping[2:-1] self.CheckFormMappings(form_mapping, registry_path=registry_path) self.assertEquals(registry_path + service_handlers._METHOD_PATTERN, mapped_registry_path) self.assertEquals(registry.RegistryService, mapped_registry_factory.service_factory.service_class) # Verify registry knows about other services. expected_registry = {registry_path: registry.RegistryService} for path, factory in dict(services).iteritems(): if isinstance(factory, type) and issubclass(factory, remote.Service): expected_registry[path] = factory else: expected_registry[path] = factory.service_class self.assertEquals(expected_registry, mapped_registry_factory().service.registry) # Verify that services are mapped to URL. self.assertEquals(len(services), len(mapped_services)) for path, service in dict(services).iteritems(): mapped_path = path + service_handlers._METHOD_PATTERN mapped_factory = dict(mapped_services)[mapped_path] self.assertEquals(service, mapped_factory.service_factory) def testServiceMapping_Empty(self): """Test an empty service mapping.""" self.DoMappingTest({}) def testServiceMapping_ByClass(self): """Test mapping a service by class.""" self.DoMappingTest({'/my-service': MyService}) def testServiceMapping_ByFactory(self): """Test mapping a service by factory.""" self.DoMappingTest({'/my-service': MyService.new_factory('new-value')}) def testServiceMapping_ByList(self): """Test mapping a service by factory.""" self.DoMappingTest( [('/my-service1', MyService.new_factory('service1')), ('/my-service2', MyService.new_factory('service2')), ]) def testServiceMapping_NoRegistry(self): """Test mapping a service by class.""" mapping = self.DoMappingTest({'/my-service': MyService}, None) def testDefaultMappingWithClass(self): """Test setting path just from the class. Path of the mapping will be the fully qualified ProtoRPC service name with '.' replaced with '/'. For example: com.nowhere.service.TheService -> /com/nowhere/service/TheService """ mapping = service_handlers.service_mapping([MyService]) mapped_services = mapping[2:-1] self.assertEquals(1, len(mapped_services)) path, factory = mapped_services[0] self.assertEquals( '/test_package/MyService' + service_handlers._METHOD_PATTERN, path) self.assertEquals(MyService, factory.service_factory) def testDefaultMappingWithFactory(self): mapping = service_handlers.service_mapping( [MyService.new_factory('service1')]) mapped_services = mapping[2:-1] self.assertEquals(1, len(mapped_services)) path, factory = mapped_services[0] self.assertEquals( '/test_package/MyService' + service_handlers._METHOD_PATTERN, path) self.assertEquals(MyService, factory.service_factory.service_class) def testMappingDuplicateExplicitServiceName(self): self.assertRaisesWithRegexpMatch( service_handlers.ServiceConfigurationError, "Path '/my_path' is already defined in service mapping", service_handlers.service_mapping, [('/my_path', MyService), ('/my_path', MyService), ]) def testMappingDuplicateServiceName(self): self.assertRaisesWithRegexpMatch( service_handlers.ServiceConfigurationError, "Path '/test_package/MyService' is already defined in service mapping", service_handlers.service_mapping, [MyService, MyService]) class GetCalled(remote.Service): def __init__(self, test): self.test = test @remote.method(Request1, Response1) def my_method(self, request): self.test.request = request return Response1(string_field='a response') class TestRunServices(test_util.TestCase): def DoRequest(self, path, request, response_type, reg_path='/protorpc'): stdin = sys.stdin stdout = sys.stdout environ = os.environ try: sys.stdin = cStringIO.StringIO(protojson.encode_message(request)) sys.stdout = cStringIO.StringIO() os.environ = webapp_test_util.GetDefaultEnvironment() os.environ['PATH_INFO'] = path os.environ['REQUEST_METHOD'] = 'POST' os.environ['CONTENT_TYPE'] = 'application/json' os.environ['wsgi.input'] = sys.stdin os.environ['wsgi.output'] = sys.stdout os.environ['CONTENT_LENGTH'] = len(sys.stdin.getvalue()) service_handlers.run_services( [('/my_service', GetCalled.new_factory(self))], reg_path) header, body = sys.stdout.getvalue().split('\n\n', 1) return (header.split('\n')[0], protojson.decode_message(response_type, body)) finally: sys.stdin = stdin sys.stdout = stdout os.environ = environ def testRequest(self): request = Request1(string_field='request value') status, response = self.DoRequest('/my_service.my_method', request, Response1) self.assertEquals('Status: 200 OK', status) self.assertEquals(request, self.request) self.assertEquals(Response1(string_field='a response'), response) def testRegistry(self): request = Request1(string_field='request value') status, response = self.DoRequest('/protorpc.services', message_types.VoidMessage(), registry.ServicesResponse) self.assertEquals('Status: 200 OK', status) self.assertEquals(registry.ServicesResponse( services=[ registry.ServiceMapping( name='/protorpc', definition='protorpc.registry.RegistryService'), registry.ServiceMapping( name='/my_service', definition='test_package.GetCalled'), ]), response) def testRunServicesWithOutRegistry(self): request = Request1(string_field='request value') status, response = self.DoRequest('/protorpc.services', message_types.VoidMessage(), registry.ServicesResponse, reg_path=None) self.assertEquals('Status: 404 Not Found', status) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import imp import inspect import new import re import sys import types import unittest from protorpc import test_util from protorpc import descriptor from protorpc import messages class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = messages class ValidationErrorTest(test_util.TestCase): def testStr_NoFieldName(self): """Test string version of ValidationError when no name provided.""" self.assertEquals('Validation error', str(messages.ValidationError('Validation error'))) def testStr_FieldName(self): """Test string version of ValidationError when no name provided.""" validation_error = messages.ValidationError('Validation error') validation_error.field_name = 'a_field' self.assertEquals('Field a_field: Validation error', str(validation_error)) class EnumTest(test_util.TestCase): def setUp(self): """Set up tests.""" # Redefine Color class in case so that changes to it (an error) in one test # does not affect other tests. global Color class Color(messages.Enum): RED = 20 ORANGE = 2 YELLOW = 40 GREEN = 4 BLUE = 50 INDIGO = 5 VIOLET = 80 def testNames(self): """Test that names iterates over enum names.""" self.assertEquals( set(['BLUE', 'GREEN', 'INDIGO', 'ORANGE', 'RED', 'VIOLET', 'YELLOW']), set(Color.names())) def testNumbers(self): """Tests that numbers iterates of enum numbers.""" self.assertEquals(set([2, 4, 5, 20, 40, 50, 80]), set(Color.numbers())) def testIterate(self): """Test that __iter__ iterates over all enum values.""" self.assertEquals(set(Color), set([Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.INDIGO, Color.VIOLET])) def testNaturalOrder(self): """Test that natural order enumeration is in numeric order.""" self.assertEquals([Color.ORANGE, Color.GREEN, Color.INDIGO, Color.RED, Color.YELLOW, Color.BLUE, Color.VIOLET], sorted(Color)) def testByName(self): """Test look-up by name.""" self.assertEquals(Color.RED, Color.lookup_by_name('RED')) self.assertRaises(KeyError, Color.lookup_by_name, 20) self.assertRaises(KeyError, Color.lookup_by_name, Color.RED) def testByNumber(self): """Test look-up by number.""" self.assertRaises(KeyError, Color.lookup_by_number, 'RED') self.assertEquals(Color.RED, Color.lookup_by_number(20)) self.assertRaises(KeyError, Color.lookup_by_number, Color.RED) def testConstructor(self): """Test that constructor look-up by name or number.""" self.assertEquals(Color.RED, Color('RED')) self.assertEquals(Color.RED, Color(u'RED')) self.assertEquals(Color.RED, Color(20)) self.assertEquals(Color.RED, Color(20L)) self.assertEquals(Color.RED, Color(Color.RED)) self.assertRaises(TypeError, Color, 'Not exists') self.assertRaises(TypeError, Color, 'Red') self.assertRaises(TypeError, Color, 100) self.assertRaises(TypeError, Color, 10.0) def testLen(self): """Test that len function works to count enums.""" self.assertEquals(7, len(Color)) def testNoSubclasses(self): """Test that it is not possible to sub-class enum classes.""" def declare_subclass(): class MoreColor(Color): pass self.assertRaises(messages.EnumDefinitionError, declare_subclass) def testClassNotMutable(self): """Test that enum classes themselves are not mutable.""" self.assertRaises(AttributeError, setattr, Color, 'something_new', 10) def testInstancesMutable(self): """Test that enum instances are not mutable.""" self.assertRaises(TypeError, setattr, Color.RED, 'something_new', 10) def testDefEnum(self): """Test def_enum works by building enum class from dict.""" WeekDay = messages.Enum.def_enum({'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 6, 'Saturday': 7, 'Sunday': 8}, 'WeekDay') self.assertEquals('Wednesday', WeekDay(3).name) self.assertEquals(6, WeekDay('Friday').number) self.assertEquals(WeekDay.Sunday, WeekDay('Sunday')) def testNonInt(self): """Test that non-integer values rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': '1'}, 'BadEnum') def testNegativeInt(self): """Test that negative numbers rejection by enum def.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': -1}, 'BadEnum') def testLowerBound(self): """Test that zero is accepted by enum def.""" class NotImportant(messages.Enum): """Testing for value zero""" VALUE = 0 self.assertEquals(0, int(NotImportant.VALUE)) def testTooLargeInt(self): """Test that numbers too large are rejected.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Bad': (2 ** 29)}, 'BadEnum') def testRepeatedInt(self): """Test duplicated numbers are forbidden.""" self.assertRaises(messages.EnumDefinitionError, messages.Enum.def_enum, {'Ok': 1, 'Repeated': 1}, 'BadEnum') def testStr(self): """Test converting to string.""" self.assertEquals('RED', str(Color.RED)) self.assertEquals('ORANGE', str(Color.ORANGE)) def testInt(self): """Test converting to int.""" self.assertEquals(20, int(Color.RED)) self.assertEquals(2, int(Color.ORANGE)) def testRepr(self): """Test enum representation.""" self.assertEquals('Color(RED, 20)', repr(Color.RED)) self.assertEquals('Color(YELLOW, 40)', repr(Color.YELLOW)) def testDocstring(self): """Test that docstring is supported ok.""" class NotImportant(messages.Enum): """I have a docstring.""" VALUE1 = 1 self.assertEquals('I have a docstring.', NotImportant.__doc__) def testDeleteEnumValue(self): """Test that enum values cannot be deleted.""" self.assertRaises(TypeError, delattr, Color, 'RED') def testEnumName(self): """Test enum name.""" self.assertEquals('messages_test.Color', Color.definition_name()) def testDefinitionName_OverrideModule(self): """Test enum module is overriden by module package name.""" global package try: package = 'my.package' self.assertEquals('my.package.Color', Color.definition_name()) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for enum.""" class Enum1(messages.Enum): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('Enum1', Enum1.definition_name()) self.assertEquals(unicode, type(Enum1.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested Enum names.""" class MyMessage(messages.Message): class NestedEnum(messages.Enum): pass class NestedMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals('messages_test.MyMessage.NestedEnum', MyMessage.NestedEnum.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedEnum', MyMessage.NestedMessage.NestedEnum.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterEnum(messages.Enum): pass self.assertEquals(None, OuterEnum.message_definition()) class OuterMessage(messages.Message): class InnerEnum(messages.Enum): pass self.assertEquals(OuterMessage, OuterMessage.InnerEnum.message_definition()) def testComparison(self): """Test comparing various enums to different types.""" class Enum1(messages.Enum): VAL1 = 1 VAL2 = 2 class Enum2(messages.Enum): VAL1 = 1 self.assertEquals(Enum1.VAL1, Enum1.VAL1) self.assertNotEquals(Enum1.VAL1, Enum1.VAL2) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertNotEquals(Enum1.VAL1, 'VAL1') self.assertNotEquals(Enum1.VAL1, 1) self.assertNotEquals(Enum1.VAL1, 2) self.assertNotEquals(Enum1.VAL1, None) self.assertNotEquals(Enum1.VAL1, Enum2.VAL1) self.assertTrue(Enum1.VAL1 < Enum1.VAL2) self.assertTrue(Enum1.VAL2 > Enum1.VAL1) self.assertNotEquals(1, Enum2.VAL1) class FieldListTest(test_util.TestCase): def setUp(self): self.integer_field = messages.IntegerField(1, repeated=True) def testConstructor(self): self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, [1, 2, 3])) self.assertEquals([1, 2, 3], messages.FieldList(self.integer_field, (1, 2, 3))) self.assertEquals([], messages.FieldList(self.integer_field, [])) def testNone(self): self.assertRaises(TypeError, messages.FieldList, self.integer_field, None) def testDoNotAutoConvertString(self): string_field = messages.StringField(1, repeated=True) self.assertRaises(messages.ValidationError, messages.FieldList, string_field, 'abc') def testConstructorCopies(self): a_list = [1, 3, 6] field_list = messages.FieldList(self.integer_field, a_list) self.assertFalse(a_list is field_list) self.assertFalse(field_list is messages.FieldList(self.integer_field, field_list)) def testNonRepeatedField(self): self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'FieldList may only accept repeated fields', messages.FieldList, messages.IntegerField(1), []) def testConstructor_InvalidValues(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 1 (type <type 'str'>)"), messages.FieldList, self.integer_field, ["1", "2", "3"]) def testConstructor_Scalars(self): self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: 3", messages.FieldList, self.integer_field, 3) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <listiterator object", messages.FieldList, self.integer_field, iter([1, 2, 3])) def testSetSlice(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) field_list[1:3] = [10, 20] self.assertEquals([1, 10, 20, 4, 5], field_list) def testSetSlice_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [1, 2, 3, 4, 5]) def setslice(): field_list[1:3] = ['10', '20'] self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setslice) def testSetItem(self): field_list = messages.FieldList(self.integer_field, [2]) field_list[0] = 10 self.assertEquals([10], field_list) def testSetItem_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def setitem(): field_list[0] = '10' self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), setitem) def testAppend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.append(10) self.assertEquals([2, 10], field_list) def testAppend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def append(): field_list.append('10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), append) def testExtend(self): field_list = messages.FieldList(self.integer_field, [2]) field_list.extend([10]) self.assertEquals([2, 10], field_list) def testExtend_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2]) def extend(): field_list.extend(['10']) self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), extend) def testInsert(self): field_list = messages.FieldList(self.integer_field, [2, 3]) field_list.insert(1, 10) self.assertEquals([2, 10, 3], field_list) def testInsert_InvalidValues(self): field_list = messages.FieldList(self.integer_field, [2, 3]) def insert(): field_list.insert(1, '10') self.assertRaisesWithRegexpMatch( messages.ValidationError, re.escape("Expected type (<type 'int'>, <type 'long'>), " "found 10 (type <type 'str'>)"), insert) class FieldTest(test_util.TestCase): def ActionOnAllFieldClasses(self, action): """Test all field classes except Message and Enum. Message and Enum require separate tests. Args: action: Callable that takes the field class as a parameter. """ for field_class in (messages.IntegerField, messages.FloatField, messages.BooleanField, messages.BytesField, messages.StringField, ): action(field_class) def testNumberAttribute(self): """Test setting the number attribute.""" def action(field_class): # Check range. self.assertRaises(messages.InvalidNumberError, field_class, 0) self.assertRaises(messages.InvalidNumberError, field_class, -1) self.assertRaises(messages.InvalidNumberError, field_class, messages.MAX_FIELD_NUMBER + 1) # Check reserved. self.assertRaises(messages.InvalidNumberError, field_class, messages.FIRST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, messages.LAST_RESERVED_FIELD_NUMBER) self.assertRaises(messages.InvalidNumberError, field_class, '1') # This one should work. field_class(number=1) self.ActionOnAllFieldClasses(action) def testRequiredAndRepeated(self): """Test setting the required and repeated fields.""" def action(field_class): field_class(1, required=True) field_class(1, repeated=True) self.assertRaises(messages.FieldDefinitionError, field_class, 1, required=True, repeated=True) self.ActionOnAllFieldClasses(action) def testInvalidVariant(self): """Test field with invalid variants.""" def action(field_class): self.assertRaises(messages.InvalidVariantError, field_class, 1, variant=messages.Variant.ENUM) self.ActionOnAllFieldClasses(action) def testDefaultVariant(self): """Test that default variant is used when not set.""" def action(field_class): field = field_class(1) self.assertEquals(field_class.DEFAULT_VARIANT, field.variant) self.ActionOnAllFieldClasses(action) def testAlternateVariant(self): """Test that default variant is used when not set.""" field = messages.IntegerField(1, variant=messages.Variant.UINT32) self.assertEquals(messages.Variant.UINT32, field.variant) def testDefaultFields_Single(self): """Test default field is correct type.""" defaults = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): field_class(1, default=defaults[field_class]) self.ActionOnAllFieldClasses(action) # Run defaults test again checking for str/unicode compatiblity. defaults[messages.StringField] = 'abc' self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidSingle(self): """Test default field is correct type.""" def action(field_class): self.assertRaises(messages.InvalidDefaultError, field_class, 1, default=object()) self.ActionOnAllFieldClasses(action) def testDefaultFields_InvalidRepeated(self): """Test default field does not accept defaults.""" self.assertRaisesWithRegexpMatch( messages.FieldDefinitionError, 'Repeated fields may not have defaults', messages.StringField, 1, repeated=True, default=[1, 2, 3]) def testDefaultFields_None(self): """Test none is always acceptable.""" def action(field_class): field_class(1, default=None) field_class(1, required=True, default=None) field_class(1, repeated=True, default=None) self.ActionOnAllFieldClasses(action) def testDefaultFields_Enum(self): """Test the default for enum fields.""" class Symbol(messages.Enum): ALPHA = 1 BETA = 2 GAMMA = 3 field = messages.EnumField(Symbol, 1, default=Symbol.ALPHA) self.assertEquals(Symbol.ALPHA, field.default) def testDefaultFields_EnumStringDelayedResolution(self): """Test that enum fields resolve default strings.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default='OPTIONAL') self.assertEquals(descriptor.FieldDescriptor.Label.OPTIONAL, field.default) def testDefaultFields_EnumIntDelayedResolution(self): """Test that enum fields resolve default integers.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=2) self.assertEquals(descriptor.FieldDescriptor.Label.REQUIRED, field.default) def testDefaultFields_EnumOkIfTypeKnown(self): """Test that enum fields accept valid default values when type is known.""" field = messages.EnumField(descriptor.FieldDescriptor.Label, 1, default='REPEATED') self.assertEquals(descriptor.FieldDescriptor.Label.REPEATED, field.default) def testDefaultFields_EnumForceCheckIfTypeKnown(self): """Test that enum fields validate default values if type is known.""" self.assertRaisesWithRegexpMatch(TypeError, 'No such value for NOT_A_LABEL in ' 'Enum Label', messages.EnumField, descriptor.FieldDescriptor.Label, 1, default='NOT_A_LABEL') def testDefaultFields_EnumInvalidDelayedResolution(self): """Test that enum fields raise errors upon delayed resolution error.""" field = messages.EnumField('protorpc.descriptor.FieldDescriptor.Label', 1, default=200) self.assertRaisesWithRegexpMatch(TypeError, 'No such value for 200 in Enum Label', getattr, field, 'default') def testValidate_Valid(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate(values[field_class]) # Required. field = field_class(1, required=True) field.validate(values[field_class]) # Repeated. field = field_class(1, repeated=True) field.validate([]) field.validate(()) field.validate([values[field_class]]) field.validate((values[field_class],)) # Right value, but not repeated. self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) self.ActionOnAllFieldClasses(action) def testValidate_Invalid(self): """Test validation of valid values.""" values = {messages.IntegerField: "10", messages.FloatField: 1, messages.BooleanField: 0, messages.BytesField: 10.20, messages.StringField: 42, } def action(field_class): # Optional. field = field_class(1) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Required. field = field_class(1, required=True) self.assertRaises(messages.ValidationError, field.validate, values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(messages.ValidationError, field.validate, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate, (values[field_class],)) self.ActionOnAllFieldClasses(action) def testValidate_None(self): """Test that None is valid for non-required fields.""" def action(field_class): # Optional. field = field_class(1) field.validate(None) # Required. field = field_class(1, required=True) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Required field is missing', field.validate, None) # Repeated. field = field_class(1, repeated=True) field.validate(None) self.assertRaisesWithRegexpMatch(messages.ValidationError, 'Repeated values may not be None', field.validate, [None]) self.assertRaises(messages.ValidationError, field.validate, (None,)) self.ActionOnAllFieldClasses(action) def testValidateElement(self): """Test validation of valid values.""" values = {messages.IntegerField: 10, messages.FloatField: 1.5, messages.BooleanField: False, messages.BytesField: 'abc', messages.StringField: u'abc', } def action(field_class): # Optional. field = field_class(1) field.validate_element(values[field_class]) # Required. field = field_class(1, required=True) field.validate_element(values[field_class]) # Repeated. field = field_class(1, repeated=True) self.assertRaises(message.VAlidationError, field.validate_element, []) self.assertRaises(message.VAlidationError, field.validate_element, ()) field.validate_element(values[field_class]) field.validate_element(values[field_class]) # Right value, but repeated. self.assertRaises(messages.ValidationError, field.validate_element, [values[field_class]]) self.assertRaises(messages.ValidationError, field.validate_element, (values[field_class],)) def testReadOnly(self): """Test that objects are all read-only.""" def action(field_class): field = field_class(10) self.assertRaises(AttributeError, setattr, field, 'number', 20) self.assertRaises(AttributeError, setattr, field, 'anything_else', 'whatever') self.ActionOnAllFieldClasses(action) def testMessageField(self): """Test the construction of message fields.""" self.assertRaises(messages.FieldDefinitionError, messages.MessageField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.MessageField, messages.Message, 10) class MyMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) self.assertEquals(MyMessage, field.type) def testMessageField_ForwardReference(self): """Test the construction of forward reference message fields.""" global MyMessage global ForwardMessage try: class MyMessage(messages.Message): self_reference = messages.MessageField('MyMessage', 1) forward = messages.MessageField('ForwardMessage', 2) nested = messages.MessageField('ForwardMessage.NestedMessage', 3) inner = messages.MessageField('Inner', 4) class Inner(messages.Message): sibling = messages.MessageField('Sibling', 1) class Sibling(messages.Message): pass class ForwardMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals(MyMessage, MyMessage.field_by_name('self_reference').type) self.assertEquals(ForwardMessage, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedMessage, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) self.assertEquals(MyMessage.Sibling, MyMessage.Inner.field_by_name('sibling').type) finally: try: del MyMessage del ForwardMessage except: pass def testMessageField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AnEnum try: class AnEnum(messages.Enum): pass class AnotherMessage(messages.Message): a_field = messages.MessageField('AnEnum', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AnEnum def testMessageFieldValidate(self): """Test validation on message field.""" class MyMessage(messages.Message): pass class AnotherMessage(messages.Message): pass field = messages.MessageField(MyMessage, 10) field.validate(MyMessage()) self.assertRaises(messages.ValidationError, field.validate, AnotherMessage()) def testIntegerField_AllowLong(self): """Test that the integer field allows for longs.""" messages.IntegerField(10, default=long(10)) def testMessageFieldValidate_Initialized(self): """Test validation on message field.""" class MyMessage(messages.Message): field1 = messages.IntegerField(1, required=True) field = messages.MessageField(MyMessage, 10) # Will validate messages where is_initialized() is False. message = MyMessage() field.validate(message) message.field1 = 20 field.validate(message) def testEnumField(self): """Test the construction of enum fields.""" self.assertRaises(messages.FieldDefinitionError, messages.EnumField, str, 10) self.assertRaises(messages.FieldDefinitionError, messages.EnumField, messages.Enum, 10) class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 field = messages.EnumField(Color, 10) self.assertEquals(Color, field.type) class Another(messages.Enum): VALUE = 1 self.assertRaises(messages.InvalidDefaultError, messages.EnumField, Color, 10, default=Another.VALUE) def testEnumField_ForwardReference(self): """Test the construction of forward reference enum fields.""" global MyMessage global ForwardEnum global ForwardMessage try: class MyMessage(messages.Message): forward = messages.EnumField('ForwardEnum', 1) nested = messages.EnumField('ForwardMessage.NestedEnum', 2) inner = messages.EnumField('Inner', 3) class Inner(messages.Enum): pass class ForwardEnum(messages.Enum): pass class ForwardMessage(messages.Message): class NestedEnum(messages.Enum): pass self.assertEquals(ForwardEnum, MyMessage.field_by_name('forward').type) self.assertEquals(ForwardMessage.NestedEnum, MyMessage.field_by_name('nested').type) self.assertEquals(MyMessage.Inner, MyMessage.field_by_name('inner').type) finally: try: del MyMessage del ForwardEnum del ForwardMessage except: pass def testEnumField_WrongType(self): """Test that forward referencing the wrong type raises an error.""" global AMessage try: class AMessage(messages.Message): pass class AnotherMessage(messages.Message): a_field = messages.EnumField('AMessage', 1) self.assertRaises(messages.FieldDefinitionError, getattr, AnotherMessage.field_by_name('a_field'), 'type') finally: del AMessage def testMessageDefinition(self): """Test that message definition is set on fields.""" class MyMessage(messages.Message): my_field = messages.StringField(1) self.assertEquals(MyMessage, MyMessage.field_by_name('my_field').message_definition()) def testNoneAssignment(self): """Test that assigning None does not change comparison.""" class MyMessage(messages.Message): my_field = messages.StringField(1) m1 = MyMessage() m2 = MyMessage() m2.my_field = None self.assertEquals(m1, m2) def testNonAsciiStr(self): """Test validation fails for non-ascii StringField values.""" class Thing(messages.Message): string_field = messages.StringField(2) thing = Thing() self.assertRaisesWithRegexpMatch( messages.ValidationError, 'Field string_field: Encountered non-ASCII string', setattr, thing, 'string_field', test_util.BINARY) class MessageTest(test_util.TestCase): """Tests for message class.""" def CreateMessageClass(self): """Creates a simple message class with 3 fields. Fields are defined in alphabetical order but with conflicting numeric order. """ class ComplexMessage(messages.Message): a3 = messages.IntegerField(3) b1 = messages.StringField(1) c2 = messages.StringField(2) return ComplexMessage def testSameNumbers(self): """Test that cannot assign two fields with same numbers.""" def action(): class BadMessage(messages.Message): f1 = messages.IntegerField(1) f2 = messages.IntegerField(1) self.assertRaises(messages.DuplicateNumberError, action) def testStrictAssignment(self): """Tests that cannot assign to unknown or non-reserved attributes.""" class SimpleMessage(messages.Message): field = messages.IntegerField(1) simple_message = SimpleMessage() self.assertRaises(AttributeError, setattr, simple_message, 'does_not_exist', 10) def testListAssignmentDoesNotCopy(self): class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message = SimpleMessage() original = message.repeated message.repeated = [] self.assertFalse(original is message.repeated) def testValidate_Optional(self): """Tests validation of optional fields.""" class SimpleMessage(messages.Message): non_required = messages.IntegerField(1) simple_message = SimpleMessage() simple_message.check_initialized() simple_message.non_required = 10 simple_message.check_initialized() def testValidate_Required(self): """Tests validation of required fields.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertRaises(messages.ValidationError, simple_message.check_initialized) simple_message.required = 10 simple_message.check_initialized() def testValidate_Repeated(self): """Tests validation of repeated fields.""" class SimpleMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) simple_message = SimpleMessage() # Check valid values. for valid_value in [], [10], [10, 20], (), (10,), (10, 20): simple_message.repeated = valid_value simple_message.check_initialized() # Check cleared. simple_message.repeated = [] simple_message.check_initialized() # Check invalid values. for invalid_value in 10, ['10', '20'], [None], (None,): self.assertRaises(messages.ValidationError, setattr, simple_message, 'repeated', invalid_value) def testIsInitialized(self): """Tests is_initialized.""" class SimpleMessage(messages.Message): required = messages.IntegerField(1, required=True) simple_message = SimpleMessage() self.assertFalse(simple_message.is_initialized()) simple_message.required = 10 self.assertTrue(simple_message.is_initialized()) def testNestedMethodsNotAllowed(self): """Test that method definitions on Message classes are not allowed.""" def action(): class WithMethods(messages.Message): def not_allowed(self): pass self.assertRaises(messages.MessageDefinitionError, action) def testNestedAttributesNotAllowed(self): """Test that attribute assignment on Message classes are not allowed.""" def int_attribute(): class WithMethods(messages.Message): not_allowed = 1 def string_attribute(): class WithMethods(messages.Message): not_allowed = 'not allowed' def enum_attribute(): class WithMethods(messages.Message): not_allowed = Color.RED for action in (int_attribute, string_attribute, enum_attribute): self.assertRaises(messages.MessageDefinitionError, action) def testNameIsSetOnFields(self): """Make sure name is set on fields after Message class init.""" class HasNamedFields(messages.Message): field = messages.StringField(1) self.assertEquals('field', HasNamedFields.field_by_number(1).name) def testSubclassingMessageDisallowed(self): """Not permitted to create sub-classes of message classes.""" class SuperClass(messages.Message): pass def action(): class SubClass(SuperClass): pass self.assertRaises(messages.MessageDefinitionError, action) def testAllFields(self): """Test all_fields method.""" ComplexMessage = self.CreateMessageClass() fields = list(ComplexMessage.all_fields()) # Order does not matter, so sort now. fields = sorted(fields, lambda f1, f2: cmp(f1.name, f2.name)) self.assertEquals(3, len(fields)) self.assertEquals('a3', fields[0].name) self.assertEquals('b1', fields[1].name) self.assertEquals('c2', fields[2].name) def testFieldByName(self): """Test getting field by name.""" ComplexMessage = self.CreateMessageClass() self.assertEquals(3, ComplexMessage.field_by_name('a3').number) self.assertEquals(1, ComplexMessage.field_by_name('b1').number) self.assertEquals(2, ComplexMessage.field_by_name('c2').number) self.assertRaises(KeyError, ComplexMessage.field_by_name, 'unknown') def testFieldByNumber(self): """Test getting field by number.""" ComplexMessage = self.CreateMessageClass() self.assertEquals('a3', ComplexMessage.field_by_number(3).name) self.assertEquals('b1', ComplexMessage.field_by_number(1).name) self.assertEquals('c2', ComplexMessage.field_by_number(2).name) self.assertRaises(KeyError, ComplexMessage.field_by_number, 4) def testGetAssignedValue(self): """Test getting the assigned value of a field.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertEquals(None, message.get_assigned_value('a_value')) message.a_value = u'a string' self.assertEquals(u'a string', message.get_assigned_value('a_value')) message.a_value = u'a default' self.assertEquals(u'a default', message.get_assigned_value('a_value')) self.assertRaisesWithRegexpMatch( AttributeError, 'Message SomeMessage has no field no_such_field', message.get_assigned_value, 'no_such_field') def testReset(self): """Test resetting a field value.""" class SomeMessage(messages.Message): a_value = messages.StringField(1, default=u'a default') message = SomeMessage() self.assertRaises(AttributeError, message.reset, 'unknown') self.assertEquals(u'a default', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) message.a_value = u'a new value' self.assertEquals(u'a new value', message.a_value) message.reset('a_value') self.assertEquals(u'a default', message.a_value) def testAllowNestedEnums(self): """Test allowing nested enums in a message definition.""" class Trade(messages.Message): class Duration(messages.Enum): GTC = 1 DAY = 2 class Currency(messages.Enum): USD = 1 GBP = 2 INR = 3 # Sorted by name order seems to be the only feasible option. self.assertEquals(['Currency', 'Duration'], Trade.__enums__) # Message definition will now be set on Enumerated objects. self.assertEquals(Trade, Trade.Duration.message_definition()) def testAllowNestedMessages(self): """Test allowing nested messages in a message definition.""" class Trade(messages.Message): class Lot(messages.Message): pass class Agent(messages.Message): pass # Sorted by name order seems to be the only feasible option. self.assertEquals(['Agent', 'Lot'], Trade.__messages__) self.assertEquals(Trade, Trade.Agent.message_definition()) self.assertEquals(Trade, Trade.Lot.message_definition()) # But not Message itself. def action(): class Trade(messages.Message): NiceTry = messages.Message self.assertRaises(messages.MessageDefinitionError, action) def testDisallowClassAssignments(self): """Test setting class attributes may not happen.""" class MyMessage(messages.Message): pass self.assertRaises(AttributeError, setattr, MyMessage, 'x', 'do not assign') def testEquality(self): """Test message class equality.""" # Comparison against enums must work. class MyEnum(messages.Enum): val1 = 1 val2 = 2 # Comparisons against nested messages must work. class AnotherMessage(messages.Message): string = messages.StringField(1) class MyMessage(messages.Message): field1 = messages.IntegerField(1) field2 = messages.EnumField(MyEnum, 2) field3 = messages.MessageField(AnotherMessage, 3) message1 = MyMessage() self.assertNotEquals('hi', message1) self.assertNotEquals(AnotherMessage(), message1) self.assertEquals(message1, message1) message2 = MyMessage() self.assertEquals(message1, message2) message1.field1 = 10 self.assertNotEquals(message1, message2) message2.field1 = 20 self.assertNotEquals(message1, message2) message2.field1 = 10 self.assertEquals(message1, message2) message1.field2 = MyEnum.val1 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val2 self.assertNotEquals(message1, message2) message2.field2 = MyEnum.val1 self.assertEquals(message1, message2) message1.field3 = AnotherMessage() message1.field3.string = 'value1' self.assertNotEquals(message1, message2) message2.field3 = AnotherMessage() message2.field3.string = 'value2' self.assertNotEquals(message1, message2) message2.field3.string = 'value1' self.assertEquals(message1, message2) def testRepr(self): """Test represtation of Message object.""" class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) unassigned = messages.StringField(3) unassigned_with_default = messages.StringField(4, default=u'a default') my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' self.assertEquals("<MyMessage\n integer_value: 42\n" " string_value: u'A string'>", repr(my_message)) def testValidation(self): """Test validation of message values.""" # Test optional. class SubMessage(messages.Message): pass class Message(messages.Message): val = messages.MessageField(SubMessage, 1) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test required. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, required=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) message.val = SubMessage() message_field.validate(message) self.assertRaises(messages.ValidationError, setattr, message, 'val', [SubMessage()]) # Test repeated. class Message(messages.Message): val = messages.MessageField(SubMessage, 1, repeated=True) message = Message() message_field = messages.MessageField(Message, 1) message_field.validate(message) self.assertRaisesWithRegexpMatch( messages.ValidationError, "Field is repeated. Found: <SubMessage>", setattr, message, 'val', SubMessage()) message.val = [SubMessage()] message_field.validate(message) def testDefinitionName(self): """Test message name.""" class MyMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) def testDefinitionName_OverrideModule(self): """Test message module is overriden by module package name.""" class MyMessage(messages.Message): pass global package package = 'my.package' try: self.assertEquals('my.package.MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: del package def testDefinitionName_NoModule(self): """Test what happens when there is no module for message.""" class MyMessage(messages.Message): pass original_modules = sys.modules sys.modules = dict(sys.modules) try: del sys.modules[__name__] self.assertEquals('MyMessage', MyMessage.definition_name()) self.assertEquals(unicode, type(MyMessage.definition_name())) finally: sys.modules = original_modules def testDefinitionName_Nested(self): """Test nested message names.""" class MyMessage(messages.Message): class NestedMessage(messages.Message): class NestedMessage(messages.Message): pass self.assertEquals('messages_test.MyMessage.NestedMessage', MyMessage.NestedMessage.definition_name()) self.assertEquals('messages_test.MyMessage.NestedMessage.NestedMessage', MyMessage.NestedMessage.NestedMessage.definition_name()) def testMessageDefinition(self): """Test that enumeration knows its enclosing message definition.""" class OuterMessage(messages.Message): class InnerMessage(messages.Message): pass self.assertEquals(None, OuterMessage.message_definition()) self.assertEquals(OuterMessage, OuterMessage.InnerMessage.message_definition()) def testConstructorKwargs(self): """Test kwargs via constructor.""" class SomeMessage(messages.Message): name = messages.StringField(1) number = messages.IntegerField(2) expected = SomeMessage() expected.name = 'my name' expected.number = 200 self.assertEquals(expected, SomeMessage(name='my name', number=200)) def testConstructorNotAField(self): """Test kwargs via constructor with wrong names.""" class SomeMessage(messages.Message): pass self.assertRaisesWithRegexpMatch( AttributeError, 'May not assign arbitrary value does_not_exist to message SomeMessage', SomeMessage, does_not_exist=10) def testGetUnsetRepeatedValue(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) instance = SomeMessage() self.assertEquals([], instance.repeated) self.assertTrue(isinstance(instance.repeated, messages.FieldList)) def testCompareAutoInitializedRepeatedFields(self): class SomeMessage(messages.Message): repeated = messages.IntegerField(1, repeated=True) message1 = SomeMessage(repeated=[]) message2 = SomeMessage() self.assertEquals(message1, message2) class FindDefinitionTest(test_util.TestCase): """Test finding definitions relative to various definitions and modules.""" def setUp(self): """Set up module-space. Starts off empty.""" self.modules = {} def DefineModule(self, name): """Define a module and its parents in module space. Modules that are already defined in self.modules are not re-created. Args: name: Fully qualified name of modules to create. Returns: Deepest nested module. For example: DefineModule('a.b.c') # Returns c. """ name_path = name.split('.') full_path = [] for node in name_path: full_path.append(node) full_name = '.'.join(full_path) self.modules.setdefault(full_name, new.module(full_name)) return self.modules[name] def DefineMessage(self, module, name, children={}, add_to_module=True): """Define a new Message class in the context of a module. Used for easily describing complex Message hierarchy. Message is defined including all child definitions. Args: module: Fully qualified name of module to place Message class in. name: Name of Message to define within module. children: Define any level of nesting of children definitions. To define a message, map the name to another dictionary. The dictionary can itself contain additional definitions, and so on. To map to an Enum, define the Enum class separately and map it by name. add_to_module: If True, new Message class is added to module. If False, new Message is not added. """ # Make sure module exists. module_instance = self.DefineModule(module) # Recursively define all child messages. for attribute, value in children.items(): if isinstance(value, dict): children[attribute] = self.DefineMessage( module, attribute, value, False) # Override default __module__ variable. children['__module__'] = module # Instantiate and possibly add to module. message_class = new.classobj(name, (messages.Message,), dict(children)) if add_to_module: setattr(module_instance, name, message_class) return message_class def Importer(self, module, globals='', locals='', fromlist=None): """Importer function. Acts like __import__. Only loads modules from self.modules. Does not try to load real modules defined elsewhere. Does not try to handle relative imports. Args: module: Fully qualified name of module to load from self.modules. """ if fromlist is None: module = module.split('.')[0] try: return self.modules[module] except KeyError: raise ImportError() def testNoSuchModule(self): """Test searching for definitions that do no exist.""" self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'does.not.exist', importer=self.Importer) def testRefersToModule(self): """Test that referring to a module does not return that module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module', importer=self.Importer) def testNoDefinition(self): """Test not finding a definition in an existing module.""" self.DefineModule('i.am.a.module') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.MyMessage', importer=self.Importer) def testNotADefinition(self): """Test trying to fetch something that is not a definition.""" module = self.DefineModule('i.am.a.module') setattr(module, 'A', 'a string') self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'i.am.a.module.A', importer=self.Importer) def testGlobalFind(self): """Test finding definitions from fully qualified module names.""" A = self.DefineMessage('a.b.c', 'A', {}) self.assertEquals(A, messages.find_definition('a.b.c.A', importer=self.Importer)) B = self.DefineMessage('a.b.c', 'B', {'C':{}}) self.assertEquals(B.C, messages.find_definition('a.b.c.B.C', importer=self.Importer)) def testRelativeToModule(self): """Test finding definitions relative to modules.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.b') c = self.DefineModule('a.b.c') # Define messages. A = self.DefineMessage('a', 'A') B = self.DefineMessage('a.b', 'B') C = self.DefineMessage('a.b.c', 'C') D = self.DefineMessage('a.b.d', 'D') # Find A, B, C and D relative to a. self.assertEquals(A, messages.find_definition( 'A', a, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.B', a, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.c.C', a, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.d.D', a, importer=self.Importer)) # Find A, B, C and D relative to b. self.assertEquals(A, messages.find_definition( 'A', b, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', b, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'c.C', b, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', b, importer=self.Importer)) # Find A, B, C and D relative to c. Module d is the same case as c. self.assertEquals(A, messages.find_definition( 'A', c, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', c, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', c, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'd.D', c, importer=self.Importer)) def testRelativeToMessages(self): """Test finding definitions relative to Message definitions.""" A = self.DefineMessage('a.b', 'A', {'B': {'C': {}, 'D': {}}}) B = A.B C = A.B.C D = A.B.D # Find relative to A. self.assertEquals(A, messages.find_definition( 'A', A, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', A, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'B.C', A, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'B.D', A, importer=self.Importer)) # Find relative to B. self.assertEquals(A, messages.find_definition( 'A', B, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', B, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', B, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', B, importer=self.Importer)) # Find relative to C. self.assertEquals(A, messages.find_definition( 'A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'D', C, importer=self.Importer)) # Find relative to C searching from c. self.assertEquals(A, messages.find_definition( 'b.A', C, importer=self.Importer)) self.assertEquals(B, messages.find_definition( 'b.A.B', C, importer=self.Importer)) self.assertEquals(C, messages.find_definition( 'b.A.B.C', C, importer=self.Importer)) self.assertEquals(D, messages.find_definition( 'b.A.B.D', C, importer=self.Importer)) def testAbsoluteReference(self): """Test finding absolute definition names.""" # Define modules. a = self.DefineModule('a') b = self.DefineModule('a.a') # Define messages. aA = self.DefineMessage('a', 'A') aaA = self.DefineMessage('a.a', 'A') # Always find a.A. self.assertEquals(aA, messages.find_definition('.a.A', None, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', a, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aA, importer=self.Importer)) self.assertEquals(aA, messages.find_definition('.a.A', aaA, importer=self.Importer)) def testFindEnum(self): """Test that Enums are found.""" class Color(messages.Enum): pass A = self.DefineMessage('a', 'A', {'Color': Color}) self.assertEquals( Color, messages.find_definition('Color', A, importer=self.Importer)) def testFalseScope(self): """Test that Message definitions nested in strange objects are hidden.""" global X class X(object): class A(messages.Message): pass self.assertRaises(TypeError, messages.find_definition, 'A', X) self.assertRaises(messages.DefinitionNotFoundError, messages.find_definition, 'X.A', sys.modules[__name__]) def testSearchAttributeFirst(self): """Make sure not faked out by module, but continues searching.""" A = self.DefineMessage('a', 'A') module_A = self.DefineModule('a.A') self.assertEquals(A, messages.find_definition( 'a.A', None, importer=self.Importer)) class FindDefinitionUnicodeTests(test_util.TestCase): def testUnicodeString(self): """Test using unicode names.""" self.assertEquals('ServiceMapping', messages.find_definition( u'protorpc.registry.ServiceMapping', None).__name__) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate_proto_test.""" import os import shutil import cStringIO import sys import tempfile import unittest from protorpc import descriptor from protorpc import generate_proto from protorpc import test_util from protorpc import util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate_proto class FormatProtoFileTest(test_util.TestCase): def setUp(self): self.file_descriptor = descriptor.FileDescriptor() self.output = cStringIO.StringIO() @property def result(self): return self.output.getvalue() def MakeMessage(self, name='MyMessage', fields=[]): message = descriptor.MessageDescriptor() message.name = name message.fields = fields messages_list = getattr(self.file_descriptor, 'fields', []) messages_list.append(message) self.file_descriptor.message_types = messages_list def testBlankPackage(self): self.file_descriptor.package = None generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('', self.result) def testEmptyPackage(self): self.file_descriptor.package = 'my_package' generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('package my_package;\n', self.result) def testSingleField(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '10' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' optional int64 integer_field = 1 [default=10];\n' '}\n', self.result) def testRepeatedFieldWithDefault(self): field = descriptor.FieldDescriptor() field.name = 'integer_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.REPEATED field.variant = descriptor.FieldDescriptor.Variant.INT64 field.default_value = '[10, 20]' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' ' repeated int64 integer_field = 1;\n' '}\n', self.result) def testSingleFieldWithDefaultString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = 'hello' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default='hello'];\n" '}\n', self.result) def testSingleFieldWithDefaultEmptyString(self): field = descriptor.FieldDescriptor() field.name = 'string_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.STRING field.default_value = '' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional string string_field = 1 [default=''];\n" '}\n', self.result) def testSingleFieldWithDefaultMessage(self): field = descriptor.FieldDescriptor() field.name = 'message_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.MESSAGE field.type_name = 'MyNestedMessage' field.default_value = 'not valid' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional MyNestedMessage message_field = 1;\n" '}\n', self.result) def testSingleFieldWithDefaultEnum(self): field = descriptor.FieldDescriptor() field.name = 'enum_field' field.number = 1 field.label = descriptor.FieldDescriptor.Label.OPTIONAL field.variant = descriptor.FieldDescriptor.Variant.ENUM field.type_name = 'my_package.MyEnum' field.default_value = '17' self.MakeMessage(fields=[field]) generate_proto.format_proto_file(self.file_descriptor, self.output) self.assertEquals('\n\n' 'message MyMessage {\n' " optional my_package.MyEnum enum_field = 1 " "[default=17];\n" '}\n', self.result) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protojson.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import base64 import sys import unittest from protorpc import messages from protorpc import protojson from protorpc import test_util from django.utils import simplejson class MyMessage(messages.Message): """Test message containing various types.""" class Color(messages.Enum): RED = 1 GREEN = 2 BLUE = 3 class Nested(messages.Message): nested_value = messages.StringField(1) a_string = messages.StringField(2) an_integer = messages.IntegerField(3) a_float = messages.FloatField(4) a_boolean = messages.BooleanField(5) an_enum = messages.EnumField(Color, 6) a_nested = messages.MessageField(Nested, 7) a_repeated = messages.IntegerField(8, repeated=True) class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protojson # TODO(rafek): Convert this test to the compliance test in test_util. class ProtojsonTest(test_util.TestCase, test_util.ProtoConformanceTestBase): """Test JSON encoding and decoding.""" PROTOLIB = protojson def CompareEncoded(self, expected_encoded, actual_encoded): """JSON encoding will be laundered to remove string differences.""" self.assertEquals(simplejson.loads(expected_encoded), simplejson.loads(actual_encoded)) encoded_empty_message = '{}' encoded_partial = """{ "double_value": 1.23, "int64_value": -100000000000, "int32_value": 1020, "string_value": "a string", "enum_value": "VAL2" } """ encoded_full = """{ "double_value": 1.23, "float_value": -2.5, "int64_value": -100000000000, "uint64_value": 102020202020, "int32_value": 1020, "bool_value": true, "string_value": "a string\u044f", "bytes_value": "YSBieXRlc//+", "enum_value": "VAL2" } """ encoded_repeated = """{ "double_value": [1.23, 2.3], "float_value": [-2.5, 0.5], "int64_value": [-100000000000, 20], "uint64_value": [102020202020, 10], "int32_value": [1020, 718], "bool_value": [true, false], "string_value": ["a string\u044f", "another string"], "bytes_value": ["YSBieXRlc//+", "YW5vdGhlciBieXRlcw=="], "enum_value": ["VAL2", "VAL1"] } """ encoded_nested = """{ "nested": { "a_value": "a string" } } """ encoded_repeated_nested = """{ "repeated_nested": [{"a_value": "a string"}, {"a_value": "another string"}] } """ unexpected_tag_message = '{"unknown": "value"}' encoded_default_assigned = '{"a_value": "a default"}' encoded_nested_empty = '{"nested": {}}' encoded_repeated_nested_empty = '{"repeated_nested": [{}, {}]}' encoded_extend_message = '{"int64_value": [400, 50, 6000]}' encoded_string_types = '{"string_value": "Latin"}' def testConvertIntegerToFloat(self): """Test that integers passed in to float fields are converted. This is necessary because JSON outputs integers for numbers with 0 decimals. """ message = protojson.decode_message(MyMessage, '{"a_float": 10}') self.assertTrue(isinstance(message.a_float, float)) self.assertEquals(10.0, message.a_float) def testWrongTypeAssignment(self): """Test when wrong type is assigned to a field.""" self.assertRaises(messages.ValidationError, protojson.decode_message, MyMessage, '{"a_string": 10}') def testNumericEnumeration(self): """Test that numbers work for enum values.""" message = protojson.decode_message(MyMessage, '{"an_enum": 2}') expected_message = MyMessage() expected_message.an_enum = MyMessage.Color.GREEN self.assertEquals(expected_message, message) def testNullValues(self): """Test that null values overwrite existing values.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, ('{"an_integer": null,' ' "a_nested": null' '}'))) def testEmptyList(self): """Test that empty lists are ignored.""" self.assertEquals(MyMessage(), protojson.decode_message(MyMessage, '{"a_repeated": []}')) def testNotJSON(self): """Test error when string is not valid JSON.""" self.assertRaises(ValueError, protojson.decode_message, MyMessage, '{this is not json}') def testDoNotEncodeStrangeObjects(self): """Test trying to encode a strange object. The main purpose of this test is to complete coverage. It ensures that the default behavior of the JSON encoder is preserved when someone tries to serialized an unexpected type. """ class BogusObject(object): def check_initialized(self): pass self.assertRaises(TypeError, protojson.encode_message, BogusObject()) def testMergeEmptyString(self): """Test merging the empty or space only string.""" message = protojson.decode_message(test_util.OptionalMessage, '') self.assertEquals(test_util.OptionalMessage(), message) message = protojson.decode_message(test_util.OptionalMessage, ' ') self.assertEquals(test_util.OptionalMessage(), message) class InvalidJsonModule(object): pass class ValidJsonModule(object): class JSONEncoder(object): pass class TestJsonDependencyLoading(test_util.TestCase): """Test loading various implementations of json.""" def setUp(self): """Save original import function.""" self.django_simplejson = sys.modules.pop('django.utils.simplejson', None) self.simplejson = sys.modules.pop('simplejson', None) self.json = sys.modules.pop('json', None) self.original_import = __builtins__.__import__ def block_all_jsons(name, *args, **kwargs): if 'json' in name: if name in sys.modules: module = sys.modules[name] module.name = name return module raise ImportError('Unable to find %s' % name) else: return self.original_import(name, *args, **kwargs) __builtins__.__import__ = block_all_jsons def tearDown(self): """Restore original import functions and any loaded modules.""" __builtins__.__import__ = self.original_import def reset_module(name, module): if module: sys.modules[name] = module else: sys.modules.pop(name, None) reset_module('django.utils.simplejson', self.django_simplejson) reset_module('simplejson', self.simplejson) reset_module('json', self.json) reload(protojson) def testLoadProtojsonWithValidJsonModule(self): """Test loading protojson module with a valid json dependency.""" sys.modules['json'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('json', protojson.json.name) def testLoadProtojsonWithSimplejsonModule(self): """Test loading protojson module with simplejson dependency.""" sys.modules['simplejson'] = ValidJsonModule # This will cause protojson to reload with the default json module # instead of simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModule(self): """Loading protojson module with an invalid json defaults to simplejson.""" sys.modules['json'] = InvalidJsonModule sys.modules['simplejson'] = ValidJsonModule # Ignore bad module and default back to simplejson. reload(protojson) self.assertEquals('simplejson', protojson.json.name) def testLoadProtojsonWithInvalidJsonModuleAndNoSimplejson(self): """Loading protojson module with invalid json and no simplejson.""" sys.modules['json'] = InvalidJsonModule # Bad module without simplejson back raises errors. self.assertRaisesWithRegexpMatch( ImportError, 'json library "json" is not compatible with ProtoRPC', reload, protojson) def testLoadProtojsonWithNoJsonModules(self): """Loading protojson module with invalid json and no simplejson.""" # No json modules raise the first exception. self.assertRaisesWithRegexpMatch( ImportError, 'Unable to find json', reload, protojson) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.generate.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import sys import unittest from protorpc import generate from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = generate class IndentWriterTest(test_util.TestCase): def setUp(self): self.out = cStringIO.StringIO() self.indent_writer = generate.IndentWriter(self.out) def testWriteLine(self): self.indent_writer.write_line('This is a line') self.indent_writer.write_line('This is another line') self.assertEquals('This is a line\n' 'This is another line\n', self.out.getvalue()) def testLeftShift(self): self.run_count = 0 def mock_write_line(line): self.run_count += 1 self.assertEquals('same as calling write_line', line) self.indent_writer.write_line = mock_write_line self.indent_writer << 'same as calling write_line' self.assertEquals(1, self.run_count) def testIndentation(self): self.indent_writer << 'indent 0' self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.indent_writer.end_indent() self.indent_writer << 'end 2' self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testAltIndentation(self): self.indent_writer = generate.IndentWriter(self.out, indent_space=3) self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.begin_indent() self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 2' self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer.end_indent() self.indent_writer << 'end 1' self.assertEquals(0, self.indent_writer.indent_level) self.assertRaises(generate.IndentationError, self.indent_writer.end_indent) self.assertEquals(0, self.indent_writer.indent_level) self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def testIndent(self): self.indent_writer << 'indent 0' self.assertEquals(0, self.indent_writer.indent_level) def indent1(): self.indent_writer << 'indent 1' self.assertEquals(1, self.indent_writer.indent_level) def indent2(): self.indent_writer << 'indent 2' self.assertEquals(2, self.indent_writer.indent_level) test_util.do_with(self.indent_writer.indent(), indent2) self.assertEquals(1, self.indent_writer.indent_level) self.indent_writer << 'end 2' test_util.do_with(self.indent_writer.indent(), indent1) self.assertEquals(0, self.indent_writer.indent_level) self.indent_writer << 'end 1' self.assertEquals('indent 0\n' ' indent 1\n' ' indent 2\n' ' end 2\n' 'end 1\n', self.out.getvalue()) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """JSON support for message types. Public classes: MessageJSONEncoder: JSON encoder for message objects. Public functions: encode_message: Encodes a message in to a JSON string. decode_message: Merge from a JSON string in to a message. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import cStringIO import base64 import logging from protorpc import messages __all__ = [ 'CONTENT_TYPE', 'encode_message', 'decode_message', ] CONTENT_TYPE = 'application/json' def _load_json_module(): """Try to load a valid json module. There are more than one json modules that might be installed. They are mostly compatible with one another but some versions may be different. This function attempts to load various json modules in a preferred order. It does a basic check to guess if a loaded version of json is compatible. Returns: Comptable json module. Raises: ImportError if there are no json modules or the loaded json module is not compatible with ProtoRPC. """ first_import_error = None for module_name in ['json', 'simplejson', 'django.utils.simplejson']: try: module = __import__(module_name, {}, {}, 'json') if not hasattr(module, 'JSONEncoder'): message = ('json library "%s" is not compatible with ProtoRPC' % module_name) logging.warning(message) raise ImportError(message) else: return module except ImportError, err: if not first_import_error: first_import_error = err logging.error('Must use valid json library (Python 2.6 json, simplejson or ' 'django.utils.simplejson)') raise first_import_error json = _load_json_module() class _MessageJSONEncoder(json.JSONEncoder): """Message JSON encoder class. Extension of JSONEncoder that can build JSON from a message object. """ def default(self, value): """Return dictionary instance from a message object. Args: value: Value to get dictionary for. If not encodable, will call superclasses default method. """ if isinstance(value, messages.Enum): return str(value) if isinstance(value, messages.Message): result = {} for field in value.all_fields(): item = value.get_assigned_value(field.name) if item not in (None, [], ()): if isinstance(field, messages.BytesField): if field.repeated: item = [base64.b64encode(i) for i in item] else: item = base64.b64encode(item) result[field.name] = item return result else: return super(_MessageJSONEncoder, self).default(value) def encode_message(message): """Encode Message instance to JSON string. Args: Message instance to encode in to JSON string. Returns: String encoding of Message instance in protocol JSON format. Raises: messages.ValidationError if message is not initialized. """ message.check_initialized() return json.dumps(message, cls=_MessageJSONEncoder) def decode_message(message_type, encoded_message): """Merge JSON structure to Message instance. Args: message_type: Message to decode data to. encoded_message: JSON encoded version of message. Returns: Decoded instance of message_type. Raises: ValueError: If encoded_message is not valid JSON. messages.ValidationError if merged message is not initialized. """ if not encoded_message.strip(): return message_type() dictionary = json.loads(encoded_message) def decode_dictionary(message_type, dictionary): """Merge dictionary in to message. Args: message: Message to merge dictionary in to. dictionary: Dictionary to extract information from. Dictionary is as parsed from JSON. Nested objects will also be dictionaries. """ message = message_type() for key, value in dictionary.iteritems(): if value is None: message.reset(key) continue try: field = message.field_by_name(key) except KeyError: # TODO(rafek): Support saving unknown values. continue # Normalize values in to a list. if isinstance(value, list): if not value: continue else: value = [value] valid_value = [] for item in value: if isinstance(field, messages.EnumField): item = field.type(item) elif isinstance(field, messages.BytesField): item = base64.b64decode(item) elif isinstance(field, messages.MessageField): item = decode_dictionary(field.type, item) elif (isinstance(field, messages.FloatField) and isinstance(item, (int, long))): item = float(item) valid_value.append(item) if field.repeated: existing_value = getattr(message, field.name) setattr(message, field.name, valid_value) else: setattr(message, field.name, valid_value[-1]) return message message = decode_dictionary(message_type, dictionary) message.check_initialized() return message
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Main module for ProtoRPC package.""" __author__ = 'rafek@google.com (Rafe Kaplan)'
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.descriptor.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import unittest from protorpc import descriptor from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util RUSSIA = u'\u0420\u043e\u0441\u0441\u0438\u044f' class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = descriptor class DescribeEnumValueTest(test_util.TestCase): def testDescribe(self): class MyEnum(messages.Enum): MY_NAME = 10 expected = descriptor.EnumValueDescriptor() expected.name = 'MY_NAME' expected.number = 10 described = descriptor.describe_enum_value(MyEnum.MY_NAME) described.check_initialized() self.assertEquals(expected, described) class DescribeEnumTest(test_util.TestCase): def testEmptyEnum(self): class EmptyEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'EmptyEnum' described = descriptor.describe_enum(EmptyEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MyScope(messages.Message): class NestedEnum(messages.Enum): pass expected = descriptor.EnumDescriptor() expected.name = 'NestedEnum' described = descriptor.describe_enum(MyScope.NestedEnum) described.check_initialized() self.assertEquals(expected, described) def testEnumWithItems(self): class EnumWithItems(messages.Enum): A = 3 B = 1 C = 2 expected = descriptor.EnumDescriptor() expected.name = 'EnumWithItems' a = descriptor.EnumValueDescriptor() a.name = 'A' a.number = 3 b = descriptor.EnumValueDescriptor() b.name = 'B' b.number = 1 c = descriptor.EnumValueDescriptor() c.name = 'C' c.number = 2 expected.values = [b, c, a] described = descriptor.describe_enum(EnumWithItems) described.check_initialized() self.assertEquals(expected, described) class DescribeFieldTest(test_util.TestCase): def testLabel(self): for repeated, required, expected_label in ( (True, False, descriptor.FieldDescriptor.Label.REPEATED), (False, True, descriptor.FieldDescriptor.Label.REQUIRED), (False, False, descriptor.FieldDescriptor.Label.OPTIONAL)): field = messages.IntegerField(10, required=required, repeated=repeated) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = expected_label expected.variant = descriptor.FieldDescriptor.Variant.INT64 described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault(self): for field_class, default, expected_default in ( (messages.IntegerField, 200, '200'), (messages.FloatField, 1.5, '1.5'), (messages.FloatField, 1e6, '1000000.0'), (messages.BooleanField, True, 'true'), (messages.BooleanField, False, 'false'), (messages.BytesField, 'ab\xF1', 'ab\\xf1'), (messages.StringField, RUSSIA, RUSSIA), ): field = field_class(10, default=default) field.name = u'a_field' expected = descriptor.FieldDescriptor() expected.name = u'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = field_class.DEFAULT_VARIANT expected.default_value = expected_default described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) def testDefault_EnumField(self): class MyEnum(messages.Enum): VAL = 1 field = messages.EnumField(MyEnum, 10, default=MyEnum.VAL) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.EnumField.DEFAULT_VARIANT expected.type_name = 'descriptor_test.MyEnum' expected.default_value = '1' described = descriptor.describe_field(field) self.assertEquals(expected, described) def testMessageField(self): field = messages.MessageField(descriptor.FieldDescriptor, 10) field.name = 'a_field' expected = descriptor.FieldDescriptor() expected.name = 'a_field' expected.number = 10 expected.label = descriptor.FieldDescriptor.Label.OPTIONAL expected.variant = messages.MessageField.DEFAULT_VARIANT expected.type_name = ('protorpc.descriptor.FieldDescriptor') described = descriptor.describe_field(field) described.check_initialized() self.assertEquals(expected, described) class DescribeMessageTest(test_util.TestCase): def testEmptyDefinition(self): class MyMessage(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MyMessage' described = descriptor.describe_message(MyMessage) described.check_initialized() self.assertEquals(expected, described) def testDefinitionWithFields(self): class MessageWithFields(messages.Message): field1 = messages.IntegerField(10) field2 = messages.StringField(30) field3 = messages.IntegerField(20) expected = descriptor.MessageDescriptor() expected.name = 'MessageWithFields' expected.fields = [ descriptor.describe_field(MessageWithFields.field_by_name('field1')), descriptor.describe_field(MessageWithFields.field_by_name('field3')), descriptor.describe_field(MessageWithFields.field_by_name('field2')), ] described = descriptor.describe_message(MessageWithFields) described.check_initialized() self.assertEquals(expected, described) def testNestedEnum(self): class MessageWithEnum(messages.Message): class Mood(messages.Enum): GOOD = 1 BAD = 2 UGLY = 3 class Music(messages.Enum): CLASSIC = 1 JAZZ = 2 BLUES = 3 expected = descriptor.MessageDescriptor() expected.name = 'MessageWithEnum' expected.enum_types = [descriptor.describe_enum(MessageWithEnum.Mood), descriptor.describe_enum(MessageWithEnum.Music)] described = descriptor.describe_message(MessageWithEnum) described.check_initialized() self.assertEquals(expected, described) def testNestedMessage(self): class MessageWithMessage(messages.Message): class Nesty(messages.Message): pass expected = descriptor.MessageDescriptor() expected.name = 'MessageWithMessage' expected.message_types = [ descriptor.describe_message(MessageWithMessage.Nesty)] described = descriptor.describe_message(MessageWithMessage) described.check_initialized() self.assertEquals(expected, described) class DescribeMethodTest(test_util.TestCase): """Test describing remote methods.""" def testDescribe(self): class Request(messages.Message): pass class Response(messages.Message): pass @remote.method(Request, Response) def remote_method(request): pass expected = descriptor.MethodDescriptor() expected.name = 'remote_method' expected.request_type = 'descriptor_test.Request' expected.response_type = 'descriptor_test.Response' described = descriptor.describe_method(remote_method) described.check_initialized() self.assertEquals(expected, described) class DescribeServiceTest(test_util.TestCase): """Test describing service classes.""" def testDescribe(self): class Request1(messages.Message): pass class Response1(messages.Message): pass class Request2(messages.Message): pass class Response2(messages.Message): pass class MyService(remote.Service): @remote.method(Request1, Response1) def method1(self, request): pass @remote.method(Request2, Response2) def method2(self, request): pass expected = descriptor.ServiceDescriptor() expected.name = 'MyService' expected.methods = [] expected.methods.append(descriptor.describe_method(MyService.method1)) expected.methods.append(descriptor.describe_method(MyService.method2)) described = descriptor.describe_service(MyService) described.check_initialized() self.assertEquals(expected, described) class DescribeFileTest(test_util.TestCase): """Test describing modules.""" def LoadModule(self, module_name, source): result = {'__name__': module_name, 'messages': messages, 'remote': remote, } exec source in result module = new.module(module_name) for name, value in result.iteritems(): setattr(module, name, value) return module def testEmptyModule(self): """Test describing an empty file.""" module = new.module('my.package.name') expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testNoPackageName(self): """Test describing a module with no module name.""" module = new.module('') expected = descriptor.FileDescriptor() described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testPackageName(self): """Test using the 'package' module attribute.""" module = new.module('my.module.name') module.package = 'my.package.name' expected = descriptor.FileDescriptor() expected.package = 'my.package.name' described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testMessages(self): """Test that messages are described.""" module = self.LoadModule('my.package', 'class Message1(messages.Message): pass\n' 'class Message2(messages.Message): pass\n') message1 = descriptor.MessageDescriptor() message1.name = 'Message1' message2 = descriptor.MessageDescriptor() message2.name = 'Message2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.message_types = [message1, message2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testEnums(self): """Test that enums are described.""" module = self.LoadModule('my.package', 'class Enum1(messages.Enum): pass\n' 'class Enum2(messages.Enum): pass\n') enum1 = descriptor.EnumDescriptor() enum1.name = 'Enum1' enum2 = descriptor.EnumDescriptor() enum2.name = 'Enum2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.enum_types = [enum1, enum2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) def testServices(self): """Test that services are described.""" module = self.LoadModule('my.package', 'class Service1(remote.Service): pass\n' 'class Service2(remote.Service): pass\n') service1 = descriptor.ServiceDescriptor() service1.name = 'Service1' service2 = descriptor.ServiceDescriptor() service2.name = 'Service2' expected = descriptor.FileDescriptor() expected.package = 'my.package' expected.service_types = [service1, service2] described = descriptor.describe_file(module) described.check_initialized() self.assertEquals(expected, described) class DescribeFileSetTest(test_util.TestCase): """Test describing multiple modules.""" def testNoModules(self): """Test what happens when no modules provided.""" described = descriptor.describe_file_set([]) described.check_initialized() # The described FileSet.files will be None. self.assertEquals(descriptor.FileSet(), described) def testWithModules(self): """Test what happens when no modules provided.""" modules = [new.module('package1'), new.module('package1')] file1 = descriptor.FileDescriptor() file1.package = 'package1' file2 = descriptor.FileDescriptor() file2.package = 'package2' expected = descriptor.FileSet() expected.files = [file1, file1] described = descriptor.describe_file_set(modules) described.check_initialized() self.assertEquals(expected, described) class DescribeTest(test_util.TestCase): def testModule(self): self.assertEquals(descriptor.describe_file(test_util), descriptor.describe(test_util)) def testMethod(self): class Param(messages.Message): pass class Service(remote.Service): @remote.method(Param, Param) def fn(self): return Param() self.assertEquals(descriptor.describe_method(Service.fn), descriptor.describe(Service.fn)) def testField(self): self.assertEquals( descriptor.describe_field(test_util.NestedMessage.a_value), descriptor.describe(test_util.NestedMessage.a_value)) def testEnumValue(self): self.assertEquals( descriptor.describe_enum_value( test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.describe(test_util.OptionalMessage.SimpleEnum.VAL1)) def testMessage(self): self.assertEquals(descriptor.describe_message(test_util.NestedMessage), descriptor.describe(test_util.NestedMessage)) def testEnum(self): self.assertEquals( descriptor.describe_enum(test_util.OptionalMessage.SimpleEnum), descriptor.describe(test_util.OptionalMessage.SimpleEnum)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testService(self): class Service(remote.Service): pass self.assertEquals(descriptor.describe_service(Service), descriptor.describe(Service)) def testUndescribable(self): class NonService(object): def fn(self): pass for value in (NonService, NonService.fn, 1, 'string', 1.2, None): self.assertEquals(None, descriptor.describe(value)) class ModuleFinderTest(test_util.TestCase): def testFindModule(self): self.assertEquals(descriptor.describe_file(registry), descriptor.import_descriptor_loader('protorpc.registry')) def testFindMessage(self): self.assertEquals( descriptor.describe_message(descriptor.FileSet), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet')) def testFindField(self): self.assertEquals( descriptor.describe_field(descriptor.FileSet.files), descriptor.import_descriptor_loader('protorpc.descriptor.FileSet.files')) def testFindEnumValue(self): self.assertEquals( descriptor.describe_enum_value(test_util.OptionalMessage.SimpleEnum.VAL1), descriptor.import_descriptor_loader( 'protorpc.test_util.OptionalMessage.SimpleEnum.VAL1')) def testFindMethod(self): self.assertEquals( descriptor.describe_method(registry.RegistryService.services), descriptor.import_descriptor_loader( 'protorpc.registry.RegistryService.services')) def testFindService(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('protorpc.registry.RegistryService')) def testFindWithAbsoluteName(self): self.assertEquals( descriptor.describe_service(registry.RegistryService), descriptor.import_descriptor_loader('.protorpc.registry.RegistryService')) def testFindWrongThings(self): for name in ('a', 'protorpc.registry.RegistryService.__init__', '', ): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, descriptor.import_descriptor_loader, name) class DescriptorLibraryTest(test_util.TestCase): def setUp(self): self.packageless = descriptor.MessageDescriptor() self.packageless.name = 'Packageless' self.library = descriptor.DescriptorLibrary( descriptors={ 'not.real.Packageless': self.packageless, 'Packageless': self.packageless, }) def testLookupPackage(self): self.assertEquals('csv', self.library.lookup_package('csv')) self.assertEquals('protorpc', self.library.lookup_package('protorpc')) self.assertEquals('protorpc.registry', self.library.lookup_package('protorpc.registry')) self.assertEquals('protorpc.registry', self.library.lookup_package('.protorpc.registry')) self.assertEquals( 'protorpc.registry', self.library.lookup_package('protorpc.registry.RegistryService')) self.assertEquals( 'protorpc.registry', self.library.lookup_package( 'protorpc.registry.RegistryService.services')) def testLookupNonPackages(self): for name in ('', 'a', 'protorpc.descriptor.DescriptorLibrary'): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for %s' % name, self.library.lookup_package, name) def testNoPackage(self): self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for not.real', self.library.lookup_package, 'not.real.Packageless') self.assertEquals(None, self.library.lookup_package('Packageless')) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stand-alone implementation of in memory protocol messages. Public Classes: Enum: Represents an enumerated type. Variant: Hint for wire format to determine how to serialize. Message: Base class for user defined messages. IntegerField: Field for integer values. FloatField: Field for float values. BooleanField: Field for boolean values. BytesField: Field for binary string values. StringField: Field for UTF-8 string values. MessageField: Field for other message type values. EnumField: Field for enumerated type values. Public Exceptions (indentation indications class hierarchy): EnumDefinitionError: Raised when enumeration is incorrectly defined. FieldDefinitionError: Raised when field is incorrectly defined. InvalidVariantError: Raised when variant is not compatible with field type. InvalidDefaultError: Raised when default is not compatiable with field. InvalidNumberError: Raised when field number is out of range or reserved. MessageDefinitionError: Raised when message is incorrectly defined. DuplicateNumberError: Raised when field has duplicate number with another. ValidationError: Raised when a message or field is not valid. DefinitionNotFoundError: Raised when definition not found. """ __author__ = 'rafek@google.com (Rafe Kaplan)' import inspect import os import sys import traceback import types import weakref from protorpc import util __all__ = ['MAX_ENUM_VALUE', 'MAX_FIELD_NUMBER', 'FIRST_RESERVED_FIELD_NUMBER', 'LAST_RESERVED_FIELD_NUMBER', 'Enum', 'Field', 'FieldList', 'Variant', 'Message', 'IntegerField', 'FloatField', 'BooleanField', 'BytesField', 'StringField', 'MessageField', 'EnumField', 'find_definition', 'Error', 'DecodeError', 'EnumDefinitionError', 'FieldDefinitionError', 'InvalidVariantError', 'InvalidDefaultError', 'InvalidNumberError', 'MessageDefinitionError', 'DuplicateNumberError', 'ValidationError', 'DefinitionNotFoundError', ] # TODO(rafek): Add extended module test to ensure all exceptions # in services extends Error. Error = util.Error class EnumDefinitionError(Error): """Enumeration definition error.""" class FieldDefinitionError(Error): """Field definition error.""" class InvalidVariantError(FieldDefinitionError): """Invalid variant provided to field.""" class InvalidDefaultError(FieldDefinitionError): """Invalid default provided to field.""" class InvalidNumberError(FieldDefinitionError): """Invalid number provided to field.""" class MessageDefinitionError(Error): """Enumeration definition error.""" class DuplicateNumberError(Error): """Duplicate number assigned to field.""" class DefinitionNotFoundError(Error): """Raised when definition is not found.""" class DecodeError(Error): """Error found decoding message from encoded form.""" class ValidationError(Error): """Invalid value for message error.""" def __str__(self): """Prints string with field name if present on exception.""" message = Error.__str__(self) try: field_name = self.field_name except AttributeError: return message else: return 'Field %s: %s' % (field_name, message) # Attributes that are reserved by a class definition that # may not be used by either Enum or Message class definitions. _RESERVED_ATTRIBUTE_NAMES = frozenset( ['__module__', '__doc__']) _POST_INIT_FIELD_ATTRIBUTE_NAMES = frozenset( ['name', '_message_definition', '_MessageField__type', '_EnumField__type', '_EnumField__resolved_default']) _POST_INIT_ATTRIBUTE_NAMES = frozenset( ['_message_definition']) # Maximum enumeration value as defined by the protocol buffers standard. # All enum values must be less than or equal to this value. MAX_ENUM_VALUE = (2 ** 29) - 1 # Maximum field number as defined by the protocol buffers standard. # All field numbers must be less than or equal to this value. MAX_FIELD_NUMBER = (2 ** 29) - 1 # Field numbers between 19000 and 19999 inclusive are reserved by the # protobuf protocol and may not be used by fields. FIRST_RESERVED_FIELD_NUMBER = 19000 LAST_RESERVED_FIELD_NUMBER = 19999 class _DefinitionClass(type): """Base meta-class used for definition meta-classes. The Enum and Message definition classes share some basic functionality. Both of these classes may be contained by a Message definition. After initialization, neither class may have attributes changed except for the protected _message_definition attribute, and that attribute may change only once. """ __initialized = False def __init__(cls, name, bases, dct): """Constructor.""" type.__init__(cls, name, bases, dct) # Base classes may never be initialized. if cls.__bases__ != (object,): cls.__initialized = True def message_definition(cls): """Get outer Message definition that contains this definition. Returns: Containing Message definition if definition is contained within one, else None. """ try: return cls._message_definition() except AttributeError: return None def __setattr__(cls, name, value): """Overridden so that cannot set variables on definition classes after init. Setting attributes on a class must work during the period of initialization to set the enumation value class variables and build the name/number maps. Once __init__ has set the __initialized flag to True prohibits setting any more values on the class. The class is in effect frozen. Args: name: Name of value to set. value: Value to set. """ if cls.__initialized and name not in _POST_INIT_ATTRIBUTE_NAMES: raise AttributeError('May not change values: %s' % name) else: type.__setattr__(cls, name, value) def __delattr__(cls, name): """Overridden so that cannot delete varaibles on definition classes.""" raise TypeError('May not delete attributes on definition class') def definition_name(cls): """Helper method for creating definition name. Names will be generated to include the classes package name, scope (if the class is nested in another definition) and class name. By default, the package name for a definition is derived from its module name. However, this value can be overriden by placing a 'package' attribute in the module that contains the definition class. For example: package = 'some.alternate.package' class MyMessage(Message): ... >>> MyMessage.definition_name() some.alternate.package.MyMessage Returns: Dot-separated fully qualified name of definition. """ outer_definition = cls.message_definition() if not outer_definition: definition_module = sys.modules.get(cls.__module__, None) if definition_module: try: package = definition_module.package except AttributeError: package = definition_module.__name__ if package == '__main__': try: file_name = definition_module.__file__ except AttributeError: pass else: base_name = os.path.basename(file_name) package = '.'.join(base_name.split('.')[:-1]) else: return unicode(cls.__name__) else: return u'%s.%s' % (outer_definition.definition_name(), cls.__name__) return u'%s.%s' % (package, cls.__name__) class _EnumClass(_DefinitionClass): """Meta-class used for defining the Enum base class. Meta-class enables very specific behavior for any defined Enum class. All attributes defined on an Enum sub-class must be integers. Each attribute defined on an Enum sub-class is translated into an instance of that sub-class, with the name of the attribute as its name, and the number provided as its value. It also ensures that only one level of Enum class hierarchy is possible. In other words it is not possible to delcare sub-classes of sub-classes of Enum. This class also defines some functions in order to restrict the behavior of the Enum class and its sub-classes. It is not possible to change the behavior of the Enum class in later classes since any new classes may be defined with only integer values, and no methods. """ def __init__(cls, name, bases, dct): # Can only define one level of sub-classes below Enum. if not (bases == (object,) or bases == (Enum,)): raise EnumDefinitionError('Enum type %s may only inherit from Enum' % (name,)) cls.__by_number = {} cls.__by_name = {} # Enum base class does not need to be initialized or locked. if bases != (object,): # Replace integer with number. for attribute, value in dct.iteritems(): # Module will be in every enum class. if attribute in _RESERVED_ATTRIBUTE_NAMES: continue # Reject anything that is not an int. if not isinstance(value, (int, long)): raise EnumDefinitionError( 'May only use integers in Enum definitions. Found: %s = %s' % (attribute, value)) # Protocol buffer standard recommends non-negative values. # Reject negative values. if value < 0: raise EnumDefinitionError( 'Must use non-negative enum values. Found: %s = %d' % (attribute, value)) if value > MAX_ENUM_VALUE: raise EnumDefinitionError( 'Must use enum values less than or equal %d. Found: %s = %d' % (MAX_ENUM_VALUE, attribute, value)) if value in cls.__by_number: raise EnumDefinitionError( 'Value for %s = %d is already defined: %s' % (attribute, value, cls.__by_number[value].name)) # Create enum instance and list in new Enum type. instance = object.__new__(cls) cls.__init__(instance, attribute, value) cls.__by_name[instance.name] = instance cls.__by_number[instance.number] = instance setattr(cls, attribute, instance) _DefinitionClass.__init__(cls, name, bases, dct) def __iter__(cls): """Iterate over all values of enum. Yields: Enumeration instances of the Enum class in arbitrary order. """ return cls.__by_number.itervalues() def names(cls): """Get all names for Enum. Returns: An iterator for names of the enumeration in arbitrary order. """ return cls.__by_name.iterkeys() def numbers(cls): """Get all numbers for Enum. Returns: An iterator for all numbers of the enumeration in arbitrary order. """ return cls.__by_number.iterkeys() def lookup_by_name(cls, name): """Look up Enum by name. Args: name: Name of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_name[name] def lookup_by_number(cls, number): """Look up Enum by number. Args: number: Number of enum to find. Returns: Enum sub-class instance of that value. """ return cls.__by_number[number] def __len__(cls): return len(cls.__by_name) class Enum(object): """Base class for all enumerated types.""" __metaclass__ = _EnumClass __slots__ = set(('name', 'number')) def __new__(cls, index): """Acts as look-up routine after class is initialized. The purpose of overriding __new__ is to provide a way to treat Enum subclasses as casting types, similar to how the int type functions. A program can pass a string or an integer and this method with "convert" that value in to an appropriate Enum instance. Args: index: Name or number to look up. During initialization this is always the name of the new enum value. Raises: TypeError: When an inappropriate index value is passed provided. """ # If is enum type of this class, return it. if isinstance(index, cls): return index # If number, look up by number. if isinstance(index, (int, long)): try: return cls.lookup_by_number(index) except KeyError: pass # If name, look up by name. if isinstance(index, basestring): try: return cls.lookup_by_name(index) except KeyError: pass raise TypeError('No such value for %s in Enum %s' % (index, cls.__name__)) def __init__(self, name, number=None): """Initialize new Enum instance. Since this should only be called during class initialization any calls that happen after the class is frozen raises an exception. """ # Immediately return if __init__ was called after _Enum.__init__(). # It means that casting operator version of the class constructor # is being used. if getattr(type(self), '_DefinitionClass__initialized'): return object.__setattr__(self, 'name', name) object.__setattr__(self, 'number', number) def __setattr__(self, name, value): raise TypeError('May not change enum values') def __str__(self): return self.name def __int__(self): return self.number def __repr__(self): return '%s(%s, %d)' % (type(self).__name__, self.name, self.number) def __cmp__(self, other): """Order is by number.""" if isinstance(other, type(self)): return cmp(self.number, other.number) return NotImplemented @classmethod def to_dict(cls): """Make dictionary version of enumerated class. Dictionary created this way can be used with def_num and import_enum. Returns: A dict (name) -> number """ return dict((item.name, item.number) for item in iter(cls)) @staticmethod def def_enum(dct, name): """Define enum class from dictionary. Args: dct: Dictionary of enumerated values for type. name: Name of enum. """ return type(name, (Enum,), dct) # TODO(rafek): Determine to what degree this enumeration should be compatible # with FieldDescriptor.Type in: # # http://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto class Variant(Enum): """Wire format variant. Used by the 'protobuf' wire format to determine how to transmit a single piece of data. May be used by other formats. See: http://code.google.com/apis/protocolbuffers/docs/encoding.html Values: DOUBLE: 64-bit floating point number. FLOAT: 32-bit floating point number. INT64: 64-bit signed integer. UINT64: 64-bit unsigned integer. INT32: 32-bit signed integer. BOOL: Boolean value (True or False). STRING: String of UTF-8 encoded text. MESSAGE: Embedded message as byte string. BYTES: String of 8-bit bytes. UINT32: 32-bit unsigned integer. ENUM: Enum value as integer. SINT32: 32-bit signed integer. Uses "zig-zag" encoding. SINT64: 64-bit signed integer. Uses "zig-zag" encoding. """ DOUBLE = 1 FLOAT = 2 INT64 = 3 UINT64 = 4 INT32 = 5 BOOL = 8 STRING = 9 MESSAGE = 11 BYTES = 12 UINT32 = 13 ENUM = 14 SINT32 = 17 SINT64 = 18 class _MessageClass(_DefinitionClass): """Meta-class used for defining the Message base class. For more details about Message classes, see the Message class docstring. Information contained there may help understanding this class. Meta-class enables very specific behavior for any defined Message class. All attributes defined on an Message sub-class must be field instances, Enum class definitions or other Message class definitions. Each field attribute defined on an Message sub-class is added to the set of field definitions and the attribute is translated in to a slot. It also ensures that only one level of Message class hierarchy is possible. In other words it is not possible to declare sub-classes of sub-classes of Message. This class also defines some functions in order to restrict the behavior of the Message class and its sub-classes. It is not possible to change the behavior of the Message class in later classes since any new classes may be defined with only field, Enums and Messages, and no methods. """ def __new__(cls, name, bases, dct): """Create new Message class instance. The __new__ method of the _MessageClass type is overridden so as to allow the translation of Field instances to slots. """ by_number = {} by_name = {} variant_map = {} if bases != (object,): # Can only define one level of sub-classes below Message. if bases != (Message,): raise MessageDefinitionError( 'Message types may only inherit from Message') enums = [] messages = [] # Must not use iteritems because this loop will change the state of dct. for key, field in dct.items(): if key in _RESERVED_ATTRIBUTE_NAMES: continue if isinstance(field, type) and issubclass(field, Enum): enums.append(key) continue if (isinstance(field, type) and issubclass(field, Message) and field is not Message): messages.append(key) continue # Reject anything that is not a field. if type(field) is Field or not isinstance(field, Field): raise MessageDefinitionError( 'May only use fields in message definitions. Found: %s = %s' % (key, field)) if field.number in by_number: raise DuplicateNumberError( 'Field with number %d declared more than once in %s' % (field.number, name)) field.name = key # Place in name and number maps. by_name[key] = field by_number[field.number] = field # Add enums if any exist. if enums: dct['__enums__'] = sorted(enums) # Add messages if any exist. if messages: dct['__messages__'] = sorted(messages) dct['_Message__by_number'] = by_number dct['_Message__by_name'] = by_name return _DefinitionClass.__new__(cls, name, bases, dct) def __init__(cls, name, bases, dct): """Initializer required to assign references to new class.""" if bases != (object,): for value in dct.itervalues(): if isinstance(value, _DefinitionClass) and not value is Message: value._message_definition = weakref.ref(cls) for field in cls.all_fields(): field._message_definition = weakref.ref(cls) _DefinitionClass.__init__(cls, name, bases, dct) class Message(object): """Base class for user defined message objects. Used to define messages for efficient transmission across network or process space. Messages are defined using the field classes (IntegerField, FloatField, EnumField, etc.). Messages are more restricted than normal classes in that they may only contain field attributes and other Message and Enum definitions. These restrictions are in place because the structure of the Message class is intentended to itself be transmitted across network or process space and used directly by clients or even other servers. As such methods and non-field attributes could not be transmitted with the structural information causing discrepancies between different languages and implementations. Initialization and validation: A Message object is considered to be initialized if it has all required fields and any nested messages are also initialized. Calling 'check_initialized' will raise a ValidationException if it is not initialized; 'is_initialized' returns a boolean value indicating if it is valid. Validation automatically occurs when Message objects are created and populated. Validation that a given value will be compatible with a field that it is assigned to can be done through the Field instances validate() method. The validate method used on a message will check that all values of a message and its sub-messages are valid. Assingning an invalid value to a field will raise a ValidationException. Example: # Trade type. class TradeType(Enum): BUY = 1 SELL = 2 SHORT = 3 CALL = 4 class Lot(Message): price = IntegerField(1, required=True) quantity = IntegerField(2, required=True) class Order(Message): symbol = StringProperty(1, required=True) total_quantity = IntegerProperty(2, required=True) trade_type = EnumProperty(TradeType, 3, required=True) lots = MessageProperty(Lot, 4, repeated=True) limit = IntegerField(5) order = Order(symbol='GOOG', total_quantity=10, trade_type=TradeType.BUY) lot1 = Lot(price=304, quantity=7) lot2 = Lot(price = 305, quantity=3) order.lots = [lot1, lot2] # Now object is initialized! order.check_initialized() """ __metaclass__ = _MessageClass def __init__(self, **kwargs): """Initialize internal messages state. Args: A message can be initialized via the constructor by passing in keyword arguments corresponding to fields. For example: class Date(Message): day = IntegerField(1) month = IntegerField(2) year = IntegerField(3) Invoking: date = Date(day=6, month=6, year=1911) is the same as doing: date = Date() date.day = 6 date.month = 6 date.year = 1911 """ # Tag being an essential implementation detail must be private. self.__tags = {} assigned = set() for name, value in kwargs.iteritems(): setattr(self, name, value) assigned.add(name) # initialize repeated fields. for field in self.all_fields(): if field.repeated and field.name not in assigned: setattr(self, field.name, []) def check_initialized(self): """Check class for initialization status. Check that all required fields are initialized Raises: ValidationError: If message is not initialized. """ for name, field in self.__by_name.iteritems(): value = getattr(self, name) if value is None: if field.required: raise ValidationError("Message %s is missing required field %s" % (type(self).__name__, name)) else: try: if isinstance(field, MessageField): if field.repeated: for item in value: item.check_initialized() else: value.check_initialized() except ValidationError, err: if not hasattr(err, 'message_name'): err.message_name = type(self).__name__ raise def is_initialized(self): """Get initialization status. Returns: True if message is valid, else False. """ try: self.check_initialized() except ValidationError: return False else: return True @classmethod def all_fields(cls): """Get all field definition objects. Ordering is arbitrary. Returns: Iterator over all values in arbitrary order. """ return cls.__by_name.itervalues() @classmethod def field_by_name(cls, name): """Get field by name. Returns: Field object associated with name. Raises: KeyError if no field found by that name. """ return cls.__by_name[name] @classmethod def field_by_number(cls, number): """Get field by number. Returns: Field object associated with number. Raises: KeyError if no field found by that number. """ return cls.__by_number[number] def get_assigned_value(self, name): """Get the assigned value of an attribute. Get the underlying value of an attribute. If value has not been set, will not return the default for the field. Args: name: Name of attribute to get. Returns: Value of attribute, None if it has not been set. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) return self.__tags.get(field.number) def reset(self, name): """Reset assigned value for field. Resetting a field will return it to its default value or None. Args: name: Name of field to reset. """ message_type = type(self) try: field = message_type.field_by_name(name) except KeyError: if name not in message_type.__by_name: raise AttributeError('Message %s has no field %s' % ( message_type.__name__, name)) self.__tags.pop(field.number, None) def __setattr__(self, name, value): """Change set behavior for messages. Messages may only be assigned values that are fields. Does not try to validate field when set. Args: name: Name of field to assign to. vlaue: Value to assign to field. Raises: AttributeError when trying to assign value that is not a field. """ if name in self.__by_name or name.startswith('_Message__'): object.__setattr__(self, name, value) else: raise AttributeError("May not assign arbitrary value %s " "to message %s" % (name, type(self).__name__)) def __repr__(self): """Make string representation of message. Example: class MyMessage(messages.Message): integer_value = messages.IntegerField(1) string_value = messages.StringField(2) my_message = MyMessage() my_message.integer_value = 42 my_message.string_value = u'A string' print my_message >>> <MyMessage ... integer_value: 42 ... string_value: u'A string'> Returns: String representation of message, including the values of all fields and repr of all sub-messages. """ body = ['<', type(self).__name__] for field in sorted(self.all_fields(), key=lambda f: f.number): attribute = field.name value = self.get_assigned_value(field.name) if value is not None: body.append('\n %s: %s' % (attribute, repr(value))) body.append('>') return ''.join(body) def __eq__(self, other): """Equality operator. Does field by field comparison with other message. For equality, must be same type and values of all fields must be equal. Messages not required to be initialized for comparison. Does not attempt to determine equality for values that have default values that are not set. In other words: class HasDefault(Message): attr1 = StringField(1, default='default value') message1 = HasDefault() message2 = HasDefault() message2.attr1 = 'default value' message1 != message2 Args: other: Other message to compare with. """ # TODO(rafek): Implement "equivalent" which does comparisons # taking default values in to consideration. if self is other: return True if type(self) is not type(other): return False return self.__tags == other.__tags def __ne__(self, other): """Not equals operator. Does field by field comparison with other message. For non-equality, must be different type or any value of a field must be non-equal to the same field in the other instance. Messages not required to be initialized for comparison. Args: other: Other message to compare with. """ return not self.__eq__(other) class FieldList(list): """List implementation that validates field values. This list implementation overrides all methods that add values in to a list in order to validate those new elements. Attempting to add or set list values that are not of the correct type will raise ValidationError. """ def __init__(self, field_instance, sequence): """Constructor. Args: field_instance: Instance of field that validates the list. sequence: List or tuple to construct list from. """ if not field_instance.repeated: raise FieldDefinitionError('FieldList may only accept repeated fields') self.__field = field_instance self.__field.validate(sequence) list.__init__(self, sequence) @property def field(self): """Field that validates list.""" return self.__field def __setslice__(self, i, j, sequence): """Validate slice assignment to list.""" self.__field.validate(sequence) list.__setslice__(self, i, j, sequence) def __setitem__(self, index, value): """Validate item assignment to list.""" self.__field.validate_element(value) list.__setitem__(self, index, value) def append(self, value): """Validate item appending to list.""" self.__field.validate_element(value) return list.append(self, value) def extend(self, sequence): """Validate extension of list.""" self.__field.validate(sequence) return list.extend(self, sequence) def insert(self, index, value): """Validate item insertion to list.""" self.__field.validate_element(value) return list.insert(self, index, value) # TODO(rafek): Prevent additional field subclasses. class Field(object): __variant_to_type = {} class __metaclass__(type): def __init__(cls, name, bases, dct): getattr(cls, '_Field__variant_to_type').update( (variant, cls) for variant in getattr(cls, 'VARIANTS', [])) type.__init__(cls, name, bases, dct) __initialized = False @util.positional(2) def __init__(self, number, required=False, repeated=False, variant=None, default=None): """Constructor. The required and repeated parameters are mutually exclusive. Setting both to True will raise a FieldDefinitionError. Sub-class Attributes: Each sub-class of Field must define the following: VARIANTS: Set of variant types accepted by that field. DEFAULT_VARIANT: Default variant type if not specified in constructor. Args: number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive with 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive with 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: InvalidVariantError when invalid variant for field is provided. InvalidDefaultError when invalid default for field is provided. FieldDefinitionError when invalid number provided or mutually exclusive fields are used. InvalidNumberError when the field number is out of range or reserved. """ if not isinstance(number, int) or not 1 <= number <= MAX_FIELD_NUMBER: raise InvalidNumberError('Invalid number for field: %s\n' 'Number must be 1 or greater and %d or less' % (number, MAX_FIELD_NUMBER)) if FIRST_RESERVED_FIELD_NUMBER <= number <= LAST_RESERVED_FIELD_NUMBER: raise InvalidNumberError('Tag number %d is a reserved number.\n' 'Numbers %d to %d are reserved' % (number, FIRST_RESERVED_FIELD_NUMBER, LAST_RESERVED_FIELD_NUMBER)) if repeated and required: raise FieldDefinitionError('Cannot set both repeated and required') if variant is None: variant = self.DEFAULT_VARIANT if repeated and default is not None: raise FieldDefinitionError('Repeated fields may not have defaults') if variant not in self.VARIANTS: raise InvalidVariantError( 'Invalid variant: %s\nValid variants for %s are %r' % (variant, type(self).__name__, sorted(self.VARIANTS))) self.number = number self.required = required self.repeated = repeated self.variant = variant if default is not None: try: self.validate_default(default) except ValidationError, err: raise InvalidDefaultError('Invalid default value for field: %s: %s' % (default, err)) self.__default = default self.__initialized = True def __setattr__(self, name, value): """Setter overidden to prevent assignment to fields after creation. Args: name: Name of attribute to set. value: Value to assign. """ # Special case post-init names. They need to be set after constructor. if name in _POST_INIT_FIELD_ATTRIBUTE_NAMES: object.__setattr__(self, name, value) return # All other attributes must be set before __initialized. if not self.__initialized: # Not initialized yet, allow assignment. object.__setattr__(self, name, value) else: raise AttributeError('Field objects are read-only') def __set__(self, message_instance, value): """Set value on message. Args: message_instance: Message instance to set value on. value: Value to set on message. """ # Reaches in to message instance directly to assign to private tags. if value is None: if self.repeated: raise ValidationError( 'May not assign None to repeated field %s' % self.name) else: message_instance._Message__tags.pop(self.number, None) else: if self.repeated: value = FieldList(self, value) else: self.validate(value) message_instance._Message__tags[self.number] = value def __get__(self, message_instance, message_class): if message_instance is None: return self result = message_instance._Message__tags.get(self.number) if result is None: return self.default else: return result def validate_element(self, value): """Validate single element of field. This is different from validate in that it is used on individual values of repeated fields. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ if not isinstance(value, self.type): if value is None: if self.required: raise ValidationError('Required field is missing') else: raise ValidationError('Expected type %s, found %s (type %s)' % (self.type, value, type(value))) def __validate(self, value, validate_element): """Internal validation function. Validate an internal value using a function to validate individual elements. Args: value: Value to validate. validate_element: Function to use to validate individual elements. Raises: ValidationError if value is not expected type. """ if not self.repeated: validate_element(value) else: # Must be a list or tuple, may not be a string. if isinstance(value, (list, tuple)): for element in value: if element is None: raise ValidationError('Repeated values may not be None') validate_element(element) elif value is not None: raise ValidationError('Field is repeated. Found: %s' % value) def validate(self, value): """Validate value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_element) def validate_default_element(self, value): """Validate value as assigned to field default field. Some fields may allow for delayed resolution of default types necessary in the case of circular definition references. In this case, the default value might be a place holder that is resolved when needed after all the message classes are defined. Args: value: Default value to validate. Raises: ValidationError if value is not expected type. """ self.validate_element(value) def validate_default(self, value): """Validate default value assigned to field. Args: value: Value to validate. Raises: ValidationError if value is not expected type. """ self.__validate(value, self.validate_default_element) def message_definition(self): """Get Message definition that contains this Field definition. Returns: Containing Message definition for Field. Will return None if for some reason Field is defined outside of a Message class. """ try: return self._message_definition() except AttributeError: return None @property def default(self): """Get default value for field.""" return self.__default @classmethod def lookup_field_type_by_variant(cls, variant): return cls.__variant_to_type[variant] class IntegerField(Field): """Field definition for integer values.""" VARIANTS = frozenset([Variant.INT32, Variant.INT64, Variant.UINT32, Variant.UINT64, Variant.SINT32, Variant.SINT64, ]) DEFAULT_VARIANT = Variant.INT64 type = (int, long) class FloatField(Field): """Field definition for float values.""" VARIANTS = frozenset([Variant.FLOAT, Variant.DOUBLE, ]) DEFAULT_VARIANT = Variant.DOUBLE type = float class BooleanField(Field): """Field definition for boolean values.""" VARIANTS = frozenset([Variant.BOOL]) DEFAULT_VARIANT = Variant.BOOL type = bool class BytesField(Field): """Field definition for byte string values.""" VARIANTS = frozenset([Variant.BYTES]) DEFAULT_VARIANT = Variant.BYTES type = str class StringField(Field): """Field definition for unicode string values.""" VARIANTS = frozenset([Variant.STRING]) DEFAULT_VARIANT = Variant.STRING type = unicode def validate_element(self, value): """Validate StringField allowing for str and unicode. Raises: ValidationError if a str value is not 7-bit ascii. """ # If value is str is it considered valid. Satisfies "required=True". if isinstance(value, str): try: unicode(value) except UnicodeDecodeError, err: validation_error = ValidationError( 'Encountered non-ASCII string %s: %s' % (value, err)) validation_error.field_name = self.name raise validation_error else: super(StringField, self).validate_element(value) class MessageField(Field): """Field definition for sub-message values. Message fields contain instance of other messages. Instances stored on messages stored on message fields are considered to be owned by the containing message instance and should not be shared between owning instances. Message fields must be defined to reference a single type of message. Normally message field are defined by passing the referenced message class in to the constructor. It is possible to define a message field for a type that does not yet exist by passing the name of the message in to the constructor instead of a message class. Resolution of the actual type of the message is deferred until it is needed, for example, during message verification. Names provided to the constructor must refer to a class within the same python module as the class that is using it. Names refer to messages relative to the containing messages scope. For example, the two fields of OuterMessage refer to the same message type: class Outer(Message): inner_relative = MessageField('Inner', 1) inner_absolute = MessageField('Outer.Inner', 2) class Inner(Message): ... When resolving an actual type, MessageField will traverse the entire scope of nested messages to match a message name. This makes it easy for siblings to reference siblings: class Outer(Message): class Inner(Message): sibling = MessageField('Sibling', 1) class Sibling(Message): ... """ VARIANTS = frozenset([Variant.MESSAGE]) DEFAULT_VARIANT = Variant.MESSAGE @util.positional(3) def __init__(self, message_type, number, required=False, repeated=False, variant=None): """Constructor. Args: message_type: Message type for field. Must be subclass of Message. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. Raises: FieldDefinitionError when invalid message_type is provided. """ valid_type = (isinstance(message_type, basestring) or (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message))) if not valid_type: raise FieldDefinitionError('Invalid message class: %s' % message_type) if isinstance(message_type, basestring): self.__type_name = message_type self.__type = None else: self.__type = message_type super(MessageField, self).__init__(number, required=required, repeated=repeated, variant=variant) @property def type(self): """Message type used for field.""" if self.__type is None: message_type = find_definition(self.__type_name, self.message_definition()) if not (message_type is not Message and isinstance(message_type, type) and issubclass(message_type, Message)): raise FieldDefinitionError('Invalid message class: %s' % message_type) self.__type = message_type return self.__type class EnumField(Field): """Field definition for enum values. Enum fields may have default values that are delayed until the associated enum type is resolved. This is necessary to support certain circular references. For example: class Message1(Message): class Color(Enum): RED = 1 GREEN = 2 BLUE = 3 # This field default value will be validated when default is accessed. animal = EnumField('Message2.Animal', 1, default='HORSE') class Message2(Message): class Animal(Enum): DOG = 1 CAT = 2 HORSE = 3 # This fields default value will be validated right away since Color is # already fully resolved. color = EnumField(Message1.Color, 1, default='RED') """ VARIANTS = frozenset([Variant.ENUM]) DEFAULT_VARIANT = Variant.ENUM def __init__(self, enum_type, number, **kwargs): """Constructor. Args: enum_type: Enum type for field. Must be subclass of Enum. number: Number of field. Must be unique per message class. required: Whether or not field is required. Mutually exclusive to 'repeated'. repeated: Whether or not field is repeated. Mutually exclusive to 'required'. variant: Wire-format variant hint. default: Default value for field if not found in stream. Raises: FieldDefinitionError when invalid enum_type is provided. """ valid_type = (isinstance(enum_type, basestring) or (enum_type is not Enum and isinstance(enum_type, type) and issubclass(enum_type, Enum))) if not valid_type: raise FieldDefinitionError('Invalid enum type: %s' % enum_type) if isinstance(enum_type, basestring): self.__type_name = enum_type self.__type = None else: self.__type = enum_type super(EnumField, self).__init__(number, **kwargs) def validate_default_element(self, value): """Validate default element of Enum field. Enum fields allow for delayed resolution of default values when the type of the field has not been resolved. The default value of a field may be a string or an integer. If the Enum type of the field has been resolved, the default value is validated against that type. Args: value: Value to validate. Raises: ValidationError if value is not expected message type. """ if isinstance(value, (basestring, int, long)): # Validation of the value does not happen for delayed resolution # enumerated types. Ignore if type is not yet resolved. if self.__type: self.__type(value) return super(EnumField, self).validate_default_element(value) @property def type(self): """Enum type used for field.""" if self.__type is None: found_type = find_definition(self.__type_name, self.message_definition()) if not (found_type is not Enum and isinstance(found_type, type) and issubclass(found_type, Enum)): raise FieldDefinitionError('Invalid enum type: %s' % found_type) self.__type = found_type return self.__type @property def default(self): """Default for enum field. Will cause resolution of Enum type and unresolved default value. """ try: return self.__resolved_default except AttributeError: resolved_default = super(EnumField, self).default if isinstance(resolved_default, (basestring, int, long)): resolved_default = self.type(resolved_default) self.__resolved_default = resolved_default return self.__resolved_default @util.positional(2) def find_definition(name, relative_to=None, importer=__import__): """Find definition by name in module-space. The find algorthm will look for definitions by name relative to a message definition or by fully qualfied name. If no definition is found relative to the relative_to parameter it will do the same search against the container of relative_to. If relative_to is a nested Message, it will search its message_definition(). If that message has no message_definition() it will search its module. If relative_to is a module, it will attempt to look for the containing module and search relative to it. If the module is a top-level module, it will look for the a message using a fully qualified name. If no message is found then, the search fails and DefinitionNotFoundError is raised. For example, when looking for any definition 'foo.bar.ADefinition' relative to an actual message definition abc.xyz.SomeMessage: find_definition('foo.bar.ADefinition', SomeMessage) It is like looking for the following fully qualified names: abc.xyz.SomeMessage. foo.bar.ADefinition abc.xyz. foo.bar.ADefinition abc. foo.bar.ADefinition foo.bar.ADefinition When resolving the name relative to Message definitions and modules, the algorithm searches any Messages or sub-modules found in its path. Non-Message values are not searched. A name that begins with '.' is considered to be a fully qualified name. The name is always searched for from the topmost package. For example, assume two message types: abc.xyz.SomeMessage xyz.SomeMessage Searching for '.xyz.SomeMessage' relative to 'abc' will resolve to 'xyz.SomeMessage' and not 'abc.xyz.SomeMessage'. For this kind of name, the relative_to parameter is effectively ignored and always set to None. For more information about package name resolution, please see: http://code.google.com/apis/protocolbuffers/docs/proto.html#packages Args: name: Name of definition to find. May be fully qualified or relative name. relative_to: Search for definition relative to message definition or module. None will cause a fully qualified name search. importer: Import function to use for resolving modules. Returns: Enum or Message class definition associated with name. Raises: DefinitionNotFoundError if no definition is found in any search path. """ # Check parameters. if not (relative_to is None or isinstance(relative_to, types.ModuleType) or isinstance(relative_to, type) and issubclass(relative_to, Message)): raise TypeError('relative_to must be None, Message definition or module. ' 'Found: %s' % relative_to) name_path = name.split('.') # Handle absolute path reference. if not name_path[0]: relative_to = None name_path = name_path[1:] def search_path(): """Performs a single iteration searching the path from relative_to. This is the function that searches up the path from a relative object. fully.qualified.object . relative.or.nested.Definition ----------------------------> ^ | this part of search --+ Returns: Message or Enum at the end of name_path, else None. """ next = relative_to for node in name_path: # Look for attribute first. attribute = getattr(next, node, None) if attribute is not None: next = attribute else: # If module, look for sub-module. if next is None or isinstance(next, types.ModuleType): if next is None: module_name = node else: module_name = '%s.%s' % (next.__name__, node) try: fromitem = module_name.split('.')[-1] next = importer(module_name, '', '', [str(fromitem)]) except ImportError: return None else: return None if (not isinstance(next, types.ModuleType) and not (isinstance(next, type) and issubclass(next, (Message, Enum)))): return None return next while True: found = search_path() if isinstance(found, type) and issubclass(found, (Enum, Message)): return found else: # Find next relative_to to search against. # # fully.qualified.object . relative.or.nested.Definition # <--------------------- # ^ # | # does this part of search if relative_to is None: # Fully qualified search was done. Nothing found. Fail. raise DefinitionNotFoundError('Could not find definition for %s' % (name,)) else: if isinstance(relative_to, types.ModuleType): # Find parent module. module_path = relative_to.__name__.split('.')[:-1] if not module_path: relative_to = None else: # Should not raise ImportError. If it does... weird and # unexepected. Propagate. relative_to = importer( '.'.join(module_path), '', '', [module_path[-1]]) elif (isinstance(relative_to, type) and issubclass(relative_to, Message)): parent = relative_to.message_definition() if parent is None: last_module_name = relative_to.__module__.split('.')[-1] relative_to = importer( relative_to.__module__, '', '', [last_module_name]) else: relative_to = parent
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.protourlencode.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import cgi import logging import unittest import urllib from protorpc import messages from protorpc import protourlencode from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = protourlencode class SuperMessage(messages.Message): """A test message with a nested message field.""" sub_message = messages.MessageField(test_util.OptionalMessage, 1) sub_messages = messages.MessageField(test_util.OptionalMessage, 2, repeated=True) class SuperSuperMessage(messages.Message): """A test message with two levels of nested.""" sub_message = messages.MessageField(SuperMessage, 1) sub_messages = messages.MessageField(SuperMessage, 2, repeated=True) class URLEncodedRequestBuilderTest(test_util.TestCase): """Test the URL Encoded request builder.""" def testMakePath(self): builder = protourlencode.URLEncodedRequestBuilder(SuperSuperMessage(), prefix='pre.') self.assertEquals(None, builder.make_path('')) self.assertEquals(None, builder.make_path('no_such_field')) self.assertEquals(None, builder.make_path('pre.no_such_field')) # Missing prefix. self.assertEquals(None, builder.make_path('sub_message')) # Valid parameters. self.assertEquals((('sub_message', None),), builder.make_path('pre.sub_message')) self.assertEquals((('sub_message', None), ('sub_messages', 1)), builder.make_path('pre.sub_message.sub_messages-1')) self.assertEquals( (('sub_message', None), ('sub_messages', 1), ('int64_value', None)), builder.make_path('pre.sub_message.sub_messages-1.int64_value')) # Missing index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_messages.integer_field')) # Has unexpected index. self.assertEquals( None, builder.make_path('pre.sub_message.sub_message-1.integer_field')) def testAddParameter_SimpleAttributes(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value', ['10'])) self.assertTrue(builder.add_parameter('pre.string_value', ['a string'])) self.assertTrue(builder.add_parameter('pre.enum_value', ['VAL1'])) self.assertEquals(10, message.int64_value) self.assertEquals('a string', message.string_value) self.assertEquals(test_util.OptionalMessage.SimpleEnum.VAL1, message.enum_value) def testAddParameter_InvalidAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') def assert_empty(): self.assertEquals(None, getattr(message, 'sub_message')) self.assertEquals([], getattr(message, 'sub_messages')) self.assertFalse(builder.add_parameter('pre.nothing', ['x'])) assert_empty() self.assertFalse(builder.add_parameter('pre.sub_messages', ['x'])) self.assertFalse(builder.add_parameter('pre.sub_messages-1.nothing', ['x'])) assert_empty() def testAddParameter_NestedAttributes(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Set an empty message fields. self.assertTrue(builder.add_parameter('pre.sub_message', [''])) self.assertTrue(isinstance(message.sub_message, SuperMessage)) # Add a basic attribute. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.int64_value', ['10'])) self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_message.string_value', ['hello'])) self.assertTrue(10, message.sub_message.sub_message.int64_value) self.assertTrue('hello', message.sub_message.sub_message.string_value) def testAddParameter_NestedMessages(self): message = SuperSuperMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') # Add a repeated empty message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-0', [''])) sub_message = message.sub_message.sub_messages[0] self.assertTrue(1, len(message.sub_message.sub_messages)) self.assertTrue(isinstance(sub_message, test_util.OptionalMessage)) self.assertEquals(None, getattr(sub_message, 'int64_value')) self.assertEquals(None, getattr(sub_message, 'string_value')) self.assertEquals(None, getattr(sub_message, 'enum_value')) # Add a repeated message with value. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.int64_value', ['10'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertTrue(10, message.sub_message.sub_messages[1].int64_value) # Add another value to the same nested message. self.assertTrue(builder.add_parameter( 'pre.sub_message.sub_messages-1.string_value', ['a string'])) self.assertTrue(2, len(message.sub_message.sub_messages)) self.assertEquals(10, message.sub_message.sub_messages[1].int64_value) self.assertEquals('a string', message.sub_message.sub_messages[1].string_value) def testAddParameter_RepeatedValues(self): message = test_util.RepeatedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.int64_value-0', ['20'])) self.assertTrue(builder.add_parameter('pre.int64_value-1', ['30'])) self.assertEquals([20, 30], message.int64_value) self.assertTrue(builder.add_parameter('pre.string_value-0', ['hi'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['lo'])) self.assertTrue(builder.add_parameter('pre.string_value-1', ['dups overwrite'])) self.assertEquals(['hi', 'dups overwrite'], message.string_value) def testAddParameter_InvalidValuesMayRepeat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('nothing', [1, 2, 3])) def testAddParameter_RepeatedParameters(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', [1, 2, 3]) self.assertRaises(messages.DecodeError, builder.add_parameter, 'pre.int64_value', []) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field.""" message = test_util.HasNestedMessage() builder = protourlencode.URLEncodedRequestBuilder(message, 'pre.') self.assertFalse(builder.add_parameter('pre.nested.a_value.whatever', ['1'])) def testInvalidFieldFormat(self): message = test_util.OptionalMessage() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertFalse(builder.add_parameter('pre.illegal%20', ['1'])) def testAddParameter_UnexpectedNestedValue(self): """Test getting a nested value on a non-message sub-field There is an odd corner case where if trying to insert a repeated value on an nested repeated message that would normally succeed in being created should fail. This case can only be tested when the first message of the nested messages already exists. Another case is trying to access an indexed value nested within a non-message field. """ class HasRepeated(messages.Message): values = messages.IntegerField(1, repeated=True) class HasNestedRepeated(messages.Message): nested = messages.MessageField(HasRepeated, 1, repeated=True) message = HasNestedRepeated() builder = protourlencode.URLEncodedRequestBuilder(message, prefix='pre.') self.assertTrue(builder.add_parameter('pre.nested-0.values-0', ['1'])) # Try to create an indexed value on a non-message field. self.assertFalse(builder.add_parameter('pre.nested-0.values-0.unknown-0', ['1'])) # Try to create an out of range indexed field on an otherwise valid # repeated message field. self.assertFalse(builder.add_parameter('pre.nested-1.values-1', ['1'])) class ProtourlencodeConformanceTest(test_util.TestCase, test_util.ProtoConformanceTestBase): PROTOLIB = protourlencode encoded_partial = urllib.urlencode([('double_value', 1.23), ('int64_value', -100000000000), ('int32_value', 1020), ('string_value', u'a string'), ('enum_value', 'VAL2'), ]) encoded_full = urllib.urlencode([('double_value', 1.23), ('float_value', -2.5), ('int64_value', -100000000000), ('uint64_value', 102020202020), ('int32_value', 1020), ('bool_value', 'true'), ('string_value', u'a string\u044f'.encode('utf-8')), ('bytes_value', 'a bytes\xff\xfe'), ('enum_value', 'VAL2'), ]) encoded_repeated = urllib.urlencode([('double_value-0', 1.23), ('double_value-1', 2.3), ('float_value-0', -2.5), ('float_value-1', 0.5), ('int64_value-0', -100000000000), ('int64_value-1', 20), ('uint64_value-0', 102020202020), ('uint64_value-1', 10), ('int32_value-0', 1020), ('int32_value-1', 718), ('bool_value-0', 'true'), ('bool_value-1', 'false'), ('string_value-0', u'a string\u044f'.encode('utf-8')), ('string_value-1', u'another string'.encode('utf-8')), ('bytes_value-0', 'a bytes\xff\xfe'), ('bytes_value-1', 'another bytes'), ('enum_value-0', 'VAL2'), ('enum_value-1', 'VAL1'), ]) encoded_nested = urllib.urlencode([('nested.a_value', 'a string'), ]) encoded_repeated_nested = urllib.urlencode( [('repeated_nested-0.a_value', 'a string'), ('repeated_nested-1.a_value', 'another string'), ]) unexpected_tag_message = 'unexpected=whatever' encoded_default_assigned = urllib.urlencode([('a_value', 'a default'), ]) encoded_nested_empty = urllib.urlencode([('nested', '')]) encoded_repeated_nested_empty = urllib.urlencode([('repeated_nested-0', ''), ('repeated_nested-1', '')]) encoded_extend_message = urllib.urlencode([('int64_value-0', 400), ('int64_value-1', 50), ('int64_value-2', 6000)]) encoded_string_types = urllib.urlencode( [('string_value', 'Latin')]) def testParameterPrefix(self): """Test using the 'prefix' parameter to encode_message.""" class MyMessage(messages.Message): number = messages.IntegerField(1) names = messages.StringField(2, repeated=True) message = MyMessage() message.number = 10 message.names = [u'Fred', u'Lisa'] encoded_message = protourlencode.encode_message(message, prefix='prefix-') self.assertEquals({'prefix-number': ['10'], 'prefix-names-0': ['Fred'], 'prefix-names-1': ['Lisa'], }, cgi.parse_qs(encoded_message)) self.assertEquals(message, protourlencode.decode_message(MyMessage, encoded_message, prefix='prefix-')) if __name__ == '__main__': unittest.main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Service regsitry for service discovery. The registry service can be deployed on a server in order to provide a central place where remote clients can discover available. On the server side, each service is registered by their name which is unique to the registry. Typically this name provides enough information to identify the service and locate it within a server. For example, for an HTTP based registry the name is the URL path on the host where the service is invocable. The registry is also able to resolve the full descriptor.FileSet necessary to describe the service and all required data-types (messages and enums). A configured registry is itself a remote service and should reference itself. """ import sys import weakref from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import util __all__ = [ 'ServiceMapping', 'ServicesResponse', 'GetFileSetRequest', 'GetFileSetResponse', 'RegistryService', ] class ServiceMapping(messages.Message): """Description of registered service. Fields: name: Name of service. On HTTP based services this will be the URL path used for invocation. definition: Fully qualified name of the service definition. Useful for clients that can look up service definitions based on an existing repository of definitions. """ name = messages.StringField(1, required=True) definition = messages.StringField(2, required=True) class ServicesResponse(messages.Message): """Response containing all registered services. May also contain complete descriptor file-set for all services known by the registry. Fields: services: Service mappings for all registered services in registry. file_set: Descriptor file-set describing all services, messages and enum types needed for use with all requested services if asked for in the request. """ services = messages.MessageField(ServiceMapping, 1, repeated=True) class GetFileSetRequest(messages.Message): """Request for service descriptor file-set. Request to retrieve file sets for specific services. Fields: names: Names of services to retrieve file-set for. """ names = messages.StringField(1, repeated=True) class GetFileSetResponse(messages.Message): """Descriptor file-set for all names in GetFileSetRequest. Fields: file_set: Descriptor file-set containing all descriptors for services, messages and enum types needed for listed names in request. """ file_set = messages.MessageField(descriptor.FileSet, 1, required=True) class RegistryService(remote.Service): """Registry service. Maps names to services and is able to describe all descriptor file-sets necessary to use contined services. On an HTTP based server, the name is the URL path to the service. """ @util.positional(2) def __init__(self, registry, modules=None): """Constructor. Args: registry: Map of name to service class. This map is not copied and may be modified after the reigstry service has been configured. modules: Module dict to draw descriptors from. Defaults to sys.modules. """ # Private Attributes: # __registry: Map of name to service class. Refers to same instance as # registry parameter. # __modules: Mapping of module name to module. # __definition_to_modules: Mapping of definition types to set of modules # that they refer to. This cache is used to make repeated look-ups # faster and to prevent circular references from causing endless loops. self.__registry = registry if modules is None: modules = sys.modules self.__modules = modules # This cache will only last for a single request. self.__definition_to_modules = {} def __find_modules_for_message(self, message_type): """Find modules referred to by a message type. Determines the entire list of modules ultimately referred to by message_type by iterating over all of its message and enum fields. Includes modules referred to fields within its referred messages. Args: message_type: Message type to find all referring modules for. Returns: Set of modules referred to by message_types by traversing all its message and enum fields. """ # TODO(rafek): Maybe this should be a method on Message and Service? def get_dependencies(message_type, seen=None): """Get all dependency definitions of a message type. This function works by collecting the types of all enumeration and message fields defined within the message type. When encountering a message field, it will recursivly find all of the associated message's dependencies. It will terminate on circular dependencies by keeping track of what definitions it already via the seen set. Args: message_type: Message type to get dependencies for. seen: Set of definitions that have already been visited. Returns: All dependency message and enumerated types associated with this message including the message itself. """ if seen is None: seen = set() seen.add(message_type) for field in message_type.all_fields(): if isinstance(field, messages.MessageField): if field.type not in seen: get_dependencies(field.type, seen) elif isinstance(field, messages.EnumField): seen.add(field.type) return seen found_modules = self.__definition_to_modules.setdefault(message_type, set()) if not found_modules: dependencies = get_dependencies(message_type) found_modules.update(self.__modules[definition.__module__] for definition in dependencies) return found_modules def __describe_file_set(self, names): """Get file-set for named services. Args: names: List of names to get file-set for. Returns: descriptor.FileSet containing all the descriptors for all modules ultimately referred to by all service types request by names parameter. """ service_modules = set() if names: for service in (self.__registry[name] for name in names): found_modules = self.__definition_to_modules.setdefault(service, set()) if not found_modules: found_modules.add(self.__modules[service.__module__]) for method_name in service.all_remote_methods(): method = getattr(service, method_name) for message_type in (method.remote.request_type, method.remote.response_type): found_modules.update( self.__find_modules_for_message(message_type)) service_modules.update(found_modules) return descriptor.describe_file_set(service_modules) @property def registry(self): """Get service registry associated with this service instance.""" return self.__registry @remote.method(message_types.VoidMessage, ServicesResponse) def services(self, request): """Get all registered services.""" response = ServicesResponse() response.services = [] for name, service_class in self.__registry.iteritems(): mapping = ServiceMapping() mapping.name = name.decode('utf-8') mapping.definition = service_class.definition_name().decode('utf-8') response.services.append(mapping) return response @remote.method(GetFileSetRequest, GetFileSetResponse) def get_file_set(self, request): """Get file-set for registered servies.""" response = GetFileSetResponse() response.file_set = self.__describe_file_set(request.names) return response
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from __future__ import with_statement __author__ = 'rafek@google.com (Rafe Kaplan)' from protorpc import descriptor from protorpc import generate from protorpc import messages from protorpc import util __all__ = ['format_python_file'] def _write_enums(enum_descriptors, out): """Write nested and non-nested Enum types. Args: enum_descriptors: List of EnumDescriptor objects from which to generate enums. out: Indent writer used for generating text. """ # Write enums. for enum in enum_descriptors or []: out << '' out << '' out << 'class %s(messages.Enum):' % enum.name out << '' with out.indent(): if not enum.values: out << 'pass' else: for enum_value in enum.values: out << '%s = %s' % (enum_value.name, enum_value.number) def _write_fields(field_descriptors, out): """Write fields for Message types. Args: field_descriptors: List of FieldDescriptor objects from which to generate fields. out: Indent writer used for generating text. """ out << '' for field in field_descriptors or []: field_type = messages.Field.lookup_field_type_by_variant(field.variant) type_format = '' label_format = '' if issubclass(field_type, (messages.EnumField, messages.MessageField)): type_format = '\'%s\', ' % field.type_name if field.label == descriptor.FieldDescriptor.Label.REQUIRED: label_format = ', required=True' elif field.label == descriptor.FieldDescriptor.Label.REPEATED: label_format = ', repeated=True' if field_type.DEFAULT_VARIANT != field.variant: variant_format = ', variant=messages.Variant.%s' % field.variant else: variant_format = '' if field.default_value: if field_type in [messages.BytesField, messages.StringField, ]: default_value = repr(field.default_value) elif field_type is messages.EnumField: try: default_value = str(int(field.default_value)) except ValueError: default_value = repr(field.default_value) else: default_value = field.default_value default_format = ', default=%s' % (default_value,) else: default_format = '' out << '%s = messages.%s(%s%s%s%s%s)' % (field.name, field_type.__name__, type_format, field.number, label_format, variant_format, default_format) def _write_messages(message_descriptors, out): """Write nested and non-nested Message types. Args: message_descriptors: List of MessageDescriptor objects from which to generate messages. out: Indent writer used for generating text. """ for message in message_descriptors or []: out << '' out << '' out << 'class %s(messages.Message):' % message.name with out.indent(): if not (message.enum_types or message.message_types or message.fields): out << '' out << 'pass' else: _write_enums(message.enum_types, out) _write_messages(message.message_types, out) _write_fields(message.fields, out) def _write_methods(method_descriptors, out): """Write methods of Service types. All service method implementations raise NotImplementedError. Args: method_descriptors: List of MethodDescriptor objects from which to generate methods. out: Indent writer used for generating text. """ for method in method_descriptors: out << '' out << "@remote.method('%s', '%s')" % (method.request_type, method.response_type) out << 'def %s(self, request):' % (method.name,) with out.indent(): out << ('raise NotImplementedError' "('Method %s is not implemented')" % (method.name)) def _write_services(service_descriptors, out): """Write Service types. Args: service_descriptors: List of ServiceDescriptor instances from which to generate services. out: Indent writer used for generating text. """ for service in service_descriptors or []: out << '' out << '' out << 'class %s(remote.Service):' % service.name with out.indent(): if service.methods: _write_methods(service.methods, out) else: out << '' out << 'pass' @util.positional(2) def format_python_file(file_descriptor, output, indent_space=2): """Format FileDescriptor object as a single Python module. Services generated by this function will raise NotImplementedError. All Python classes generated by this function use delayed binding for all message fields, enum fields and method parameter types. For example a service method might be generated like so: class MyService(remote.Service): @remote.method('my_package.MyRequestType', 'my_package.MyResponseType') def my_method(self, request): raise NotImplementedError('Method my_method is not implemented') Args: file_descriptor: FileDescriptor instance to format as python module. output: File-like object to write module source code to. indent_space: Number of spaces for each level of Python indentation. """ out = generate.IndentWriter(output, indent_space=indent_space) out << 'from protorpc import messages' if file_descriptor.service_types: out << 'from protorpc import remote' if file_descriptor.package: out << "package = '%s'" % file_descriptor.package _write_enums(file_descriptor.enum_types, out) _write_messages(file_descriptor.message_types, out) _write_services(file_descriptor.service_types, out)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.message.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import registry from protorpc import remote from protorpc import test_util class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = registry class MyService1(remote.Service): """Test service that refers to messages in another module.""" @remote.method(test_util.NestedMessage, test_util.NestedMessage) def a_method(self, request): pass class MyService2(remote.Service): """Test service that does not refer to messages in another module.""" class RegistryServiceTest(test_util.TestCase): def setUp(self): self.registry = { 'my-service1': MyService1, 'my-service2': MyService2, } self.modules = { __name__: sys.modules[__name__], test_util.__name__: test_util, } self.registry_service = registry.RegistryService(self.registry, modules=self.modules) def CheckServiceMappings(self, mappings): service1_mapping = registry.ServiceMapping() service1_mapping.name = 'my-service1' service1_mapping.definition = '__main__.MyService1' service2_mapping = registry.ServiceMapping() service2_mapping.name = 'my-service2' service2_mapping.definition = '__main__.MyService2' mappings.sort(key=lambda mapping: mapping.name) self.assertEquals(mappings[0], service1_mapping) self.assertEquals(mappings[1], service2_mapping) def testServices(self): response = self.registry_service.services(message_types.VoidMessage()) self.CheckServiceMappings(response.services) def testGetFileSet_All(self): request = registry.GetFileSetRequest() request.names = ['my-service1', 'my-service2'] response = self.registry_service.get_file_set(request) expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_None(self): request = registry.GetFileSetRequest() response = self.registry_service.get_file_set(request) self.assertEquals(descriptor.FileSet(), response.file_set) def testGetFileSet_ReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service1'] response = self.registry_service.get_file_set(request) # Will suck in and describe the test_util module. expected_file_set = descriptor.describe_file_set(self.modules.values()) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def testGetFileSet_DoNotReferenceOtherModules(self): request = registry.GetFileSetRequest() request.names = ['my-service2'] response = self.registry_service.get_file_set(request) # Service does not reference test_util, so will only describe this module. expected_file_set = descriptor.describe_file_set([self.modules[__name__]]) expected_file_set.files.sort(key=lambda f: f.package) response.file_set.files.sort(key=lambda f: f.package) self.assertEquals(expected_file_set.files, response.file_set.files) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.remote.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import sys import types import unittest from protorpc import descriptor from protorpc import message_types from protorpc import messages from protorpc import remote from protorpc import test_util from protorpc import transport import mox class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = remote class Request(messages.Message): """Test request message.""" value = messages.StringField(1) class Response(messages.Message): """Test response message.""" value = messages.StringField(1) class MyService(remote.Service): @remote.method(Request, Response) def remote_method(self, request): response = Response() response.value = request.value return response class SimpleRequest(messages.Message): """Simple request message type used for tests.""" param1 = messages.StringField(1) param2 = messages.StringField(2) class SimpleResponse(messages.Message): """Simple response message type used for tests.""" class BasicService(remote.Service): """A basic service with decorated remote method.""" def __init__(self): self.request_ids = [] @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, request): self.request_ids.append(id(request)) return SimpleResponse() class RpcErrorTest(test_util.TestCase): def testFromStatus(self): for state in remote.RpcState: exception = remote.RpcError.from_state self.assertEquals(remote.ServerError, remote.RpcError.from_state('SERVER_ERROR')) class ApplicationErrorTest(test_util.TestCase): def testErrorCode(self): self.assertEquals('blam', remote.ApplicationError('an error', 'blam').error_name) def testStr(self): self.assertEquals('an error', str(remote.ApplicationError('an error', 1))) def testRepr(self): self.assertEquals("ApplicationError('an error', 1)", repr(remote.ApplicationError('an error', 1))) self.assertEquals("ApplicationError('an error')", repr(remote.ApplicationError('an error'))) class RemoteTest(test_util.TestCase): """Test remote method decorator.""" def testRemote(self): """Test use of remote decorator.""" self.assertEquals(SimpleRequest, BasicService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, BasicService.remote_method.remote.response_type) self.assertTrue(isinstance(BasicService.remote_method.remote.method, types.FunctionType)) def testRemoteMessageResolution(self): """Test use of remote decorator to resolve message types by name.""" class OtherService(remote.Service): @remote.method('SimpleRequest', 'SimpleResponse') def remote_method(self, request): pass self.assertEquals(SimpleRequest, OtherService.remote_method.remote.request_type) self.assertEquals(SimpleResponse, OtherService.remote_method.remote.response_type) def testRemoteMessageResolution_NotFound(self): """Test failure to find message types.""" class OtherService(remote.Service): @remote.method('NoSuchRequest', 'NoSuchResponse') def remote_method(self, request): pass self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchRequest', getattr, OtherService.remote_method.remote, 'request_type') self.assertRaisesWithRegexpMatch( messages.DefinitionNotFoundError, 'Could not find definition for NoSuchResponse', getattr, OtherService.remote_method.remote, 'response_type') def testInvocation(self): """Test that invocation passes request through properly.""" service = BasicService() request = SimpleRequest() self.assertEquals(SimpleResponse(), service.remote_method(request)) self.assertEquals([id(request)], service.request_ids) def testInvocation_WrongRequestType(self): """Wrong request type passed to remote method.""" service = BasicService() self.assertRaises(remote.RequestError, service.remote_method, 'wrong') self.assertRaises(remote.RequestError, service.remote_method, None) self.assertRaises(remote.RequestError, service.remote_method, SimpleResponse()) def testInvocation_WrongResponseType(self): """Wrong response type returned from remote method.""" class AnotherService(object): @remote.method(SimpleRequest, SimpleResponse) def remote_method(self, unused_request): return self.return_this service = AnotherService() service.return_this = 'wrong' self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = None self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) service.return_this = SimpleRequest() self.assertRaises(remote.ServerError, service.remote_method, SimpleRequest()) def testBadRequestType(self): """Test bad request types used in remote definition.""" for request_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(request_type, SimpleResponse) def remote_method(self, request): pass self.assertRaises(TypeError, declare) def testBadResponseType(self): """Test bad response types used in remote definition.""" for response_type in (None, 1020, messages.Message, str): def declare(): class BadService(object): @remote.method(SimpleRequest, response_type) def remote_method(self, request): pass self.assertRaises(TypeError, declare) class GetRemoteMethodTest(test_util.TestCase): """Test for is_remote_method.""" def testGetRemoteMethod(self): """Test valid remote method detection.""" class Service(object): @remote.method(Request, Response) def remote_method(self, request): pass self.assertEquals(Service.remote_method.remote, remote.get_remote_method_info(Service.remote_method)) self.assertTrue(Service.remote_method.remote, remote.get_remote_method_info(Service().remote_method)) def testGetNotRemoteMethod(self): """Test positive result on a remote method.""" class NotService(object): def not_remote_method(self, request): pass def fn(self): pass class NotReallyRemote(object): """Test negative result on many bad values for remote methods.""" def not_really(self, request): pass not_really.remote = 'something else' for not_remote in [NotService.not_remote_method, NotService().not_remote_method, NotReallyRemote.not_really, NotReallyRemote().not_really, None, 1, 'a string', fn]: self.assertEquals(None, remote.get_remote_method_info(not_remote)) class RequestStateTest(test_util.TestCase): """Test request state.""" def testConstructor(self): """Test constructor.""" state = remote.RequestState(remote_host='remote-host', remote_address='remote-address', server_host='server-host', server_port=10) self.assertEquals('remote-host', state.remote_host) self.assertEquals('remote-address', state.remote_address) self.assertEquals('server-host', state.server_host) self.assertEquals(10, state.server_port) state = remote.RequestState() self.assertEquals(None, state.remote_host) self.assertEquals(None, state.remote_address) self.assertEquals(None, state.server_host) self.assertEquals(None, state.server_port) def testConstructorError(self): """Test unexpected keyword argument.""" self.assertRaises(TypeError, remote.RequestState, x=10) def testRepr(self): """Test string representation.""" self.assertEquals('<remote.RequestState>', repr(remote.RequestState())) self.assertEquals('<remote.RequestState remote_host=abc>', repr(remote.RequestState(remote_host='abc'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def>', repr(remote.RequestState(remote_host='abc', remote_address='def'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi'))) self.assertEquals('<remote.RequestState remote_host=abc ' 'remote_address=def ' 'server_host=ghi ' 'server_port=102>', repr(remote.RequestState(remote_host='abc', remote_address='def', server_host='ghi', server_port=102))) class ServiceTest(test_util.TestCase): """Test Service class.""" def testServiceBase_AllRemoteMethods(self): """Test that service base class has no remote methods.""" self.assertEquals({}, remote.Service.all_remote_methods()) def testAllRemoteMethods(self): """Test all_remote_methods with properly Service subclass.""" self.assertEquals({'remote_method': MyService.remote_method}, MyService.all_remote_methods()) def testAllRemoteMethods_SubClass(self): """Test all_remote_methods on a sub-class of a service.""" class SubClass(MyService): @remote.method(Request, Response) def sub_class_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, 'sub_class_method': SubClass.sub_class_method, }, SubClass.all_remote_methods()) def testOverrideMethod(self): """Test that trying to override a remote method with remote decorator.""" class SubClass(MyService): def remote_method(self, request): pass self.assertEquals({'remote_method': SubClass.remote_method, }, SubClass.all_remote_methods()) def testOverrideMethodWithRemote(self): """Test trying to override a remote method with remote decorator.""" def do_override(): class SubClass(MyService): @remote.method(Request, Response) def remote_method(self, request): pass self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Do not use remote decorator when ' 'overloading remote method remote_method ' 'on service SubClass', do_override) def testOverrideMethodWithInvalidValue(self): """Test trying to override a remote method with remote decorator.""" def do_override(bad_value): class SubClass(MyService): remote_method = bad_value for bad_value in [None, 1, 'string', {}]: self.assertRaisesWithRegexpMatch(remote.ServiceDefinitionError, 'Must override remote_method in ' 'SubClass with a method', do_override, bad_value) def testCallingRemoteMethod(self): """Test invoking a remote method.""" expected = Response() expected.value = 'what was passed in' request = Request() request.value = 'what was passed in' service = MyService() self.assertEquals(expected, service.remote_method(request)) def testFactory(self): """Test using factory to pass in state.""" class StatefulService(remote.Service): def __init__(self, a, b, c=None): self.a = a self.b = b self.c = c state = [1, 2, 3] factory = StatefulService.new_factory(1, state) self.assertEquals('Creates new instances of service StatefulService.\n\n' 'Returns:\n' ' New instance of __main__.StatefulService.', factory.func_doc) self.assertEquals('StatefulService_service_factory', factory.func_name) self.assertEquals(StatefulService, factory.service_class) service = factory() self.assertEquals(1, service.a) self.assertEquals(id(state), id(service.b)) self.assertEquals(None, service.c) factory = StatefulService.new_factory(2, b=3, c=4) service = factory() self.assertEquals(2, service.a) self.assertEquals(3, service.b) self.assertEquals(4, service.c) def testFactoryError(self): """Test misusing a factory.""" # Passing positional argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(1)) # Passing keyword argument that is not accepted by class. self.assertRaises(TypeError, remote.Service.new_factory(x=1)) class StatefulService(remote.Service): def __init__(self, a): pass # Missing required parameter. self.assertRaises(TypeError, StatefulService.new_factory()) def testDefinitionName(self): """Test getting service definition name.""" class TheService(remote.Service): pass self.assertEquals('__main__.TheService', TheService.definition_name()) def testDefinitionNameWithPackage(self): """Test getting service definition name when package defined.""" global package package = 'my.package' try: class TheService(remote.Service): pass self.assertEquals('my.package.TheService', TheService.definition_name()) finally: del package def testDefinitionNameWithNoModule(self): """Test getting service definition name when package defined.""" module = sys.modules[__name__] try: del sys.modules[__name__] class TheService(remote.Service): pass self.assertEquals('TheService', TheService.definition_name()) finally: sys.modules[__name__] = module class StubTest(test_util.TestCase): def setUp(self): self.mox = mox.Mox() self.transport = self.mox.CreateMockAnything() def testDefinitionName(self): self.assertEquals(BasicService.definition_name(), BasicService.Stub.definition_name()) def testRemoteMethods(self): self.assertEquals(BasicService.all_remote_methods(), BasicService.Stub.all_remote_methods()) def testSync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(request)) self.mox.VerifyAll() def testSync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) rpc.set_response(response) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(SimpleResponse(), stub.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequest(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(request)) self.mox.VerifyAll() def testAsync_WithKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() rpc = transport.Rpc(request) self.transport.send_rpc(BasicService.remote_method.remote, request).AndReturn(rpc) self.mox.ReplayAll() self.assertEquals(rpc, stub.async.remote_method(param1='val1', param2='val2')) self.mox.VerifyAll() def testAsync_WithRequestAndKwargs(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'May not provide both args and kwargs', stub.async.remote_method, request, param1='val1', param2='val2') self.mox.VerifyAll() def testAsync_WithTooManyPositionals(self): stub = BasicService.Stub(self.transport) request = SimpleRequest() request.param1 = 'val1' request.param2 = 'val2' response = SimpleResponse() self.mox.ReplayAll() self.assertRaisesWithRegexpMatch( TypeError, r'remote_method\(\) takes at most 2 positional arguments \(3 given\)', stub.async.remote_method, request, 'another value') self.mox.VerifyAll() class IsErrorStatusTest(test_util.TestCase): def testIsError(self): for state in (s for s in remote.RpcState if s > remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertTrue(remote.is_error_status(status)) def testIsNotError(self): for state in (s for s in remote.RpcState if s <= remote.RpcState.RUNNING): status = remote.RpcStatus(state=state) self.assertFalse(remote.is_error_status(status)) def testStateNone(self): self.assertRaises(messages.ValidationError, remote.is_error_status, remote.RpcStatus()) class CheckRpcStatusTest(test_util.TestCase): def testStateNone(self): self.assertRaises(messages.ValidationError, remote.check_rpc_status, remote.RpcStatus()) def testNoError(self): for state in (remote.RpcState.OK, remote.RpcState.RUNNING): remote.check_rpc_status(remote.RpcStatus(state=state)) def testErrorState(self): status = remote.RpcStatus(state=remote.RpcState.REQUEST_ERROR, error_message='a request error') self.assertRaisesWithRegexpMatch(remote.RequestError, 'a request error', remote.check_rpc_status, status) def testApplicationErrorState(self): status = remote.RpcStatus(state=remote.RpcState.APPLICATION_ERROR, error_message='an application error', error_name='blam') try: remote.check_rpc_status(status) self.fail('Should have raised application error.') except remote.ApplicationError, err: self.assertEquals('an application error', str(err)) self.assertEquals('blam', err.error_name) def main(): unittest.main() if __name__ == '__main__': main()
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Stub library.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import new import sys import urllib2 from protorpc import descriptor from protorpc import messages from protorpc import protobuf from protorpc import remote from protorpc import util __all__ = [ 'define_enum', 'define_field', 'define_file', 'define_message', 'define_service', 'import_file', 'import_file_set', ] # Map variant back to message field classes. def _build_variant_map(): """Map variants to fields. Returns: Dictionary mapping field variant to its associated field type. """ result = {} for name in dir(messages): value = getattr(messages, name) if isinstance(value, type) and issubclass(value, messages.Field): for variant in getattr(value, 'VARIANTS', []): result[variant] = value return result _VARIANT_MAP = _build_variant_map() def _get_or_define_module(full_name, modules): """Helper method for defining new modules. Args: full_name: Fully qualified name of module to create or return. modules: Dictionary of all modules. Defaults to sys.modules. Returns: Named module if found in 'modules', else creates new module and inserts in 'modules'. Will also construct parent modules if necessary. """ module = modules.get(full_name) if not module: module = new.module(full_name) modules[full_name] = module split_name = full_name.rsplit('.', 1) if len(split_name) > 1: parent_module_name, sub_module_name = split_name parent_module = _get_or_define_module(parent_module_name, modules) setattr(parent_module, sub_module_name, module) return module def define_enum(enum_descriptor, module_name): """Define Enum class from descriptor. Args: enum_descriptor: EnumDescriptor to build Enum class from. module_name: Module name to give new descriptor class. Returns: New messages.Enum sub-class as described by enum_descriptor. """ enum_values = enum_descriptor.values or [] class_dict = dict((value.name, value.number) for value in enum_values) class_dict['__module__'] = module_name return type(str(enum_descriptor.name), (messages.Enum,), class_dict) def define_field(field_descriptor): """Define Field instance from descriptor. Args: field_descriptor: FieldDescriptor class to build field instance from. Returns: New field instance as described by enum_descriptor. """ field_class = _VARIANT_MAP[field_descriptor.variant] params = {'number': field_descriptor.number, 'variant': field_descriptor.variant, } if field_descriptor.label == descriptor.FieldDescriptor.Label.REQUIRED: params['required'] = True elif field_descriptor.label == descriptor.FieldDescriptor.Label.REPEATED: params['repeated'] = True if field_class in (messages.EnumField, messages.MessageField): return field_class(field_descriptor.type_name, **params) else: return field_class(**params) def define_message(message_descriptor, module_name): """Define Message class from descriptor. Args: message_descriptor: MessageDescriptor to describe message class from. module_name: Module name to give to new descriptor class. Returns: New messages.Message sub-class as described by message_descriptor. """ class_dict = {'__module__': module_name} for enum in message_descriptor.enum_types or []: enum_instance = define_enum(enum, module_name) class_dict[enum.name] = enum_instance # TODO(rafek): support nested messages when supported by descriptor. for field in message_descriptor.fields or []: field_instance = define_field(field) class_dict[field.name] = field_instance class_name = message_descriptor.name.encode('utf-8') return type(class_name, (messages.Message,), class_dict) def define_service(service_descriptor, module): """Define a new service proxy. Args: service_descriptor: ServiceDescriptor class that describes the service. module: Module to add service to. Request and response types are found relative to this module. Returns: Service class proxy capable of communicating with a remote server. """ class_dict = {'__module__': module.__name__} class_name = service_descriptor.name.encode('utf-8') for method_descriptor in service_descriptor.methods or []: request_definition = messages.find_definition( method_descriptor.request_type, module) response_definition = messages.find_definition( method_descriptor.response_type, module) method_name = method_descriptor.name.encode('utf-8') def remote_method(self, request): """Actual service method.""" raise NotImplementedError('Method is not implemented') remote_method.__name__ = method_name remote_method_decorator = remote.method(request_definition, response_definition) class_dict[method_name] = remote_method_decorator(remote_method) service_class = type(class_name, (remote.Service,), class_dict) return service_class def define_file(file_descriptor, module=None): """Define module from FileDescriptor. Args: file_descriptor: FileDescriptor instance to describe module from. module: Module to add contained objects to. Module name overrides value in file_descriptor.package. Definitions are added to existing module if provided. Returns: If no module provided, will create a new module with its name set to the file descriptor's package. If a module is provided, returns the same module. """ if module is None: module = new.module(file_descriptor.package) for enum_descriptor in file_descriptor.enum_types or []: enum_class = define_enum(enum_descriptor, module.__name__) setattr(module, enum_descriptor.name, enum_class) for message_descriptor in file_descriptor.message_types or []: message_class = define_message(message_descriptor, module.__name__) setattr(module, message_descriptor.name, message_class) for service_descriptor in file_descriptor.service_types or []: service_class = define_service(service_descriptor, module) setattr(module, service_descriptor.name, service_class) return module @util.positional(1) def import_file(file_descriptor, modules=None): """Import FileDescriptor in to module space. This is like define_file except that a new module and any required parent modules are created and added to the modules parameter or sys.modules if not provided. Args: file_descriptor: FileDescriptor instance to describe module from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. Returns: Module found in modules, else a new module. """ if not file_descriptor.package: raise ValueError('File descriptor must have package name') if modules is None: modules = sys.modules module = _get_or_define_module(file_descriptor.package.encode('utf-8'), modules) return define_file(file_descriptor, module) @util.positional(1) def import_file_set(file_set, modules=None, _open=open): """Import FileSet in to module space. Args: file_set: If string, open file and read serialized FileSet. Otherwise, a FileSet instance to import definitions from. modules: Dictionary of modules to update. Modules and their parents that do not exist will be created. If an existing module is found that matches file_descriptor.package, that module is updated with the FileDescriptor contents. _open: Used for dependency injection during tests. """ if isinstance(file_set, basestring): encoded_file = _open(file_set, 'rb') try: encoded_file_set = encoded_file.read() finally: encoded_file.close() file_set = protobuf.decode_message(descriptor.FileSet, encoded_file_set) for file_descriptor in file_set.files: # Do not reload built in protorpc classes. if not file_descriptor.package.startswith('protorpc.'): import_file(file_descriptor, modules=modules)
Python
#!/usr/bin/env python # # Copyright 2010 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Tests for protorpc.dynamic.""" __author__ = 'rafek@google.com (Rafe Kaplan)' import unittest from protorpc import dynamic from protorpc import test_util from google.protobuf import descriptor class ModuleInterfaceTest(test_util.ModuleInterfaceTest, test_util.TestCase): MODULE = dynamic class Color(dynamic.Enum): RED = 1 GREEN = 2L BLUE = 4 class EnumTest(test_util.TestCase): """Tests for Enum class.""" def testDefinition(self): """Test correct Enum definition.""" self.assertEquals(1, Color.RED) self.assertEquals(2, Color.GREEN) self.assertEquals(4, Color.BLUE) self.assertEquals([('RED', 1), ('GREEN', 2), ('BLUE', 4)], Color._VALUES) def testIntegerOnly(self): """Test that only integers are permitted values for class.""" def do_test(): class Stuff(dynamic.Enum): A_STRING = 'a string' self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoEnumSubclass(self): """Test that it is not possible to sub-class Enum types.""" def do_test(): class MoreColor(Color): MAGENTA = 4 self.assertRaises(dynamic.EnumDefinitionError, do_test) def testNoConstructor(self): """Test that it is not possible to construct Enum values.""" self.assertRaises(NotImplementedError, Color) class MessageTest(test_util.TestCase): """Tests for Message class.""" def testEmptyDefinition(self): """Test the creation of an empty message definition.""" class MyMessage(dynamic.Message): pass self.assertEquals([], MyMessage.DESCRIPTOR.enum_types) self.assertEquals([], MyMessage.DESCRIPTOR.nested_types) self.assertEquals([], MyMessage.DESCRIPTOR.fields) self.assertEquals('MyMessage', MyMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage', MyMessage.DESCRIPTOR.full_name) def testNestedDefinition(self): """Test nesting message definitions in another.""" class MyMessage(dynamic.Message): class NestedMessage(dynamic.Message): pass self.assertEquals('NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.name) self.assertEquals('__main__.MyMessage.NestedMessage', MyMessage.NestedMessage.DESCRIPTOR.full_name) self.assertEquals(MyMessage.DESCRIPTOR, MyMessage.NestedMessage.DESCRIPTOR.containing_type) self.assertEquals([MyMessage.NestedMessage.DESCRIPTOR], MyMessage.DESCRIPTOR.nested_types) def testNestedEnum(self): """Test nesting an Enum type within a definition.""" class MyMessage(dynamic.Message): class Greek(dynamic.Enum): ALPHA = 1 BETA = 2 GAMMA = 4 self.assertFalse(hasattr(MyMessage, 'Greek')) self.assertEquals(1, len(MyMessage.DESCRIPTOR.enum_types)) Greek = MyMessage.DESCRIPTOR.enum_types[0] self.assertEquals('Greek', Greek.name) self.assertEquals('__main__.MyMessage.Greek', Greek.full_name) self.assertEquals(MyMessage.DESCRIPTOR, Greek.containing_type) self.assertEquals(3, len(Greek.values)) ALPHA = Greek.values[0] BETA = Greek.values[1] GAMMA = Greek.values[2] self.assertEquals(Greek, ALPHA.type) self.assertEquals(Greek, BETA.type) self.assertEquals(Greek, GAMMA.type) self.assertEquals(1, MyMessage.ALPHA) self.assertEquals(2, MyMessage.BETA) self.assertEquals(4, MyMessage.GAMMA) def testOptionalFields(self): """Test optional fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.StringField(2) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1 = 102 self.assertTrue(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRequiredFields(self): """Test required fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True) f2 = dynamic.StringField(2, required=True) m1 = MyMessage() self.assertFalse(m1.IsInitialized()) m1.f1 = 102 self.assertFalse(m1.IsInitialized()) m1.f2 = 'a string' self.assertTrue(m1.IsInitialized()) def testRepeatedFields(self): """Test repeated fields.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True) f2 = dynamic.StringField(2, repeated=True) m1 = MyMessage() self.assertTrue(m1.IsInitialized()) m1.f1.append(102) self.assertTrue(m1.IsInitialized()) m1.f2.append('a string') self.assertTrue(m1.IsInitialized()) def testFieldDescriptor(self): """Test field descriptors after message class creation.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) class Nested(dynamic.Message): f2 = dynamic.IntegerField(1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) self.assertEquals(1, len(MyMessage.Nested.DESCRIPTOR.fields)) f1 = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('f1', f1.name) self.assertEquals('__main__.MyMessage.f1', f1.full_name) self.assertEquals(MyMessage.DESCRIPTOR, f1.containing_type) f2 = MyMessage.Nested.DESCRIPTOR.fields[0] self.assertEquals('f2', f2.name) self.assertEquals('__main__.MyMessage.Nested.f2', f2.full_name) self.assertEquals(MyMessage.Nested.DESCRIPTOR, f2.containing_type) def testRequiredAndRepeated(self): """Test using required and repeated flags together.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, required=True, repeated=True) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDefaults(self): """Test using default values.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, default=10) m = MyMessage() self.assertEquals(10, m.f1) def testNoDefaultList(self): """Test that default does not work for repeated fields.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1, repeated=True, default=[1, 2, 3]) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testUnexpectedFieldArgument(self): """Test that unknown keyword arguments may not be used.""" self.assertRaises(TypeError, dynamic.IntegerField, 1, whatever=10) def testOverrideVariant(self): """Test overriding the variant of a field.""" class MyMessage(dynamic.Message): f1 = dynamic.IntegerField(1) f2 = dynamic.IntegerField(2, variant=descriptor.FieldDescriptor.TYPE_UINT32) self.assertEquals(descriptor.FieldDescriptor.TYPE_INT64, MyMessage.DESCRIPTOR.fields[0].type) self.assertEquals(descriptor.FieldDescriptor.TYPE_UINT32, MyMessage.DESCRIPTOR.fields[1].type) def testOverrideWrongVariant(self): """Test assigning an incompatible variant.""" def do_test(): class MyMessage(dynamic.Message): f1 = dynamic.IntegerField( 1, variant=descriptor.FieldDescriptor.TYPE_STRING) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowNonDefinitionValues(self): """Test that non-definitions may not be assigned to class.""" def do_test(): class MyMessage(dynamic.Message): f1 = 'A non-field value.' self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotAllowMethods(self): """Test that methods may not be defined on class.""" def do_test(): class MyMessage(dynamic.Message): def i_dont_think_so(self): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testDoNotSubclassing(self): """Test that messages may not be sub-classed.""" class MyMessage(dynamic.Message): pass def do_test(): class SubClass(MyMessage): pass self.assertRaises(dynamic.MessageDefinitionError, do_test) def testEnumField(self): """Test a basic enum field.""" class MyMessage(dynamic.Message): class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertEquals(1, len(MyMessage.DESCRIPTOR.fields)) color = MyMessage.DESCRIPTOR.fields[0] self.assertEquals('color', color.name) self.assertEquals('__main__.MyMessage.color', color.full_name) Color = color.enum_type self.assertEquals(MyMessage.DESCRIPTOR.enum_types[0], Color) def testEnumFieldWithNonEnum(self): """Test enum field with a non-enum class.""" def do_test(): class MyMessage(dynamic.Message): class Color(object): RED = 1 GREEN = 2 BLUE = 3 color = dynamic.EnumField(Color, 1) self.assertRaises(TypeError, do_test) def testEnumFieldWithNonNestedEnum(self): """Test enum field with a non-peer Enum class.""" class Color(dynamic.Enum): RED = 1 GREEN = 2 BLUE = 3 def do_test(): class MyMessage(dynamic.Message): color = dynamic.EnumField(Color, 1) self.assertRaises(dynamic.MessageDefinitionError, do_test) def testNoAbitraryAssignment(self): """Test that not possible to assing non-field attributes.""" class MyMessage(dynamic.Message): pass self.assertRaises(AttributeError, setattr, MyMessage(), 'a', 10) if __name__ == '__main__': unittest.main()
Python