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

collective / sphinxcontrib-httpexample / 40aa715e724f8f648cba1d866f125314786d44a8

15 Aug 2024 09:37PM UTC coverage: 96.629% (+2.7%) from 93.933%
40aa715e724f8f648cba1d866f125314786d44a8

push

github

datakurre
WIP

430 of 445 relevant lines covered (96.63%)

1.93 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
2✔
32
          pkg_resources.resource_exists(*spec.split(':', 1))):
33
        return pkg_resources.resource_filename(*spec.split(':', 1))
2✔
34
    else:
35
        return spec
2✔
36

37

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

51

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

62

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

66

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

70

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

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

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

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

87
    return False
2✔
88

89

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

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

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

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

111
    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