51 lines
1.3 KiB
Python
51 lines
1.3 KiB
Python
from pathlib import Path
|
|
from datetime import datetime
|
|
import humanize
|
|
import json
|
|
import os
|
|
|
|
# load json file
|
|
def loadJSON(file_path):
|
|
# open the file
|
|
path = Path.cwd() / file_path
|
|
with open(path, 'r', encoding="utf-8") as file:
|
|
# return loaded file
|
|
return json.load(file)
|
|
|
|
def escapeThemeName(name):
|
|
return name.lower().replace(" ", "-")
|
|
|
|
def formatRelativeTime(date_str: str) -> str:
|
|
date_format = "%Y-%m-%d %H:%M:%S"
|
|
past_date = datetime.strptime(date_str, date_format).replace(tzinfo=None)
|
|
|
|
now = datetime.now()
|
|
time_difference = now - past_date
|
|
|
|
return humanize.naturaltime(time_difference)
|
|
|
|
def trimContent(var, trim) -> str:
|
|
trim = int(trim)
|
|
if trim > 0:
|
|
trimmed = var[:trim] + '…' if len(var) >= trim else var
|
|
trimmed = trimmed.rstrip()
|
|
return trimmed
|
|
else:
|
|
return var
|
|
|
|
def slugify(text):
|
|
return text.replace(" ", "-")
|
|
|
|
def readPlainFile(file, split = False):
|
|
if os.path.exists(file):
|
|
with open(file, 'r', encoding="utf-8") as file:
|
|
if split:
|
|
return file.read().splitlines()
|
|
else:
|
|
return file.read()
|
|
else:
|
|
with open(file, 'a+', encoding="utf-8") as file:
|
|
if split:
|
|
return file.read().splitlines()
|
|
else:
|
|
return file.read()
|