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

collective / sphinxcontrib-httpexample / 49258f60741829e90eb81b93aaa95cb9c0a66a10

18 Aug 2024 06:00PM UTC coverage: 95.94% (+1.9%) from 94.03%
49258f60741829e90eb81b93aaa95cb9c0a66a10

push

github

datakurre
docs: Add sphinx-copybutton

449 of 468 relevant lines covered (95.94%)

1.92 hits per line

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

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

4
import os
2✔
5
import pkg_resources
2✔
6

7

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

21

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

27

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

36

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

50

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

61

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

65

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

69

70
def is_json(content_type):
2✔
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(';')}
2✔
79
    if 'application/json' in parts:
2✔
80
        return True
2✔
81

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

86
    return False
2✔
87

88

89
def add_url_params(url, params):
2✔
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)
2✔
102
    parsed_url = urlparse(url)
2✔
103
    new_params = parse_qsl(parsed_url.query) + params
2✔
104
    new_params_encoded = urlencode(new_params, doseq=True)
2✔
105
    new_url = ParseResult(
2✔
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
2✔
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