formatters.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import json
  2. import logging
  3. import uuid
  4. from abc import ABCMeta, abstractmethod
  5. from lakesuperior.model.ldp.ldpr import RES_CREATED, RES_DELETED, RES_UPDATED
  6. class BaseASFormatter(metaclass=ABCMeta):
  7. """
  8. Format message as ActivityStreams.
  9. This is not really a `logging.Formatter` subclass, but a plain string
  10. builder.
  11. """
  12. ev_types = {
  13. RES_CREATED : 'Create',
  14. RES_DELETED : 'Delete',
  15. RES_UPDATED : 'Update',
  16. }
  17. ev_names = {
  18. RES_CREATED : 'Resource Creation',
  19. RES_DELETED : 'Resource Deletion',
  20. RES_UPDATED : 'Resource Modification',
  21. }
  22. def __init__(
  23. self, rsrc_uri, ev_type, timestamp, rsrc_type, actor, data=None):
  24. """
  25. Format output according to granularity level.
  26. NOTE: Granularity level does not refer to the logging levels, i.e.
  27. *when* a message gets logged, in fact all the Messaging logger messages
  28. are logged under the same level. This it is rather about *what* gets
  29. logged in a message.
  30. :param rdflib.URIRef rsrc_uri: URI of the resource.
  31. :param str ev_type: one of `create`, `delete` or `update`
  32. :param str timestamp: Timestamp of the event.
  33. :param data: (tuple(set)) if messaging is configured with `provenance`
  34. level, this is a 2-tuple with one set (as 3-tuples of
  35. RDFlib.Identifier instances) for removed triples, and one set for
  36. added triples.
  37. """
  38. self.rsrc_uri = rsrc_uri
  39. self.ev_type = ev_type
  40. self.timestamp = timestamp
  41. self.rsrc_type = rsrc_type
  42. self.actor = actor
  43. self.data = data
  44. @abstractmethod
  45. def __str__(self):
  46. pass
  47. class ASResourceFormatter(BaseASFormatter):
  48. """
  49. Sends information about a resource being created, updated or deleted, by
  50. who and when, with no further information about what changed.
  51. """
  52. def __str__(self):
  53. """Output structured data as string."""
  54. ret = {
  55. '@context': 'https://www.w3.org/ns/activitystreams',
  56. 'id' : 'urn:uuid:{}'.format(uuid.uuid4()),
  57. 'type' : self.ev_types[self.ev_type],
  58. 'name' : self.ev_names[self.ev_type],
  59. 'object' : {
  60. 'id' : self.rsrc_uri,
  61. 'updated' : self.timestamp,
  62. 'type' : self.rsrc_type,
  63. },
  64. 'actor' : self.actor,
  65. }
  66. return json.dumps(ret)
  67. class ASDeltaFormatter(BaseASFormatter):
  68. """
  69. Sends the same information as `ASResourceFormatter` with the addition of
  70. the triples that were added and the ones that were removed in the request.
  71. This may be used to send rich provenance data to a preservation system.
  72. """
  73. def __str__(self):
  74. """Output structured data as string."""
  75. ret = {
  76. '@context': 'https://www.w3.org/ns/activitystreams',
  77. 'id' : 'urn:uuid:{}'.format(uuid.uuid4()),
  78. 'type' : self.ev_types[self.ev_type],
  79. 'name' : self.ev_names[self.ev_type],
  80. 'object' : {
  81. 'id' : self.rsrc_uri,
  82. 'updated' : self.timestamp,
  83. 'type' : self.rsrc_type,
  84. },
  85. 'actor' : self.actor,
  86. 'data' : self.data,
  87. }
  88. return json.dumps(ret)