Python protorpc.messages.MessageField() Examples

The following are 27 code examples of protorpc.messages.MessageField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module protorpc.messages , or try the search function .
Example #1
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testNestedMessageField(self):
    class Inner(messages.Message):
      count = messages.IntegerField(1)
      greet = messages.MessageField(Greeting, 2)

    class Outer(messages.Message):
      inner = messages.MessageField(Inner, 1)
      extra = messages.StringField(2)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer,
                                      indexed_fields=['inner.greet.text'])
    greet = Greeting(text='abc', when=123)
    inner = Inner(count=42, greet=greet)
    outer = Outer(inner=inner)
    st = Store(outer=outer)
    st.put()
    res = Store.query(Store.outer.inner.greet.text == 'abc').fetch()
    self.assertEqual(res, [st]) 
Example #2
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testRepeatedNestedMessageField(self):
    class Outer(messages.Message):
      greeting = messages.MessageField(Greeting, 1)
      extra = messages.IntegerField(2)

    class Store(model.Model):
      outers = msgprop.MessageProperty(Outer, repeated=True,
                                       indexed_fields=['greeting.text'])
    gr1 = Greeting(text='abc', when=123)
    gr2 = Greeting(text='def', when=456)
    outer1 = Outer(greeting=gr1, extra=1)
    outer2 = Outer(greeting=gr2, extra=2)
    store1 = Store(outers=[outer1])
    store1.put()
    store2 = Store(outers=[outer2])
    store2.put()
    store3 = Store(outers=[outer1, outer2])
    store3.put()
    res = Store.query(Store.outers.greeting.text == 'abc').fetch()
    self.assertEqual(res, [store1, store3]) 
Example #3
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testNestedRepeatedMessageField(self):
    class Outer(messages.Message):
      greetings = messages.MessageField(Greeting, 1, repeated=True)
      extra = messages.IntegerField(2)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer, indexed_fields=['greetings.text',
                                                             'extra'])
    gr1 = Greeting(text='abc', when=123)
    gr2 = Greeting(text='def', when=456)
    outer1 = Outer(greetings=[gr1], extra=1)
    outer2 = Outer(greetings=[gr2], extra=2)
    outer3 = Outer(greetings=[gr1, gr2], extra=3)
    store1 = Store(outer=outer1)
    store1.put()
    store2 = Store(outer=outer2)
    store2.put()
    store3 = Store(outer=outer3)
    store3.put()
    res = Store.query(Store.outer.greetings.text == 'abc').fetch()
    self.assertEqual(res, [store1, store3]) 
Example #4
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testNestedFieldErrors(self):
    class Outer(messages.Message):
      greetings = messages.MessageField(Greeting, 1, repeated=True)
      extra = messages.IntegerField(2)
    # Parent/child conflicts.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.text', 'greetings'])
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings', 'greetings.text'])
    # Duplicate inner field.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.text',
                                             'greetings.text'])
    # Can't index MessageField.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings'])
    # Can't specify subfields for non-MessageField.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['extra.foobar'])
    # Non-existent subfield.
    self.assertRaises(ValueError, msgprop.MessageProperty,
                      Outer, indexed_fields=['greetings.foobar']) 
Example #5
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testProjectionQueries(self):
    class Wrapper(messages.Message):
      greet = messages.MessageField(Greeting, 1)

    class Storage(model.Model):
      wrap = msgprop.MessageProperty(Wrapper, indexed_fields=['greet.text',
                                                              'greet.when'])
    gr1 = Greeting(text='abc', when=123)
    wr1 = Wrapper(greet=gr1)
    st1 = Storage(wrap=wr1)
    st1.put()
    res1 = Storage.query().get(projection=['wrap.greet.text',
                                           Storage.wrap.greet.when])
    self.assertNotEqual(res1, st1)
    self.assertEqual(res1.wrap, st1.wrap)
    res2 = Storage.query().get(projection=['wrap.greet.text'])
    self.assertEqual(res2.wrap, Wrapper(greet=Greeting(text='abc'))) 
