summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Ever Hadani <adamhadani@gmail.com>2017-02-06 15:00:57 -0800
committerAdam Ever Hadani <adamhadani@gmail.com>2017-02-06 15:00:57 -0800
commit279e1d1f8792f41de523da2445e0665626b4be09 (patch)
tree40953ea368a8fd6084028782c01e1c1db96a5a43
parent776b90be0f79fd9b424dd9718b732c96b37b9f2f (diff)
downloadrdflib-279e1d1f8792f41de523da2445e0665626b4be09.tar.gz
make namespace module flake8-compliant, change exceptions in that module to specific ones
-rw-r--r--rdflib/namespace.py65
-rwxr-xr-xrun_tests.py2
2 files changed, 35 insertions, 32 deletions
diff --git a/rdflib/namespace.py b/rdflib/namespace.py
index b89e3100..b954c9eb 100644
--- a/rdflib/namespace.py
+++ b/rdflib/namespace.py
@@ -2,6 +2,20 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
+import logging
+
+import os
+from unicodedata import category
+
+from six import string_types
+from six import text_type
+
+from six.moves.urllib.request import pathname2url
+from six.moves.urllib.parse import urldefrag
+from six.moves.urllib.parse import urljoin
+
+from rdflib.term import URIRef, Variable, _XSD_PFX, _is_valid_uri
+
__doc__ = """
===================
Namespace Utilities
@@ -58,26 +72,14 @@ The following namespaces are available by directly importing from rdflib:
"""
-import logging
-logger = logging.getLogger(__name__)
-
-import os
-
-from six import string_types
-from six import text_type
-
-from six.moves.urllib.request import pathname2url
-from six.moves.urllib.parse import urldefrag
-from six.moves.urllib.parse import urljoin
-
-from rdflib.term import URIRef, Variable, _XSD_PFX, _is_valid_uri
-
__all__ = [
'is_ncname', 'split_uri', 'Namespace',
'ClosedNamespace', 'NamespaceManager',
'XMLNS', 'RDF', 'RDFS', 'XSD', 'OWL',
'SKOS', 'DOAP', 'FOAF', 'DC', 'DCTERMS', 'VOID']
+logger = logging.getLogger(__name__)
+
class Namespace(text_type):
@@ -93,7 +95,6 @@ class Namespace(text_type):
"""
-
def __new__(cls, value):
try:
rt = text_type.__new__(cls, value)
@@ -101,7 +102,6 @@ class Namespace(text_type):
rt = text_type.__new__(cls, value, 'utf-8')
return rt
-
@property
def title(self):
return URIRef(self + 'title')
@@ -120,7 +120,7 @@ class Namespace(text_type):
return self.term(name)
def __repr__(self):
- return "Namespace(%s)"%text_type.__repr__(self)
+ return "Namespace(%s)" % text_type.__repr__(self)
class URIPattern(text_type):
@@ -150,8 +150,7 @@ class URIPattern(text_type):
return URIRef(text_type.format(self, *args, **kwargs))
def __repr__(self):
- return "URIPattern(%r)"%text_type.__repr__(self)
-
+ return "URIPattern(%r)" % text_type.__repr__(self)
class ClosedNamespace(object):
@@ -170,8 +169,9 @@ class ClosedNamespace(object):
def term(self, name):
uri = self.__uris.get(name)
if uri is None:
- raise Exception(
- "term '%s' not in namespace '%s'" % (name, self.uri))
+ raise KeyError(
+ "term '{}' not in namespace '{}'".format(name, self.uri)
+ )
else:
return uri
@@ -226,8 +226,10 @@ class _RDFNamespace(ClosedNamespace):
except ValueError:
return super(_RDFNamespace, self).term(name)
+
RDF = _RDFNamespace()
+
RDFS = ClosedNamespace(
uri=URIRef("http://www.w3.org/2000/01/rdf-schema#"),
terms=[
@@ -248,7 +250,6 @@ DCTERMS = Namespace('http://purl.org/dc/terms/')
VOID = Namespace('http://rdfs.org/ns/void#')
-
class NamespaceManager(object):
"""
@@ -329,17 +330,19 @@ class NamespaceManager(object):
def compute_qname(self, uri, generate=True):
if not _is_valid_uri(uri):
- raise Exception('"%s" does not look like a valid URI, I cannot serialize this. Perhaps you wanted to urlencode it?'%uri)
-
+ raise ValueError(
+ '"{}" does not look like a valid URI, cannot serialize this. Did you want to urlencode it?'.format(uri)
+ )
- if not uri in self.__cache:
+ if uri not in self.__cache:
namespace, name = split_uri(uri)
namespace = URIRef(namespace)
prefix = self.store.prefix(namespace)
if prefix is None:
if not generate:
- raise Exception(
- "No known prefix for %s and generate=False")
+ raise KeyError(
+ "No known prefix for {} and generate=False".format(namespace)
+ )
num = 1
while 1:
prefix = "ns%s" % num
@@ -403,8 +406,7 @@ class NamespaceManager(object):
elif bound_prefix == prefix:
pass # already bound
else:
- if override or bound_prefix.startswith("_"): # or a generated
- # prefix
+ if override or bound_prefix.startswith("_"): # or a generated prefix
self.store.bind(prefix, namespace)
def namespaces(self):
@@ -455,12 +457,12 @@ class NamespaceManager(object):
#
# * Characters '-' and '.' are allowed as name characters.
-from unicodedata import category
NAME_START_CATEGORIES = ["Ll", "Lu", "Lo", "Lt", "Nl"]
NAME_CATEGORIES = NAME_START_CATEGORIES + ["Mc", "Me", "Mn", "Lm", "Nd"]
ALLOWED_NAME_CHARS = [u"\u00B7", u"\u0387", u"-", u".", u"_"]
+
# http://www.w3.org/TR/REC-xml-names/#NT-NCName
# [4] NCName ::= (Letter | '_') (NCNameChar)* /* An XML Name, minus
# the ":" */
@@ -485,6 +487,7 @@ def is_ncname(name):
else:
return 0
+
XMLNS = "http://www.w3.org/XML/1998/namespace"
@@ -505,4 +508,4 @@ def split_uri(uri):
ln = uri[j:]
return (ns, ln)
break
- raise Exception("Can't split '%s'" % uri)
+ raise ValueError("Can't split '{}'".format(uri))
diff --git a/run_tests.py b/run_tests.py
index ea380421..77bc9fec 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -8,7 +8,7 @@ This test runner uses Nose for test discovery and running. It uses the argument
spec of Nose, but with some options pre-set. To begin with, make sure you have
Nose installed, e.g.:
- $ sudo easy_install nose
+ $ pip nose doctest-ignore-unicode
For daily test runs, use: