def simplify(specification): try: spec = Specification.parse_obj( unref_spec(json.loads(specification.read()))) spec.components = Components() print(print_spec(Specification.validate(spec))) except (pydantic.ValidationError, json.JSONDecodeError) as e: print(e, file=sys.stderr) exit(1)
def validate(specifications): fail = False for specification in specifications: try: Specification.parse_obj( unref_spec(json.loads(specification.read()))) click.secho(f"[{specification.name}] Specification is correct!", fg="green") except (pydantic.ValidationError, json.JSONDecodeError) as e: click.secho(f"[{specification.name}] {e}", fg="red") fail = True if fail: exit(1)
def make_fish(specification): try: spec = Specification.parse_obj( unref_spec(json.loads(specification.read()))) print(render_fish(spec)) except (pydantic.ValidationError, json.JSONDecodeError) as e: print(e, file=sys.stderr) exit(1)
def do_parse_man(command: str) -> Specification: main_pattern = GroupPattern( type="group", patterns=[make_option(options, description) for options, description in parse(command)], ) return Specification( openautocomplete=OpenAutoComplete(version="1.0"), components=Components(), cli=Cli(name=command, pattern_groups=[main_pattern]), )
def parse_fish_document(fish_completes: str) -> Specification: names = [] options: Dict[str, List[Union[GroupPattern, CommandPattern, ArgumentPattern, OptionPattern]]] = defaultdict(list) for comp in iter_completes(fish_completes): command, x = parse_complete(comp) names += [command] if isinstance(x, OptionPattern): options[""].append(x) cli_obj = Cli( name=names[0], pattern_groups=[GroupPattern(type="group", patterns=options[""])]) return Specification(openautocomplete=OpenAutoComplete(version="1.0"), components=Components(), cli=cli_obj)
def print_spec(spec: Specification) -> str: return spec.json(exclude_unset=True, indent=2)
def jsonschema(): print(Specification.schema_json())