Skip to content

Commit

Permalink
fix: url validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
simojo committed Sep 19, 2023
1 parent f6c11a6 commit e264ae9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
22 changes: 14 additions & 8 deletions chasten/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
from typing import Any, Dict, List, Tuple, Union
from pathlib import Path
from purl import URL
import yaml
import requests

import platformdirs
from rich.logging import RichHandler
from rich.traceback import install
import yaml
import requests

from chasten import constants, validate, util, output
from chasten import (
constants,
filesystem,
output,
util,
validate,
)


def configure_tracebacks() -> None:
Expand Down Expand Up @@ -104,7 +110,7 @@ def display_configuration_directory(


def validate_configuration_files(
config: Union[Path, URL],
config: str,
verbose: bool = False,
) -> Tuple[
bool, Union[Dict[str, List[Dict[str, Union[str, Dict[str, int]]]]], Dict[Any, Any]]
Expand All @@ -118,12 +124,12 @@ def validate_configuration_files(
# be used instead of the platform-specific directory

# input configuration is valid URL
if (util.is_url(str(config))):
if util.is_url(config):
# re-parse input config so it is of type URL
config = URL(str(config))
chasten_user_config_url_str = str(config)
# input configuration is valid file path
elif (Path(str(config)).exists()):
elif Path(config).exists():
# re-parse input config so it is of type Path
config = Path(str(config))
chasten_user_config_dir_str = str(config)
Expand Down Expand Up @@ -290,11 +296,11 @@ def extract_configuration_details_from_config_dir(
return (False, None, None, None) # type: ignore
configuration_file_yaml_str = configuration_file_path.read_text()
# load the contents of the main configuration file
with open(configuration_file_path_str) as user_configuration_file_text:
with open(str(configuration_file_path)) as user_configuration_file_text:
(yaml_success, yaml_data) = convert_configuration_text_to_yaml(user_configuration_file_text)
# return success status, filename, file contents, and yaml parsed data upon success
if yaml_success:
return (True, configuration_file_path_str, configuration_file_yaml_str, yaml_data)
return (True, str(configuration_file_path), configuration_file_yaml_str, yaml_data)
# return none types upon failure in yaml parsing
else:
return (False, None, None, None) # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions chasten/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def configure( # noqa: PLR0913
None,
"--config",
"-c",
help="A directory or URL with configuration file(s).",
help="A directory with configuration file(s) or URL to configuration file.",
),
debug_level: debug.DebugLevel = typer.Option(
debug.DebugLevel.ERROR.value,
Expand Down Expand Up @@ -242,7 +242,7 @@ def analyze( # noqa: PLR0913, PLR0915
None,
"--config",
"-c",
help="A directory with configuration file(s).",
help="A directory with configuration file(s) or URL to configuration file.",
),
debug_level: debug.DebugLevel = typer.Option(
debug.DebugLevel.ERROR.value,
Expand Down
3 changes: 3 additions & 0 deletions chasten/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def is_url(url: str) -> bool:
# parse input url
url_parsed = URL(url)
# only input characters for initiatig query and/or fragments if necessary
port_character = ":" if (url_parsed.port() is not None) else ""
query_character = "?" if url_parsed.query() else ""
fragment_character = "#" if url_parsed.fragment() else ""
# piece the url back together to make sure it matches what was input
Expand All @@ -62,6 +63,8 @@ def is_url(url: str) -> bool:
url_parsed.scheme(),
"://",
url_parsed.netloc(),
port_character,
str((url_parsed.port() is not None) or ""), # to handle if port() is `None` type
url_parsed.path(),
query_character,
url_parsed.query(),
Expand Down

0 comments on commit e264ae9

Please sign in to comment.