-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Parse more complex definitions, cli utility * update typeit * Release 0.0.13
- Loading branch information
Showing
8 changed files
with
177 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import argparse | ||
import sys | ||
|
||
from pkg_resources import get_distribution | ||
|
||
from . import check | ||
from ..info import DISTRIBUTION_NAME | ||
|
||
|
||
def main(args=None, in_channel=sys.stdin, out_channel=sys.stdout): | ||
parser = argparse.ArgumentParser(description='OpenAPI Type') | ||
parser.add_argument('-V', '--version', action='version', | ||
version=f'{DISTRIBUTION_NAME} {get_distribution(DISTRIBUTION_NAME).version}') | ||
subparsers = parser.add_subparsers(title='sub-commands', | ||
description='valid sub-commands', | ||
help='additional help', | ||
dest='sub-command') | ||
# make subparsers required (see http://stackoverflow.com/a/23354355/458106) | ||
subparsers.required = True | ||
|
||
# $ <cmd> gen | ||
# --------------------------- | ||
check.setup(subparsers) | ||
|
||
# Parse arguments and config | ||
# -------------------------- | ||
if args is None: | ||
args = sys.argv[1:] | ||
args = parser.parse_args(args) | ||
|
||
# Set up and run | ||
# -------------- | ||
args.run_cmd(args, in_channel=in_channel, out_channel=out_channel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import argparse | ||
import json | ||
import sys | ||
from pathlib import Path | ||
from typing import Mapping | ||
from itertools import islice | ||
|
||
from openapi_type import parse_spec | ||
|
||
|
||
def is_empty_dir(p: Path) -> bool: | ||
return p.is_dir() and not bool(list(islice(p.iterdir(), 1))) | ||
|
||
|
||
def setup(subparsers: argparse._SubParsersAction) -> argparse.ArgumentParser: | ||
sub = subparsers.add_parser('check', help='Check whether a provided schema (JSON, YAML) can be parsed.') | ||
sub.add_argument('-s', '--source', help="Path to a spec (JSON, YAML). " | ||
"If not specified, then the data will be read from stdin.") | ||
sub.set_defaults(run_cmd=main) | ||
return sub | ||
|
||
|
||
def main(args: argparse.Namespace, in_channel=sys.stdin, out_channel=sys.stdout) -> None: | ||
""" $ <cmd-prefix> gen <source> <target> | ||
""" | ||
try: | ||
with Path(args.source).open('r') as f: | ||
python_data = _read_data(f) | ||
except TypeError: | ||
# source is None, read from stdin | ||
python_data = _read_data(in_channel) | ||
|
||
_spec = parse_spec(python_data) | ||
|
||
out_channel.write('Successfully parsed.\n') | ||
|
||
|
||
def _read_data(fd) -> Mapping: | ||
buf = fd.read() # because stdin does not support seek and we want to try both json and yaml parsing | ||
try: | ||
struct = json.loads(buf) | ||
except ValueError: | ||
try: | ||
import yaml | ||
except ImportError: | ||
raise RuntimeError( | ||
"Could not parse data as JSON, and could not locate PyYAML library " | ||
"to try to parse the data as YAML. You can either install PyYAML as a separate " | ||
"dependency, or use the `third_party` extra tag:\n\n" | ||
"$ pip install openapi-client-generator[third_party]" | ||
) | ||
struct = yaml.full_load(buf) | ||
return struct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DISTRIBUTION_NAME = 'openapi-type' | ||
PACKAGE_NAME = 'openapi_type' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Third-party requirements that the library uses in CLI | ||
pyyaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
typeit>=3.9.1.3 | ||
typeit>=3.9.1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters