-
Notifications
You must be signed in to change notification settings - Fork 18
/
utilities.py
58 lines (49 loc) · 1.56 KB
/
utilities.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
54
55
56
57
58
from loguru import logger
import glob
import os
from pathlib import Path
from textwrap import dedent
def clear_folder(path: str, verbose: bool = False):
"""
Takes the path provided and deletes all the files within it
Arguments:
path: The folder path for deleting the files
verbose: Add more detailed logging
"""
# Safety check: Make sure path ends with trailing slash
if not path.endswith("/") and not path.endswith("\\"):
path = f"{path}/"
# Get all files and delete them
files = glob.glob(f"{path}*")
for f in files:
try:
os.remove(f)
if verbose:
logger.info(f"Deleted {f}")
except:
logger.error(f"Failed to delete {f}")
def safe_delete_file(path: str, verbose: bool = False):
"""
Takes the file path provided and deletes it if it exists
Arguments:
path: The file path for deleting the file
verbose: Add more detailed logging
"""
if os.path.exists(path):
os.remove(path)
if verbose:
logger.info(f"Deleted {path}")
else:
logger.warning(f'File "{path}" does not exist')
def init_logs(verbose: str = False):
Path("log.json").touch()
if verbose:
logger.info("Written log.json")
with open("log.md", "w+") as f:
f.write("# pipeline error log\n\n")
f.write("## Unaccessible Webpages\n\n")
f.write("|URL | Error Code | Error Reason|\n")
f.write("|--- | --- | ---|\n")
f.close()
if verbose:
logger.info("Written log.md")