Completed
Push — master ( 7c157d...b04ec2 )
by Koen
9s
created

is_uri()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
1
# -*- coding: utf-8 -*-
2
'''
3
This module provides utilities for working with :term:`URIS <URI>`.
4
5
.. versionadded:: 0.3.0
6
'''
7
8
from __future__ import unicode_literals
9
10
import abc
11
import rfc3987
12
13
def is_uri(uri):
14
    '''
15
    Check if a string is a valid URI according to rfc3987
16
17
    :param string uri:
18
    :rtype: boolean
19
    '''
20
    if uri is None:
21
        return False
22
    return rfc3987.match(uri, rule='URI')
23
24
25
class UriGenerator(object):
26
    '''
27
    An abstract class for generating URIs.
28
    '''
29
30
    __metaclass__ = abc.ABCMeta
31
32
    @abc.abstractmethod
33
    def generate(self, **kwargs):
34
        '''
35
        Generate a :term:`URI` based on parameters passed.
36
        '''
37
38
39
class UriPatternGenerator(UriGenerator):
40
    '''
41
    Generate a :term:`URI` based on a simple pattern.
42
    '''
43
44
    def __init__(self, pattern):
45
        self.pattern = pattern
46
47
    def generate(self, **kwargs):
48
        '''
49
        Generate a :term:`URI` based on parameters passed.
50
51
        :param id: The id of the concept or collection.
52
        :rtype: string
53
        '''
54
        return self.pattern % kwargs['id']
55
56
57
class DefaultUrnGenerator(UriGenerator):
58
    '''
59
    Generate a :term:`URN` specific to skosprovider.
60
61
    Used for providers that do not implement a specific :class:`UriGenerator`.
62
63
    :param vocabulary_id: An identifier for the vocabulary we're generating
64
        URIs for.
65
    '''
66
67
    pattern = 'urn:x-skosprovider:%s:%s'
68
69
    def __init__(self, vocabulary_id):
70
        self.vocabulary_id = vocabulary_id
71
72
    def generate(self, **kwargs):
73
        '''
74
        Generate a :term:`URI` based on parameters passed.
75
76
        :param id: The id of the concept or collection.
77
        :rtype: string
78
        '''
79
        return (self.pattern % (self.vocabulary_id, kwargs['id'])).lower()
80
81
82
class DefaultConceptSchemeUrnGenerator(UriGenerator):
83
    '''
84
    Generate a :term:`URN` for a conceptscheme specific to skosprovider.
85
86
    Used for generating default :term:`URI <URIS>` for providers that do
87
    not have an explicit conceptscheme.
88
    '''
89
90
    pattern = 'urn:x-skosprovider:%s'
91
92
    def generate(self, **kwargs):
93
        '''
94
        Generate a :term:`URI` based on parameters passed.
95
96
        :param id: The id of the conceptscheme.
97
        :rtype: string
98
        '''
99
        return (self.pattern % (kwargs['id'])).lower()
100
101
102
class TypedUrnGenerator(DefaultUrnGenerator):
103
    '''
104
    Generate a :term:`URN` specific to skosprovider that contains a type.
105
106
    :param vocabulary_id: An identifier for the vocabulary we're generating
107
        URIs for.
108
    '''
109
110
    pattern = 'urn:x-skosprovider:%s:%s:%s'
111
112
    def __init__(self, vocabulary_id):
113
        self.vocabulary_id = vocabulary_id
114
115
    def generate(self, **kwargs):
116
        '''
117
        Generate a :term:`URI` based on parameters passed.
118
119
        :param id: The id of the concept or collection.
120
        :param type: What we're generating a :term:`URI` for: `concept`
121
            or `collection`.
122
        :rtype: string
123
        '''
124
        if kwargs['type'] not in ['concept', 'collection']:
125
            raise ValueError('Type %s is invalid' % kwargs['type'])
126
        return (
127
            self.pattern % (self.vocabulary_id, kwargs['type'], kwargs['id'])
128
        ).lower()
129