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

collective / sphinxcontrib-httpexample / c38a74b5a6c2ba7d2d10a4d28d752d815014026d

18 Aug 2024 12:01PM UTC coverage: 95.94% (+1.9%) from 94.03%
c38a74b5a6c2ba7d2d10a4d28d752d815014026d

Pull #99

github

web-flow
Bump requests from 2.27.1 to 2.32.2 in /nix

Bumps [requests](https://github.com/psf/requests) from 2.27.1 to 2.32.2.
- [Release notes](https://github.com/psf/requests/releases)
- [Changelog](https://github.com/psf/requests/blob/main/HISTORY.md)
- [Commits](https://github.com/psf/requests/compare/v2.27.1...v2.32.2)

---
updated-dependencies:
- dependency-name: requests
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #99: Bump requests from 2.27.1 to 2.32.2 in /nix

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

96.58
/src/sphinxcontrib/httpexample/directives.py
1
# -*- coding: utf-8 -*-
2
from docutils import nodes
2✔
3
from docutils.parsers.rst import directives
2✔
4
from docutils.statemachine import StringList
2✔
5
from sphinx.directives.code import CodeBlock
2✔
6
from sphinxcontrib.httpexample import builders
2✔
7
from sphinxcontrib.httpexample import parsers
2✔
8
from sphinxcontrib.httpexample import utils
2✔
9

10
import os
2✔
11
import re
2✔
12

13

14
AVAILABLE_BUILDERS = {
2✔
15
    'curl': (builders.build_curl_command, 'bash'),
16
    'wget': (builders.build_wget_command, 'bash'),
17
    'httpie': (builders.build_httpie_command, 'bash'),
18
    'requests': (builders.build_requests_command, 'python'),
19
    'python-requests': (builders.build_requests_command, 'python'),
20
    'plone-javascript': (builders.build_plone_javascript_command, 'javascript'),
21
}
22

23
AVAILABLE_FIELDS = ['query']
2✔
24

25

26
def choose_builders(arguments):
2✔
27
    return [
2✔
28
        directives.choice(argument, AVAILABLE_BUILDERS)
29
        for argument in (arguments or [])
30
    ]
31

32

33
class HTTPExample(CodeBlock):
2✔
34

35
    required_arguments = 0
2✔
36
    optional_arguments = len(AVAILABLE_BUILDERS)
2✔
37

38
    option_spec = utils.merge_dicts(
2✔
39
        CodeBlock.option_spec,
40
        {
41
            'request': directives.unchanged,
42
            'response': directives.unchanged,
43
        },
44
    )
45

46
    @staticmethod
2✔
47
    def process_content(content):
48
        if content:
2✔
49
            raw = ('\r\n'.join(content)).encode('utf-8')
2✔
50
            request = parsers.parse_request(raw)
2✔
51
            params, _ = request.extract_fields('query')
2✔
52
            params = [(p[1], p[2]) for p in params]
2✔
53
            new_path = utils.add_url_params(request.path, params)
2✔
54
            content[0] = ' '.join([request.command, new_path, request.request_version])
2✔
55

56
        # split the request and optional response in the content.
57
        # The separator is two empty lines followed by a line starting with
58
        # 'HTTP/' or 'HTTP '
59
        request_content = StringList()
2✔
60
        request_content_no_fields = StringList()
2✔
61
        response_content = None
2✔
62
        emptylines_count = 0
2✔
63
        in_response = False
2✔
64
        is_field = r':({}) (.+): (.+)'.format('|'.join(AVAILABLE_FIELDS))
2✔
65
        for i, line in enumerate(content):
2✔
66
            source = content.source(i)
2✔
67
            if in_response:
2✔
68
                response_content.append(line, source)
2✔
69
            else:
70
                if emptylines_count >= 2 and (
2✔
71
                    line.startswith('HTTP/') or line.startswith('HTTP ')
72
                ):
73
                    in_response = True
2✔
74
                    response_content = StringList()
2✔
75
                    response_content.append(line, source)
2✔
76
                elif line == '':
2✔
77
                    emptylines_count += 1
2✔
78
                else:
79
                    request_content.extend(StringList([''] * emptylines_count, source))
2✔
80
                    request_content.append(line, source)
2✔
81

82
                    if not re.match(is_field, line):
2✔
83
                        request_content_no_fields.extend(
2✔
84
                            StringList([''] * emptylines_count, source)
85
                        )
86
                        request_content_no_fields.append(line, source)
2✔
87

88
                    emptylines_count = 0
2✔
89

90
        return (request_content, request_content_no_fields, response_content)
2✔
91

92
    def run(self):
2✔
93
        if self.content:
2✔
94
            processed = self.process_content(StringList(self.content))
2✔
95
            have_request = bool(processed[1])
2✔
96
            have_response = bool(processed[2])
2✔
97
        else:
98
            have_request = 'request' in self.options
2✔
99
            have_response = 'response' in self.options
2✔
100

101
        # Wrap and render main directive as 'http-example-http'
102
        klass = 'http-example-http'
2✔
103
        container = nodes.container('', classes=[klass])
2✔
104
        container.append(nodes.caption('', 'http'))
2✔
105
        block = HTTPExampleBlock(
2✔
106
            'http:example-block',
107
            ['http'],
108
            self.options,
109
            self.content,
110
            self.lineno,
111
            self.content_offset,
112
            self.block_text,
113
            self.state,
114
            self.state_machine,
115
        )
116
        container.extend(block.run())
2✔
117

118
        # Init result node list
119
        result = [container]
2✔
120

121
        # Append builder responses
122
        if have_request:
2✔
123
            for argument in self.arguments:
2✔
124
                name = argument
2✔
125
                # Setting plone JavaScript tab name
126
                name = 'JavaScript' if name == 'plone-javascript' else name
2✔
127

128
                options = self.options.copy()
2✔
129
                options.pop('name', None)
2✔
130
                options.pop('caption', None)
2✔
131

132
                block = HTTPExampleBlock(
2✔
133
                    'http:example-block',
134
                    [argument],
135
                    options,
136
                    self.content,
137
                    self.lineno,
138
                    self.content_offset,
139
                    self.block_text,
140
                    self.state,
141
                    self.state_machine,
142
                )
143

144
                # Wrap and render main directive as 'http-example-{name}'
145
                klass = 'http-example-{}'.format(name)
2✔
146
                container = nodes.container('', classes=[klass])
2✔
147
                container.append(nodes.caption('', name))
2✔
148
                container.extend(block.run())
2✔
149

150
                # Append to result nodes
151
                result.append(container)
2✔
152

153
        # Append optional response
154
        if have_response:
2✔
155
            options = self.options.copy()
2✔
156
            options.pop('name', None)
2✔
157
            options.pop('caption', None)
2✔
158

159
            block = HTTPExampleBlock(
2✔
160
                'http:example-block',
161
                ['http'],
162
                options,
163
                self.content,
164
                self.lineno,
165
                self.content_offset,
166
                self.block_text,
167
                self.state,
168
                self.state_machine,
169
            )
170

171
            # Wrap and render main directive as 'http-example-response'
172
            klass = 'http-example-response'
2✔
173
            container = nodes.container('', classes=[klass])
2✔
174
            container.append(nodes.caption('', 'response'))
2✔
175
            container.extend(block.run())
2✔
176

177
            # Append to result nodes
178
            result.append(container)
2✔
179

180
        # Final wrap
181
        container_node = nodes.container('', classes=['http-example'])
2✔
182
        container_node.extend(result)
2✔
183

184
        return [container_node]
2✔
185

186

187
class HTTPExampleBlock(CodeBlock):
2✔
188
    required_arguments = 1
2✔
189

190
    option_spec = utils.merge_dicts(
2✔
191
        CodeBlock.option_spec,
192
        {
193
            'request': directives.unchanged,
194
            'response': directives.unchanged,
195
        },
196
    )
197

198
    def read_http_file(self, path):
2✔
199
        cwd = os.path.dirname(self.state.document.current_source)
2✔
200
        request = utils.resolve_path(path, cwd)
2✔
201
        with open(request) as fp:
2✔
202
            return StringList(list(map(str.rstrip, fp.readlines())), request)
2✔
203

204
    def run(self):
2✔
205
        if self.arguments == ['http']:
2✔
206
            if 'request' in self.options:
2✔
207
                self.content = self.read_http_file(self.options['request'])
2✔
208
            else:
209
                self.content = HTTPExample.process_content(self.content)[1]
2✔
210
        elif self.arguments == ['response']:
2✔
211
            if 'response' in self.options:
×
212
                self.content = self.read_http_file(self.options['response'])
×
213
            else:
214
                self.content = HTTPExample.process_content(self.content)[2]
×
215

216
            self.arguments = ['http']
×
217
        else:
218
            if 'request' in self.options:
2✔
219
                request_content_no_fields = self.read_http_file(self.options['request'])
2✔
220
            else:
221
                request_content_no_fields = HTTPExample.process_content(self.content)[1]
2✔
222

223
            raw = ('\r\n'.join(request_content_no_fields)).encode('utf-8')
2✔
224

225
            config = self.env.config
2✔
226
            request = parsers.parse_request(raw, config.httpexample_scheme)
2✔
227
            name = choose_builders(self.arguments)[0]
2✔
228
            builder_, language = AVAILABLE_BUILDERS[name]
2✔
229
            self.arguments = [language]
2✔
230

231
            command = builder_(request)
2✔
232
            self.content = StringList([command], request_content_no_fields.source(0))
2✔
233

234
        return super(HTTPExampleBlock, self).run()
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

© 2025 Coveralls, Inc