-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
74 lines (61 loc) · 2.28 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
import re
import os
from pathlib import Path
from typing import Union
from datetime import datetime, timedelta, timezone
class CutOffDate:
def __init__(self, x: Union[str, int]):
if isinstance(x, int):
self.datetime = datetime.now(timezone.utc) - timedelta(days=x)
elif isinstance(x, str):
def parse_date(date_str):
for date_format in [
"%Y-%m-%d",
"%d.%m.%Y",
]:
try:
date = datetime.strptime(date_str, date_format)
return date
except ValueError:
logging.debug(
f"Failed to parse date with format: {date_format}"
)
logging.error("Failed to parse date for CutOff Query!")
raise NotImplementedError
self.datetime = parse_date(x)
self.datetime = self.datetime.replace(tzinfo=timezone.utc)
def __str__(self):
return self.datetime.strftime("%d-%b-%Y")
def increment_filename(filepath: str) -> str:
base, extension = os.path.splitext(filepath)
counter = 1
new_filepath = filepath
while os.path.exists(new_filepath):
new_filepath = f"{base}_{counter}{extension}"
counter += 1
return new_filepath
def sanitize_filename(filename: str) -> str:
"""
Sanitize the filename to make it safe for filesystem use.
This function replaces special characters in the filename with underscores
to ensure the filename is safe for use in the filesystem.
Args:
filename (str): The original filename to sanitize.
Returns:
str: The sanitized filename with special characters replaced by underscores.
"""
sanitized = re.sub(r"[^\w\-_\.:\\ ]", "_", filename)
sanitized_path = Path(sanitized)
if sanitized_path.is_absolute():
sanitized_path = Path(
*(
p if i == 0 else p.replace(":", "_")
for i, p in enumerate(sanitized_path.parts)
)
)
sanitized = str(sanitized_path)
else:
sanitized = sanitized.replace(":", "_")
logging.debug(f"Sanitized filename: {filename} -> {sanitized}")
return sanitized