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

collective / sphinxcontrib-httpexample / 10411075696

15 Aug 2024 09:52PM UTC coverage: 93.933% (-2.7%) from 96.629%
10411075696

push

github

datakurre
Cleanup

418 of 445 relevant lines covered (93.93%)

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

37

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

51

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

62

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

66

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

70

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

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

87
    return False
15✔
88

89

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