예제 #1
0
 def deleteStatements(self, quads):
     """Delete a collection of statements from the repository."""
     nullRequest(self,
                 "POST",
                 "/statements/delete",
                 encode_json(quads),
                 contentType="application/json")
예제 #2
0
 def addStatement(self, subj, pred, obj, context=None):
     """Add a single statement to the repository."""
     nullRequest(self,
                 "POST",
                 "/statements",
                 encode_json([[subj, pred, obj, context]]),
                 contentType="application/json")
예제 #3
0
    def loadFile(self, file, rdf_format, baseURI=None, context=None, serverSide=False,
                 commitEvery=None, content_encoding=None, attributes=None,
                 json_ld_store_source=None,
                 json_ld_context=None, allow_external_references=None,
                 external_reference_timeout=None):
        mime = Format.mime_type_for_format(rdf_format)

        if serverSide:
            # file is a server-side path, body should be empty
            body_context = wrap_context(None)
        elif isinstance(file, basestring) and not serverSide:
            # file is a local path, read it and send as body
            body_context = open(file, 'rb')
            file = None
        else:
            # We were passed a file-like object that we must not close,
            # so we'll just pass it as body.
            body_context = wrap_context(file)
            file = None

        with body_context as body:
            params = urlenc(file=file, context=context, baseURI=baseURI, commit=commitEvery,
                            attributes=attributes and encode_json(attributes),
                            jsonLdStoreSource=json_ld_store_source,
                            jsonLdContext=json_ld_context and fix_json_ld_context(json_ld_context),
                            externalReferences=allow_external_references,
                            extermalReferenceTimeout=external_reference_timeout)
            nullRequest(self, "POST", "/statements?" + params, body,
                        content_type=mime, content_encoding=content_encoding)
예제 #4
0
 def getHeaders(self):
     """
     Return a dictionary of headers that should be added to every request.
     Return ``None`` instead of an empty dictionary if there are no such
     headers.
     """
     s = self.transaction_settings
     values = []
     if s:
         if s.distributed_transaction_timeout is not None:
             values.append(
                 ('distributedTransactionTimeout',
                  time_in_seconds(s.distributed_transaction_timeout)))
         if s.durability is not None:
             values.append(('durability', s.durability))
         if s.transaction_latency_count is not None:
             values.append(('transactionLatencyCount',
                            s.transaction_latency_count))
         if s.transaction_latency_timeout is not None:
             values.append(('transactionLatencyTimeout',
                            time_in_seconds(s.transaction_latency_timeout)))
     result = {}
     if values:
         result['x-repl-settings'] = ' '.join('%s=%s' % value for value in values)
     if self.user_attributes:
         result['x-user-attributes'] = encode_json(self.user_attributes)
     return result or None
예제 #5
0
 def addStatements(self, quads, commitEvery=None):
     """Add a collection of statements to the repository. Quads
     should be an array of four-element arrays, where the fourth
     element, the graph name, may be None."""
     nullRequest(self,
                 "POST",
                 "/statements?" + urlenc(commit=commitEvery),
                 encode_json(quads),
                 contentType="application/json")
예제 #6
0
 def addStatements(self, quads, commitEvery=None):
     """Add a collection of statements to the repository. Quads
     should be an array of or five four-element arrays, where the fourth
     element, the graph name, may be None. The fifth element, if present,
     must be a string with a JSON-encoded dictionary of attribute values.
     """
     nullRequest(self, "POST", "/statements?" +
                 urlenc(commit=commitEvery),
                 encode_json(quads), content_type="application/json")
예제 #7
0
def fix_json_ld_context(context):
    """
    Take a dict or a string and return a JSON string.

    If the input dict does not contain "@context", wrap it.
    """
    if not isinstance(context, dict) or "@context" not in context:
        context = {"@context": context}
    return encode_json(context)
예제 #8
0
 def addStatement(self, subj, pred, obj, context=None, attributes=None):
     """Add a single statement to the repository."""
     args = {
         'subj': subj,
         'pred': pred,
         'obj': obj,
         'context': context,
         'attributes': attributes and encode_json(attributes)
     }
     nullRequest(self, "PUT", "/statement?" + urlenc(**args))
