<commit_before>import logging
import os

logger = logging.getLogger('mopidy.backends.local.translator')

from mopidy.models import Track, Artist, Album
from mopidy.utils.path import path_to_uri

def parse_m3u(file_path):
    """
    Convert M3U file list of uris

    Example M3U data::

        # This is a comment
        Alternative\Band - Song.mp3
        Classical\Other Band - New Song.mp3
        Stuff.mp3
        D:\More Music\Foo.mp3
        http://www.example.com:8000/Listen.pls
        http://www.example.com/~user/Mine.mp3

    - Relative paths of songs should be with respect to location of M3U.
    - Paths are normaly platform specific.
    - Lines starting with # should be ignored.
    - m3u files are latin-1.
    - This function does not bother with Extended M3U directives.
    """

    uris = []
    folder = os.path.dirname(file_path)

    try:
        with open(file_path) as m3u:
            contents = m3u.readlines()
    except IOError, e:
        logger.error('Couldn\'t open m3u: %s', e)
        return uris

    for line in contents:
        line = line.strip().decode('latin1')

        if line.startswith('#'):
            continue

        # FIXME what about other URI types?
        if line.startswith('file://'):
            uris.append(line)
        else:
            path = path_to_uri(folder, line)
            uris.append(path)

    return uris

def parse_mpd_tag_cache(tag_cache, music_dir=''):
    """
    Converts a MPD tag_cache into a lists of tracks, artists and albums.
    """
    tracks = set()

    try:
        with open(tag_cache) as library:
            contents = library.read()
    except IOError, e:
        logger.error('Could not open tag cache: %s', e)
        return tracks

    current = {}
    state = None

    for line in contents.split('\n'):
        if line == 'songList begin':
            state = 'songs'
            continue
        elif line == 'songList end':
            state = None
            continue
        elif not state:
            continue

        key, value = line.split(': ', 1)

        if key == 'key':
            _convert_mpd_data(current, tracks, music_dir)
            current.clear()

        current[key.lower()] = value

    _convert_mpd_data(current, tracks, music_dir)

    return tracks

def _convert_mpd_data(data, tracks, music_dir):
    if not data:
        return

    track_kwargs = {}
    album_kwargs = {}

    if 'track' in data:
        album_kwargs['num_tracks'] = int(data['track'].split('/')[1])
        track_kwargs['track_no'] = int(data['track'].split('/')[0])

    if 'artist' in data:
        artist = Artist(name=data['artist'])
        track_kwargs['artists'] = [artist]
        album_kwargs['artists'] = [artist]

    if 'album' in data:
        album_kwargs['name'] = data['album']
        album = Album(**album_kwargs)
        track_kwargs['album'] = album

    if 'title' in data:
        track_kwargs['name'] = data['title']

    if data['file'][0] == '/':
        path = data['file'][1:]
    else:
        path = data['file']

    track_kwargs['uri'] = path_to_uri(music_dir, path)
    track_kwargs['length'] = int(data.get('time', 0)) * 1000

    track = Track(**track_kwargs)
    tracks.add(track)<commit_msg>Add some basic fixmes<commit_after>import logging
import os

logger = logging.getLogger('mopidy.backends.local.translator')

from mopidy.models import Track, Artist, Album
from mopidy.utils.path import path_to_uri

def parse_m3u(file_path):
    """
    Convert M3U file list of uris

    Example M3U data::

        # This is a comment
        Alternative\Band - Song.mp3
        Classical\Other Band - New Song.mp3
        Stuff.mp3
        D:\More Music\Foo.mp3
        http://www.example.com:8000/Listen.pls
        http://www.example.com/~user/Mine.mp3

    - Relative paths of songs should be with respect to location of M3U.
    - Paths are normaly platform specific.
    - Lines starting with # should be ignored.
    - m3u files are latin-1.
    - This function does not bother with Extended M3U directives.
    """

    uris = []
    folder = os.path.dirname(file_path)

    try:
        with open(file_path) as m3u:
            contents = m3u.readlines()
    except IOError, e:
        logger.error('Couldn\'t open m3u: %s', e)
        return uris

    for line in contents:
        line = line.strip().decode('latin1')

        if line.startswith('#'):
            continue

        # FIXME what about other URI types?
        if line.startswith('file://'):
            uris.append(line)
        else:
            path = path_to_uri(folder, line)
            uris.append(path)

    return uris

def parse_mpd_tag_cache(tag_cache, music_dir=''):
    """
    Converts a MPD tag_cache into a lists of tracks, artists and albums.
    """
    tracks = set()

    try:
        with open(tag_cache) as library:
            contents = library.read()
    except IOError, e:
        logger.error('Could not open tag cache: %s', e)
        return tracks

    current = {}
    state = None

    for line in contents.split('\n'):
        if line == 'songList begin':
            state = 'songs'
            continue
        elif line == 'songList end':
            state = None
            continue
        elif not state:
            continue

        key, value = line.split(': ', 1)

        if key == 'key':
            _convert_mpd_data(current, tracks, music_dir)
            current.clear()

        current[key.lower()] = value

    _convert_mpd_data(current, tracks, music_dir)

    return tracks

def _convert_mpd_data(data, tracks, music_dir):
    if not data:
        return

    track_kwargs = {}
    album_kwargs = {}

    if 'track' in data:
        album_kwargs['num_tracks'] = int(data['track'].split('/')[1])
        track_kwargs['track_no'] = int(data['track'].split('/')[0])

    if 'artist' in data:
        artist = Artist(name=data['artist'])
        track_kwargs['artists'] = [artist]
        album_kwargs['artists'] = [artist]

    # FIXME Newer mpd tag caches support albumartist names
    if 'album' in data:
        album_kwargs['name'] = data['album']
        album = Album(**album_kwargs)
        track_kwargs['album'] = album

    if 'title' in data:
        track_kwargs['name'] = data['title']

    # FIXME what if file is uri - generated tag cache needs to allways make
    # LOCAL_MUSIC_PATH relative paths or this code must handle uris
    if data['file'][0] == '/':
        path = data['file'][1:]
    else:
        path = data['file']

    # FIXME newer mpd tag caches provide musicbrainz ids

    track_kwargs['uri'] = path_to_uri(music_dir, path)
    track_kwargs['length'] = int(data.get('time', 0)) * 1000

    track = Track(**track_kwargs)
    tracks.add(track)
{'fraction_non_alphanumeric': 0.118634001484781, 'fraction_numerical': 0.008908685968819599, 'mean_word_length': 2.212207916070577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280566', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2065, 'n_words': 566}