<commit_before>from subdaap import provider, config, database, cache, webserver

from daapserver import DaapServer

import logging
import errno
import os

# Logger instance
logger = logging.getLogger(__name__)


class Application(object):

    def __init__(self, config_file, data_dir, verbose=0):
        """
        Construct a new application instance.
        """

        self.config_file = config_file
        self.data_dir = data_dir
        self.verbose = verbose

        self.server = None
        self.provider = None
        self.bonjour = None

        # Setup all parts of the application
        self.setup_config()
        self.setup_provider()
        self.setup_server()

    def setup_config(self):
        """
        Load the application config from file.
        """

        logger.debug("Loading config from %s", self.config_file)
        self.config = config.get_config(self.config_file)

    def setup_provider(self):
        """
        Setup the database connection, the SubSonic connection and provider.
        """

        # Subsonic is imported here, because it somehow causes Python to crash
        # after a fork, when it's opening a database.
        from subdaap import subsonic

        # Initialize database
        db = database.Database(self.config["Provider"]["database"])

        # Initialize connections.
        connections = {}

        for name, section in self.config["SubSonic"].iteritems():
            connections[len(connections) + 1] = subsonic.Connection(
                name, section["url"], section["username"], section["password"])

        # Initialize cache
        artwork_cache_dir = self.get_cache_dir(
            self.config["Provider"]["artwork cache dir"])
        item_cache_dir = self.get_cache_dir(
            self.config["Provider"]["item cache dir"])
        artwork_cache = cache.ArtworkCache(
            artwork_cache_dir,
            self.config["Provider"]["artwork cache size"],
            self.config["Provider"]["artwork cache prune threshold"])
        item_cache = cache.ItemCache(
            item_cache_dir,
            self.config["Provider"]["item cache size"],
            self.config["Provider"]["item cache prune threshold"])

        # Create provider
        logger.debug(
            "Setting up Provider with %d connections", len(connections))

        self.provider = provider.SubSonicProvider(
            db=db,
            connections=connections,
            artwork_cache=artwork_cache,
            item_cache=item_cache,
            state_file=os.path.join(self.get_cache_dir(), "provider.state"),
            transcode=self.config["Provider"]["item transcode"],
            transcode_unsupported=self.config["Provider"]["item transcode"])

    def setup_server(self):
        """
        Create DAAP server.
        """

        logger.debug(
            "Setting up DAAP server at %s:%d",
            self.config["Daap"]["interface"], self.config["Daap"]["port"])

        self.server = DaapServer(
            provider=self.provider,
            server_name=self.config["Daap"]["name"],
            password=self.config["Daap"]["password"],
            ip=self.config["Daap"]["interface"],
            port=self.config["Daap"]["port"],
            cache=self.config["Daap"]["cache"],<commit_msg>fix transcoding: Use correct filetype parameter<commit_after>from subdaap import provider, config, database, cache, webserver

from daapserver import DaapServer

import logging
import errno
import os

# Logger instance
logger = logging.getLogger(__name__)


class Application(object):

    def __init__(self, config_file, data_dir, verbose=0):
        """
        Construct a new application instance.
        """

        self.config_file = config_file
        self.data_dir = data_dir
        self.verbose = verbose

        self.server = None
        self.provider = None
        self.bonjour = None

        # Setup all parts of the application
        self.setup_config()
        self.setup_provider()
        self.setup_server()

    def setup_config(self):
        """
        Load the application config from file.
        """

        logger.debug("Loading config from %s", self.config_file)
        self.config = config.get_config(self.config_file)

    def setup_provider(self):
        """
        Setup the database connection, the SubSonic connection and provider.
        """

        # Subsonic is imported here, because it somehow causes Python to crash
        # after a fork, when it's opening a database.
        from subdaap import subsonic

        # Initialize database
        db = database.Database(self.config["Provider"]["database"])

        # Initialize connections.
        connections = {}

        for name, section in self.config["SubSonic"].iteritems():
            connections[len(connections) + 1] = subsonic.Connection(
                name, section["url"], section["username"], section["password"])

        # Initialize cache
        artwork_cache_dir = self.get_cache_dir(
            self.config["Provider"]["artwork cache dir"])
        item_cache_dir = self.get_cache_dir(
            self.config["Provider"]["item cache dir"])
        artwork_cache = cache.ArtworkCache(
            artwork_cache_dir,
            self.config["Provider"]["artwork cache size"],
            self.config["Provider"]["artwork cache prune threshold"])
        item_cache = cache.ItemCache(
            item_cache_dir,
            self.config["Provider"]["item cache size"],
            self.config["Provider"]["item cache prune threshold"])

        # Create provider
        logger.debug(
            "Setting up Provider with %d connections", len(connections))

        self.provider = provider.SubSonicProvider(
            db=db,
            connections=connections,
            artwork_cache=artwork_cache,
            item_cache=item_cache,
            state_file=os.path.join(self.get_cache_dir(), "provider.state"),
            transcode=self.config["Provider"]["item transcode"],
            transcode_unsupported=self.config["Provider"][
                "item transcode unsupported"])

    def setup_server(self):
        """
        Create DAAP server.
        """

        logger.debug(
            "Setting up DAAP server at %s:%d",
            self.config["Daap"]["interface"], self.config["Daap"]["port"])

        self.server = DaapServer(
            provider=self.provider,
            server_name=self.config["Daap"]["name"],
            password=self.config["Daap"]["password"],
            ip=self.config["Daap"]["interface"],
            port=self.config["Daap"]["port"],
            cache=self.config["Daap"]["cache"],
{'fraction_non_alphanumeric': 0.11297824456114028, 'fraction_numerical': 0.0006001500375093774, 'mean_word_length': 2.3666666666666667, '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': '7113124', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1743, 'n_words': 404}