예제 #9
0
    def registerEncodedIdPrefixes(self, registrations):
        body = []
        for reg in registrations:
            body.append({
                "prefix":
                    reg.prefix if hasattr(reg, "prefix") else reg[0],
                "format":
                    reg.format if hasattr(reg, "format") else reg[1]})

        nullRequest(self, "POST", "/encodedIds/prefixes",
                    content_type="application/json", body=encode_json(body))
예제 #10
0
    def registerEncodedIdPrefixes(self, registrations):
        body = []
        for reg in registrations:
            body.append({
                "prefix":
                    reg.prefix if hasattr(reg, "prefix") else reg[0],
                "format":
                    reg.format if hasattr(reg, "format") else reg[1]})

        nullRequest(self, "POST", "/encodedIds/prefixes",
                    content_type="application/json", body=encode_json(body))
예제 #11
0
    def loadDocument(self, doc, doc_format, base=None,
                     rules=None, subject=None, prefix=None,
                     rename=None, rdf_type=None, lang=None, skip=None,
                     transform=None, graph=None, json_store_source=None,
                     csv_columns=None, csv_separator=None, csv_quote=None,
                     csv_whitespace=None, csv_double_quote=None, csv_escape=None,
                     attributes=None, commit=None, context=None,
                     encoding=None, content_encoding=None):
        mime = Format.mime_type_for_format(doc_format)
        if encoding:
            mime += ';charset=' + encoding

        if prefix is None:
            prefix = {}
        if rename is not None:
            # Copy the keys, since we will be changing some renames
            for k, v in list(six.iteritems(rename)):
                if isinstance(v, URI):
                    # To provide a full URI for a key we need to use both 'prefix'
                    # and 'rename' options
                    namespace, local_name = v.split()
                    if local_name != k:
                        rename[k] = local_name
                    prefix[k] = namespace

        prefix_list = uri_dict_to_string_list(prefix) or []
        if base is not None:
            prefix_list.append(uri_to_string(base))

        args = {
            'transform-rules': rules,
            'tr-id': uri_to_string(subject),
            'tr-prefix': prefix_list,
            'tr-rename': dict_to_string_list(rename),
            'tr-type': uri_dict_to_string_list(rdf_type),
            'tr-lang':  dict_to_string_list(lang),
            'tr-skip': skip,
            'tr-transform':  dict_to_string_list(transform),
            'tr-graph': ['%s=%s' % (k, URI(str(v)))
                         for k, v in six.iteritems(graph or {})],
            'json-store-source': bool(json_store_source),
            'csv-columns': ','.join(csv_columns) if csv_columns else None,
            'csv-separator': csv_separator,
            'csv-quote': csv_quote,
            'csv-whitespace': ','.join(csv_whitespace) if csv_whitespace is not None else None,
            'csv-double-quote-p': bool(csv_double_quote) if csv_double_quote is not None else None,
            'csv-escape': csv_escape,
            'context': str(URI(str(context))) if context else None,
            'commit': commit,
            'attributes': attributes and encode_json(attributes)
        }
        return nullRequest(self, 'POST', '/statements/transform?' + urlenc(**args),
                           doc, content_type=mime, content_encoding=content_encoding)
예제 #12
0
 def loadData(self, data, rdf_format, base_uri=None, context=None,
              commit_every=None, content_encoding=None, attributes=None,
              json_ld_store_source=None,
              json_ld_context=None, allow_external_references=None,
              external_reference_timeout=None):
     nullRequest(self, "POST",
                 "/statements?" + urlenc(
                     context=context,
                     baseURI=base_uri,
                     commit=commit_every,
                     attributes=attributes and encode_json(attributes),
                     jsonLdStoreSource=json_ld_store_source,
                     jsonLdContext=json_ld_context and fix_json_ld_context(json_ld_context),
                     externalReferences=allow_external_references,
                     extermalReferenceTimeout=external_reference_timeout),
                 data,
                 content_type=Format.mime_type_for_format(rdf_format),
                 content_encoding=content_encoding,)
예제 #13
0
 def deleteStatementsById(self, ids):
     nullRequest(self,
                 "POST",
                 "/statements/delete?ids=true",
                 encode_json(ids),
                 contentType="application/json")
예제 #14
0
 def deleteStatementsById(self, ids):
     nullRequest(self, "POST", "/statements/delete?ids=true", encode_json(ids), content_type="application/json")
예제 #15
0
 def deleteStatements(self, quads):
     """Delete a collection of statements from the repository."""
     nullRequest(self, "POST", "/statements/delete", encode_json(quads), content_type="application/json")