• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

collective / sphinxcontrib-httpexample / 10442703330

18 Aug 2024 06:06PM UTC coverage: 94.03% (-1.9%) from 95.94%
10442703330

push

github

datakurre
Back to development: 1.4

441 of 469 relevant lines covered (94.03%)

14.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.16
/src/sphinxcontrib/httpexample/utils.py
1
# -*- coding: utf-8 -*-
2
from collections import OrderedDict
15✔
3

4
import os
15✔
5
import pkg_resources
15✔
6

7

8
try:
15✔
9
    from urllib import unquote
15✔
10
    from urllib import urlencode
×
11
    from urlparse import parse_qsl
×
12
    from urlparse import ParseResult
×
13
    from urlparse import urlparse
×
14
except ImportError:
15✔
15
    from urllib.parse import parse_qsl
15✔
16
    from urllib.parse import ParseResult
15✔
17
    from urllib.parse import unquote
15✔
18
    from urllib.parse import urlencode
15✔
19
    from urllib.parse import urlparse
15✔
20

21

22
def merge_dicts(a, b):
15✔
23
    c = a.copy()
15✔
24
    c.update(b)
15✔
25
    return c
15✔
26

27

28
def resolve_path(spec, cwd=''):
15✔
29
    if os.path.isfile(os.path.normpath(os.path.join(cwd, spec))):
15✔
30
        return os.path.normpath(os.path.join(cwd, spec))
15✔
31
    elif spec.count(':') and pkg_resources.resource_exists(*spec.split(':', 1)):
15✔
32
        return pkg_resources.resource_filename(*spec.split(':', 1))
15✔
33
    else:
34
        return spec
15✔
35

36

37
def maybe_str(v):
15✔
38
    """Convert any strings to local 'str' instances"""
39
    if isinstance(v, str) and isinstance(v, bytes):
15✔
40
        return v  # Python 2 encoded
×
41
    elif str(type(v)) == "<type 'unicode'>":
15✔
42
        return v.encode('utf-8')  # Python 2 unicode
×
43
    elif isinstance(v, bytes):
15✔
44
        return v.decode('utf-8')  # Python 3 encoded
15✔
45
    elif isinstance(v, str):
15✔
46
        return v  # Python 3 unicode
15✔
47
    else:
48
        return v  # not a string
15✔
49

50

51
def ordered(dict_):
15✔
52
    if isinstance(dict_, dict):
15✔
53
        # http://stackoverflow.com/a/22721724
54
        results = OrderedDict()
15✔
55
        for k, v in sorted(dict_.items()):
15✔
56
            results[k] = ordered(v)
15✔
57
    else:
58
        results = dict_
15✔
59
    return results
15✔
60

61

62
def capitalize(s):
15✔
63
    return '-'.join(map(str.capitalize, s.split('-')))
15✔
64

65

66
def capitalize_keys(d):
15✔
67
    return dict([(capitalize(k), v) for k, v in d.items()])
15✔
68

69

70
def is_json(content_type):
15✔
71
    """Checks if the given content type should be treated as JSON.
72

73
    The primary use cases to be recognized as JSON are
74

75
    - `application/json` mimetype
76
    - `+json` structured syntax suffix
77
    """
78
    parts = {part.strip() for part in content_type.lower().strip().split(';')}
15✔
79
    if 'application/json' in parts:
15✔
80
        return True
15✔
81

82
    for p in parts:
15✔
83
        if p.endswith('+json'):
15✔
84
            return True
15✔
85

86
    return False
15✔
87

88

89
def add_url_params(url, params):
15✔
90
    """Add GET query parameters to provided URL.
91

92
    https://stackoverflow.com/a/25580545/1262843
93

94
    Args:
95
        url (str): target URL
96
        params (list of tuples): query parameters to be added
97

98
    Returns:
99
        new_url (str): updated URL
100
    """
101
    url = unquote(url)
15✔
102
    parsed_url = urlparse(url)
15✔
103
    new_params = parse_qsl(parsed_url.query) + params
15✔
104
    new_params_encoded = urlencode(new_params, doseq=True)
15✔
105
    new_url = ParseResult(
15✔
106
        parsed_url.scheme,
107
        parsed_url.netloc,
108
        parsed_url.path,
109
        parsed_url.params,
110
        new_params_encoded,
111
        parsed_url.fragment,
112
    ).geturl()
113

114
    return new_url
15✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc