caselawclient.models.documents.versions

  1import json
  2from enum import Enum
  3from typing import Any, Optional, TypedDict
  4
  5from typing_extensions import NotRequired
  6
  7
  8class AnnotationDataDict(TypedDict):
  9    type: str
 10    calling_function: str
 11    calling_agent: str
 12    message: NotRequired[str]
 13    payload: NotRequired[dict[str, Any]]
 14    automated: bool
 15
 16
 17class VersionType(Enum):
 18    """Valid types of version."""
 19
 20    SUBMISSION = "submission"
 21    """ This version has been created as a result of a submission of a new document. """
 22
 23    ENRICHMENT = "enrichment"
 24    """ This version has been created through an enrichment process. """
 25
 26    EDIT = "edit"
 27    """ This version has been created as the result of a manual edit. """
 28
 29
 30class VersionAnnotation:
 31    """A class holding structured data about the reason for a version."""
 32
 33    def __init__(
 34        self,
 35        version_type: VersionType,
 36        automated: bool,
 37        message: Optional[str] = None,
 38        payload: Optional[dict[str, Any]] = None,
 39    ):
 40        """
 41        :param version_type: The type of version being created
 42        :param automated: `True` if this action has happened as the result of an automated process, rather than a human
 43            action
 44        :param message: A human-readable string containing information about the version which can't be expressed in the
 45            structured data.
 46        :param payload: A dict containing additional information relevant to this version change
 47        """
 48        self.version_type = version_type
 49        self.automated = automated
 50        self.message = message
 51        self.payload = payload
 52
 53        self.calling_function: Optional[str] = None
 54        self.calling_agent: Optional[str] = None
 55
 56    def set_calling_function(self, calling_function: str) -> None:
 57        """
 58        Set the name of the calling function for tracing purposes
 59
 60        :param calling_function: The name of the function which is performing the database write
 61        """
 62        self.calling_function = calling_function
 63
 64    def set_calling_agent(self, calling_agent: str) -> None:
 65        """
 66        Set the name of the calling agent for tracing purposes
 67
 68        :param calling_agent: The name of the agent which is performing the database write
 69        """
 70        self.calling_agent = calling_agent
 71
 72    @property
 73    def structured_annotation_dict(self) -> AnnotationDataDict:
 74        """
 75        :return: A structured dict representing this `VersionAnnotation`
 76
 77        :raises AttributeError: The name of the calling function has not been set; use `set_calling_function()`
 78        :raises AttributeError: The name of the calling agent has not been set; use `set_calling_agent()`
 79        """
 80        if not self.calling_function:
 81            raise AttributeError(
 82                "The name of the calling function has not been set; use set_calling_function()",
 83            )
 84
 85        if not self.calling_agent:
 86            raise AttributeError(
 87                "The name of the calling agent has not been set; use set_calling_agent()",
 88            )
 89
 90        annotation_data: AnnotationDataDict = {
 91            "type": self.version_type.value,
 92            "calling_function": self.calling_function,
 93            "calling_agent": self.calling_agent,
 94            "automated": self.automated,
 95        }
 96
 97        if self.message:
 98            annotation_data["message"] = self.message
 99
100        if self.payload:
101            annotation_data["payload"] = self.payload
102
103        return annotation_data
104
105    @property
106    def as_json(self) -> str:
107        """Render the structured annotation data as JSON, so it can be stored in the MarkLogic dls:annotation field.
108
109        :return: A JSON string representing this `VersionAnnotation`"""
110
111        return json.dumps(self.structured_annotation_dict)
112
113    def __str__(self) -> str:
114        return self.as_json
class AnnotationDataDict(typing.TypedDict):
 9class AnnotationDataDict(TypedDict):
10    type: str
11    calling_function: str
12    calling_agent: str
13    message: NotRequired[str]
14    payload: NotRequired[dict[str, Any]]
15    automated: bool
type: str
calling_function: str
calling_agent: str
message: NotRequired[str]
payload: NotRequired[dict[str, Any]]
automated: bool
class VersionType(enum.Enum):
18class VersionType(Enum):
19    """Valid types of version."""
20
21    SUBMISSION = "submission"
22    """ This version has been created as a result of a submission of a new document. """
23
24    ENRICHMENT = "enrichment"
25    """ This version has been created through an enrichment process. """
26
27    EDIT = "edit"
28    """ This version has been created as the result of a manual edit. """

Valid types of version.

SUBMISSION = <VersionType.SUBMISSION: 'submission'>

This version has been created as a result of a submission of a new document.

ENRICHMENT = <VersionType.ENRICHMENT: 'enrichment'>

This version has been created through an enrichment process.

EDIT = <VersionType.EDIT: 'edit'>

This version has been created as the result of a manual edit.

