diff --git a/Utils/Common/Coding.py b/Utils/Common/Coding.py index 52f53bf..5bd6d15 100644 --- a/Utils/Common/Coding.py +++ b/Utils/Common/Coding.py @@ -3,7 +3,7 @@ import base64 import urllib.parse -from Utils.Logger import logger + def urlEncode(content: str) -> str: # url encode (RFC3986) return urllib.parse.quote(content, encoding = 'utf-8') @@ -31,30 +31,3 @@ def base64Decode(content: str) -> str: # base64 decode return base64.b64decode(content).decode(encoding = 'utf-8') except: raise RuntimeError('Invalid base64 encode') - - -def checkScheme(url: str, scheme: str, name: str) -> str: # check url scheme and remove it - if not url.startswith('%s://' % scheme): - logger.warning('%s url should start with `%s://`' % (name, scheme)) - raise RuntimeError('%s scheme error' % name) - return url[len(scheme) + 3:] - - -def splitTag(url: str, fromRight: bool = True, spaceRemark: bool = True) -> tuple[str, str]: # split tag after `#` - if '#' not in url: # without tag - return url, '' - if not fromRight: - url, remark = url.split('#', 1) # from left search - else: - url, remark = url.rsplit('#', 1) # from right search - if spaceRemark: # deal with space remark for space - remark = remark.replace('+', ' ') - return url, urlDecode(remark) - - -def splitParam(params: str) -> dict: # split params - ret = {} - if params != '': - for param in params.split('&'): - ret[param.split('=', 1)[0]] = urlDecode(param.split('=', 1)[1]) - return ret diff --git a/Utils/Common/Url.py b/Utils/Common/Url.py new file mode 100644 index 0000000..9e7f370 --- /dev/null +++ b/Utils/Common/Url.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from Utils.Logger import logger +from Utils.Common.Coding import * + +def checkScheme(url: str, scheme: str, name: str) -> str: # check url scheme and remove it + if not url.startswith('%s://' % scheme): + logger.warning('%s url should start with `%s://`' % (name, scheme)) + raise RuntimeError('%s scheme error' % name) + return url[len(scheme) + 3:] + + +def splitTag(url: str, fromRight: bool = True, spaceRemark: bool = True) -> tuple[str, str]: # split tag after `#` + if '#' not in url: # without tag + return url, '' + if not fromRight: + url, remark = url.split('#', 1) # from left search + else: + url, remark = url.rsplit('#', 1) # from right search + if spaceRemark: # deal with space remark for space + remark = remark.replace('+', ' ') + return url, urlDecode(remark) + + +def splitParam(params: str) -> dict: # split params + ret = {} + if params != '': + for param in params.split('&'): + ret[param.split('=', 1)[0]] = urlDecode(param.split('=', 1)[1]) + return ret diff --git a/Utils/Common/__init__.py b/Utils/Common/__init__.py index f7beec6..23f51d7 100644 --- a/Utils/Common/__init__.py +++ b/Utils/Common/__init__.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from Utils.Common.Coding import urlEncode, urlDecode -from Utils.Common.Host import hostFormat, v6AddBracket -from Utils.Common.Coding import base64Encode, base64Decode -from Utils.Common.Network import isVacantPort, getAvailablePort -from Utils.Common.Coding import checkScheme, splitTag, splitParam +from Utils.Common.Url import * +from Utils.Common.Host import * +from Utils.Common.Network import *