summaryrefslogtreecommitdiff
path: root/jsonschema/cli.py
diff options
context:
space:
mode:
authorwillson-chen <willson.chenwx@gmail.com>2020-08-29 16:03:31 +0800
committerwillson-chen <willson.chenwx@gmail.com>2020-08-29 16:36:34 +0800
commit26aa142e78ff7d788f888745ef58f07275ac3bfc (patch)
tree5dcf8ee0cc860723fce9dbb360762a34abfac3c8 /jsonschema/cli.py
parentb936179ddc60cb88fed0a06f5bb9ebb13f4d0ae3 (diff)
downloadjsonschema-26aa142e78ff7d788f888745ef58f07275ac3bfc.tar.gz
support for cli specifying --base-uri local or remote and add test cases
Diffstat (limited to 'jsonschema/cli.py')
-rw-r--r--jsonschema/cli.py37
1 files changed, 24 insertions, 13 deletions
diff --git a/jsonschema/cli.py b/jsonschema/cli.py
index 2ae93c2..9314a56 100644
--- a/jsonschema/cli.py
+++ b/jsonschema/cli.py
@@ -10,6 +10,7 @@ import json
import os
import sys
import traceback
+import urllib
import attr
@@ -263,20 +264,30 @@ def run(arguments, stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin):
if arguments["base_uri"] is None:
resolver = None
- elif "http:" in arguments["base_uri"] or "https:" in arguments["base_uri"]\
- or "urn:" in arguments["base_uri"]:
- resolver = RefResolver(
- base_uri=arguments["base_uri"],
- referrer=schema,
- )
else:
- file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
- resolver = RefResolver(
- base_uri=file_prefix.format(
- os.path.abspath(arguments["base_uri"])
- ),
- referrer=schema,
- )
+ scheme = urllib.parse.urlsplit(arguments["base_uri"]).scheme
+ alphabet_list = [chr(ord('a') + i) for i in range(0, 26)]
+ local_path_flag = True \
+ if scheme == '' or scheme in alphabet_list else False
+
+ if local_path_flag:
+ file_prefix = "file:///{}/" if "nt" == os.name else "file://{}/"
+ abs_path_flag = os.path.isabs(arguments["base_uri"])
+
+ resolver = RefResolver(
+ base_uri=file_prefix.format(arguments["base_uri"]),
+ referrer=schema,
+ ) if abs_path_flag is True else RefResolver(
+ base_uri=file_prefix.format(
+ os.path.abspath(arguments["base_uri"])
+ ),
+ referrer=schema,
+ )
+ else:
+ resolver = RefResolver(
+ base_uri=arguments["base_uri"],
+ referrer=schema,
+ )
validator = arguments["validator"](schema, resolver=resolver)
exit_code = 0