Skip to content

Commit

Permalink
Added a few new functions to helpers.data.types
Browse files Browse the repository at this point in the history
  • Loading branch information
dhondta committed Mar 26, 2020
1 parent a6414cf commit d1be351
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Tinyscript provides some type checking functions, for common data:
`ts.is_bool` | boolean
`ts.is_dict` | dictionary
`ts.is_dir` / `ts.is_folder` | dummy shortcuts to `os.path.isdir`
`ts.is_executable` | whether the given path has the execution flag
`ts.is_file` | dummy shortcut to `os.path.isfile`
`ts.is_hex` | hexadecimal string (case insensitive)
`ts.is_int` / `ts.is_pos_int` / `ts.is_neg_int` | integer (positive / negative)
Expand Down Expand Up @@ -209,6 +210,7 @@ And for network-related data:
`ts.is_mac` | MAC address
`ts.is_netif` | network interface
`ts.is_port` | port number
`ts.is_url` | Uniform Resource Locator

## Common argument types

Expand Down Expand Up @@ -260,6 +262,7 @@ And for network-related types:
`ts.network_interface` | `str` | valid network interface on the current system
`ts.port_number` | `int` | valid port number
`ts.port_number_range` | `list(int)` | valid list of port numbers, ranging from and to the given bounds
`ts.url` | valid Uniform Resource Locator

-----

Expand Down
13 changes: 13 additions & 0 deletions tests/test_helpers_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ def test_network_related_types(self):
self.assertIsNotNone(hostname("www.example.com"))
self.assertRaises(ValueError, domain_name, "bad_name")
self.assertRaises(ValueError, hostname, "www.example-.com")
self.assertIsNotNone(url("http://www.example.com"))
self.assertIsNotNone(url("http://john:doe@www.example.com/path?p=true"))
self.assertRaises(ValueError, url, "bad_url")
self.assertRaises(ValueError, url, "A" * 2050)
self.assertRaises(ValueError, url, "A" * 40 + "://example.com")
self.assertRaises(ValueError, url, "http://www.example-.com/")
self.assertRaises(ValueError, url, "http://:@www.example.com/")
self.assertIsNotNone(email_address("john.doe@www.example.com"))
self.assertRaises(ValueError, email_address, "bad_email")
self.assertRaises(ValueError, email_address, "user@bad_name")
self.assertIsInstance(ip_address("127.0.0.1"), netaddr.IPAddress)
Expand Down Expand Up @@ -184,6 +192,11 @@ def test_data_format_check(self):
def test_network_format_check(self):
self.assertTrue(is_domain("example.com"))
self.assertTrue(is_hostname("www.example.com"))
self.assertTrue(is_url("http://www.example.com"))
self.assertTrue(is_url("http://john:doe@www.example.com/path?p=true"))
self.assertFalse(is_url("http://www.example-.com/"))
self.assertFalse(is_url("A" * 40 + "://example.com"))
self.assertFalse(is_url("http://:@www.example.com/"))
self.assertFalse(is_email("example.com"))
self.assertTrue(is_email("test@example.com"))
self.assertTrue(is_ip("1234"))
Expand Down
5 changes: 3 additions & 2 deletions tinyscript/helpers/data/types/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Files/Folders-related checking functions and argument types.
"""
from os import makedirs
from os import access, makedirs, X_OK
from os.path import exists, isdir, isfile

from .strings import _str2list
Expand All @@ -12,8 +12,9 @@


# dummy shortcuts, compliant with the is_* naming convention
__all__ += ["is_dir", "is_file", "is_folder"]
__all__ += ["is_dir", "is_executable", "is_file", "is_folder"]
is_dir = is_folder = isdir
is_executable = lambda f: access(f, X_OK)
is_file = isfile


Expand Down
44 changes: 41 additions & 3 deletions tinyscript/helpers/data/types/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# network-related check functions
__all__ += ["is_defgw", "is_domain", "is_email", "is_gw", "is_hostname",
"is_ifaddr", "is_ip", "is_ipv4", "is_ipv6", "is_mac", "is_netif",
"is_port"]
"is_port", "is_url"]
is_defgw = lambda gw: __gateway_address(gw, True, False) is not None
is_domain = lambda n: __domain_name(n, False, False) is not None
is_email = lambda e: __email_address(e, False) is not None
Expand All @@ -29,7 +29,8 @@
is_ipv6 = lambda ip: __ip_address(ip, 6, False) is not None
is_mac = lambda mac: __mac_address(mac, False) is not None
is_netif = lambda nif: __network_interface(nif, False) is not None
is_port = lambda p: is_int(p) and 0 < p < 2**16
is_port = lambda p: isinstance(p, int) and 0 < p < 2**16
is_url = lambda url: __url(url, False) is not None


# network-related argument types
Expand All @@ -42,7 +43,7 @@
"ipv6_address_network", "interface_address",
"interface_address_list", "interface_address_filtered_list",
"mac_address", "network_interface", "port_number",
"port_number_range"]
"port_number_range", "url"]


def __domain_name(name, dotted=False, fail=True):
Expand Down Expand Up @@ -202,6 +203,43 @@ def __network_interface(netif, fail=True):
network_interface = lambda nif: __network_interface(nif)


def __url(url, fail=True):
""" URL hyperlink. """
if len(url) > 2048:
raise ValueError("Not a valid URL (too long)")
try:
scheme, link = url.split("://")
except ValueError:
if fail:
raise ValueError("Not a valid URL (cannot parse scheme)")
else:
return
if len(scheme) > 36:
if fail:
raise ValueError("Not a valid URL (invalid scheme)")
else:
return
link = link.split("?")
link, query = link if len(link) == 2 else (link[0], "")
link = link.split("/")
link, reqpath = link if len(link) == 2 else (link[0], "")
link = link.split("@")
auth, domain = link if len(link) == 2 else ("", link[0])
if not is_domain(domain):
if fail:
raise ValueError("Not a valid URL (bad domain)")
else:
return
auth = auth.split(":")
if auth != [''] and (len(auth) != 2 or auth[0] == "" and auth[1] == ""):
if fail:
raise ValueError("Not a valid URL (bad auth)")
else:
return
return url
url = lambda url: __url(url)


def port_number(port):
""" Port number validation. """
try:
Expand Down
1 change: 1 addition & 0 deletions tinyscript/helpers/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def generate(self, prefix="", suffix="", length=8,
new = self.joinpath(str(prefix) + _ + str(suffix))
if not new.exists():
return new
rand_folder_name = generate

def is_hidden(self):
""" Check if the current path is hidden. """
Expand Down

0 comments on commit d1be351

Please sign in to comment.