class VersionAnnotation:
 31class VersionAnnotation:
 32    """A class holding structured data about the reason for a version."""
 33
 34    def __init__(
 35        self,
 36        version_type: VersionType,
 37        automated: bool,
 38        message: Optional[str] = None,
 39        payload: Optional[dict[str, Any]] = None,
 40    ):
 41        """
 42        :param version_type: The type of version being created
 43        :param automated: `True` if this action has happened as the result of an automated process, rather than a human
 44            action
 45        :param message: A human-readable string containing information about the version which can't be expressed in the
 46            structured data.
 47        :param payload: A dict containing additional information relevant to this version change
 48        """
 49        self.version_type = version_type
 50        self.automated = automated
 51        self.message = message
 52        self.payload = payload
 53
 54        self.calling_function: Optional[str] = None
 55        self.calling_agent: Optional[str] = None
 56
 57    def set_calling_function(self, calling_function: str) -> None:
 58        """
 59        Set the name of the calling function for tracing purposes
 60
 61        :param calling_function: The name of the function which is performing the database write
 62        """
 63        self.calling_function = calling_function
 64
 65    def set_calling_agent(self, calling_agent: str) -> None:
 66        """
 67        Set the name of the calling agent for tracing purposes
 68
 69        :param calling_agent: The name of the agent which is performing the database write
 70        """
 71        self.calling_agent = calling_agent
 72
 73    @property
 74    def structured_annotation_dict(self) -> AnnotationDataDict:
 75        """
 76        :return: A structured dict representing this `VersionAnnotation`
 77
 78        :raises AttributeError: The name of the calling function has not been set; use `set_calling_function()`
 79        :raises AttributeError: The name of the calling agent has not been set; use `set_calling_agent()`
 80        """
 81        if not self.calling_function:
 82            raise AttributeError(
 83                "The name of the calling function has not been set; use set_calling_function()",
 84            )
 85
 86        if not self.calling_agent:
 87            raise AttributeError(
 88                "The name of the calling agent has not been set; use set_calling_agent()",
 89            )
 90
 91        annotation_data: AnnotationDataDict = {
 92            "type": self.version_type.value,
 93            "calling_function": self.calling_function,
 94            "calling_agent": self.calling_agent,
 95            "automated": self.automated,
 96        }
 97
 98        if self.message:
 99            annotation_data["message"] = self.message
100
101        if self.payload:
102            annotation_data["payload"] = self.payload
103
104        return annotation_data
105
106    @property
107    def as_json(self) -> str:
108        """Render the structured annotation data as JSON, so it can be stored in the MarkLogic dls:annotation field.
109
110        :return: A JSON string representing this `VersionAnnotation`"""
111
112        return json.dumps(self.structured_annotation_dict)
113
114    def __str__(self) -> str:
115        return self.as_json

A class holding structured data about the reason for a version.

VersionAnnotation( version_type: VersionType, automated: bool, message: Optional[str] = None, payload: Optional[dict[str, Any]] = None)
34    def __init__(
35        self,
36        version_type: VersionType,
37        automated: bool,
38        message: Optional[str] = None,
39        payload: Optional[dict[str, Any]] = None,
40    ):
41        """
42        :param version_type: The type of version being created
43        :param automated: `True` if this action has happened as the result of an automated process, rather than a human
44            action
45        :param message: A human-readable string containing information about the version which can't be expressed in the
46            structured data.
47        :param payload: A dict containing additional information relevant to this version change
48        """
49        self.version_type = version_type
50        self.automated = automated
51        self.message = message
52        self.payload = payload
53
54        self.calling_function: Optional[str] = None
55        self.calling_agent: Optional[str] = None
Parameters
  • version_type: The type of version being created
  • automated: True if this action has happened as the result of an automated process, rather than a human action
  • message: A human-readable string containing information about the version which can't be expressed in the structured data.
  • payload: A dict containing additional information relevant to this version change
version_type
automated
message
payload
calling_function: Optional[str]
calling_agent: Optional[str]
def set_calling_function(self, calling_function: str) -> None:
57    def set_calling_function(self, calling_function: str) -> None:
58        """
59        Set the name of the calling function for tracing purposes
60
61        :param calling_function: The name of the function which is performing the database write
62        """
63        self.calling_function = calling_function

Set the name of the calling function for tracing purposes

Parameters
  • calling_function: The name of the function which is performing the database write
def set_calling_agent(self, calling_agent: str) -> None:
65    def set_calling_agent(self, calling_agent: str) -> None:
66        """
67        Set the name of the calling agent for tracing purposes
68
69        :param calling_agent: The name of the agent which is performing the database write
70        """
71        self.calling_agent = calling_agent

Set the name of the calling agent for tracing purposes

Parameters
  • calling_agent: The name of the agent which is performing the database write
structured_annotation_dict: AnnotationDataDict
 73    @property
 74    def structured_annotation_dict(self) -> AnnotationDataDict:
 75        """
 76        :return: A structured dict representing this `VersionAnnotation`
 77
 78        :raises AttributeError: The name of the calling function has not been set; use `set_calling_function()`
 79        :raises AttributeError: The name of the calling agent has not been set; use `set_calling_agent()`
 80        """
 81        if not self.calling_function:
 82            raise AttributeError(
 83                "The name of the calling function has not been set; use set_calling_function()",
 84            )
 85
 86        if not self.calling_agent:
 87            raise AttributeError(
 88                "The name of the calling agent has not been set; use set_calling_agent()",
 89            )
 90
 91        annotation_data: AnnotationDataDict = {
 92            "type": self.version_type.value,
 93            "calling_function": self.calling_function,
 94            "calling_agent": self.calling_agent,
 95            "automated": self.automated,
 96        }
 97
 98        if self.message:
 99            annotation_data["message"] = self.message
100
101        if self.payload:
102            annotation_data["payload"] = self.payload
103
104        return annotation_data
Returns

A structured dict representing this VersionAnnotation

Raises
as_json: str
106    @property
107    def as_json(self) -> str:
108        """Render the structured annotation data as JSON, so it can be stored in the MarkLogic dls:annotation field.
109
110        :return: A JSON string representing this `VersionAnnotation`"""
111
112        return json.dumps(self.structured_annotation_dict)

Render the structured annotation data as JSON, so it can be stored in the MarkLogic dls:annotation field.

Returns

A JSON string representing this VersionAnnotation