Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 2b1b3c0

Browse files
Merge pull request #5 from core-api/dump-command
Version 1.0
2 parents f98fa96 + 3b56f86 commit 2b1b3c0

6 files changed

Lines changed: 64 additions & 33 deletions

File tree

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global-exclude __pycache__
2+
global-exclude *.pyc
3+
global-exclude *.pyo

coreapi_cli/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.4"
1+
__version__ = "1.0.0"

coreapi_cli/main.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,31 @@
1010
import sys
1111

1212

13+
def codec_sorting_func(item):
14+
key, codec = item
15+
if key == 'corejson':
16+
return (0, codec.media_type.count('*'), codec.media_type)
17+
18+
if ('decoding' in codec.supports and 'encoding' in codec.supports):
19+
return (1, codec.media_type.count('*'), codec.media_type)
20+
elif ('decoding' in codec.supports):
21+
return (2, codec.media_type.count('*'), codec.media_type)
22+
elif ('encoding' in codec.supports):
23+
return (3, codec.media_type.count('*'), codec.media_type)
24+
elif ('data' in codec.supports):
25+
return (4, codec.media_type.count('*'), codec.media_type)
26+
27+
return (5, codec.media_type.count('*'), codec.media_type)
28+
29+
1330
def get_codecs():
1431
unsorted_codecs = {}
1532
for package in pkg_resources.iter_entry_points(group='coreapi.codecs'):
1633
codec_class = package.load()
1734
unsorted_codecs[package.name] = codec_class()
1835

1936
codecs = collections.OrderedDict()
20-
for name, codec in sorted(unsorted_codecs.items(), key=lambda item: -len(item[1].media_type)):
37+
for name, codec in sorted(unsorted_codecs.items(), key=codec_sorting_func):
2138
codecs[name] = codec
2239

2340
return codecs
@@ -32,7 +49,9 @@ def get_codecs():
3249
bookmarks_path = None
3350

3451
codec_lookup = get_codecs()
35-
codec_keys = [key for key in codec_lookup.keys() if key not in ('json', 'text')]
52+
decoder_formats = [key for key, codec in codec_lookup.items() if ('decoding' in codec.supports)]
53+
encoder_formats = [key for key, codec in codec_lookup.items() if ('encoding' in codec.supports)]
54+
data_formats = [key for key, codec in codec_lookup.items() if ('data' in codec.supports)]
3655

3756

3857
def setup_paths():
@@ -139,7 +158,11 @@ def get_client(decoders=None, debug=False):
139158
callbacks = {}
140159

141160
if decoders is None:
142-
decoders = list(codec_lookup.values())
161+
decoders = [
162+
codec_lookup[key] for key in decoder_formats
163+
] + [
164+
codec_lookup[key] for key in data_formats
165+
]
143166

144167
http_transport = coreapi.transports.HTTPTransport(credentials, headers, **callbacks)
145168
return coreapi.Client(decoders=decoders, transports=[http_transport])
@@ -203,13 +226,13 @@ def client(ctx, version):
203226
@click.command(help='Fetch a document from the given URL.')
204227
@click.argument('url')
205228
@click.option('--debug', '-d', is_flag=True, help='Display the request/response')
206-
@click.option('--format', default=None, type=click.Choice(codec_keys))
229+
@click.option('--format', default=None, type=click.Choice(decoder_formats))
207230
def get(url, debug, format):
208231
if format:
209232
decoders = [codec_lookup[format]]
210233
force_codec = True
211234
else:
212-
decoders = [codec_lookup[key] for key in codec_keys]
235+
decoders = [codec_lookup[key] for key in decoder_formats]
213236
force_codec = False
214237
client = get_client(decoders=decoders, debug=debug)
215238
history = get_history()
@@ -227,7 +250,7 @@ def get(url, debug, format):
227250

