def compile(ctx, url_or_path, output_file): """ Compile the given spec, resolving references if required. Resolves references and uses backends exactly as in the "validate" command, but only works on single URLs. If an output file name is given, output is written there, otherwise it is written to the terminal. """ # Create parser to use parser, name = __parser_for_url(url_or_path, ctx.obj['resolve'], ctx.obj['backend'], ctx.obj['strict'], ctx.obj['encoding']) # Try parsing __validate(parser, name) # Write output from prance.util import formats contents = formats.serialize_spec(parser.specification, output_file) if output_file is None: click.echo(contents) else: from .util import fs fs.write_file(output_file, contents) click.echo('Output written to "%s".' % (output_file,))
def convert(url_or_path, output_file): """ Convert the given spec to OpenAPI 3.x.y. The conversion uses the web API provided by mermade.org.uk to perform the conversion. As long as that service is kept up-to-date and you have an internet connection, conversion should work and should convert to the latest version of the specs. """ # Convert call from .util import url import os absurl = url.absurl(url_or_path, os.getcwd()) from .convert import convert_url content, content_type = convert_url(absurl) # Write output if output_file is None: click.echo(content) else: from .util import fs fs.write_file(output_file, content)
def __write_to_file(filename, specs): # noqa: N802 """ Write specs to the given filename. This takes into account file name extensions as per `fs.write_file`. """ from prance.util import fs, formats contents = formats.serialize_spec(specs, filename) fs.write_file(filename, contents)
def test_write_file(tmpdir): with sandbox.sandbox(tmpdir): test_text = "söme täxt" fs.write_file("test.out", test_text) # File must have been written files = [f for f in os.listdir(".") if os.path.isfile(f)] assert "test.out" in files # File contents must work contents = fs.read_file("test.out") assert test_text == contents
def test_write_file(tmpdir): with sandbox.sandbox(tmpdir): test_text = u'söme täxt' fs.write_file('test.out', test_text) # File must have been written files = [f for f in os.listdir('.') if os.path.isfile(f)] assert 'test.out' in files # File contents must work contents = fs.read_file('test.out') assert test_text == contents
def merge_swagger_file(main_file: str): """ Generate swagger into a single file :param main_file: Swagger file path :return: """ input_file = Path(main_file) output_file = Path(input_file.parent, 'swagger-complete.yaml') contents = formats.serialize_spec( specs=get_bundled_specs(input_file), filename=output_file, ) fs.write_file(filename=output_file, contents=contents, encoding='utf-8')
def test_write_file_bom(tmpdir): with sandbox.sandbox(tmpdir): test_text = "söme täxt" fs.write_file("test.out", test_text, "utf-8-sig") # File must have been written files = [f for f in os.listdir(".") if os.path.isfile(f)] assert "test.out" in files # Encoding must match the one we've given encoding = fs.detect_encoding("test.out") assert encoding == "utf-8-sig" # File contents must work contents = fs.read_file("test.out") assert test_text == contents
def test_write_file_bom(tmpdir): with sandbox.sandbox(tmpdir): test_text = u'söme täxt' fs.write_file('test.out', test_text, 'utf-8-sig') # File must have been written files = [f for f in os.listdir('.') if os.path.isfile(f)] assert 'test.out' in files # Encoding must match the one we've given encoding = fs.detect_encoding('test.out') assert encoding == 'utf-8-sig' # File contents must work contents = fs.read_file('test.out') assert test_text == contents
def test_write_file(): # What we're doing here has really nothing to do with click's CliRunner, # but since we have it, we might as well use its sandboxing feature. from click.testing import CliRunner runner = CliRunner() with runner.isolated_filesystem(): test_text = u'söme täxt' fs.write_file('test.out', test_text) # File must have been written files = [f for f in os.listdir('.') if os.path.isfile(f)] assert 'test.out' in files # File contents must work contents = fs.read_file('test.out') assert test_text == contents