mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-09 14:55:03 -06:00
Use a separate function to validate IP address
CURA-6483
This commit is contained in:
parent
defcba6927
commit
c8872cb4a1
4 changed files with 79 additions and 1 deletions
21
cura/Utils/QtUtil.py
Normal file
21
cura/Utils/QtUtil.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from PyQt5.QtCore import QObject, pyqtSlot
|
||||
|
||||
from . import networking
|
||||
|
||||
|
||||
#
|
||||
# Exposes the util functions to QML using a QObject.
|
||||
#
|
||||
class QtUtil(QObject):
|
||||
|
||||
def __init__(self, parent: Optional["QObject"] = None) -> None:
|
||||
super().__init__(parent = parent)
|
||||
|
||||
@pyqtSlot(str, result = bool)
|
||||
def isValidIP(self, address: str) -> bool:
|
||||
return networking.isValidIP(address)
|
26
cura/Utils/networking.py
Normal file
26
cura/Utils/networking.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
import re
|
||||
|
||||
|
||||
_REGEX_IPV4 = re.compile(r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$")
|
||||
_REGEX_IPV6 = re.compile(r"^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$")
|
||||
|
||||
|
||||
# Checks if the given string is a valid IPv4 address.
|
||||
def isIPv4(address: str) -> bool:
|
||||
return _REGEX_IPV4.fullmatch(address) is not None
|
||||
|
||||
|
||||
# Checks if the given string is a valid IPv6 address.
|
||||
def isIPv6(address: str) -> bool:
|
||||
return _REGEX_IPV6.fullmatch(address) is not None
|
||||
|
||||
|
||||
# Checks if the given string is a valid IPv4 or IPv6 address.
|
||||
def isValidIP(address: str) -> bool:
|
||||
return isIPv4(address) or isIPv6(address)
|
||||
|
||||
|
||||
__all__ = ["isIPv4", "isIPv6", "isValidIP"]
|
Loading…
Add table
Add a link
Reference in a new issue