From 7b9bc9dca5ab467759639a7be2f0219a78c97a67 Mon Sep 17 00:00:00 2001 From: Nicholas Car Date: Fri, 10 Apr 2020 13:55:52 +1000 Subject: blacked __init__ & namespace --- rdflib/__init__.py | 109 +++++++++----- rdflib/namespace.py | 405 ++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 372 insertions(+), 142 deletions(-) diff --git a/rdflib/__init__.py b/rdflib/__init__.py index 7f7775e9..fa24ae4f 100644 --- a/rdflib/__init__.py +++ b/rdflib/__init__.py @@ -49,44 +49,62 @@ __version__ = "5.0.0" __date__ = "2020-04-03" __all__ = [ - 'URIRef', - 'BNode', - 'Literal', - 'Variable', - - 'Namespace', - - 'Dataset', - 'Graph', - 'ConjunctiveGraph', - - 'CSVW', 'DC', 'DCAT', 'DCTERMS', 'DOAP', - 'FOAF', 'ODRL2', 'ORG', 'OWL', 'PROF', - 'PROV', 'QB', 'RDF', 'RDFS', 'SDO', - 'SH', 'SKOS', 'SOSA', 'SSN', 'TIME', - 'VOID', 'XMLNS', 'XSD', - - 'util', + "URIRef", + "BNode", + "Literal", + "Variable", + "Namespace", + "Dataset", + "Graph", + "ConjunctiveGraph", + "CSVW", + "DC", + "DCAT", + "DCTERMS", + "DOAP", + "FOAF", + "ODRL2", + "ORG", + "OWL", + "PROF", + "PROV", + "QB", + "RDF", + "RDFS", + "SDO", + "SH", + "SKOS", + "SOSA", + "SSN", + "TIME", + "VOID", + "XMLNS", + "XSD", + "util", ] import sys + assert sys.version_info >= (2, 7, 0), "rdflib requires Python 2.7 or higher" import logging + logger = logging.getLogger(__name__) _interactive_mode = False try: import __main__ - if not hasattr(__main__, '__file__') and sys.stdout!=None and sys.stderr.isatty(): + + if not hasattr(__main__, "__file__") and sys.stdout != None and sys.stderr.isatty(): # show log messages in interactive mode _interactive_mode = True logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler()) del __main__ except ImportError: - #Main already imported from elsewhere + # Main already imported from elsewhere import warnings - warnings.warn('__main__ already imported', ImportWarning) + + warnings.warn("__main__ already imported", ImportWarning) del warnings if _interactive_mode: @@ -98,18 +116,21 @@ del sys import six + try: six.unichr(0x10FFFF) except ValueError: import warnings + warnings.warn( - 'You are using a narrow Python build!\n' - 'This means that your Python does not properly support chars > 16bit.\n' + "You are using a narrow Python build!\n" + "This means that your Python does not properly support chars > 16bit.\n" 'On your system chars like c=u"\\U0010FFFF" will have a len(c)==2.\n' - 'As this can cause hard to debug problems with string processing\n' - '(slicing, regexp, ...) later on, we strongly advise to use a wide\n' - 'Python build in production systems.', - ImportWarning) + "As this can cause hard to debug problems with string processing\n" + "(slicing, regexp, ...) later on, we strongly advise to use a wide\n" + "Python build in production systems.", + ImportWarning, + ) del warnings del six @@ -157,21 +178,41 @@ In particular, this determines how the rich comparison operators for Literal work, eq, __neq__, __lt__, etc. """ -from rdflib.term import ( - URIRef, BNode, Literal, Variable) +from rdflib.term import URIRef, BNode, Literal, Variable from rdflib.namespace import Namespace from rdflib.graph import Dataset, Graph, ConjunctiveGraph -from rdflib.namespace import CSVW, DC, DCAT, DCTERMS, DOAP, \ - FOAF, ODRL2, ORG, OWL, PROF, \ - PROV, QB, RDF, RDFS, SDO, \ - SH, SKOS, SOSA, SSN, TIME, \ - VOID, XMLNS, XSD +from rdflib.namespace import ( + CSVW, + DC, + DCAT, + DCTERMS, + DOAP, + FOAF, + ODRL2, + ORG, + OWL, + PROF, + PROV, + QB, + RDF, + RDFS, + SDO, + SH, + SKOS, + SOSA, + SSN, + TIME, + VOID, + XMLNS, + XSD, +) from rdflib import plugin from rdflib import query + # tedious sop to flake8 assert plugin assert query diff --git a/rdflib/namespace.py b/rdflib/namespace.py index 247d9413..275e025d 100644 --- a/rdflib/namespace.py +++ b/rdflib/namespace.py @@ -73,13 +73,34 @@ The following namespaces are available by directly importing from rdflib: """ __all__ = [ - 'is_ncname', 'split_uri', 'Namespace', - 'ClosedNamespace', 'NamespaceManager', - 'CSVW', 'DC', 'DCAT', 'DCTERMS', 'DOAP', - 'FOAF', 'ODRL2', 'ORG', 'OWL', 'PROF', - 'PROV', 'QB', 'RDF', 'RDFS', 'SDO', - 'SH', 'SKOS', 'SOSA', 'SSN', 'TIME', - 'VOID', 'XMLNS', 'XSD' + "is_ncname", + "split_uri", + "Namespace", + "ClosedNamespace", + "NamespaceManager", + "CSVW", + "DC", + "DCAT", + "DCTERMS", + "DOAP", + "FOAF", + "ODRL2", + "ORG", + "OWL", + "PROF", + "PROV", + "QB", + "RDF", + "RDFS", + "SDO", + "SH", + "SKOS", + "SOSA", + "SSN", + "TIME", + "VOID", + "XMLNS", + "XSD", ] logger = logging.getLogger(__name__) @@ -103,16 +124,16 @@ class Namespace(text_type): try: rt = text_type.__new__(cls, value) except UnicodeDecodeError: - rt = text_type.__new__(cls, value, 'utf-8') + rt = text_type.__new__(cls, value, "utf-8") return rt @property def title(self): - return URIRef(self + 'title') + return URIRef(self + "title") def term(self, name): # need to handle slices explicitly because of __getitem__ override - return URIRef(self + (name if isinstance(name, string_types) else '')) + return URIRef(self + (name if isinstance(name, string_types) else "")) def __getitem__(self, key, default=None): return self.term(key) @@ -144,7 +165,7 @@ class URIPattern(text_type): try: rt = text_type.__new__(cls, value) except UnicodeDecodeError: - rt = text_type.__new__(cls, value, 'utf-8') + rt = text_type.__new__(cls, value, "utf-8") return rt def __mod__(self, *args, **kwargs): @@ -173,9 +194,7 @@ class ClosedNamespace(object): def term(self, name): uri = self.__uris.get(name) if uri is None: - raise KeyError( - "term '{}' not in namespace '{}'".format(name, self.uri) - ) + raise KeyError("term '{}' not in namespace '{}'".format(name, self.uri)) else: return uri @@ -208,31 +227,49 @@ class _RDFNamespace(ClosedNamespace): URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), terms=[ # Syntax Names - "RDF", "Description", "ID", "about", "parseType", - "resource", "li", "nodeID", "datatype", - + "RDF", + "Description", + "ID", + "about", + "parseType", + "resource", + "li", + "nodeID", + "datatype", # RDF Classes - "Seq", "Bag", "Alt", "Statement", "Property", - "List", "PlainLiteral", - + "Seq", + "Bag", + "Alt", + "Statement", + "Property", + "List", + "PlainLiteral", # RDF Properties - "subject", "predicate", "object", "type", - "value", "first", "rest", + "subject", + "predicate", + "object", + "type", + "value", + "first", + "rest", # and _n where n is a non-negative integer - # RDF Resources "nil", - # Added in RDF 1.1 - "XMLLiteral", "HTML", "langString", - + "XMLLiteral", + "HTML", + "langString", # Added in JSON-LD 1.1 - "JSON", "CompoundLiteral", "language", "direction"] + "JSON", + "CompoundLiteral", + "language", + "direction", + ], ) def term(self, name): # Container membership properties - if name.startswith('_'): + if name.startswith("_"): try: i = int(name[1:]) except ValueError: @@ -244,80 +281,220 @@ class _RDFNamespace(ClosedNamespace): return super(_RDFNamespace, self).term(name) -CSVW = Namespace('http://www.w3.org/ns/csvw#') -DC = Namespace('http://purl.org/dc/elements/1.1/') -DCAT = Namespace('http://www.w3.org/ns/dcat#') -DCTERMS = Namespace('http://purl.org/dc/terms/') -DOAP = Namespace('http://usefulinc.com/ns/doap#') +CSVW = Namespace("http://www.w3.org/ns/csvw#") +DC = Namespace("http://purl.org/dc/elements/1.1/") +DCAT = Namespace("http://www.w3.org/ns/dcat#") +DCTERMS = Namespace("http://purl.org/dc/terms/") +DOAP = Namespace("http://usefulinc.com/ns/doap#") FOAF = ClosedNamespace( - uri=URIRef('http://xmlns.com/foaf/0.1/'), + uri=URIRef("http://xmlns.com/foaf/0.1/"), terms=[ # all taken from http://xmlns.com/foaf/spec/ - 'Agent', 'Person', 'name', 'title', 'img', - 'depiction', 'depicts', 'familyName', - 'givenName', 'knows', 'based_near', 'age', 'made', - 'maker', 'primaryTopic', 'primaryTopicOf', 'Project', 'Organization', - 'Group', 'member', 'Document', 'Image', 'nick', - 'mbox', 'homepage', 'weblog', 'openid', 'jabberID', - 'mbox_sha1sum', 'interest', 'topic_interest', 'topic', 'page', - 'workplaceHomepage', 'workInfoHomepage', 'schoolHomepage', 'publications', 'currentProject', - 'pastProject', 'account', 'OnlineAccount', 'accountName', 'accountServiceHomepage', - 'PersonalProfileDocument', 'tipjar', 'sha1', 'thumbnail', 'logo' - ] + "Agent", + "Person", + "name", + "title", + "img", + "depiction", + "depicts", + "familyName", + "givenName", + "knows", + "based_near", + "age", + "made", + "maker", + "primaryTopic", + "primaryTopicOf", + "Project", + "Organization", + "Group", + "member", + "Document", + "Image", + "nick", + "mbox", + "homepage", + "weblog", + "openid", + "jabberID", + "mbox_sha1sum", + "interest", + "topic_interest", + "topic", + "page", + "workplaceHomepage", + "workInfoHomepage", + "schoolHomepage", + "publications", + "currentProject", + "pastProject", + "account", + "OnlineAccount", + "accountName", + "accountServiceHomepage", + "PersonalProfileDocument", + "tipjar", + "sha1", + "thumbnail", + "logo", + ], ) -ODRL2 = Namespace('http://www.w3.org/ns/odrl/2/') -ORG = Namespace('http://www.w3.org/ns/org#') -OWL = Namespace('http://www.w3.org/2002/07/owl#') -PROF = Namespace('http://www.w3.org/ns/dx/prof/') +ODRL2 = Namespace("http://www.w3.org/ns/odrl/2/") +ORG = Namespace("http://www.w3.org/ns/org#") +OWL = Namespace("http://www.w3.org/2002/07/owl#") +PROF = Namespace("http://www.w3.org/ns/dx/prof/") PROV = ClosedNamespace( - uri=URIRef('http://www.w3.org/ns/prov#'), + uri=URIRef("http://www.w3.org/ns/prov#"), terms=[ - 'Entity', 'Activity', 'Agent', 'wasGeneratedBy', 'wasDerivedFrom', - 'wasAttributedTo', 'startedAtTime', 'used', 'wasInformedBy', 'endedAtTime', - 'wasAssociatedWith', 'actedOnBehalfOf', 'Collection', 'EmptyCollection', 'Bundle', - 'Person', 'SoftwareAgent', 'Organization', 'Location', 'alternateOf', - 'specializationOf', 'generatedAtTime', 'hadPrimarySource', 'value', 'wasQuotedFrom', - 'wasRevisionOf', 'invalidatedAtTime', 'wasInvalidatedBy', 'hadMember', 'wasStartedBy', - 'wasEndedBy', 'invalidated', 'influenced', 'atLocation', 'generated', - 'Influence', 'EntityInfluence', 'Usage', 'Start', 'End', - 'Derivation', 'PrimarySource', 'Quotation', 'Revision', 'ActivityInfluence', - 'Generation', 'Communication', 'Invalidation', 'AgentInfluence', - 'Attribution', 'Association', 'Plan', 'Delegation', 'InstantaneousEvent', - 'Role', 'wasInfluencedBy', 'qualifiedInfluence', 'qualifiedGeneration', 'qualifiedDerivation', - 'qualifiedPrimarySource', 'qualifiedQuotation', 'qualifiedRevision', 'qualifiedAttribution', - 'qualifiedInvalidation', 'qualifiedStart', 'qualifiedUsage', 'qualifiedCommunication', 'qualifiedAssociation', - 'qualifiedEnd', 'qualifiedDelegation', 'influencer', 'entity', 'hadUsage', 'hadGeneration', - 'activity', 'agent', 'hadPlan', 'hadActivity', 'atTime', 'hadRole' - ] + "Entity", + "Activity", + "Agent", + "wasGeneratedBy", + "wasDerivedFrom", + "wasAttributedTo", + "startedAtTime", + "used", + "wasInformedBy", + "endedAtTime", + "wasAssociatedWith", + "actedOnBehalfOf", + "Collection", + "EmptyCollection", + "Bundle", + "Person", + "SoftwareAgent", + "Organization", + "Location", + "alternateOf", + "specializationOf", + "generatedAtTime", + "hadPrimarySource", + "value", + "wasQuotedFrom", + "wasRevisionOf", + "invalidatedAtTime", + "wasInvalidatedBy", + "hadMember", + "wasStartedBy", + "wasEndedBy", + "invalidated", + "influenced", + "atLocation", + "generated", + "Influence", + "EntityInfluence", + "Usage", + "Start", + "End", + "Derivation", + "PrimarySource", + "Quotation", + "Revision", + "ActivityInfluence", + "Generation", + "Communication", + "Invalidation", + "AgentInfluence", + "Attribution", + "Association", + "Plan", + "Delegation", + "InstantaneousEvent", + "Role", + "wasInfluencedBy", + "qualifiedInfluence", + "qualifiedGeneration", + "qualifiedDerivation", + "qualifiedPrimarySource", + "qualifiedQuotation", + "qualifiedRevision", + "qualifiedAttribution", + "qualifiedInvalidation", + "qualifiedStart", + "qualifiedUsage", + "qualifiedCommunication", + "qualifiedAssociation", + "qualifiedEnd", + "qualifiedDelegation", + "influencer", + "entity", + "hadUsage", + "hadGeneration", + "activity", + "agent", + "hadPlan", + "hadActivity", + "atTime", + "hadRole", + ], ) -QB = Namespace('http://purl.org/linked-data/cube#') +QB = Namespace("http://purl.org/linked-data/cube#") RDF = _RDFNamespace() RDFS = ClosedNamespace( uri=URIRef("http://www.w3.org/2000/01/rdf-schema#"), terms=[ - "Resource", "Class", "subClassOf", "subPropertyOf", "comment", "label", - "domain", "range", "seeAlso", "isDefinedBy", "Literal", "Container", - "ContainerMembershipProperty", "member", "Datatype"] + "Resource", + "Class", + "subClassOf", + "subPropertyOf", + "comment", + "label", + "domain", + "range", + "seeAlso", + "isDefinedBy", + "Literal", + "Container", + "ContainerMembershipProperty", + "member", + "Datatype", + ], ) -SDO = Namespace('https://schema.org/') -SH = Namespace('http://www.w3.org/ns/shacl#') +SDO = Namespace("https://schema.org/") +SH = Namespace("http://www.w3.org/ns/shacl#") SKOS = ClosedNamespace( - uri=URIRef('http://www.w3.org/2004/02/skos/core#'), + uri=URIRef("http://www.w3.org/2004/02/skos/core#"), terms=[ # all taken from https://www.w3.org/TR/skos-reference/#L1302 - 'Concept', 'ConceptScheme', 'inScheme', 'hasTopConcept', 'topConceptOf', - 'altLabel', 'hiddenLabel', 'prefLabel', 'notation', 'changeNote', - 'definition', 'editorialNote', 'example', 'historyNote', 'note', - 'scopeNote', 'broader', 'broaderTransitive', 'narrower', 'narrowerTransitive', - 'related', 'semanticRelation', 'Collection', 'OrderedCollection', 'member', - 'memberList', 'broadMatch', 'closeMatch', 'exactMatch', 'mappingRelation', - 'narrowMatch', 'relatedMatch' - ] + "Concept", + "ConceptScheme", + "inScheme", + "hasTopConcept", + "topConceptOf", + "altLabel", + "hiddenLabel", + "prefLabel", + "notation", + "changeNote", + "definition", + "editorialNote", + "example", + "historyNote", + "note", + "scopeNote", + "broader", + "broaderTransitive", + "narrower", + "narrowerTransitive", + "related", + "semanticRelation", + "Collection", + "OrderedCollection", + "member", + "memberList", + "broadMatch", + "closeMatch", + "exactMatch", + "mappingRelation", + "narrowMatch", + "relatedMatch", + ], ) -SOSA = Namespace('http://www.w3.org/ns/ssn/') -SSN = Namespace('http://www.w3.org/ns/sosa/') -TIME = Namespace('http://www.w3.org/2006/time#') -VOID = Namespace('http://rdfs.org/ns/void#') +SOSA = Namespace("http://www.w3.org/ns/ssn/") +SSN = Namespace("http://www.w3.org/ns/sosa/") +TIME = Namespace("http://www.w3.org/2006/time#") +VOID = Namespace("http://rdfs.org/ns/void#") XMLNS = Namespace("http://www.w3.org/XML/1998/namespace") XSD = Namespace(_XSD_PFX) @@ -377,6 +554,7 @@ class NamespaceManager(object): def __get_store(self): return self.graph.store + store = property(__get_store) def qname(self, uri): @@ -388,10 +566,10 @@ class NamespaceManager(object): def qname_strict(self, uri): prefix, namespace, name = self.compute_qname_strict(uri) - if prefix == '': + if prefix == "": return name else: - return ':'.join((prefix, name)) + return ":".join((prefix, name)) def normalizeUri(self, rdfTerm): """ @@ -416,13 +594,15 @@ class NamespaceManager(object): return "<%s>" % rdfTerm else: qNameParts = self.compute_qname(rdfTerm) - return ':'.join([qNameParts[0], qNameParts[-1]]) + return ":".join([qNameParts[0], qNameParts[-1]]) def compute_qname(self, uri, generate=True): if not _is_valid_uri(uri): raise ValueError( - '"{}" does not look like a valid URI, cannot serialize this. Did you want to urlencode it?'.format(uri) + '"{}" does not look like a valid URI, cannot serialize this. Did you want to urlencode it?'.format( + uri + ) ) if uri not in self.__cache: @@ -440,7 +620,7 @@ class NamespaceManager(object): pl_namespace = get_longest_namespace(self.__strie[namespace], uri) if pl_namespace is not None: namespace = pl_namespace - name = uri[len(namespace):] + name = uri[len(namespace) :] namespace = URIRef(namespace) prefix = self.store.prefix(namespace) # warning multiple prefixes problem @@ -472,32 +652,38 @@ class NamespaceManager(object): try: namespace, name = split_uri(uri, NAME_START_CATEGORIES) except ValueError as e: - message = ('This graph cannot be serialized to a strict format ' - 'because there is no valid way to shorten {}'.format(uri)) + message = ( + "This graph cannot be serialized to a strict format " + "because there is no valid way to shorten {}".format(uri) + ) raise ValueError(message) # omitted for strict since NCNames cannot be empty - #namespace = URIRef(uri) - #prefix = self.store.prefix(namespace) - #if not prefix: - #raise e + # namespace = URIRef(uri) + # prefix = self.store.prefix(namespace) + # if not prefix: + # raise e if namespace not in self.__strie: insert_strie(self.__strie, self.__trie, namespace) # omitted for strict - #if self.__strie[namespace]: - #pl_namespace = get_longest_namespace(self.__strie[namespace], uri) - #if pl_namespace is not None: - #namespace = pl_namespace - #name = uri[len(namespace):] + # if self.__strie[namespace]: + # pl_namespace = get_longest_namespace(self.__strie[namespace], uri) + # if pl_namespace is not None: + # namespace = pl_namespace + # name = uri[len(namespace):] namespace = URIRef(namespace) - prefix = self.store.prefix(namespace) # warning multiple prefixes problem + prefix = self.store.prefix( + namespace + ) # warning multiple prefixes problem if prefix is None: if not generate: raise KeyError( - "No known prefix for {} and generate=False".format(namespace) + "No known prefix for {} and generate=False".format( + namespace + ) ) num = 1 while 1: @@ -523,7 +709,7 @@ class NamespaceManager(object): namespace = URIRef(text_type(namespace)) # When documenting explain that override only applies in what cases if prefix is None: - prefix = '' + prefix = "" bound_namespace = self.store.namespace(prefix) # Check if the bound_namespace contains a URI # and if so convert it into a URIRef for comparison @@ -582,6 +768,7 @@ class NamespaceManager(object): result = "%s#" % result return URIRef(result) + # From: http://www.w3.org/TR/REC-xml#NT-CombiningChar # # * Name start characters must have one of the categories Ll, Lu, Lo, @@ -617,7 +804,7 @@ class NamespaceManager(object): NAME_START_CATEGORIES = ["Ll", "Lu", "Lo", "Lt", "Nl"] -SPLIT_START_CATEGORIES = NAME_START_CATEGORIES + ['Nd'] +SPLIT_START_CATEGORIES = NAME_START_CATEGORIES + ["Nd"] NAME_CATEGORIES = NAME_START_CATEGORIES + ["Mc", "Me", "Mn", "Lm", "Nd"] ALLOWED_NAME_CHARS = [u"\u00B7", u"\u0387", u"-", u".", u"_", u":"] @@ -636,7 +823,7 @@ def is_ncname(name): for i in range(1, len(name)): c = name[i] if not category(c) in NAME_CATEGORIES: - if c != ':' and c in ALLOWED_NAME_CHARS: + if c != ":" and c in ALLOWED_NAME_CHARS: continue return 0 # if in compatibility area @@ -683,7 +870,9 @@ def insert_trie(trie, value): # aka get_subtrie_or_insert if not multi_check: trie[value] = {} multi_check = True # there can be multiple longer existing prefixes - dict_ = trie.pop(key) # does not break strie since key<->dict_ remains unchanged + dict_ = trie.pop( + key + ) # does not break strie since key<->dict_ remains unchanged trie[value][key] = dict_ if value not in trie: trie[value] = {} -- cgit v1.2.1