Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_posix_abs_posix():
test = "/etc/passwd"
expected = test
assert fs.from_posix(test) == expected
def test_from_posix_abs_win32():
test = "/c:/windows/notepad.exe"
expected = "c:\\windows\\notepad.exe"
assert fs.from_posix(test) == expected
elif os.path.isabs(from_posix(parsed.path)):
# We have an absolute path, so we can ignore the reference entirely!
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
else:
# If we have a relative path, we require a reference.
if not reference:
raise ResolutionError('Cannot build an absolute file URL from a relative'
' path without a reference!')
if reference.scheme not in (None, '', 'file'):
raise ResolutionError('Cannot build an absolute file URL with a non-file'
' reference!')
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
result_list[2] = abspath(from_posix(parsed.path),
from_posix(reference.path))
# Reassemble the result and return it
result = parse.ParseResult(*result_list)
return result
def __parser_for_url(url, resolve, backend, strict, encoding): # noqa: N802
"""Return a parser instance for the URL and the given parameters."""
# Try the URL
formatted = click.format_filename(url)
click.echo('Processing "%s"...' % (formatted, ))
from prance.util import fs
fsurl = fs.abspath(url)
import os.path
if os.path.exists(fs.from_posix(fsurl)):
url = fsurl
# Create parser to use
parser = None
if resolve:
click.echo(' -> Resolving external references.')
parser = prance.ResolvingParser(url, lazy = True, backend = backend,
strict = strict, encoding = encoding)
else:
click.echo(' -> Not resolving external references.')
parser = prance.BaseParser(url, lazy = True, backend = backend,
strict = strict, encoding = encoding)
# XXX maybe enable tihs in debug mode or something.
# click.echo(' -> Using backend: {0.backend}'.format(parser))
return parser, formatted
content_type = None
if url.scheme in (None, '', 'file'):
from .fs import read_file, from_posix
content = read_file(from_posix(url.path))
elif url.scheme == 'python':
# Resolve package path
package = url.netloc
path = url.path
if path[0] == '/':
path = path[1:]
import pkg_resources
path = pkg_resources.resource_filename(package, path)
from .fs import read_file, from_posix
content = read_file(from_posix(path))
else:
import requests
response = requests.get(url.geturl())
if not response.ok: # pragma: nocover
raise ResolutionError('Cannot fetch URL "%s": %d %s' % (
url.geturl(), response.status_code, response.reason))
content_type = response.headers.get('content-type', 'text/plain')
content = response.text
cache[url_key] = (content, content_type)
return content, content_type
# We have an absolute path, so we can ignore the reference entirely!
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
else:
# If we have a relative path, we require a reference.
if not reference:
raise ResolutionError('Cannot build an absolute file URL from a relative'
' path without a reference!')
if reference.scheme not in (None, '', 'file'):
raise ResolutionError('Cannot build an absolute file URL with a non-file'
' reference!')
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
result_list[2] = abspath(from_posix(parsed.path),
from_posix(reference.path))
# Reassemble the result and return it
result = parse.ParseResult(*result_list)
return result
reference = fs.to_posix(reference)
reference = parse.urlparse(reference)
# If the input URL has no path, we assume only its fragment matters.
# That is, we'll have to set the fragment of the reference URL to that
# of the input URL, and return the result.
import os.path
from .fs import from_posix, abspath
result_list = None
if not parsed.path:
if not reference or not reference.path:
raise ResolutionError('Cannot build an absolute file URL from a fragment'
' without a reference with path!')
result_list = list(reference)
result_list[5] = parsed.fragment
elif os.path.isabs(from_posix(parsed.path)):
# We have an absolute path, so we can ignore the reference entirely!
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
else:
# If we have a relative path, we require a reference.
if not reference:
raise ResolutionError('Cannot build an absolute file URL from a relative'
' path without a reference!')
if reference.scheme not in (None, '', 'file'):
raise ResolutionError('Cannot build an absolute file URL with a non-file'
' reference!')
result_list = list(parsed)
result_list[0] = 'file' # in case it was empty
result_list[2] = abspath(from_posix(parsed.path),
from_posix(reference.path))