Package musicbrainz2 :: Module utils

[frames] | no frames]

# Module utils

source code

Various utilities to simplify common tasks.

This module contains helper functions to make common tasks easier.

Author: Matthias Friedrich <matt@mafr.de>

Functions

extractUuid(uriStr, resType=None) Extract the UUID part from a MusicBrainz identifier. | source code

extractFragment(uriStr, uriPrefix=None) Extract the fragment part from a URI. | source code

extractEntityType(uriStr) Returns the entity type an entity URI is referring to. | source code

getReleaseTypeName(releaseType) Returns the name of a release type URI. | source code

getCountryName(id_) Returns a country's name based on an ISO-3166 country code. | source code

getLanguageName(id_) Returns a language name based on an ISO-639-2/T code. | source code

getScriptName(id_) Returns a script name based on an ISO-15924 code. | source code

Function Details

### extractUuid(uriStr, resType=None)

source code

Extract the UUID part from a MusicBrainz identifier.

This function takes a MusicBrainz ID (an absolute URI) as the input and returns the UUID part of the URI, thus turning it into a relative URI. If `uriStr` is None or a relative URI, then it is returned unchanged.

The `resType` parameter can be used for error checking. Set it to 'artist', 'release', or 'track' to make sure `uriStr` is a syntactically valid MusicBrainz identifier of the given resource type. If it isn't, a `ValueError` exception is raised. This error checking only works if `uriStr` is an absolute URI, of course.

Example:

    >>> from musicbrainz2.utils import extractUuid
    >>>  extractUuid('http://musicbrainz.org/artist/c0b2500e-0cef-4130-869d-732b23ed9df5', 'artist')
    'c0b2500e-0cef-4130-869d-732b23ed9df5'
    >>>

Parameters:
  - `uriStr` - a string containing a MusicBrainz ID (an URI), or None
  - `resType` - a string containing a resource type
Returns:
  a string containing a relative URI, or None
Raises:
  - `ValueError` - the given URI is no valid MusicBrainz ID

### extractFragment(uriStr, uriPrefix=None)

source code

Extract the fragment part from a URI.

If `uriStr` is None or no absolute URI, then it is returned unchanged.

The `uriPrefix` parameter can be used for error checking. If `uriStr` is an absolute URI, then the function checks if it starts with `uriPrefix`. If it doesn't, a `ValueError` exception is raised.

Parameters:
  - `uriStr` - a string containing an absolute URI
  - `uriPrefix` - a string containing an URI prefix
Returns:
  a string containing the fragment, or None
Raises:
  - `ValueError` - the given URI doesn't start with `uriPrefix`

### extractEntityType(uriStr)

source code

Returns the entity type an entity URI is referring to.

Parameters:
  - `uriStr` - a string containing an absolute entity URI
Returns:
  a string containing 'artist', 'release', 'track', or 'label'
Raises:
  - `ValueError` - if the given URI is no valid MusicBrainz ID

### getReleaseTypeName(releaseType)

source code

Returns the name of a release type URI.

Parameters:
  - `releaseType` - a string containing a release type URI
Returns:
  a string containing a printable name for the release type

See Also: musicbrainz2.model.Release

### getCountryName(id_)

source code

Returns a country's name based on an ISO-3166 country code.

The country table this function is based on has been modified for MusicBrainz purposes by using the extension mechanism defined in ISO-3166. All IDs are still valid ISO-3166 country codes, but some IDs have been added to include historic countries and some of the country names have been modified to make them better suited for display purposes.

If the country ID is not found, None is returned. This may happen for example, when new countries are added to the MusicBrainz web service which aren't known to this library yet.

Parameters:
  - `id_` - a two-letter upper case string containing an ISO-3166 code
Returns:
  a string containing the country's name, or None

See Also: musicbrainz2.model

### getLanguageName(id_)

source code

Returns a language name based on an ISO-639-2/T code.

This function uses a subset of the ISO-639-2/T code table to map language IDs (terminologic, not bibliographic ones!) to names.

Parameters:
  - `id_` - a three-letter upper case string containing an ISO-639-2/T code
Returns:
  a string containing the language's name, or None

See Also: musicbrainz2.model

### getScriptName(id_)

source code

Returns a script name based on an ISO-15924 code.

This function uses a subset of the ISO-15924 code table to map script IDs to names.

Parameters:
  - `id_` - a four-letter string containing an ISO-15924 script code
Returns:
  a string containing the script's name, or None

See Also: musicbrainz2.model