Example #6
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 6 votes vote down vote up
def testProjectionQueriesRepeatedField(self):
    class Wrapper(messages.Message):
      greets = messages.MessageField(Greeting, 1, repeated=True)

    class Storage(model.Model):
      wrap = msgprop.MessageProperty(Wrapper, indexed_fields=['greets.text',
                                                              'greets.when'])
    gr1 = Greeting(text='abc', when=123)
    wr1 = Wrapper(greets=[gr1])
    st1 = Storage(wrap=wr1)
    st1.put()
    res1 = Storage.query().get(projection=['wrap.greets.text',
                                           Storage.wrap.greets.when])
    self.assertNotEqual(res1, st1)
    self.assertEqual(res1.wrap, st1.wrap)
    res2 = Storage.query().get(projection=['wrap.greets.text'])
    self.assertEqual(res2.wrap, Wrapper(greets=[Greeting(text='abc')])) 
Example #7
Source File: messages_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
def testMessageFieldValueToMessageWrongType(self):
    class MyMessage(messages.Message):
      pass

    class MyOtherMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    instance = MyOtherMessage()

    self.assertRaisesWithRegexpMatch(
        messages.EncodeError,
        'Expected type MyMessage, got MyOtherMessage: <MyOtherMessage>',
        HasMessage.field.value_to_message, instance) 
Example #8
Source File: messages_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
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 
Example #9
Source File: definition_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
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') 
Example #10
Source File: messages_test.py    From protorpc with Apache License 2.0 6 votes vote down vote up
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) 
Example #11
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testMessageFieldValueToMessage(self):
    class MyMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    instance = MyMessage()

    self.assertTrue(instance is HasMessage.field.value_to_message(instance)) 
Example #12
Source File: msgprop_test.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def testNestedMessageFieldIsNone(self):
    class Outer(messages.Message):
      greeting = messages.MessageField(Greeting, 1)

    class Store(model.Model):
      outer = msgprop.MessageProperty(Outer, indexed_fields=['greeting.text'])
    outer1 = Outer(greeting=None)
    store1 = Store(outer=outer1)
    store1.put()
    res = Store.query(Store.outer.greeting.text == 'abc').fetch()
    self.assertEqual(res, []) 
Example #13
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
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) 
Example #14
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testInitializeNestedFieldFromDict(self):
    """Tests initializing nested fields from dict."""
    class SimpleMessage(messages.Message):
      required = messages.IntegerField(1, required=True)

    class NestedMessage(messages.Message):
      simple = messages.MessageField(SimpleMessage, 1)

    class RepeatedMessage(messages.Message):
      simple = messages.MessageField(SimpleMessage, 1, repeated=True)

    nested_message1 = NestedMessage(simple={'required': 10})
    self.assertTrue(nested_message1.is_initialized())
    self.assertTrue(nested_message1.simple.is_initialized())

    nested_message2 = NestedMessage()
    nested_message2.simple = {'required': 10}
    self.assertTrue(nested_message2.is_initialized())
    self.assertTrue(nested_message2.simple.is_initialized())

    repeated_values = [{}, {'required': 10}, SimpleMessage(required=20)]

    repeated_message1 = RepeatedMessage(simple=repeated_values)
    self.assertEquals(3, len(repeated_message1.simple))
    self.assertFalse(repeated_message1.is_initialized())

    repeated_message1.simple[0].required = 0
    self.assertTrue(repeated_message1.is_initialized())

    repeated_message2 = RepeatedMessage()
    repeated_message2.simple = repeated_values
    self.assertEquals(3, len(repeated_message2.simple))
    self.assertFalse(repeated_message2.is_initialized())

    repeated_message2.simple[0].required = 0
    self.assertTrue(repeated_message2.is_initialized()) 
Example #15
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testPickle(self):
    """Testing pickling and unpickling of Message instances."""
    global MyEnum
    global AnotherMessage
    global MyMessage

    class MyEnum(messages.Enum):
      val1 = 1
      val2 = 2

    class AnotherMessage(messages.Message):
      string = messages.StringField(1, repeated=True)

    class MyMessage(messages.Message):
      field1 = messages.IntegerField(1)
      field2 = messages.EnumField(MyEnum, 2)
      field3 = messages.MessageField(AnotherMessage, 3)

    message = MyMessage(field1=1, field2=MyEnum.val2,
                        field3=AnotherMessage(string=['a', 'b', 'c']))
    message.set_unrecognized_field('exists', 'value', messages.Variant.STRING)
    message.set_unrecognized_field('repeated', ['list', 0, ('test',)],
                                   messages.Variant.STRING)
    unpickled = pickle.loads(pickle.dumps(message))
    self.assertEquals(message, unpickled)
    self.assertTrue(AnotherMessage.string is unpickled.field3.string.field)
    self.assertTrue('exists' in message.all_unrecognized_fields())
    self.assertEquals(('value', messages.Variant.STRING),
                      message.get_unrecognized_field_info('exists'))
    self.assertEquals((['list', 0, ('test',)], messages.Variant.STRING),
                      message.get_unrecognized_field_info('repeated')) 
