Skip to content
This repository was archived by the owner on Feb 25, 2022. It is now read-only.

Commit c30e48b

Browse files
authored
Blackify (#648)
* Blackify * Add black to requirements * Upgrade requirements * Ues xenial distribution
1 parent ac3e976 commit c30e48b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1988
-1936
lines changed

.travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
sudo: required
2+
dist: xenial
13
language: python
24

35
cache: pip
46

57
python:
68
- "3.6"
7-
- "3.7-dev"
9+
- "3.7"
810

911
install:
10-
- pip install -r requirements.txt
12+
- pip install -U -r requirements.txt
1113

1214
script:
1315
- scripts/lint

apistar/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
from apistar.core import docs, parse, validate
1212
from apistar.document import Document, Field, Link, Section
1313

14-
__version__ = '0.6.0'
14+
__version__ = "0.6.0"
1515
__all__ = [
16-
'Client', 'Document', 'Section', 'Link', 'Field', 'cli', 'docs', 'parse', 'validate'
16+
"Client",
17+
"Document",
18+
"Section",
19+
"Link",
20+
"Field",
21+
"cli",
22+
"docs",
23+
"parse",
24+
"validate",
1725
]

apistar/cli.py

+79-102
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,23 @@
1010
import apistar
1111
from apistar.client import Client
1212
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
1614

1715

1816
def _encoding_from_filename(filename):
1917
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)
2519

2620

2721
def _echo_error(exc, content, verbose=False):
2822
if verbose:
2923
# Verbose output style.
3024
lines = content.splitlines()
3125
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 += "^ "
3428
error_str += message.text
35-
error_str = click.style(error_str, fg='red')
29+
error_str = click.style(error_str, fg="red")
3630
lines.insert(message.position.line_no, error_str)
3731
for line in lines:
3832
click.echo(line)
@@ -42,20 +36,20 @@ def _echo_error(exc, content, verbose=False):
4236
# Compact output style.
4337
for message in exc.messages:
4438
pos = message.position
45-
if message.code == 'required':
39+
if message.code == "required":
4640
index = message.index[:-1]
4741
else:
4842
index = message.index
4943
if index:
50-
fmt = '* %s (At %s, line %d, column %d.)'
44+
fmt = "* %s (At %s, line %d, column %d.)"
5145
output = fmt % (message.text, index, pos.line_no, pos.column_no)
5246
click.echo(output)
5347
else:
54-
fmt = '* %s (At line %d, column %d.)'
48+
fmt = "* %s (At line %d, column %d.)"
5549
output = fmt % (message.text, pos.line_no, pos.column_no)
5650
click.echo(output)
5751

58-
click.echo(click.style('✘ ', fg='red') + exc.summary)
52+
click.echo(click.style("✘ ", fg="red") + exc.summary)
5953

6054

6155
def _copy_tree(src, dst, verbose=False):
@@ -74,18 +68,18 @@ def _copy_tree(src, dst, verbose=False):
7468

7569

7670
def _load_config(options, verbose=False):
77-
if not os.path.exists('apistar.yml'):
71+
if not os.path.exists("apistar.yml"):
7872
# 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:
8074
raise click.UsageError('Missing option "--path".')
8175
config = options
8276

8377
else:
84-
with open('apistar.yml', 'rb') as config_file:
78+
with open("apistar.yml", "rb") as config_file:
8579
content = config_file.read()
8680

8781
try:
88-
config = apistar.validate(content, format='config', encoding="yaml")
82+
config = apistar.validate(content, format="config", encoding="yaml")
8983
except (ParseError, ValidationError) as exc:
9084
click.echo('Errors in configuration file "apistar.yml":')
9185
_echo_error(exc, content, verbose=verbose)
@@ -100,20 +94,20 @@ def _load_config(options, verbose=False):
10094
if value is not None:
10195
config[section][key] = value
10296

