split utils, added manual tests

This commit is contained in:
2018-11-25 11:16:02 +02:00
parent 74ee5edd7e
commit 8452a7034e
10 changed files with 294 additions and 116 deletions

View File

@@ -1 +1,3 @@
from utils.misc import *
from utils.files import *
from utils.crypt import *

View File

@@ -5,21 +5,14 @@ import requests
import re
import json
import stat
from .misc import *
from .crypt import *
try:
import magic
except ImportError:
pass
try:
from werkzeug.utils import secure_filename
except ImportError:
pass
try:
from urllib.request import pathname2url
from urllib.request import urlparse
except ImportError:
from urllib import pathname2url
from urlparse import urlparse
class Logger:
def __init__(self, filename, uid = 0, gid = 0):
@@ -71,15 +64,6 @@ class Logger:
fp.flush()
def bool_short(var):
if type(var) == bool:
if var:
return "Y"
else:
return "N"
return var
def download_url(url, filename):
try:
r = requests.get(url, stream=True)
@@ -132,12 +116,6 @@ def file_age(path):
)
def file_date_human(num):
return datetime.fromtimestamp(
num
).strftime(app.config['DATE_FORMAT'])
def file_name_version(full_path):
""" New name versioned with date of the file """
file_dir = os.path.dirname(full_path)
@@ -188,20 +166,6 @@ def file_stat(path, filename):
}
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_folder_size(path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(path):
@@ -211,11 +175,24 @@ def get_folder_size(path):
return total_size
def get_or_none(key,d,none = None):
if key in d:
return d[key]
def get_download_url(share, file, token):
direct = get_or_none('direct_links', share, False)
if direct:
return "/".join((
app.config['PUBLIC_URL'],
'direct',
share['name'],
get_direct_token(share, file),
path2url(file)
))
else:
return none
return "/".join((
app.config['PUBLIC_URL'],
'download',
share['name'],
token,
path2url(file)
))
def get_script_url(public_url, share, end_point, token = "[TOKEN]"):
@@ -244,22 +221,6 @@ def get_script_url(public_url, share, end_point, token = "[TOKEN]"):
return {'cmd': cmd, 'doc': doc}
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 iter_folder_files(path, recursive = True, version_folder = None):
if recursive:
for dirpath, dirnames, filenames in os.walk(path, topdown = False):
@@ -287,10 +248,6 @@ def iter_folder_files(path, recursive = True, version_folder = None):
yield fp
def path2url(path):
return pathname2url(path)
def read_config(app):
# Read config from json
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
@@ -318,22 +275,6 @@ def read_config(app):
return config_values
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
def set_rights(path):
os.chown(path, app.config['UID'], app.config['GID'])
st = os.stat(path)

88
code/utils/misc.py Normal file
View File

@@ -0,0 +1,88 @@
from datetime import datetime
from flask import current_app as app
try:
from urllib.request import pathname2url
from urllib.request import urlparse
except ImportError:
from urllib import pathname2url
from urlparse import urlparse
try:
from werkzeug.utils import secure_filename
except ImportError:
pass
# String handling etc
def bool_short(var):
if type(var) == bool:
if var:
return "Y"
else:
return "N"
return var
def file_date_human(num):
return datetime.fromtimestamp(
num
).strftime(app.config['DATE_FORMAT'])
def file_size_human(num, HTML = True):
space = '&nbsp;' 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