Exemple #1
0
    def put(self,
            path: str,
            document: Document,
            force_replace: bool = False) -> DocumentFamily:
        """

        Args:
          path (str): The path to the document family
          document (Document): The document you wish to upload
          force_replace (bool): True if you want to delete the family in this path first

        Returns:
            The new document family instance
        """

        # We can only add a document if it doesn't already exist as a family
        if self.get_family_by_path(path) is None:
            new_document_family = DocumentFamily(path=path)
            new_event = self.add_document(new_document_family, document)
            document.to_kdxa(
                os.path.join(self.store_path, new_event.content_object.id) +
                ".kdxa")

            self.metastore.append(new_document_family)
            self.write_metastore()

            # Notify the listeners
            self.notify_listeners(new_event)

        document_family = self.get_family_by_path(path)
        if document_family is not None:
            return document_family

        raise Exception("Unable to get document family?")
Exemple #2
0
    def replace_content_object(self, document_family: DocumentFamily,
                               content_object_id: str,
                               document: Document) -> Optional[DocumentFamily]:

        for co in document_family.content_objects:
            if co.id == content_object_id:
                document.to_kdxa(
                    os.path.join(self.store_path, content_object_id) + ".kdxa")
                co.labels = document.labels
                co.classes = document.classes
                self.write_metastore()
                return document_family

        return None
Exemple #3
0
    def put_native(self, path: str, content: Any, force_replace=False):
        """

        Args:
          path (str): The path to the native file
          content (Any): The content to store
          force_replace (bool): Replace the object in the store
        Returns:

        """

        # In order to store a native document we will first get the family
        # then we will create a content object for the native object
        # and also a content object for the document that references it

        family = self.get_family_by_path(path)

        if family is None:
            family = DocumentFamily(path=path)
            self.metastore.append(family)

        native_content_object = ContentObject(**{'contentType': 'NATIVE'})
        native_content_object.id = str(uuid.uuid4()).replace("-", "")
        native_content_object.created_on = datetime.now()
        if family.content_objects is None:
            family.content_objects = []
        family.content_objects.append(native_content_object)
        with open(os.path.join(self.store_path, native_content_object.id),
                  'wb') as file:
            file.write(content)

        document = Document()
        document.source.connector = "document-store"
        document.source.headers = {
            "ref": family.store_ref,
            "family": family.id,
            "id": native_content_object.id
        }
        content_event = self.add_document(family, document)
        document.to_kdxa(
            os.path.join(self.store_path, content_event.content_object.id) +
            ".kdxa")
Exemple #4
0
    def add_related_document_to_family(self, document_family_id: str,
                                       transition: DocumentTransition,
                                       document: Document):
        """

        Args:
          document_family_id: str:
          transition: DocumentTransition:
          document: Document:

        Returns:

        """
        self.read_metastore()
        for family in self.metastore:
            if family.id == document_family_id:
                new_event = self.add_document(family, document, transition)
                document.to_kdxa(
                    os.path.join(self.store_path, new_event.content_object.id)
                    + ".kdxa")
                self.write_metastore()