228251
@click.command(help='Load a document from disk.')
229252
@click.argument('input_file', type=click.File('rb'))
230-
@click.option('--format', default='corejson', type=click.Choice(codec_keys))
253+
@click.option('--format', default='corejson', type=click.Choice(decoder_formats))
231254
def load(input_file, format):
232255
input_bytes = input_file.read()
233256
input_file.close()
@@ -242,6 +265,19 @@ def load(input_file, format):
242265
set_history(history)
243266

244267

268+
@click.command(help='Dump a document to console.')
269+
@click.option('--format', default='corejson', type=click.Choice(encoder_formats))
270+
def dump(format):
271+
doc = get_document()
272+
if doc is None:
273+
click.echo('No current document. Use `coreapi get` to fetch a document first.')
274+
sys.exit(1)
275+
276+
encoder = codec_lookup[format]
277+
output = encoder.dump(doc)
278+
click.echo(output)
279+
280+
245281
@click.command(help='Clear the active document and other state.\n\nThis includes the current document, history, credentials, headers and bookmarks.')
246282
def clear():
247283
for path in [
@@ -429,7 +465,7 @@ def action(path, params, strings, data, files, action, encoding, transform, debu
429465

430466
@click.command(help='Reload the current document.')
431467
@click.option('--debug', '-d', is_flag=True, help='Display the request/response')
432-
@click.option('--format', default=None, type=click.Choice(codec_keys))
468+
@click.option('--format', default=None, type=click.Choice(decoder_formats))
433469
def reload_document(debug, format):
434470
doc = get_document()
435471
if doc is None:
@@ -440,7 +476,7 @@ def reload_document(debug, format):
440476
decoders = [codec_lookup[format]]
441477
force_codec = True
442478
else:
443-
decoders = [codec_lookup[key] for key in codec_keys]
479+
decoders = [codec_lookup[key] for key in decoder_formats]
444480
force_codec = False
445481

446482
client = get_client(debug=debug, decoders=decoders)
@@ -747,8 +783,12 @@ def codecs():
747783
def codecs_show():
748784
# Note that this omits the data codecs of JSON and Text.
749785
click.echo(click.style('Codecs', bold=True))
750-
for key in codec_keys:
751-
click.echo('%s "%s"' % (key, codec_lookup[key].media_type))
786+
col_1_len = max([len(key) for key in codec_lookup.keys()])
787+
col_2_len = max([len(codec.media_type) for codec in codec_lookup.values()])
788+
fmt = '{key:%d} {media_type:%s} {supports}' % (col_1_len, col_2_len)
789+
for key, codec in codec_lookup.items():
790+
supports = ', '.join(codec.supports)
791+
click.echo(fmt.format(key=key, media_type=codec.media_type, supports=supports))
752792

753793

754794
client.add_command(get)
@@ -757,6 +797,7 @@ def codecs_show():
757797
client.add_command(reload_document, name='reload')
758798
client.add_command(clear)
759799
client.add_command(load)
800+
client.add_command(dump)
760801
client.add_command(describe)
761802

762803
client.add_command(credentials)

requirements-unfrozen.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

requirements.txt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
click==6.6
2-
coreapi==1.31.0
3-
coverage==4.1
4-
flake8==2.6.2
5-
itypes==1.1.0
6-
mccabe==0.5.0
7-
py==1.4.31
8-
pycodestyle==2.0.0
9-
pyflakes==1.2.3
10-
pytest==2.9.2
11-
requests==2.10.0
12-
simplejson==3.8.2
13-
uritemplate==0.6
1+
# Package requirements
2+
coreapi
3+
click
4+
5+
# Testing requirements
6+
coverage
7+
flake8
8+
pytest

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def get_package_data(package):
6262
packages=get_packages('coreapi_cli'),
6363
package_data=get_package_data('coreapi_cli'),
6464
install_requires=[
65-
'coreapi>=1.31.0',
65+
'coreapi>=1.32.0',
6666
'click>=6.0'
6767
],
6868
classifiers=[

0 commit comments

Comments
 (0)