Spaces:
Runtime error
Runtime error
File size: 8,353 Bytes
1a942eb |
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
"""
Module which defines custom exception and enumerations used when
instiating and re-raising those exceptions.
"""
from enum import StrEnum
from ultimate_rvc.typing_extra import StrPath
class Entity(StrEnum):
"""Enumeration of entities that can be provided."""
DIRECTORY = "directory"
DIRECTORIES = "directories"
FILE = "file"
FILES = "files"
URL = "URL"
MODEL_NAME = "model name"
MODEL_NAMES = "model names"
MODEL_FILE = "model file"
SOURCE = "source"
SONG_DIR = "song directory"
AUDIO_TRACK = "audio track"
AUDIO_TRACK_GAIN_PAIRS = "pairs of audio track and gain"
SONG = "song"
VOCALS_TRACK = "vocals track"
INSTRUMENTALS_TRACK = "instrumentals track"
BACKUP_VOCALS_TRACK = "backup vocals track"
MAIN_VOCALS_TRACK = "main vocals track"
class Location(StrEnum):
"""Enumeration of locations where entities can be found."""
INTERMEDIATE_AUDIO_ROOT = "the root of the intermediate audio base directory"
OUTPUT_AUDIO_ROOT = "the root of the output audio directory"
EXTRACTED_ZIP_FILE = "extracted zip file"
class UIMessage(StrEnum):
"""
Enumeration of messages that can be displayed in the UI
in place of core exception messages.
"""
NO_AUDIO_TRACK = "No audio tracks provided."
NO_SONG_DIR = "No song directory selected."
NO_SONG_DIRS = (
"No song directories selected. Please select one or more song directories"
" containing intermediate audio files to delete."
)
NO_OUTPUT_AUDIO_FILES = (
"No files selected. Please select one or more output audio files to delete."
)
NO_UPLOADED_FILES = "No files selected."
NO_VOICE_MODEL = "No voice model selected."
NO_VOICE_MODELS = "No voice models selected."
NO_SOURCE = (
"No source provided. Please provide a valid Youtube URL, local audio file"
" or song directory."
)
class NotProvidedError(ValueError):
"""Raised when an entity is not provided."""
def __init__(self, entity: Entity, ui_msg: UIMessage | None = None) -> None:
"""
Initialize a NotProvidedError instance.
Exception message will be formatted as:
"No `<entity>` provided."
Parameters
----------
entity : Entity
The entity that was not provided.
ui_msg : UIMessage, default=None
Message which, if provided, is displayed in the UI
instead of the default exception message.
"""
super().__init__(f"No {entity} provided.")
self.ui_msg = ui_msg
class NotFoundError(OSError):
"""Raised when an entity is not found."""
def __init__(
self,
entity: Entity,
location: StrPath | Location,
is_path: bool = True,
) -> None:
"""
Initialize a NotFoundError instance.
Exception message will be formatted as:
"`<entity>` not found `(`in `|` as:`)` `<location>`."
Parameters
----------
entity : Entity
The entity that was not found.
location : StrPath | Location
The location where the entity was not found.
is_path : bool, default=True
Whether the location is a path to the entity.
"""
proposition = "at:" if is_path else "in"
entity_cap = entity.capitalize() if not entity.isupper() else entity
super().__init__(
f"{entity_cap} not found {proposition} {location}",
)
class VoiceModelNotFoundError(OSError):
"""Raised when a voice model is not found."""
def __init__(self, name: str) -> None:
r"""
Initialize a VoiceModelNotFoundError instance.
Exception message will be formatted as:
'Voice model with name "`<name>`" not found.'
Parameters
----------
name : str
The name of the voice model that was not found.
"""
super().__init__(f'Voice model with name "{name}" not found.')
class VoiceModelExistsError(OSError):
"""Raised when a voice model already exists."""
def __init__(self, name: str) -> None:
r"""
Initialize a VoiceModelExistsError instance.
Exception message will be formatted as:
"Voice model with name '`<name>`' already exists. Please provide
a different name for your voice model."
Parameters
----------
name : str
The name of the voice model that already exists.
"""
super().__init__(
f'Voice model with name "{name}" already exists. Please provide a different'
" name for your voice model.",
)
class InvalidLocationError(OSError):
"""Raised when an entity is in a wrong location."""
def __init__(self, entity: Entity, location: Location, path: StrPath) -> None:
r"""
Initialize an InvalidLocationError instance.
Exception message will be formatted as:
"`<entity>` should be located in `<location>` but found at:
`<path>`"
Parameters
----------
entity : Entity
The entity that is in a wrong location.
location : Location
The correct location for the entity.
path : StrPath
The path to the entity.
"""
entity_cap = entity.capitalize() if not entity.isupper() else entity
super().__init__(
f"{entity_cap} should be located in {location} but found at: {path}",
)
class HttpUrlError(OSError):
"""Raised when a HTTP-based URL is invalid."""
def __init__(self, url: str) -> None:
"""
Initialize a HttpUrlError instance.
Exception message will be formatted as:
"Invalid HTTP-based URL: `<url>`"
Parameters
----------
url : str
The invalid HTTP-based URL.
"""
super().__init__(
f"Invalid HTTP-based URL: {url}",
)
class YoutubeUrlError(OSError):
"""
Raised when an URL does not point to a YouTube video or
, potentially, a Youtube playlist.
"""
def __init__(self, url: str, playlist: bool) -> None:
"""
Initialize a YoutubeURlError instance.
Exception message will be formatted as:
"URL does not point to a YouTube video `[`or playlist`]`:
`<url>`"
Parameters
----------
url : str
The URL that does not point to a YouTube video or playlist.
playlist : bool
Whether the URL might point to a YouTube playlist.
"""
suffix = "or playlist" if playlist else ""
super().__init__(
f"Not able to access Youtube video {suffix} at: {url}",
)
class UploadLimitError(ValueError):
"""Raised when the upload limit for an entity is exceeded."""
def __init__(self, entity: Entity, limit: str | float) -> None:
"""
Initialize an UploadLimitError instance.
Exception message will be formatted as:
"At most `<limit>` `<entity>` can be uploaded."
Parameters
----------
entity : Entity
The entity for which the upload limit was exceeded.
limit : str
The upload limit.
"""
super().__init__(f"At most {limit} {entity} can be uploaded.")
class UploadFormatError(ValueError):
"""
Raised when one or more uploaded entities have an invalid format
.
"""
def __init__(self, entity: Entity, formats: list[str], multiple: bool) -> None:
"""
Initialize an UploadFileFormatError instance.
Exception message will be formatted as:
"Only `<entity>` with the following formats can be uploaded
`(`by themselves | together`)`: `<formats>`."
Parameters
----------
entity : Entity
The entity that was uploaded with an invalid format.
formats : list[str]
Valid formats.
multiple : bool
Whether multiple entities are uploaded.
"""
suffix = "by themselves" if not multiple else "together (at most one of each)"
super().__init__(
f"Only {entity} with the following formats can be uploaded {suffix}:"
f" {', '.join(formats)}.",
)
|