genre_manager
¶
Genre management functionality for Music Genre Updater.
This module handles determining dominant genres for artists and updating track genres accordingly.
GenreManager
¶
Bases: BaseProcessor
Manages genre determination and updates for tracks.
Initialize the GenreManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track_processor
|
TrackProcessor
|
Track processor for updating tracks |
required |
console_logger
|
Logger
|
Logger for console output |
required |
error_logger
|
Logger
|
Logger for error messages |
required |
analytics
|
AnalyticsProtocol
|
Service for performance tracking |
required |
config
|
AppConfig
|
Typed application configuration |
required |
dry_run
|
bool
|
Whether to run in dry-run mode |
False
|
Source code in src/core/tracks/genre_manager.py
is_missing_or_unknown_genre
staticmethod
¶
Check if track has missing or unknown genre.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
TrackDict
|
Track to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if genre is missing, empty, or 'unknown' |
Source code in src/core/tracks/genre_manager.py
parse_date_added
staticmethod
¶
Parse track's date_added field to datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
TrackDict
|
Track with date_added field |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
Parsed datetime with UTC timezone, or None if parsing fails |
Source code in src/core/tracks/genre_manager.py
filter_tracks_for_incremental_update
¶
Filter tracks to only include those added since the last run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracks
|
list[TrackDict]
|
All tracks |
required |
last_run_time
|
datetime | None
|
Time of last incremental run |
required |
Returns:
| Type | Description |
|---|---|
list[TrackDict]
|
Filtered list of tracks added since last run |
Source code in src/core/tracks/genre_manager.py
process_batch_results
staticmethod
¶
Process batch results and update collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_results
|
list[Any]
|
Results from batch processing |
required |
updated_tracks
|
list[TrackDict]
|
List to append updated tracks to |
required |
change_logs
|
list[ChangeLogEntry]
|
List to append change logs to |
required |
Source code in src/core/tracks/genre_manager.py
update_genres_by_artist_async
async
¶
Update genres for all tracks, grouped by artist.
Uses two-level concurrency control: 1. Artist-level: concurrent_limit (default 5) - how many artists process in parallel 2. AppleScript-level: apple_script_concurrency (default 2) - global limit on concurrent AppleScript operations across ALL artists to prevent overwhelming Music.app
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracks
|
list[TrackDict]
|
All tracks to process |
required |
last_run_time
|
datetime | None
|
Time of last run for incremental updates |
None
|
force
|
bool
|
Force update all tracks (bypass incremental filtering) |
False
|
fresh
|
bool
|
Fresh mode - implies force=True. For genres, equivalent to force since there's no cache to invalidate (included for API consistency) |
False
|
Returns:
| Type | Description |
|---|---|
tuple[list[TrackDict], list[ChangeLogEntry]]
|
Tuple of (all_updated_tracks, all_change_logs) |
Source code in src/core/tracks/genre_manager.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
deduplicate_tracks_by_id
staticmethod
¶
Remove duplicate tracks based on track ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tracks
|
list[TrackDict]
|
List of tracks that may contain duplicates |
required |
Returns:
| Type | Description |
|---|---|
list[TrackDict]
|
List of unique tracks without duplicates |
Source code in src/core/tracks/genre_manager.py
get_dry_run_actions
¶
Get the list of dry-run actions recorded.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dry-run action dictionaries |
test_update_track_genre
async
¶
Test-only access to _update_track_genre method.
Source code in src/core/tracks/genre_manager.py
test_gather_with_error_handling
async
¶
Test-only access to _gather_with_error_handling method.
Source code in src/core/tracks/genre_manager.py
test_filter_tracks_for_update
¶
Test-only access to _filter_tracks_for_update method.
Source code in src/core/tracks/genre_manager.py
test_select_tracks_to_update_for_artist
¶
Test-only access to _select_tracks_to_update_for_artist method.
Source code in src/core/tracks/genre_manager.py
test_process_artist_genres
async
¶
test_process_artist_genres(
artist_name,
all_artist_tracks,
force_update,
applescript_semaphore,
tracks_to_update=None,
)
Test-only access to _process_artist_genres method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist_name
|
str
|
Name of the artist. |
required |
all_artist_tracks
|
list[TrackDict]
|
All tracks by this artist. |
required |
force_update
|
bool
|
Whether to force update all tracks. |
required |
applescript_semaphore
|
Semaphore
|
Semaphore for AppleScript concurrency control. |
required |
tracks_to_update
|
list[TrackDict] | None
|
Specific tracks to update. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[TrackDict], list[ChangeLogEntry]]
|
Tuple of (updated_tracks, change_logs). |
Source code in src/core/tracks/genre_manager.py
test_process_single_artist_wrapper
async
¶
test_process_single_artist_wrapper(
artist_name,
artist_tracks,
last_run,
force,
artist_semaphore,
applescript_semaphore=None,
)
Test-only access to _process_single_artist_wrapper method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
artist_name
|
str
|
The artist name. |
required |
artist_tracks
|
list[TrackDict]
|
All tracks for the artist. |
required |
last_run
|
datetime | None
|
Last run timestamp for incremental logic. |
required |
force
|
bool
|
Force updates regardless of filters. |
required |
artist_semaphore
|
Semaphore
|
Concurrency guard for artist-level processing. |
required |
applescript_semaphore
|
Semaphore | None
|
Global semaphore for AppleScript concurrency control. If None, creates a default semaphore for backward compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[list[TrackDict], list[ChangeLogEntry]]
|
Tuple of (updated_tracks, change_logs) for this artist. |