new
This commit is contained in:
62
code/utils/misc.py
Normal file
62
code/utils/misc.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from datetime import datetime
|
||||
from flask import current_app as app
|
||||
|
||||
|
||||
def file_date_human(num):
|
||||
return datetime.fromtimestamp(num).strftime("%y-%m-%d %H:%M")
|
||||
|
||||
|
||||
def file_size_human(num, HTML=True):
|
||||
space = " " if HTML else " "
|
||||
for x in [space + "B", "KB", "MB", "GB", "TB"]:
|
||||
if num < 1024.0:
|
||||
if x == space + "B":
|
||||
return "%d%s%s" % (num, space, x)
|
||||
return "%3.1f%s%s" % (num, space, x)
|
||||
num /= 1024.0
|
||||
|
||||
|
||||
def file_size_MB(num):
|
||||
return "{:,.2f}".format(num / (1024 * 1024))
|
||||
|
||||
|
||||
def get_or_none(key, d, none=None):
|
||||
if key in d:
|
||||
return d[key]
|
||||
else:
|
||||
return none
|
||||
|
||||
|
||||
def is_path_safe(path):
|
||||
if path.startswith("."):
|
||||
return False
|
||||
if "/." in path:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_valid_url(url, qualifying=None):
|
||||
min_attributes = ("scheme", "netloc")
|
||||
qualifying = min_attributes if qualifying is None else qualifying
|
||||
token = urlparse(url)
|
||||
return all([getattr(token, qualifying_attr) for qualifying_attr in qualifying])
|
||||
|
||||
|
||||
def path2url(path):
|
||||
return pathname2url(path)
|
||||
|
||||
|
||||
def safe_name(s):
|
||||
return safe_string(s, "-_")
|
||||
|
||||
|
||||
def safe_path(s):
|
||||
return safe_string(s, "-_/")
|
||||
|
||||
|
||||
def safe_string(s, valid, no_repeat=False):
|
||||
"""return a safe string, replace non alnum characters with _ . all characters in valid are considered valid."""
|
||||
safe = "".join([c if c.isalnum() or c in valid else "_" for c in s])
|
||||
if no_repeat:
|
||||
safe = re.sub(r"_+", "_", safe)
|
||||
return safe
|
||||
Reference in New Issue
Block a user