pyramid_skosprovider.utils.parse_range_header()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nop 1
dl 0
loc 22
rs 9.8
c 0
b 0
f 0
1
# -*- coding: utf8 -*-
2
'''
3
This module contains a few utility functions.
4
'''
5
6
import re
7
8
import logging
9
log = logging.getLogger(__name__)
10
11
12
class QueryBuilder:
13
14
    def __init__(self, request, postprocess=False):
15
        self.request = request
16
        self.postprocess = postprocess
17
        self.no_result = False
18
        self.mode = self.request.params.get('mode', 'default')
19
        self.language = self.request.params.get(
20
            'language',
21
            self.request.locale_name
22
        )
23
        self.label = self.request.params.get('label', None)
24
25
    def _build_type(self, query):
26
        type = self.request.params.get('type', None)
27
        if type in ['concept', 'collection']:
28
            query['type'] = type
29
        return query
30
31
    def _build_label(self, query):
32
        if self.label not in [None, '*', '']:
33
            if self.mode == 'dijitFilteringSelect' and '*' in self.label:
34
                self.postprocess = True
35
                query['label'] = self.label.replace('*', '')
36
            else:
37
                query['label'] = self.label
38
        return query
39
40
    def _build_collection(self, query):
41
        coll = self.request.params.get('collection', None)
42
        if coll is not None:
43
            query['collection'] = {'id': coll, 'depth': 'all'}
44
        return query
45
46
    def _build_matches(self, query):
47
        match_uri = self.request.params.get('match', None)
48
        if match_uri is not None:
49
            query['matches'] = {'uri': match_uri}
50
            match_type = self.request.params.get('match_type', None)
51
            if match_type is not None:
52
                query['matches']['type'] = match_type
53
        return query
54
55
    def __call__(self):
56
        if self.mode == 'dijitFilteringSelect' and self.label == '':
57
            self.no_result = True
58
        q = {}
59
        q = self._build_type(q)
60
        q = self._build_label(q)
61
        q = self._build_collection(q)
62
        q = self._build_matches(q)
63
        return q
64
65
66
def parse_range_header(range):
67
    '''
68
    Parse a range header as used by the dojo Json Rest store.
69
70
    :param str range: The content of the range header to be parsed.
71
        eg. `items=0-9`
72
    :returns: A dict with keys start, finish and number or `False` if the
73
        range is invalid.
74
    '''
75
    match = re.match('^items=([0-9]+)-([0-9]+)$', range)
76
    if match:
77
        start = int(match.group(1))
78
        finish = int(match.group(2))
79
        if finish < start:
80
            finish = start
81
        return {
82
            'start': start,
83
            'finish': finish,
84
            'number': finish - start + 1
85
        }
86
    else:
87
        return False
88