Completed
Push — file-actions ( fb4fef...9d04f1 )
by Felipe A.
01:06 queued 11s
created

Headers.genpair()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
1
2
import re
3
4
from werkzeug.http import dump_options_header
5
from werkzeug.datastructures import Headers as BaseHeaders
6
7
8
class Headers(BaseHeaders):
9
    '''
10
    A wrapper around :class:`werkzeug.datastructures.Headers`, allowing
11
    to specify headers with options.
12
    '''
13
    snake_replace = staticmethod(re.compile(r'(^|-)[a-z]').sub)
14
15
    @classmethod
16
    def genpair(cls, key, optkey, values):
17
        '''
18
        Extract value and options from values dict based on given key and
19
        options-key.
20
21
        :param key: value key
22
        :type key: str
23
        :param optkey: options key
24
        :type optkey: str
25
        :param values: value dictionary
26
        :type values: dict
27
        :returns: tuple of (key, value)
28
        :rtype: tuple of str
29
        '''
30
        return (
31
            cls.snake_replace(lambda x: x[0].upper(), key.replace('_', '-')),
32
            dump_options_header(values[key], values.get(optkey, {})),
33
            )
34
35
    def __init__(self, options_suffix='_options', **kwargs):
36
        items = [
37
            self.genpair(key, '%s_%s' % (key, options_suffix), kwargs)
38
            for key in kwargs
39
            if not key.endswith(options_suffix)
40
            ]
41
        return super(Headers, self).__init__(items)
42