gnome-music.in 4.82 KB
Newer Older
Vadim Rutkovsky's avatar
Vadim Rutkovsky committed
1
#!/usr/bin/env python3
2
# Copyright © 2018 The GNOME Music Developers
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#
# GNOME Music 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 2 of the License, or
# (at your option) any later version.
#
# GNOME Music 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 GNOME Music; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# The GNOME Music authors hereby grant permission for non-GPL compatible
# GStreamer plugins to be used and distributed together with GStreamer
# and GNOME Music.  This permission is above and beyond the permissions
# granted by the GPL license by which GNOME Music is covered.  If you
# modify this code, you may extend this exception to your version of the
# code, but you are not obligated to do so.  If you do not wish to do so,
# delete this exception statement from your version.
Guillaume Quintard's avatar
Guillaume Quintard committed
25

26
import argparse
27
import gettext
28 29 30 31 32
import locale
import logging
import os
import signal
import sys
33 34

# Make sure we'll find the pygobject module, even in JHBuild
35
sys.path.insert(1, '@pyexecdir@')
36 37
# Make sure we'll find the gnomemusic module, even in JHBuild
sys.path.insert(1, '@pythondir@')
38

39 40 41 42
_LOCAL = @local_build@

# In the local use case the installed schemas go in <builddir>/data
if _LOCAL:
43
    os.environ["XDG_DATA_DIRS"] = '@schemasdir@:' + os.environ.get("XDG_DATA_DIRS", "")
44

45
import gi
46

47
gi.require_version('Gtk', '3.0')
48
gi.require_version('GIRepository', '2.0')
49 50 51 52
gi.require_version('Gst', '1.0')
from gi.repository import GIRepository, Gio, Gtk, Gst

Gst.init(None)
Guillaume Quintard's avatar
Guillaume Quintard committed
53

54
LOCALE_DIR = '@localedir@'
55
PKGDATA_DIR = '@pkgdatadir@'
56 57 58 59 60 61 62 63

# Log settings
LOG_FORMAT = '%(asctime)s %(levelname)s\t%(message)s'
LOG_DATE_FORMAT = '%H:%M:%S'


def set_libgd():
    """Configures application to run our own libgd copy."""
64
    libgd_libdir = '@pkglibdir@'
65
    if _LOCAL:
66
        libgd_typelibdir = '@pkglibdir@'
67 68 69 70 71 72
    else:
        libgd_typelibdir = '@pkglibdir@/girepository-1.0'

    GIRepository.Repository.prepend_search_path(libgd_typelibdir)
    GIRepository.Repository.prepend_library_path(libgd_libdir)

Marinus Schraal's avatar
Marinus Schraal committed
73 74 75 76 77 78 79 80 81 82
def set_gfm():
    """Configures application to use gfm."""
    gfm_libdir = '@gfmlibdir@'
    if _LOCAL:
        gfm_typelibdir = '@gfmlibdir@'
    else:
        gfm_typelibdir = '@gfmlibdir@/girepository-1.0'

    GIRepository.Repository.prepend_search_path(gfm_typelibdir)
    GIRepository.Repository.prepend_library_path(gfm_libdir)
83 84 85

def set_exception_hook():
    """Configures sys.excepthook to enforce Gtk application exiting."""
86

87 88 89 90 91
    def new_hook(etype, evalue, etb):
        old_hook(etype, evalue, etb)
        while Gtk.main_level():
            Gtk.main_quit()
        sys.exit()
92 93

    old_hook = sys.excepthook
94 95 96
    sys.excepthook = new_hook


97 98
def set_log_level():
    """Sets application log level according to debug value."""
99
    parser = argparse.ArgumentParser()
100 101
    parser.add_argument('-d', '--debug', action='store_true',
                        default=False, dest='debug')
102 103
    args = parser.parse_args()
    if args.debug:
104 105
        logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT,
                            datefmt=LOG_DATE_FORMAT)
106 107
        # Gtk hates "-d" switch, so lets drop it
        if '-d' in sys.argv:
108
            sys.argv.remove('-d')
109
        if '--debug' in sys.argv:
110
            sys.argv.remove('--debug')
111
    else:
112 113 114
        logging.basicConfig(level=logging.WARN, format=LOG_FORMAT,
                            datefmt=LOG_DATE_FORMAT)

115

116 117
def set_internationalization():
    """Sets application internationalization."""
118 119 120 121
    locale.bindtextdomain('@application_id@', LOCALE_DIR)
    locale.textdomain('@application_id@')
    gettext.bindtextdomain('@application_id@', LOCALE_DIR)
    gettext.textdomain('@application_id@')
Arnel A. Borja's avatar
Arnel A. Borja committed
122

123

124 125 126
def set_resources():
    """Sets application ressource file."""
    resource = Gio.resource_load(
127
        os.path.join(PKGDATA_DIR, '@rdnn_name@.gresource'))
128 129 130 131 132
    Gio.Resource._register(resource)  # nopep8


def run_application():
    """Runs GNOME Music application and returns its exit code."""
133 134
    from gnomemusic.application import Application

135
    app = Application('@application_id@')
Vadim Rutkovsky's avatar
Vadim Rutkovsky committed
136
    signal.signal(signal.SIGINT, signal.SIG_DFL)
137 138 139 140 141 142
    return app.run(sys.argv)


def main():
    """Sets environment and runs GNOME Music."""
    set_libgd()
Marinus Schraal's avatar
Marinus Schraal committed
143
    set_gfm()
144 145 146 147 148 149 150 151 152 153
    set_exception_hook()
    set_log_level()
    set_internationalization()
    set_resources()
    return run_application()

if __name__ == '__main__':
    if _LOCAL:
        print('Running from source tree, using local files.')
    sys.exit(main())