Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify is_url function #7317

Merged
merged 4 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rasa/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ async def load_agent_on_start(
)
except Exception as e:
rasa.shared.utils.io.raise_warning(
f"The model at '{model_path}' could not be loaded. " f"Error: {e}"
f"The model at '{model_path}' could not be loaded. "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

f"Error: {type(e)}: {e}"
)
app.agent = None

Expand Down
27 changes: 13 additions & 14 deletions rasa/nlu/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import re
from typing import Any, Optional, Text
from pathlib import Path

Expand Down Expand Up @@ -49,23 +48,23 @@ def is_model_dir(model_dir: Text) -> bool:
def is_url(resource_name: Text) -> bool:
"""Check whether the url specified is a well formed one.

Regex adapted from https://stackoverflow.com/a/7160778/3001665

Args:
resource_name: Remote URL to validate

Returns: `True` if valid, otherwise `False`.
Returns:
`True` if valid, otherwise `False`.
"""
URL_REGEX = re.compile(
r"^(?:http|ftp|file)s?://" # http:// or https:// or file://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|" # domain
r"localhost|" # localhost
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" # or ip
r"(?::\d+)?" # optional port
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)
return URL_REGEX.match(resource_name) is not None
from urllib.parse import urlparse
federicotdn marked this conversation as resolved.
Show resolved Hide resolved

try:
result = urlparse(resource_name)
except Exception:
return False

if result.scheme == "file":
return bool(result.path)

return bool(result.scheme in ["http", "https", "ftp", "ftps"] and result.netloc)


def remove_model(model_dir: Text) -> bool:
Expand Down
11 changes: 10 additions & 1 deletion tests/nlu/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

from rasa.shared.exceptions import RasaException
import rasa.shared.nlu.training_data.message
from rasa.nlu.tokenizers.convert_tokenizer import (
ORIGINAL_TF_HUB_MODULE_URL,
RESTRICTED_ACCESS_URL,
)
import rasa.shared.utils.io
import rasa.utils.io as io_utils
from rasa.nlu import utils
Expand Down Expand Up @@ -115,7 +119,12 @@ def test_remove_model_invalid(empty_model_dir):
("https://www.google.com", True),
("http://google.com", True),
("http://www.google.com", True),
("http://a/b/c", False),
("http://a/b/c", True),
("http://localhost:5002/api/projects/default/models/tags/production", True),
federicotdn marked this conversation as resolved.
Show resolved Hide resolved
("http://rasa-x:5002/api/projects/default/models/tags/production", True),
(ORIGINAL_TF_HUB_MODULE_URL, True),
(RESTRICTED_ACCESS_URL, True),
("file:///some/path/file", True),
],
)
def test_is_url(url: Text, result: bool):
Expand Down