-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (44 loc) · 1.31 KB
/
utils.py
File metadata and controls
62 lines (44 loc) · 1.31 KB
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
"""Utility functions for HabitHub calendar extraction."""
import json
import os
from typing import Any, Optional
import config
def load_json(filepath: str) -> Any:
"""Load JSON data from a file.
Args:
filepath: Path to JSON file
Returns:
Parsed JSON data
"""
with open(filepath, 'r') as f:
return json.load(f)
def save_json(data: Any, filepath: str):
"""Save data to JSON file.
Args:
data: Data to save
filepath: Output file path
"""
with open(filepath, 'w') as f:
json.dump(data, f, indent=2)
def find_first_image(directory: str) -> Optional[str]:
"""Find the first image file (png/jpg) in a directory.
Args:
directory: Directory path to search
Returns:
Full path to the first image file found, or None if no images found
"""
if not os.path.exists(directory):
return None
# Get all files in directory
files = os.listdir(directory)
# Filter for image files with supported extensions
image_files = [
f for f in files
if f.endswith(config.SUPPORTED_EXTENSIONS)
]
if not image_files:
return None
# Sort to ensure consistent behavior
image_files.sort()
# Return full path to first image
return os.path.join(directory, image_files[0])