From 4e83f7933feecd4d82ac38978b8c3da57227760d Mon Sep 17 00:00:00 2001 From: kk Date: Tue, 27 Aug 2019 19:51:02 +0300 Subject: [PATCH] move lib/url to core/url --- cm/migrations/0003_access_profile.py | 2 +- core/url.py | 38 ++++++++++++++++++++++++++++ tests/test_url.py | 30 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 core/url.py create mode 100644 tests/test_url.py diff --git a/cm/migrations/0003_access_profile.py b/cm/migrations/0003_access_profile.py index 178685e95e..ee3ffea078 100644 --- a/cm/migrations/0003_access_profile.py +++ b/cm/migrations/0003_access_profile.py @@ -10,7 +10,7 @@ from django.db import models # NOC modules -from noc.lib.url import URL +from noc.core.url import URL from noc.core.script.scheme import TELNET, SSH from noc.core.migration.base import BaseMigration diff --git a/core/url.py b/core/url.py new file mode 100644 index 0000000000..33de70a119 --- /dev/null +++ b/core/url.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# --------------------------------------------------------------------- +# URL Processing functions +# --------------------------------------------------------------------- +# Copyright (C) 2007-2012 The NOC Project +# See LICENSE for details +# --------------------------------------------------------------------- + +# Python modules +import re + +# Third-party modules +from six.moves.urllib.parse import unquote as urllib_unquote + +rx_url = re.compile( + r"^(?P[^:]+)://(?:(?P[^:]+):(?P[^@]+)@)(?P[^/:]+)(?::(?P\d+))?(?P.*)$" +) + + +class InvalidURLException(Exception): + pass + + +class URL(object): + def __init__(self, url): + self.url = url + match = rx_url.match(self.url) + if not match: + raise InvalidURLException + self.scheme = match.group("scheme") + self.user = urllib_unquote(match.group("user")) + self.password = urllib_unquote(match.group("password")) + self.host = match.group("host") + if match.group("port"): + self.port = int(match.group("port")) + else: + self.port = None + self.path = match.group("path") diff --git a/tests/test_url.py b/tests/test_url.py new file mode 100644 index 0000000000..59037ef1e8 --- /dev/null +++ b/tests/test_url.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# ---------------------------------------------------------------------- +# noc.core.url unittests +# ---------------------------------------------------------------------- +# Copyright (C) 2007-2019 The NOC Project +# See LICENSE for details +# ---------------------------------------------------------------------- + +# Third-party modules +import pytest + +# NOC modules +from noc.core.url import URL + + +@pytest.mark.parametrize( + "config, expected", + [("https://user:password@www.www.ru/login", "https://user:password@www.www.ru/login")], +) +def test_url(config, expected): + assert URL(config).url == expected + + +@pytest.mark.parametrize( + "config, expected", + [("https:///user::password@www.www.ru/login", "https://user:password@www.www.ru/login")], +) +def test_url_error(config, expected): + with pytest.raises(Exception): + assert URL(config).url == expected -- GitLab