File: launch.py

package info (click to toggle)
mpd-sima 0.18.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: python: 4,364; sh: 222; makefile: 166
file content (212 lines) | stat: -rw-r--r-- 7,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
# Copyright (c) 2013, 2014, 2015, 2020-2022 kaliko <kaliko@azylum.org>
#
#  This file is part of sima
#
#  sima is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  sima is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with sima.  If not, see <http://www.gnu.org/licenses/>.
#
#
"""Sima
"""

# standard library import
import logging
import sys

from importlib import __import__ as sima_import
from os.path import isfile
from os import rename
##

# third parties components
##

# local import
from . import core, info
from .lib.logger import set_logger
from .lib.simadb import SimaDB
from .mpdclient import PlayerError
from .utils.config import ConfMan
from .utils.startopt import StartOpt
from .utils.utils import exception_log, SigHup, MPDSimaException
from .utils.blcli import BLCli
# core plugins
from .plugins.core.history import History
from .plugins.core.mpdoptions import MpdOptions
from .plugins.core.uniq import Uniq
##


def load_plugins(sima, source):
    """Handles internal/external plugins
        sima:   sima.core.Sima instance
        source: ['internal', 'contrib']
    """# pylint: disable=logging-not-lazy,logging-format-interpolation
    if not sima.config.get('sima', source):
        return
    logger = logging.getLogger('sima')
    # TODO: Sanity check for "sima.config.get('sima', source)" ?
    for plugin in sima.config.get('sima', source).split(','):
        plugin = plugin.strip(' \n')
        module = f'sima.plugins.{source}.{plugin.lower()}'
        try:
            mod_obj = sima_import(module, fromlist=[plugin])
        except ImportError as err:
            logger.error(f'Failed to load "{plugin}" plugin\'s module: ' +
                         f'{module} ({err})')
            sima.shutdown()
            sys.exit(1)
        try:
            plugin_obj = getattr(mod_obj, plugin)
        except AttributeError as err:
            logger.error('Failed to load plugin %s (%s)', plugin, err)
            sima.shutdown()
            sys.exit(1)
        logger.info('Loading {0} plugin: {name} ({doc})'.format(
            source, **plugin_obj.info()))
        sima.register_plugin(plugin_obj)


def start(sopt, restart=False):
    """starts application
    """
    # loads configuration
    cfg_mgmt = ConfMan(sopt.options)
    config = cfg_mgmt.config
    # set logger
    logger = logging.getLogger('sima')
    logfile = config.get('log', 'logfile', fallback=None)
    verbosity = config.get('log', 'verbosity')
    if sopt.options.get('command'):  # disable file logging
        set_logger(verbosity)
    else:
        set_logger(verbosity, logfile)
    logger.debug('Command line say: %s', sopt.options)

    # Create database if not present
    db_file = config.get('sima', 'db_file')
    if not isfile(db_file):
        logger.debug('Creating database in "%s"', db_file)
        SimaDB(db_path=db_file).create_db()
    # Migration from v0.17.0
    dbinfo = SimaDB(db_path=db_file).get_info()
    if not dbinfo:  # v0.17.0 → v0.18+ migration
        logger.warning('Backing up database!')
        rename(db_file, db_file + '-old-version-backup')
        logger.info('Creating an new database in "%s"', db_file)
        SimaDB(db_path=db_file).create_db()

    if sopt.options.get('command'):
        cmd = sopt.options.get('command')
        if cmd.startswith('bl-'):
            BLCli(config, sopt.options)
            sys.exit(0)
        if cmd == "generate-config":
            config.write(sys.stdout, space_around_delimiters=True)
            sys.exit(0)
        logger.info('Running "%s" and exit', cmd)
        if cmd == "config-test":
            logger.info('Config location: "%s"', cfg_mgmt.conf_file)
            from .utils.configtest import config_test
            config_test(config)
            sys.exit(0)
        if cmd == "create-db":
            if not isfile(db_file):
                logger.info('Creating database in "%s"', db_file)
                SimaDB(db_path=db_file).create_db()
            else:
                logger.info('Database already there, not overwriting %s', db_file)
            logger.info('Done, bye...')
            sys.exit(0)
        if cmd == "purge-history":
            db_file = config.get('sima', 'db_file')
            if not isfile(db_file):
                logger.warning('No db found: %s', db_file)
                sys.exit(1)
            SimaDB(db_path=db_file).purge_history(duration=0)
            sys.exit(0)
        if cmd == 'random':
            config['sima']['internal'] = 'Crop, Random'
            if sopt.options.get('nbtracks'):
                config['random']['track_to_add'] = str(sopt.options.get('nbtracks'))

    logger.info('Starting (%s)...', info.__version__)
    sima = core.Sima(config)

    # required core plugins
    core_plugins = [History, MpdOptions, Uniq]
    if config.getboolean('sima', 'mopidy_compat'):
        logger.warning('Running with mopidy compat. mode!')
        core_plugins = [History, MpdOptions]
        config['sima']['musicbrainzid'] = 'False'
    for cplgn in core_plugins:
        logger.debug('Register core %(name)s (%(doc)s)', cplgn.info())
        sima.register_core_plugin(cplgn)
    logger.debug('core loaded, prioriy: %s',
                 ' > '.join(map(str, sima.core_plugins)))

    #  Loading internal plugins
    load_plugins(sima, 'internal')
    #  Loading contrib plugins
    load_plugins(sima, 'contrib')
    logger.info('plugins loaded, prioriy: %s', ' > '.join(map(str, sima.plugins)))

    # Run as a daemon
    if config.getboolean('daemon', 'daemon'):
        if restart:
            sima.run()
        else:
            logger.info('Daemonize process...')
            sima.start()

    try:
        sima.foreground()
    except KeyboardInterrupt:
        logger.info('Caught KeyboardInterrupt, stopping')
        sys.exit(0)


def run(sopt, restart=False):
    """
    Handles SigHup exception
    Catches Unhandled exception
    """
    # pylint: disable=broad-except
    logger = logging.getLogger('sima')
    try:
        start(sopt, restart)
    except SigHup:  # SigHup inherit from Exception
        run(sopt, True)
    except (MPDSimaException, PlayerError) as err:
        logger.error(err)
        sys.exit(2)
    except Exception:  # Unhandled exception
        exception_log()


# Script starts here
def main():
    """Entry point"""
    nfo = dict({'version': info.__version__,
                'prog': 'mpd-sima'})
    # StartOpt gathers options from command line call (in StartOpt().options)
    sopt = StartOpt(nfo)
    run(sopt)


if __name__ == '__main__':
    main()

# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab