<commit_before>from __future__ import unicode_literals

import collections
import logging

from mopidy import models

import spotify


logger = logging.getLogger(__name__)


class memoized(object):
    def __init__(self, func):
        self.func = func
        self.cache = {}

    def __call__(self, *args, **kwargs):
        # NOTE Only args, not kwargs, are part of the memoization key.
        if not isinstance(args, collections.Hashable):
            return self.func(*args, **kwargs)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args, **kwargs)
            if value is not None:
                self.cache[args] = value
            return value


@memoized
def to_artist(sp_artist):
    if not sp_artist.is_loaded:
        return  # TODO Return placeholder "[loading]" artist?

    return models.Artist(uri=sp_artist.link.uri, name=sp_artist.name)


@memoized
def to_artist_ref(sp_artist):
    if not sp_artist.is_loaded:
        return  # TODO Return placeholder "[loading]" artist?

    return models.Ref.artist(uri=sp_artist.link.uri, name=sp_artist.name)


def to_artist_refs(sp_artists):
    for sp_artist in sp_artists:
        sp_artist.load()
        ref = to_artist_ref(sp_artist)
        if ref is not None:
            yield ref


@memoized
def to_album(sp_album):
    if not sp_album.is_loaded:
        return  # TODO Return placeholder "[loading]" album?

    if sp_album.artist is not None:
        artists = [to_artist(sp_album.artist)]
    else:
        artists = []

    if sp_album.year is not None:
        date = '%d' % sp_album.year
    else:
        date = None

    return models.Album(
        uri=sp_album.link.uri,
        name=sp_album.name,
        artists=artists,
        date=date)


@memoized
def to_album_ref(sp_album):
    if not sp_album.is_loaded:
        return  # TODO Return placeholder "[loading]" album?

    if sp_album.artist is None or not sp_album.artist.is_loaded:
        name = sp_album.name
    else:
        name = '%s - %s' % (sp_album.artist.name, sp_album.name)

    return models.Ref.album(uri=sp_album.link.uri, name=name)


def to_album_refs(sp_albums):
    for sp_album in sp_albums:
        sp_album.load()
        ref = to_album_ref(sp_album)
        if ref is not None:
            yield ref


@memoized
def to_track(sp_track, bitrate=None):
    if not sp_track.is_loaded:
        return  # TODO Return placeholder "[loading]" track?

    if sp_track.error != spotify.ErrorType.OK:
        return  # TODO Return placeholder "[error]" track?

    if sp_track.availability != spotify.TrackAvailability.AVAILABLE:
        return  # TODO Return placeholder "[unavailable]" track?

    artists = [to_artist(sp_artist) for sp_artist in sp_track.artists]
    artists = filter(None, artists)

    album = to_album(sp_track.album)

    return models.Track(
        uri=sp_track.link.uri,
        name=sp_track.name,
        artists=artists,
        album=album,
        date=album.date,
        length=sp_track.duration,
        disc_no=sp_track.disc,
        track_no=sp_track.index,
        bitrate=bitrate)


@memoized
def to_track_ref(sp_track):
    if not sp_track.is_loaded:
        return  # TODO Return placeholder "[loading]" track?

    if sp_track.error != spotify.ErrorType.OK:
        return  # TODO Return placeholder "[error]" track?

    if sp_track.availability != spotify.TrackAvailability.AVAILABLE:
        return  # TODO Return placeholder "[unavailable]" track?

    return models.Ref.track(uri=sp_track.link.uri, name=sp_track.name)


def to_track_refs(sp_tracks):
    for sp_track in sp_tracks:
        sp_track.load()
        ref = to_track_ref(sp_track)
        if ref is not None:
            yield ref


def to_playlist(sp_playlist, folders=None, username=None, bitrate=None):
    if not isinstance(sp_playlist, spotify.Playlist):
        return

    if not sp_playlist.is_loaded:
        return  # TODO Return placeholder "[loading]" playlist?

    tracks = [
        to_track(sp_track, bitrate=bitrate)
        for sp_track in sp_playlist.tracks]
    tracks = filter(None, tracks)

    name = sp_playlist.name
    if name is None:
        name = 'Starred'
        tracks = reversed(tracks)  # Use same order as the Spotify client
    if folders is not None:
        name = '/'.join(folders + [name])
    if username is not None and sp_playlist.owner.canonical_name != username:
        name = '%s (by %s)' % (name, sp_playlist.owner.canonical_name)

    return models.Playlist(
        uri=sp_playlist.link.uri,
        name=name,
        tracks=tracks)
