encodable.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. """Interfaces implemented by serializable objects."""
  3. # Using lowercase function naming to match the JavaScript names.
  4. # pylint: disable-msg=g-bad-name
  5. class Encodable(object):
  6. """An interface implemented by objects that can serialize themselves."""
  7. def encode(self, encoder):
  8. """Encodes the object in a format compatible with Serializer.
  9. Args:
  10. encoder: A function that can be called to encode the components of
  11. an object.
  12. Returns:
  13. The encoded form of the object.
  14. """
  15. raise NotImplementedError('Encodable classes must implement encode().')
  16. def encode_cloud_value(self, encoder):
  17. """Encodes the object as a ValueNode.
  18. Args:
  19. encoder: A function that can be called to encode the components of
  20. an object.
  21. Returns:
  22. The encoded form of the object.
  23. """
  24. raise NotImplementedError(
  25. 'Encodable classes must implement encode_cloud_value().')
  26. class EncodableFunction(object):
  27. """An interface implemented by functions that can serialize themselves."""
  28. def encode_invocation(self, encoder):
  29. """Encodes the function in a format compatible with Serializer.
  30. Args:
  31. encoder: A function that can be called to encode the components of
  32. an object.
  33. Returns:
  34. The encoded form of the function.
  35. """
  36. raise NotImplementedError(
  37. 'EncodableFunction classes must implement encode_invocation().')
  38. def encode_cloud_invocation(self, encoder):
  39. """Encodes the function as a FunctionInvocation.
  40. Args:
  41. encoder: A function that can be called to encode the components of
  42. an object. Returns a reference to the encoded value.
  43. Returns:
  44. The encoded form of the function.
  45. """
  46. raise NotImplementedError(
  47. 'EncodableFunction classes must implement encode_cloud_invocation().')