<commit_before>from __future__ import unicode_literals

import logging
import os

from mopidy.backends import base
from mopidy.models import Album, SearchResult
from mopidy.utils.encoding import locale_decode

from .translator import parse_mpd_tag_cache

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


class LocalLibraryProvider(base.BaseLibraryProvider):
    def __init__(self, *args, **kwargs):
        super(LocalLibraryProvider, self).__init__(*args, **kwargs)
        self._uri_mapping = {}
        self._media_dir = self.backend.config['local']['media_dir']
        self._tag_cache_file = self.backend.config['local']['tag_cache_file']
        self.refresh()

    def refresh(self, uri=None):
        try:
            if not os.path.exists(self._tag_cache_file):
                basedir = os.path.dirname(self._tag_cache_file)
                if not os.path.exists(basedir):
                    os.makedirs(basedir)
                open(self._tag_cache_file, 'a').close()
        except IOError as error:
            logger.warning(
                'Could not create empty tag cache: %s', locale_decode(error))
            return

        tracks = parse_mpd_tag_cache(self._tag_cache_file, self._media_dir)

        for track in tracks:<commit_msg>local: Use path helpers to create tag cache<commit_after>from __future__ import unicode_literals

import logging
from mopidy.backends import base
from mopidy.models import Album, SearchResult
from mopidy.utils import encoding, path

from .translator import parse_mpd_tag_cache

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


class LocalLibraryProvider(base.BaseLibraryProvider):
    def __init__(self, *args, **kwargs):
        super(LocalLibraryProvider, self).__init__(*args, **kwargs)
        self._uri_mapping = {}
        self._media_dir = self.backend.config['local']['media_dir']
        self._tag_cache_file = self.backend.config['local']['tag_cache_file']
        self.refresh()

    def refresh(self, uri=None):
        try:
            path.get_or_create_file(self._tag_cache_file)
        except EnvironmentError as error:
            logger.warning(
                'Could not create empty tag cache: %s',
                encoding.locale_decode(error))
            return

        tracks = parse_mpd_tag_cache(self._tag_cache_file, self._media_dir)

        for track in tracks:
{'fraction_non_alphanumeric': 0.09453158117846545, 'fraction_numerical': 0.0, 'mean_word_length': 3.041095890410959, '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': '2625266', 'n_tokens_mistral': 689, 'n_tokens_neox': 671, 'n_words': 151}