-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathutils.py
53 lines (37 loc) · 1.4 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu
import json
import os
from pathlib import Path
from slugify import slugify
def get_slug(text, *, js_safe=True):
"""slug from text to build URL parts"""
if js_safe:
return slugify(text, regex_pattern=r"[^-a-z0-9_]+").replace("-", "_")
return slugify(text)
def clean_text(text):
"""cleaned-down version of text as Youtube is very permissive with descriptions"""
return text.strip().replace("\n", " ").replace("\r", " ")
def save_json(cache_dir: Path, key, data):
"""save JSON collection to path"""
with open(cache_dir.joinpath(f"{key}.json"), "w") as fp:
json.dump(data, fp, indent=4)
def load_json(cache_dir: Path, key):
"""load JSON collection from path or None"""
fname = cache_dir.joinpath(f"{key}.json")
if not fname.exists():
return None
try:
return json.loads(fname.read_bytes())
except Exception:
return None
def load_mandatory_json(cache_dir: Path, key):
"""load mandatory JSON collection from path"""
return json.loads(cache_dir.joinpath(f"{key}.json").read_bytes())
def has_argument(arg_name, all_args):
"""whether --arg_name is specified in all_args"""
return list(filter(lambda x: x.startswith(f"--{arg_name}"), all_args))
def delete_callback(fpath: str | Path):
"""callback to delete file"""
if Path(fpath).exists():
os.unlink(fpath)