foley.sources.music_library.acquisition#

Acquire a music library’s titles: verified files, one provenance row each, a resumable table.

acquire() turns titles into download URLs (the library’s rule), fetches each from the first of its mirrors whose bytes verify accepts, writes the file to out and returns one AcquiredTrack per title. Every step is a seam (keyword arguments with working defaults):

A row’s licence is a LicenseRecord (track_license()), put through foley’s fail-closed gate (foley.licensing.refusal_reason()) before anything is fetched: a title it refuses is a refused row, with the reason, and is not downloaded. url is where the bytes came from (the mirror that served them), page_url the library’s own address. A title that fails is a failed row with its reasons; the batch goes on. A file whose digest a previous run recorded (kept, from kept_digests() or kept_sources()) is not fetched again, so a rerun resumes. A paid library fetches nothing: its rows are wishlist rows.

AcquiredTrack.row() is a flat, JSON-safe dict (a caller such as an bridges it into its own asset rows without foley knowing it); tracks_table() is those rows as CSV, and attribution_text() the blocks to paste under a video. The way back is read_tracks_table() (the CSV’s rows, as written) and AcquiredTrack.from_row() (a row as the track it records, its licence rebuilt from the library’s registry); held_rows() keeps those whose file is held.

Module Attributes

ACQUIRED

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library's title.

KEPT

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library's title.

FAILED

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library's title.

REFUSED

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library's title.

WISHLIST

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library's title.

STATUSES

Every status a row may have.

HELD

The statuses whose file is in out (what a rerun may keep, what is credited).

TRACK_COLUMNS

The tracks table's columns, in order (a row's other metadata, a catalogue's feel or genre, follows in name order).

METADATA_COLUMNS

The metadata keys a titles file may set that are columns (the rest the library states).

LICENSE_COLUMNS

what the library states, which AcquiredTrack.from_row() checks a row's cells against.

LIST_SEPARATOR

How a list is joined in one cell of the tracks table (credited_by).

BYTE_ORDER_MARK

The byte-order mark an editor (Excel) writes before a UTF-8 CSV's first column.

DURATION_DECIMALS

Decimal places a duration is written with.

Kept

a file's recorded sha256, or {"sha256", "url", "mirror"} (kept_sources()), whose url / mirror a kept row carries on.

Functions

acquire(library, titles, *, out[, fetch, ...])

Download titles of library into out, verified, one row each.

attribution_text(rows)

The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once.

fetch_verified(url, *, mirrors, fetch, verify)

