Exemple #1
0
    def load_graph(self, rdf_graph, tmp_file_name):
        """Load a rdflib graph into the triplestore

        Write rdf to a tmp file, and send the url to this file
        to the triplestore with a LOAD request

        Parameters
        ----------
        rdf_graph : Graph
            rdf graph to load
        tmp_file_name : string
            Path to a tmp file
        """
        sparql = SparqlQueryLauncher(self.app, self.session)

        temp_file_path = '{}/{}'.format(self.ttl_dir, tmp_file_name)

        encoding = 'utf-8' if self.serialization_format != 'nt' else None
        rdf_graph.serialize(format=self.serialization_format,
                            encoding=encoding,
                            destination=temp_file_path)

        # Load the chunk
        sparql.load_data(tmp_file_name, self.file_graph, self.host_url)

        # Remove tmp file
        if not self.settings.getboolean('askomics', 'debug_ttl'):
            os.remove(temp_file_path)
Exemple #2
0
    def integrate(self, public=False):
        """Integrate the file into the triplestore

        Parameters
        ----------
        public : bool, optional
            Integrate in private or public graph
        """
        sparql = SparqlQueryLauncher(self.app, self.session)

        self.public = public

        method = self.settings.get('triplestore', 'upload_method')

        # insert metadata
        sparql.insert_data(self.get_metadata(), self.file_graph, metadata=True)

        if method == "load":
            # cp file into ttl dir
            tmp_file_name = 'tmp_{}_{}.ttl'.format(
                Utils.get_random_string(5),
                self.name,
            )
            temp_file_path = '{}/{}'.format(self.ttl_dir, tmp_file_name)
            copyfile(self.path, temp_file_path)
            # Load the chunk
            sparql.load_data(tmp_file_name, self.file_graph, self.host_url)

            # Remove tmp file
            if not self.settings.getboolean('askomics', 'debug_ttl'):
                os.remove(temp_file_path)
        else:

            with open(self.path) as ttl_file:
                ttl_content = ttl_file.read()
                sparql.insert_ttl_string(ttl_content, self.user_graph)

        self.set_triples_number()