<commit_msg>translator: Remove todos on loading/unavailable<commit_after>from __future__ import unicode_literals

import collections
import logging

from mopidy import models

import spotify


logger = logging.getLogger(__name__)


class memoized(object):
    def __init__(self, func):
        self.func = func
        self.cache = {}

    def __call__(self, *args, **kwargs):
        # NOTE Only args, not kwargs, are part of the memoization key.
        if not isinstance(args, collections.Hashable):
            return self.func(*args, **kwargs)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args, **kwargs)
            if value is not None:
                self.cache[args] = value
            return value


@memoized
def to_artist(sp_artist):
    if not sp_artist.is_loaded:
        return

    return models.Artist(uri=sp_artist.link.uri, name=sp_artist.name)


@memoized
def to_artist_ref(sp_artist):
    if not sp_artist.is_loaded:
        return

    return models.Ref.artist(uri=sp_artist.link.uri, name=sp_artist.name)


def to_artist_refs(sp_artists):
    for sp_artist in sp_artists:
        sp_artist.load()
        ref = to_artist_ref(sp_artist)
        if ref is not None:
            yield ref


@memoized
def to_album(sp_album):
    if not sp_album.is_loaded:
        return

    if sp_album.artist is not None:
        artists = [to_artist(sp_album.artist)]
    else:
        artists = []

    if sp_album.year is not None:
        date = '%d' % sp_album.year
    else:
        date = None

    return models.Album(
        uri=sp_album.link.uri,
        name=sp_album.name,
        artists=artists,
        date=date)


@memoized
def to_album_ref(sp_album):
    if not sp_album.is_loaded:
        return

    if sp_album.artist is None or not sp_album.artist.is_loaded:
        name = sp_album.name
    else:
        name = '%s - %s' % (sp_album.artist.name, sp_album.name)

    return models.Ref.album(uri=sp_album.link.uri, name=name)


def to_album_refs(sp_albums):
    for sp_album in sp_albums:
        sp_album.load()
        ref = to_album_ref(sp_album)
        if ref is not None:
            yield ref


@memoized
def to_track(sp_track, bitrate=None):
    if not sp_track.is_loaded:
        return

    if sp_track.error != spotify.ErrorType.OK:
        return  # TODO Return placeholder "[error]" track?

    if sp_track.availability != spotify.TrackAvailability.AVAILABLE:
        return

    artists = [to_artist(sp_artist) for sp_artist in sp_track.artists]
    artists = filter(None, artists)

    album = to_album(sp_track.album)

    return models.Track(
        uri=sp_track.link.uri,
        name=sp_track.name,
        artists=artists,
        album=album,
        date=album.date,
        length=sp_track.duration,
        disc_no=sp_track.disc,
        track_no=sp_track.index,
        bitrate=bitrate)


@memoized
def to_track_ref(sp_track):
    if not sp_track.is_loaded:
        return

    if sp_track.error != spotify.ErrorType.OK:
        return  # TODO Return placeholder "[error]" track?

    if sp_track.availability != spotify.TrackAvailability.AVAILABLE:
        return

    return models.Ref.track(uri=sp_track.link.uri, name=sp_track.name)


def to_track_refs(sp_tracks):
    for sp_track in sp_tracks:
        sp_track.load()
        ref = to_track_ref(sp_track)
        if ref is not None:
            yield ref


def to_playlist(sp_playlist, folders=None, username=None, bitrate=None):
    if not isinstance(sp_playlist, spotify.Playlist):
        return

    if not sp_playlist.is_loaded:
        return

    tracks = [
        to_track(sp_track, bitrate=bitrate)
        for sp_track in sp_playlist.tracks]
    tracks = filter(None, tracks)

    name = sp_playlist.name
    if name is None:
        name = 'Starred'
        tracks = reversed(tracks)  # Use same order as the Spotify client
    if folders is not None:
        name = '/'.join(folders + [name])
    if username is not None and sp_playlist.owner.canonical_name != username:
        name = '%s (by %s)' % (name, sp_playlist.owner.canonical_name)

    return models.Playlist(
        uri=sp_playlist.link.uri,
        name=name,
        tracks=tracks)

{'fraction_non_alphanumeric': 0.09243982370889366, 'fraction_numerical': 0.0, 'mean_word_length': 2.5499398315282793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717173', 'n_tokens_mistral': 2958, 'n_tokens_neox': 2852, 'n_words': 699}