(bytes, seconds, the URL that served them, its mirror's name) from the first of mirrors whose bytes verify accepts; every mirror's reason in one MusicLibraryError when none does.

files_store(out)

out as a {file name: bytes} store: a mapping is used as it is; a folder becomes foley's file store over it (foley.stores.make_byte_store(), which makes the folder on its first write).

held_rows(table)

The acquired or kept rows (with a file and a digest) of a tracks table, as row dicts: table is tracks_table()'s CSV text (read_tracks_table()) or the rows themselves (AcquiredTrack or their row() dicts).

kept_digests(table)

{file: sha256} of the acquired or kept rows of a tracks table: what a rerun may keep.

kept_sources(table)

{file: {"sha256", "url", "mirror"}} of the acquired or kept rows of a tracks table: kept_digests() with where each file came from, so a kept row still says which URL and mirror served it (acquire(kept=...) takes either).

read_tracks_table(text)

The rows of a tracks table (tracks_table()'s CSV text), as written; a byte-order mark an editor added (Excel does) is not part of the first column.

tracks_table(rows)

The tracks table as CSV text: TRACK_COLUMNS, then any metadata the rows carry, in name order (read_tracks_table() reads it back).

Classes

AcquiredTrack(title, library, status, license)

One row of the tracks table: a title, what became of it, and its provenance.

Exceptions

DroppedColumnsWarning

A tracks-table row named columns that set nothing, and AcquiredTrack.from_row() dropped them.

foley.sources.music_library.acquisition.ACQUIRED = 'acquired'#

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library’s title.

Type:

The row statuses. refused

class foley.sources.music_library.acquisition.AcquiredTrack(title, library, status, license, file=None, sha256=None, duration_s=None, url=None, mirror=None, page_url=None, credited_by=(), metadata=dict(), error=None)[source]#

Bases: SerializableMixin

One row of the tracks table: a title, what became of it, and its provenance.

url is the URL that served the bytes (a mirror’s), mirror that mirror’s name, page_url the library’s own address for the title; credited_by the videos that credit it and metadata a titles file’s other fields (feel, genre, …).

classmethod from_row(row, *, libraries=None)[source]#

The track a tracks-table row records: the inverse of row().

row is a row() dict (or one read back from JSON), or a line of the table (read_tracks_table(): every cell text, an empty one nothing). Its library is looked up in libraries (default MUSIC_LIBRARIES) and its licence rebuilt from it (track_license()), never read from the row. The row’s licence cells (LICENSE_COLUMNS) must still say what the library states now: a cell someone edited, or a library re-registered under other terms since the table was written, is refused rather than silently widened or narrowed.

A column outside TRACK_COLUMNS is metadata, as the table wrote it (a list or mapping a titles file gave comes back as its cell’s text). A column a titles file may not set (a downstream provenance field such as cost_usd or cacheable, a licence flag) is dropped with a DroppedColumnsWarning: it never sets anything.

Raises:

MusicLibraryError – For a row with a key that is not text, a blank title, no library or status, a status not in STATUSES, a library that is not registered, a duration_s that is not a finite number, or licence cells that differ from the library’s (naming them).

Return type:

AcquiredTrack

row()[source]#

The row as the tracks table writes it: flat and JSON-safe (a list joined by ``; ``, a mapping as JSON).

Return type:

dict[str, Any]

foley.sources.music_library.acquisition.BYTE_ORDER_MARK: str = '\ufeff'#

The byte-order mark an editor (Excel) writes before a UTF-8 CSV’s first column.

foley.sources.music_library.acquisition.DURATION_DECIMALS: int = 3#

Decimal places a duration is written with.

exception foley.sources.music_library.acquisition.DroppedColumnsWarning[source]#

Bases: UserWarning

A tracks-table row named columns that set nothing, and AcquiredTrack.from_row() dropped them.

foley.sources.music_library.acquisition.FAILED = 'failed'#

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library’s title.

Type:

The row statuses. refused

foley.sources.music_library.acquisition.HELD: tuple[str, ...] = ('acquired', 'kept')#

The statuses whose file is in out (what a rerun may keep, what is credited).

foley.sources.music_library.acquisition.KEPT = 'kept'#

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library’s title.

Type:

The row statuses. refused

foley.sources.music_library.acquisition.Kept#

a file’s recorded sha256, or {"sha256", "url", "mirror"} (kept_sources()), whose url / mirror a kept row carries on.

Type:

kept’s values

alias of Mapping[str, str | Mapping[str, Any]]

foley.sources.music_library.acquisition.LICENSE_COLUMNS: tuple[str, ...] = ('license', 'license_url', 'rights_verified', 'commercial_ok', 'author', 'attribution')#

what the library states, which AcquiredTrack.from_row() checks a row’s cells against.

Type:

The columns AcquiredTrack.row() reads off the licence

foley.sources.music_library.acquisition.LIST_SEPARATOR: str = '; '#

How a list is joined in one cell of the tracks table (credited_by).

foley.sources.music_library.acquisition.METADATA_COLUMNS: tuple[str, ...] = ('credited_by',)#

The metadata keys a titles file may set that are columns (the rest the library states).

foley.sources.music_library.acquisition.REFUSED = 'refused'#

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library’s title.

Type:

The row statuses. refused

foley.sources.music_library.acquisition.STATUSES: tuple[str, ...] = ('acquired', 'kept', 'failed', 'refused', 'wishlist')#

Every status a row may have.

foley.sources.music_library.acquisition.TRACK_COLUMNS: tuple[str, ...] = ('title', 'library', 'status', 'file', 'duration_s', 'sha256', 'license', 'license_url', 'rights_verified', 'commercial_ok', 'author', 'url', 'source_page_url', 'mirror', 'attribution', 'credited_by', 'error')#

The tracks table’s columns, in order (a row’s other metadata, a catalogue’s feel or genre, follows in name order).

foley.sources.music_library.acquisition.WISHLIST = 'wishlist'#

the licence gate refused the title for the intended use (nothing fetched); wishlist: a paid library’s title.

Type:

The row statuses. refused

foley.sources.music_library.acquisition.acquire(library, titles, *, out, fetch=None, transport=None, mirrors=(<function direct>, <function wayback>), verify=<function probe_verify>, kept=None, intended_use=IntendedUse(commercial=True, publish=True, redistribute_standalone=False, will_train=False, can_attribute=True, revenue_usd=0, allow_voice_or_trademark=False, territory=None), libraries=None, config=None, clock=<built-in function monotonic>, sleep=<built-in function sleep>)[source]#

Download titles of library into out, verified, one row each.

titles is a list, or {title: metadata} whose metadata (credited_by: the videos that credit it; a catalogue’s feel, genre, …) rides on the row. library is a MusicLibrary or a name in libraries (default MUSIC_LIBRARIES).

Each title’s licence is put through foley.licensing.refusal_reason() for intended_use first (fail-closed: an error in the gate is a refusal); a refused title is a refused row and is not fetched. The others are fetched from each of mirrors in turn until one gives bytes verify accepts. fetch (url -> bytes) replaces the HTTP path, paced per host by the config’s rate; otherwise the files come through transport (default: requests) wrapped by foley’s resilience layer per host; that path honours foley.offline(). config is laid over MUSIC_LIBRARY_HTTP; clock / sleep time the waits. kept is {file name: sha256} a previous run recorded (kept_digests()), or {file name: {"sha256", "url", "mirror"}} (kept_sources(), whose url and mirror the kept row keeps): a file in out with that digest is kept, any other is fetched again. A paid library downloads nothing and returns wishlist rows.

Raises:
  • MusicLibraryError – For an unknown library, titles of the wrong shape, metadata that restates the row, two titles saved as one file, or a kept value that is neither a digest nor a mapping with one.

  • MusicToolMissing – When verify is the default and no probe can run (before anything is downloaded).

  • ValueError – When both fetch and transport are given.

Return type:

list[AcquiredTrack]

foley.sources.music_library.acquisition.attribution_text(rows)[source]#

The attribution blocks to paste under a video, one per track that has one (acquired or kept), blank-line separated, each once.

Each block is foley.provenance.credits.attribution_line() of the row’s licence: the library’s own wording, verbatim.

>>> from foley.sources.music_library import MUSIC_LIBRARIES, track_license
>>> lic = track_license(MUSIC_LIBRARIES["incompetech"], "A")
>>> print(attribution_text([AcquiredTrack("A", "incompetech", ACQUIRED, lic)] * 2))
"A" Kevin MacLeod (incompetech.com)
Licensed under Creative Commons: By Attribution 4.0 License
http://creativecommons.org/licenses/by/4.0/
Return type:

str

foley.sources.music_library.acquisition.fetch_verified(url, *, mirrors, fetch, verify)[source]#

(bytes, seconds, the URL that served them, its mirror's name) from the first of mirrors whose bytes verify accepts; every mirror’s reason in one MusicLibraryError when none does.

The one mirror chain: acquire() and the registered library sources’ download (foley.sources.music_library.adapter) both run it.

Return type:

tuple[bytes, float, str, str]

foley.sources.music_library.acquisition.files_store(out)[source]#

out as a {file name: bytes} store: a mapping is used as it is; a folder becomes foley’s file store over it (foley.stores.make_byte_store(), which makes the folder on its first write).

Return type:

MutableMapping[str, bytes]

foley.sources.music_library.acquisition.held_rows(table)[source]#

The acquired or kept rows (with a file and a digest) of a tracks table, as row dicts: table is tracks_table()’s CSV text (read_tracks_table()) or the rows themselves (AcquiredTrack or their row() dicts).

Return type:

Iterator[Mapping[str, Any]]

foley.sources.music_library.acquisition.kept_digests(table)[source]#

{file: sha256} of the acquired or kept rows of a tracks table: what a rerun may keep. table is tracks_table()’s CSV text, or the rows themselves (AcquiredTrack or their row() dicts, read back from JSON).

Return type:

dict[str, str]

foley.sources.music_library.acquisition.kept_sources(table)[source]#

{file: {"sha256", "url", "mirror"}} of the acquired or kept rows of a tracks table: kept_digests() with where each file came from, so a kept row still says which URL and mirror served it (acquire(kept=...) takes either).

Return type:

dict[str, dict[str, Any]]

foley.sources.music_library.acquisition.read_tracks_table(text)[source]#

The rows of a tracks table (tracks_table()’s CSV text), as written; a byte-order mark an editor added (Excel does) is not part of the first column.

>>> read_tracks_table("\ufefftitle,library\r\nA,incompetech\r\n")
[{'title': 'A', 'library': 'incompetech'}]
Raises:

MusicLibraryError – For a header that names a column twice, or a line with more cells than the header has columns (naming its line).

Return type:

list[dict[str, str]]

foley.sources.music_library.acquisition.tracks_table(rows)[source]#

The tracks table as CSV text: TRACK_COLUMNS, then any metadata the rows carry, in name order (read_tracks_table() reads it back).

Return type:

str