10
10
import apistar
11
11
from apistar .client import Client
12
12
from apistar .client .debug import DebugSession
13
- from apistar .exceptions import (
14
- ClientError , ErrorResponse , ParseError , ValidationError
15
- )
13
+ from apistar .exceptions import ClientError , ErrorResponse , ParseError , ValidationError
16
14
17
15
18
16
def _encoding_from_filename (filename ):
19
17
base , extension = os .path .splitext (filename )
20
- return {
21
- '.json' : 'json' ,
22
- '.yml' : 'yaml' ,
23
- '.yaml' : 'yaml'
24
- }.get (extension )
18
+ return {".json" : "json" , ".yml" : "yaml" , ".yaml" : "yaml" }.get (extension )
25
19
26
20
27
21
def _echo_error (exc , content , verbose = False ):
28
22
if verbose :
29
23
# Verbose output style.
30
24
lines = content .splitlines ()
31
25
for message in reversed (exc .messages ):
32
- error_str = ' ' * (message .position .column_no - 1 )
33
- error_str += '^ '
26
+ error_str = " " * (message .position .column_no - 1 )
27
+ error_str += "^ "
34
28
error_str += message .text
35
- error_str = click .style (error_str , fg = ' red' )
29
+ error_str = click .style (error_str , fg = " red" )
36
30
lines .insert (message .position .line_no , error_str )
37
31
for line in lines :
38
32
click .echo (line )
@@ -42,20 +36,20 @@ def _echo_error(exc, content, verbose=False):
42
36
# Compact output style.
43
37
for message in exc .messages :
44
38
pos = message .position
45
- if message .code == ' required' :
39
+ if message .code == " required" :
46
40
index = message .index [:- 1 ]
47
41
else :
48
42
index = message .index
49
43
if index :
50
- fmt = ' * %s (At %s, line %d, column %d.)'
44
+ fmt = " * %s (At %s, line %d, column %d.)"
51
45
output = fmt % (message .text , index , pos .line_no , pos .column_no )
52
46
click .echo (output )
53
47
else :
54
- fmt = ' * %s (At line %d, column %d.)'
48
+ fmt = " * %s (At line %d, column %d.)"
55
49
output = fmt % (message .text , pos .line_no , pos .column_no )
56
50
click .echo (output )
57
51
58
- click .echo (click .style ('✘ ' , fg = ' red' ) + exc .summary )
52
+ click .echo (click .style ("✘ " , fg = " red" ) + exc .summary )
59
53
60
54
61
55
def _copy_tree (src , dst , verbose = False ):
@@ -74,18 +68,18 @@ def _copy_tree(src, dst, verbose=False):
74
68
75
69
76
70
def _load_config (options , verbose = False ):
77
- if not os .path .exists (' apistar.yml' ):
71
+ if not os .path .exists (" apistar.yml" ):
78
72
# If the config file is not used, then --path is required.
79
- if options [' schema' ][ ' path' ] is None :
73
+ if options [" schema" ][ " path" ] is None :
80
74
raise click .UsageError ('Missing option "--path".' )
81
75
config = options
82
76
83
77
else :
84
- with open (' apistar.yml' , 'rb' ) as config_file :
78
+ with open (" apistar.yml" , "rb" ) as config_file :
85
79
content = config_file .read ()
86
80
87
81
try :
88
- config = apistar .validate (content , format = ' config' , encoding = "yaml" )
82
+ config = apistar .validate (content , format = " config" , encoding = "yaml" )
89
83
except (ParseError , ValidationError ) as exc :
90
84
click .echo ('Errors in configuration file "apistar.yml":' )
91
85
_echo_error (exc , content , verbose = verbose )
@@ -100,20 +94,20 @@ def _load_config(options, verbose=False):
100
94
if value is not None :
101
95
config [section ][key ] = value
102
96
103
- path = config [' schema' ][ ' path' ]
97
+ path = config [" schema" ][ " path" ]
104
98
if not os .path .exists (path ):
105
99
raise click .UsageError ('Schema file "%s" not found.' % path )
106
100
107
- if config [' schema' ][ ' encoding' ] is None :
108
- config [' schema' ][ ' encoding' ] = _encoding_from_filename (path )
101
+ if config [" schema" ][ " encoding" ] is None :
102
+ config [" schema" ][ " encoding" ] = _encoding_from_filename (path )
109
103
110
104
return config
111
105
112
106
113
- FORMAT_SCHEMA_CHOICES = click .Choice ([' openapi' , ' swagger' ])
114
- FORMAT_ALL_CHOICES = click .Choice ([' config' , ' jsonschema' , ' openapi' , ' swagger' ])
115
- ENCODING_CHOICES = click .Choice ([' json' , ' yaml' ])
116
- THEME_CHOICES = click .Choice ([' apistar' , ' redoc' , ' swaggerui' ])
107
+ FORMAT_SCHEMA_CHOICES = click .Choice ([" openapi" , " swagger" ])
108
+ FORMAT_ALL_CHOICES = click .Choice ([" config" , " jsonschema" , " openapi" , " swagger" ])
109
+ ENCODING_CHOICES = click .Choice ([" json" , " yaml" ])
110
+ THEME_CHOICES = click .Choice ([" apistar" , " redoc" , " swaggerui" ])
117
111
118
112
119
113
@click .group ()
@@ -122,25 +116,19 @@ def cli():
122
116
123
117
124
118
@click .command ()
125
- @click .option (' --path' , type = click .Path (exists = True , dir_okay = False ))
126
- @click .option (' --format' , type = FORMAT_ALL_CHOICES )
127
- @click .option (' --encoding' , type = ENCODING_CHOICES )
128
- @click .option (' --verbose' , '-v' , is_flag = True , default = False )
119
+ @click .option (" --path" , type = click .Path (exists = True , dir_okay = False ))
120
+ @click .option (" --format" , type = FORMAT_ALL_CHOICES )
121
+ @click .option (" --encoding" , type = ENCODING_CHOICES )
122
+ @click .option (" --verbose" , "-v" , is_flag = True , default = False )
129
123
def validate (path , format , encoding , verbose ):
130
- options = {
131
- 'schema' : {
132
- 'path' : path ,
133
- 'format' : format ,
134
- 'encoding' : encoding
135
- }
136
- }
124
+ options = {"schema" : {"path" : path , "format" : format , "encoding" : encoding }}
137
125
config = _load_config (options , verbose = verbose )
138
126
139
- path = config [' schema' ][ ' path' ]
140
- format = config [' schema' ][ ' format' ]
141
- encoding = config [' schema' ][ ' encoding' ]
127
+ path = config [" schema" ][ " path" ]
128
+ format = config [" schema" ][ " format" ]
129
+ encoding = config [" schema" ][ " encoding" ]
142
130
143
- with open (path , 'rb' ) as schema_file :
131
+ with open (path , "rb" ) as schema_file :
144
132
content = schema_file .read ()
145
133
146
134
try :
@@ -150,56 +138,51 @@ def validate(path, format, encoding, verbose):
150
138
sys .exit (1 )
151
139
152
140
success_summary = {
153
- ' json' : ' Valid JSON' ,
154
- ' yaml' : ' Valid YAML' ,
155
- ' config' : ' Valid APIStar config.' ,
156
- ' jsonschema' : ' Valid JSONSchema document.' ,
157
- ' openapi' : ' Valid OpenAPI schema.' ,
158
- ' swagger' : ' Valid Swagger schema.' ,
141
+ " json" : " Valid JSON" ,
142
+ " yaml" : " Valid YAML" ,
143
+ " config" : " Valid APIStar config." ,
144
+ " jsonschema" : " Valid JSONSchema document." ,
145
+ " openapi" : " Valid OpenAPI schema." ,
146
+ " swagger" : " Valid Swagger schema." ,
159
147
}[format ]
160
- click .echo (click .style ('✓ ' , fg = ' green' ) + success_summary )
148
+ click .echo (click .style ("✓ " , fg = " green" ) + success_summary )
161
149
162
150
163
151
@click .command ()
164
- @click .option (' --path' , type = click .Path (exists = True , dir_okay = False ))
165
- @click .option (' --format' , type = FORMAT_SCHEMA_CHOICES )
166
- @click .option (' --encoding' , type = ENCODING_CHOICES )
167
- @click .option (' --output-dir' , type = click .Path ())
168
- @click .option (' --theme' , type = THEME_CHOICES )
169
- @click .option (' --serve' , is_flag = True , default = False )
170
- @click .option (' --verbose' , '-v' , is_flag = True , default = False )
152
+ @click .option (" --path" , type = click .Path (exists = True , dir_okay = False ))
153
+ @click .option (" --format" , type = FORMAT_SCHEMA_CHOICES )
154
+ @click .option (" --encoding" , type = ENCODING_CHOICES )
155
+ @click .option (" --output-dir" , type = click .Path ())
156
+ @click .option (" --theme" , type = THEME_CHOICES )
157
+ @click .option (" --serve" , is_flag = True , default = False )
158
+ @click .option (" --verbose" , "-v" , is_flag = True , default = False )
171
159
def docs (path , format , encoding , output_dir , theme , serve , verbose ):
172
160
options = {
173
- 'schema' : {
174
- 'path' : path ,
175
- 'format' : format ,
176
- 'encoding' : encoding ,
177
- },
178
- 'docs' : {
179
- 'output_dir' : output_dir ,
180
- 'theme' : theme ,
181
- }
161
+ "schema" : {"path" : path , "format" : format , "encoding" : encoding },
162
+ "docs" : {"output_dir" : output_dir , "theme" : theme },
182
163
}
183
164
config = _load_config (options , verbose = verbose )
184
165
185
- path = config [' schema' ][ ' path' ]
186
- format = config [' schema' ][ ' format' ]
187
- encoding = config [' schema' ][ ' encoding' ]
188
- output_dir = config [' docs' ][ ' output_dir' ]
189
- theme = config [' docs' ][ ' theme' ]
166
+ path = config [" schema" ][ " path" ]
167
+ format = config [" schema" ][ " format" ]
168
+ encoding = config [" schema" ][ " encoding" ]
169
+ output_dir = config [" docs" ][ " output_dir" ]
170
+ theme = config [" docs" ][ " theme" ]
190
171
191
172
if output_dir is None :
192
- output_dir = ' build'
173
+ output_dir = " build"
193
174
if theme is None :
194
- theme = ' apistar'
175
+ theme = " apistar"
195
176
196
177
schema_filename = os .path .basename (path )
197
- schema_url = '/' + schema_filename
198
- with open (path , 'rb' ) as schema_file :
178
+ schema_url = "/" + schema_filename
179
+ with open (path , "rb" ) as schema_file :
199
180
content = schema_file .read ()
200
181
201
182
try :
202
- index_html = apistar .docs (content , format = format , encoding = encoding , schema_url = schema_url )
183
+ index_html = apistar .docs (
184
+ content , format = format , encoding = encoding , schema_url = schema_url
185
+ )
203
186
except (ParseError , ValidationError ) as exc :
204
187
_echo_error (exc , content , verbose = verbose )
205
188
sys .exit (1 )
@@ -208,10 +191,10 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
208
191
os .makedirs (output_dir )
209
192
210
193
# Write 'index.html' to the docs.
211
- output_path = os .path .join (output_dir , ' index.html' )
194
+ output_path = os .path .join (output_dir , " index.html" )
212
195
if verbose :
213
196
click .echo (output_path )
214
- output_file = open (output_path , 'w' )
197
+ output_file = open (output_path , "w" )
215
198
output_file .write (index_html )
216
199
output_file .close ()
217
200
@@ -223,7 +206,7 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
223
206
224
207
# Write static files to the docs.
225
208
package_dir = os .path .dirname (apistar .__file__ )
226
- static_dir = os .path .join (package_dir , ' themes' , theme , ' static' )
209
+ static_dir = os .path .join (package_dir , " themes" , theme , " static" )
227
210
_copy_tree (static_dir , output_dir , verbose = verbose )
228
211
229
212
# All done.
@@ -234,39 +217,33 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
234
217
socketserver .TCPServer .allow_reuse_address = True
235
218
with socketserver .TCPServer (addr , handler ) as httpd :
236
219
msg = 'Documentation available at "http://127.0.0.1:8000/" (Ctrl+C to quit)'
237
- click .echo (click .style ('✓ ' , fg = ' green' ) + msg )
220
+ click .echo (click .style ("✓ " , fg = " green" ) + msg )
238
221
httpd .serve_forever ()
239
222
else :
240
223
msg = 'Documentation built at "%s".'
241
- click .echo (click .style ('✓ ' , fg = ' green' ) + (msg % output_path ))
224
+ click .echo (click .style ("✓ " , fg = " green" ) + (msg % output_path ))
242
225
243
226
244
227
@click .command ()
245
- @click .argument (' operation' )
246
- @click .argument (' params' , nargs = - 1 )
247
- @click .option (' --path' , type = click .Path (exists = True , dir_okay = False ))
248
- @click .option (' --format' , type = FORMAT_SCHEMA_CHOICES )
249
- @click .option (' --encoding' , type = ENCODING_CHOICES )
250
- @click .option (' --verbose' , '-v' , is_flag = True , default = False )
228
+ @click .argument (" operation" )
229
+ @click .argument (" params" , nargs = - 1 )
230
+ @click .option (" --path" , type = click .Path (exists = True , dir_okay = False ))
231
+ @click .option (" --format" , type = FORMAT_SCHEMA_CHOICES )
232
+ @click .option (" --encoding" , type = ENCODING_CHOICES )
233
+ @click .option (" --verbose" , "-v" , is_flag = True , default = False )
251
234
@click .pass_context
252
235
def request (ctx , operation , params , path , format , encoding , verbose ):
253
- options = {
254
- 'schema' : {
255
- 'path' : path ,
256
- 'format' : format ,
257
- 'encoding' : encoding
258
- }
259
- }
236
+ options = {"schema" : {"path" : path , "format" : format , "encoding" : encoding }}
260
237
config = _load_config (options , verbose = verbose )
261
238
262
- path = config [' schema' ][ ' path' ]
263
- format = config [' schema' ][ ' format' ]
264
- encoding = config [' schema' ][ ' encoding' ]
239
+ path = config [" schema" ][ " path" ]
240
+ format = config [" schema" ][ " format" ]
241
+ encoding = config [" schema" ][ " encoding" ]
265
242
266
- with open (path , 'rb' ) as schema_file :
243
+ with open (path , "rb" ) as schema_file :
267
244
schema = schema_file .read ()
268
245
269
- params = [param .partition ('=' ) for param in params ]
246
+ params = [param .partition ("=" ) for param in params ]
270
247
params = dict ([(key , value ) for key , sep , value in params ])
271
248
272
249
session = ctx .obj
@@ -284,18 +261,18 @@ def request(ctx, operation, params, path, format, encoding, verbose):
284
261
result = client .request (operation , ** params )
285
262
except ClientError as exc :
286
263
for message in exc .messages :
287
- if message .code == ' invalid_property' :
264
+ if message .code == " invalid_property" :
288
265
text = '* Invalid parameter "%s".' % message .index [0 ]
289
- elif message .code == ' required' :
266
+ elif message .code == " required" :
290
267
text = '* Missing required parameter "%s".' % message .index [0 ]
291
268
else :
292
- text = ' * %s' % message .text
269
+ text = " * %s" % message .text
293
270
click .echo (text )
294
- click .echo (click .style ('✘ ' , fg = ' red' ) + ' Client error' )
271
+ click .echo (click .style ("✘ " , fg = " red" ) + " Client error" )
295
272
sys .exit (1 )
296
273
except ErrorResponse as exc :
297
274
click .echo (json .dumps (exc .content , indent = 4 ))
298
- click .echo (click .style ('✘ ' , fg = ' red' ) + exc .title )
275
+ click .echo (click .style ("✘ " , fg = " red" ) + exc .title )
299
276
sys .exit (1 )
300
277
click .echo (json .dumps (result , indent = 4 ))
301
278
0 commit comments