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

collective / sphinxcontrib-httpexample / 7956692684

pending completion
7956692684

push

github

datakurre
ci: Install sphinxcontrib-httpexample in readthedocs

418 of 445 relevant lines covered (93.93%)

0.94 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
4✔
3

4
import os
4✔
5
import pkg_resources
4✔
6

7

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

21

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

27

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

37

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

51

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

62

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

66

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

70

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

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

87
    return False
4✔
88

89

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

© 2025 Coveralls, Inc