Example #16
Source File: descriptor_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
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) 
Example #17
Source File: protourlencode_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
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'])) 
Example #18
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testMessageFieldValueFromMessageWrongType(self):
    class MyMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    self.assertRaisesWithRegexpMatch(
        messages.DecodeError,
        'Expected type MyMessage, got int: 10',
        HasMessage.field.value_from_message, 10) 
Example #19
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
def testMessageFieldValueFromMessage(self):
    class MyMessage(messages.Message):
      pass

    class HasMessage(messages.Message):
      field = messages.MessageField(MyMessage, 1)

    instance = MyMessage()

    self.assertTrue(instance is HasMessage.field.value_from_message(instance)) 
Example #20
Source File: messages_test.py    From protorpc with Apache License 2.0 5 votes vote down vote up
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()) 
Example #21
Source File: msgprop.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def _projected_entity_to_message(ent, message_type):
  """Recursive helper for _from_base_type() to convert an entity to a message.

  Args:
    ent: A Model instance.
    message_type: A Message subclass.

  Returns:
    An instance of message_type.
  """
  msg = message_type()
  analyzed = _analyze_indexed_fields(ent._projection)
  for name, sublist in analyzed.iteritems():
    prop = ent._properties[name]
    val = prop._get_value(ent)
    assert isinstance(prop, model.StructuredProperty) == bool(sublist)
    if sublist:
      field = message_type.field_by_name(name)
      assert isinstance(field, messages.MessageField)
      assert prop._repeated == field.repeated
      if prop._repeated:
        assert isinstance(val, list)
        val = [_projected_entity_to_message(v, field.type) for v in val]
      else:
        assert isinstance(val, prop._modelclass)
        val = _projected_entity_to_message(val, field.type)
    setattr(msg, name, val)
  return msg 
Example #22
Source File: msgprop.py    From datastore-ndb-python with Apache License 2.0 5 votes vote down vote up
def _projected_entity_to_message(ent, message_type):
  """Recursive helper for _from_base_type() to convert an entity to a message.

  Args:
    ent: A Model instance.
    message_type: A Message subclass.

  Returns:
    An instance of message_type.
  """
  msg = message_type()
  analyzed = _analyze_indexed_fields(ent._projection)
  for name, sublist in analyzed.iteritems():
    prop = ent._properties[name]
    val = prop._get_value(ent)
    assert isinstance(prop, model.StructuredProperty) == bool(sublist)
    if sublist:
      field = message_type.field_by_name(name)
      assert isinstance(field, messages.MessageField)
      assert prop._repeated == field.repeated
      if prop._repeated:
        assert isinstance(val, list)
        val = [_projected_entity_to_message(v, field.type) for v in val]
      else:
        assert isinstance(val, prop._modelclass)
        val = _projected_entity_to_message(val, field.type)
    setattr(msg, name, val)
  return msg 
Example #23
Source File: messages_test.py    From protorpc with Apache License 2.0 4 votes vote down vote up
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 
Example #24
Source File: messages_test.py    From protorpc with Apache License 2.0 4 votes vote down vote up
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 = u'value1'
    self.assertNotEquals(message1, message2)

    message2.field3 = AnotherMessage()
    message2.field3.string = u'value2'
    self.assertNotEquals(message1, message2)

    message2.field3.string = u'value1'
    self.assertEquals(message1, message2) 
Example #25
Source File: messages_test.py    From protorpc with Apache License 2.0 4 votes vote down vote up
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 val is repeated. Found: <SubMessage>",
      setattr, message, 'val', SubMessage())
    message.val = [SubMessage()]
    message_field.validate(message) 
