formatters.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import json
  2. import logging
  3. import uuid
  4. from abc import ABCMeta, abstractmethod
  5. from lakesuperior.globals 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 rsrc_uri (rdflib.URIRef) URI of the resource.
  31. @param ev_type (string) one of `create`, `delete` or `update`
  32. @param timestamp (string) 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. '''
  54. Output structured data as string.
  55. '''
  56. ret = {
  57. '@context': 'https://www.w3.org/ns/activitystreams',
  58. 'id' : 'urn:uuid:{}'.format(uuid.uuid4()),
  59. 'type' : self.ev_types[self.ev_type],
  60. 'name' : self.ev_names[self.ev_type],
  61. 'object' : {
  62. 'id' : self.rsrc_uri,
  63. 'updated' : self.timestamp,
  64. 'type' : self.rsrc_type,
  65. },
  66. 'actor' : self.actor,
  67. }
  68. return json.dumps(ret)
  69. class ASDeltaFormatter(BaseASFormatter):
  70. '''
  71. Sends the same information as `ASResourceFormatter` with the addition of
  72. the triples that were added and the ones that were removed in the request.
  73. This may be used to send rich provenance data to a preservation system.
  74. '''
  75. def __str__(self):
  76. '''
  77. Output structured data as string.
  78. '''
  79. ret = {
  80. '@context': 'https://www.w3.org/ns/activitystreams',
  81. 'id' : 'urn:uuid:{}'.format(uuid.uuid4()),
  82. 'type' : self.ev_types[self.ev_type],
  83. 'name' : self.ev_names[self.ev_type],
  84. 'object' : {
  85. 'id' : self.rsrc_uri,
  86. 'updated' : self.timestamp,
  87. 'type' : self.rsrc_type,
  88. },
  89. 'actor' : self.actor,
  90. 'data' : self.data,
  91. }
  92. return json.dumps(ret)