-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
137 lines (104 loc) · 4.04 KB
/
Copy pathutils.py
File metadata and controls
137 lines (104 loc) · 4.04 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from collections import deque
import os
from typing import List
PLAYLIST_DIR = "./playlists"
def tail_log(file_path: str, lines: int = 50) -> List[str]:
"""
Fetch the last `lines` lines from a file.
Args:
file_path: Path to the log file.
lines: Number of lines to fetch from the end.
Returns:
List of lines (each as a string, stripped of trailing newlines).
"""
with open(file_path, 'r', encoding='utf-8') as f:
return [line.rstrip('\n') for line in deque(f, maxlen=lines)]
def _get_playlist_path(filename: str) -> str:
"""Return full path to playlist file."""
return os.path.join(PLAYLIST_DIR, filename)
def _read_playlist_lines(path: str) -> List[str]:
"""Read playlist file and return stripped lines."""
if not os.path.exists(path):
raise FileNotFoundError(f"Playlist '{path}' does not exist.")
with open(path, "r", encoding="utf-8") as f:
return [line.rstrip("\n") for line in f]
def _write_playlist_lines(path: str, lines: List[str]) -> None:
"""Write lines back to playlist file."""
with open(path, "w", encoding="utf-8") as f:
for line in lines:
f.write(f"{line}\n")
def _append_playlist_lines(path: str, lines: List[str]) -> None:
"""Write lines back to playlist file."""
with open(path, "a", encoding="utf-8") as f:
for line in lines:
f.write(f"{line}\n")
# ---------------------------------------------------------
# Check if song exists
# ---------------------------------------------------------
def song_exists(playlist_filename: str, song_path: str) -> bool:
path = _get_playlist_path(playlist_filename)
lines = _read_playlist_lines(path)
return song_path in lines
# ---------------------------------------------------------
# Add song (if not already present)
# ---------------------------------------------------------
def add_song(playlist_filename: str, song_path: str) -> bool:
"""
Adds song to playlist.
Returns True if added, False if already existed.
"""
path = _get_playlist_path(playlist_filename)
lines = _read_playlist_lines(path)
if song_path in lines:
return False # already exists
lines.append(song_path)
_write_playlist_lines(path, lines)
return True
def add_songs(playlist_filename: str, song_paths: List[str]) -> bool:
"""
Adds song to playlist.
Returns True if added, False if already existed.
"""
path = _get_playlist_path(playlist_filename)
lines = _read_playlist_lines(path)
for song_path in song_paths:
if song_path in lines:
continue
else:
lines.append(song_path)
_write_playlist_lines(path, lines)
return True
# ---------------------------------------------------------
# Remove song (if present)
# ---------------------------------------------------------
def remove_song(playlist_filename: str, song_path: str) -> bool:
"""
Removes song from playlist.
Returns True if removed, False if not found.
"""
path = _get_playlist_path(playlist_filename)
lines = _read_playlist_lines(path)
if song_path not in lines:
return False
lines = [line for line in lines if line != song_path]
_write_playlist_lines(path, lines)
return True
# ---------------------------------------------------------
# Create Playlist
# ---------------------------------------------------------
def create_playlist(playlist_filename: str, overwrite: bool = False) -> bool:
"""
Creates a new empty M3U8 playlist file with #EXTM3U header.
Args:
playlist_filename (str): Name of the playlist file (e.g., "mylist.m3u8")
overwrite (bool): If True, overwrite existing file.
Returns:
bool: True if created, False if file already exists and overwrite=False
"""
os.makedirs(PLAYLIST_DIR, exist_ok=True)
path = os.path.join(PLAYLIST_DIR, playlist_filename)
if os.path.exists(path) and not overwrite:
return False
with open(path, "w", encoding="utf-8") as f:
f.write("#EXTM3U\n")
return True