Example #26
Source File: msgprop.py    From python-compat-runtime with Apache License 2.0 4 votes vote down vote up
def _make_model_class(message_type, indexed_fields, **props):
  """Construct a Model subclass corresponding to a Message subclass.

  Args:
    message_type: A Message subclass.
    indexed_fields: A list of dotted and undotted field names.
    **props: Additional properties with which to seed the class.

  Returns:
    A Model subclass whose properties correspond to those fields of
    message_type whose field name is listed in indexed_fields, plus
    the properties specified by the **props arguments.  For dotted
    field names, a StructuredProperty is generated using a Model
    subclass created by a recursive call.

  Raises:
    Whatever _analyze_indexed_fields() raises.
    ValueError if a field name conflicts with a name in **props.
    ValueError if a field name is not valid field of message_type.
    ValueError if an undotted field name designates a MessageField.
  """
  analyzed = _analyze_indexed_fields(indexed_fields)
  for field_name, sub_fields in analyzed.iteritems():
    if field_name in props:
      raise ValueError('field name %s is reserved' % field_name)
    try:
      field = message_type.field_by_name(field_name)
    except KeyError:
      raise ValueError('Message type %s has no field named %s' %
                       (message_type.__name__, field_name))
    if isinstance(field, messages.MessageField):
      if not sub_fields:
        raise ValueError(
            'MessageField %s cannot be indexed, only sub-fields' % field_name)
      sub_model_class = _make_model_class(field.type, sub_fields)
      prop = model.StructuredProperty(sub_model_class, field_name,
                                      repeated=field.repeated)
    else:
      if sub_fields is not None:
        raise ValueError(
            'Unstructured field %s cannot have indexed sub-fields' % field_name)
      if isinstance(field, messages.EnumField):
        prop = EnumProperty(field.type, field_name, repeated=field.repeated)
      elif isinstance(field, messages.BytesField):
        prop = model.BlobProperty(field_name,
                                  repeated=field.repeated, indexed=True)
      else:
        # IntegerField, FloatField, BooleanField, StringField.
        prop = model.GenericProperty(field_name, repeated=field.repeated)
    props[field_name] = prop
  return model.MetaModel('_%s__Model' % message_type.__name__,
                         (model.Model,), props) 
Example #27
Source File: msgprop.py    From datastore-ndb-python with Apache License 2.0 4 votes vote down vote up
def _make_model_class(message_type, indexed_fields, **props):
  """Construct a Model subclass corresponding to a Message subclass.

  Args:
    message_type: A Message subclass.
    indexed_fields: A list of dotted and undotted field names.
    **props: Additional properties with which to seed the class.

  Returns:
    A Model subclass whose properties correspond to those fields of
    message_type whose field name is listed in indexed_fields, plus
    the properties specified by the **props arguments.  For dotted
    field names, a StructuredProperty is generated using a Model
    subclass created by a recursive call.

  Raises:
    Whatever _analyze_indexed_fields() raises.
    ValueError if a field name conflicts with a name in **props.
    ValueError if a field name is not valid field of message_type.
    ValueError if an undotted field name designates a MessageField.
  """
  analyzed = _analyze_indexed_fields(indexed_fields)
  for field_name, sub_fields in analyzed.iteritems():
    if field_name in props:
      raise ValueError('field name %s is reserved' % field_name)
    try:
      field = message_type.field_by_name(field_name)
    except KeyError:
      raise ValueError('Message type %s has no field named %s' %
                       (message_type.__name__, field_name))
    if isinstance(field, messages.MessageField):
      if not sub_fields:
        raise ValueError(
            'MessageField %s cannot be indexed, only sub-fields' % field_name)
      sub_model_class = _make_model_class(field.type, sub_fields)
      prop = model.StructuredProperty(sub_model_class, field_name,
                                      repeated=field.repeated)
    else:
      if sub_fields is not None:
        raise ValueError(
            'Unstructured field %s cannot have indexed sub-fields' % field_name)
      if isinstance(field, messages.EnumField):
        prop = EnumProperty(field.type, field_name, repeated=field.repeated)
      elif isinstance(field, messages.BytesField):
        prop = model.BlobProperty(field_name,
                                  repeated=field.repeated, indexed=True)
      else:
        # IntegerField, FloatField, BooleanField, StringField.
        prop = model.GenericProperty(field_name, repeated=field.repeated)
    props[field_name] = prop
  return model.MetaModel('_%s__Model' % message_type.__name__,
                         (model.Model,), props)