103-
path = config['schema']['path']
97+
path = config["schema"]["path"]
10498
if not os.path.exists(path):
10599
raise click.UsageError('Schema file "%s" not found.' % path)
106100

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)
109103

110104
return config
111105

112106

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"])
117111

118112

119113
@click.group()
@@ -122,25 +116,19 @@ def cli():
122116

123117

124118
@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)
129123
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}}
137125
config = _load_config(options, verbose=verbose)
138126

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"]
142130

143-
with open(path, 'rb') as schema_file:
131+
with open(path, "rb") as schema_file:
144132
content = schema_file.read()
145133

146134
try:
@@ -150,56 +138,51 @@ def validate(path, format, encoding, verbose):
150138
sys.exit(1)
151139

152140
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.",
159147
}[format]
160-
click.echo(click.style('✓ ', fg='green') + success_summary)
148+
click.echo(click.style("✓ ", fg="green") + success_summary)
161149

162150

163151
@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)
171159
def docs(path, format, encoding, output_dir, theme, serve, verbose):
172160
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},
182163
}
183164
config = _load_config(options, verbose=verbose)
184165

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"]
190171

191172
if output_dir is None:
192-
output_dir = 'build'
173+
output_dir = "build"
193174
if theme is None:
194-
theme = 'apistar'
175+
theme = "apistar"
195176

196177
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:
199180
content = schema_file.read()
200181

201182
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+
)
203186
except (ParseError, ValidationError) as exc:
204187
_echo_error(exc, content, verbose=verbose)
205188
sys.exit(1)
@@ -208,10 +191,10 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
208191
os.makedirs(output_dir)
209192

210193
# 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")
212195
if verbose:
213196
click.echo(output_path)
214-
output_file = open(output_path, 'w')
197+
output_file = open(output_path, "w")
215198
output_file.write(index_html)
216199
output_file.close()
217200

@@ -223,7 +206,7 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
223206

224207
# Write static files to the docs.
225208
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")
227210
_copy_tree(static_dir, output_dir, verbose=verbose)
228211

229212
# All done.
@@ -234,39 +217,33 @@ def docs(path, format, encoding, output_dir, theme, serve, verbose):
234217
socketserver.TCPServer.allow_reuse_address = True
235218
with socketserver.TCPServer(addr, handler) as httpd:
236219
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)
238221
httpd.serve_forever()
239222
else:
240223
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))
242225

243226

244227
@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)
251234
@click.pass_context
252235
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}}
260237
config = _load_config(options, verbose=verbose)
261238

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"]
265242

266-
with open(path, 'rb') as schema_file:
243+
with open(path, "rb") as schema_file:
267244
schema = schema_file.read()
268245

269-
params = [param.partition('=') for param in params]
246+
params = [param.partition("=") for param in params]
270247
params = dict([(key, value) for key, sep, value in params])
271248

272249
session = ctx.obj
@@ -284,18 +261,18 @@ def request(ctx, operation, params, path, format, encoding, verbose):
284261
result = client.request(operation, **params)
285262
except ClientError as exc:
286263
for message in exc.messages:
287-
if message.code == 'invalid_property':
264+
if message.code == "invalid_property":
288265
text = '* Invalid parameter "%s".' % message.index[0]
289-
elif message.code == 'required':
266+
elif message.code == "required":
290267
text = '* Missing required parameter "%s".' % message.index[0]
291268
else:
292-
text = '* %s' % message.text
269+
text = "* %s" % message.text
293270
click.echo(text)
294-
click.echo(click.style('✘ ', fg='red') + 'Client error')
271+
click.echo(click.style("✘ ", fg="red") + "Client error")
295272
sys.exit(1)
296273
except ErrorResponse as exc:
297274
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)
299276
sys.exit(1)
300277
click.echo(json.dumps(result, indent=4))
301278

apistar/client/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
from apistar.client.client import Client
22

3-
__all__ = [
4-
'Client'
5-
]
3+
__all__ = ["Client"]

0 commit comments

Comments
 (0)