mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-15 10:22:26 +12:00
9287e85b7e
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
96 lines
2.1 KiB
Python
96 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
import time
|
|
import traceback
|
|
import urllib.parse
|
|
|
|
from manuskript.util.counter import CounterKind, CharCounter, WordCounter, PageCounter
|
|
|
|
|
|
def safeInt(s: str, d: int) -> int:
|
|
if s is None:
|
|
return d
|
|
|
|
try:
|
|
return int(s)
|
|
except ValueError:
|
|
return d
|
|
|
|
|
|
def validString(invalid) -> str:
|
|
return "" if invalid is None else str(invalid)
|
|
|
|
|
|
def invalidString(valid: str) -> str:
|
|
return None if len(valid) == 0 else valid
|
|
|
|
|
|
def validInt(invalid) -> int:
|
|
return 0 if invalid is None else int(invalid)
|
|
|
|
|
|
def invalidInt(valid: int) -> int:
|
|
return None if valid == 0 else valid
|
|
|
|
|
|
def safeFilename(filename: str, extension: str = None) -> str:
|
|
if extension is not None:
|
|
filename = "%s.%s" % (filename, extension)
|
|
|
|
name = filename.encode('ascii', 'replace').decode('ascii')
|
|
filenamesToAvoid = list(["CON", "PRN", "AUX", "NUL"])
|
|
|
|
for i in range(1, 9):
|
|
filenamesToAvoid.append("COM" + str(i))
|
|
filenamesToAvoid.append("LPT" + str(i))
|
|
|
|
if name.upper() in filenamesToAvoid:
|
|
name = "_" + name
|
|
|
|
return re.sub(r"[^a-zA-Z0-9._\-+()]", "_", name)
|
|
|
|
|
|
def parseFilenameFromURL(url: str) -> str | None:
|
|
result = urllib.parse.urlparse(url)
|
|
|
|
if result is None:
|
|
return None
|
|
|
|
if result.scheme != "file":
|
|
return None
|
|
|
|
return result.path
|
|
|
|
|
|
def countText(text: str, kind: CounterKind = CounterKind.WORDS):
|
|
if text is None:
|
|
return 0
|
|
|
|
if kind == CounterKind.CHARACTERS:
|
|
return CharCounter.count(text)
|
|
elif kind == CounterKind.WORDS:
|
|
return WordCounter.count(text)
|
|
elif kind == CounterKind.PAGES:
|
|
return PageCounter.count(text)
|
|
else:
|
|
return 0
|
|
|
|
|
|
def safeFraction(value, low, high) -> float:
|
|
if value < low or high <= low:
|
|
return 0.0
|
|
elif value > high:
|
|
return 1.0
|
|
else:
|
|
return 1.0 * (value - low) / (high - low)
|
|
|
|
|
|
def profileTime(func, *args):
|
|
start = time.perf_counter()
|
|
result = func(*args)
|
|
end = time.perf_counter()
|
|
print("{}.{}: {}".format(func.__module__, func.__name__, end - start))
|
|
return result
|