-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
102 lines (82 loc) · 2.61 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import logging
from datetime import datetime, timedelta
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def get_logger(name):
"""
Get a logger with the given name.
"""
return logging.getLogger(name)
def get_date_range(days_back=30):
"""
Get a date range for fetching data.
Args:
days_back: Number of days to go back from yesterday
Returns:
Tuple of (start_date, end_date) in YYYY-MM-DD format
"""
yesterday = datetime.now().date() - timedelta(days=1)
start_date = yesterday - timedelta(days=days_back)
return start_date.strftime("%Y-%m-%d"), yesterday.strftime("%Y-%m-%d")
def is_valid_date_format(date_str):
"""
Check if a string is in the YYYY-MM-DD format.
Args:
date_str: String to check
Returns:
Boolean indicating if the string is in YYYY-MM-DD format
"""
if not isinstance(date_str, str):
return False
# Check if the string matches the YYYY-MM-DD pattern
import re
pattern = r'^\d{4}-\d{2}-\d{2}$'
if not re.match(pattern, date_str):
return False
# Try to parse it as a date to ensure it's valid
try:
year, month, day = map(int, date_str.split('-'))
datetime(year, month, day)
return True
except (ValueError, TypeError):
return False
def format_error(e):
"""
Format an exception for logging.
Args:
e: Exception to format
Returns:
Formatted error message
"""
import traceback
return f"{type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
def validate_token(token_name, token_value, required=True):
"""
Validate that a token is set and not a placeholder.
Args:
token_name: Name of the token for error messages
token_value: Value of the token to validate
required: Whether the token is required
Returns:
True if the token is valid, False otherwise
"""
if not token_value:
if required:
raise ValueError(f"{token_name} is not set")
return False
# Check if the token is a placeholder
placeholders = [
"your_token",
"your_github_token",
"your_motherduck_token",
"your_slack_api_token",
"your_matomo_key"
]
if any(placeholder in token_value.lower() for placeholder in placeholders):
if required:
raise ValueError(f"{token_name} appears to be a placeholder")
return False
return True