gaupol/main.py

Source code for module gaupol.main from file gaupol/main.py.

  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
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.

"""GTK+ user interface initialization."""

import aeidon
import atexit
import gaupol
import gettext
import locale
import optparse
import os
import re
import sys
_ = aeidon.i18n._

from gi.repository import Gtk


def _init_application(opts, args):
    """Initialize application and open files from `args`."""
    application = gaupol.Application()
    paths = list(map(os.path.abspath, args))
    application.open_main(paths, opts.encoding)
    page = application.get_current_page()
    if page is None: return
    if opts.translation_file is not None:
        path = os.path.abspath(opts.translation_file)
        align_method = opts.align_method.upper()
        align_method = getattr(aeidon.align_methods, align_method)
        application.open_translation(path, opts.encoding, align_method)
    if opts.video_file is not None:
        path = os.path.abspath(opts.video_file)
        page.project.video_path = path
        application.update_gui()
    if opts.jump is not None:
        page.view.set_focus(opts.jump)
        page.view.scroll_to_row(opts.jump)

def _init_configuration():
    """Read configuration values from file at `path`."""
    path = os.path.join(aeidon.CONFIG_HOME_DIR, "gaupol.conf")
    gaupol.conf.path = path
    gaupol.conf.read_from_file()
    atexit.register(gaupol.conf.write_to_file)
    Gtk.Settings.get_default().set_property(
        "gtk-application-prefer-dark_theme",
        gaupol.conf.general.dark_theme)

def _init_gettext():
    """Initialize translation settings."""
    try:
        # Might fail with misconfigured locales.
        locale.setlocale(locale.LC_ALL, "")
    except Exception:
        print("Failed to set default locale.", file=sys.stderr)
        print("Please check your locale settings.", file=sys.stderr)
        print("Falling back to the 'C' locale.", file=sys.stderr)
        locale.setlocale(locale.LC_ALL, "C")
    try:
        # Not available on all platforms.
        locale.bindtextdomain("gaupol", aeidon.LOCALE_DIR)
        locale.textdomain("gaupol")
    except AttributeError:
        pass
    gettext.bindtextdomain("gaupol", aeidon.LOCALE_DIR)
    gettext.textdomain("gaupol")

def main(args):
    """Parse arguments from `args` and start application."""
    opts, args = _parse_args(args)
    sys.excepthook = gaupol.util.show_exception
    _init_gettext()
    _init_configuration()
    _init_application(opts, args)
    Gtk.main()

def _on_parser_list_encodings(*args):
    """List all available character encodings and exit."""
    encodings = [x[0] for x in aeidon.encodings.get_valid()]
    if aeidon.util.chardet_available():
        encodings.insert(0, "auto")
    print(", ".join(encodings))
    raise SystemExit(0)

def _on_parser_version(*args):
    """Show the version number and exit."""
    print("gaupol {}".format(gaupol.__version__))
    raise SystemExit(0)

def _parse_args(args):
    """Parse and return options and arguments from `args`."""
    parser = optparse.OptionParser(
        formatter=optparse.IndentedHelpFormatter(2, 42),
        usage=_("gaupol [OPTION...] [FILE...] [+[NUM]]"))

    parser.add_option(
        "--version",
        action="callback",
        callback=_on_parser_version,
        help=_("show version number and exit"))

    parser.add_option(
        "-e", "--encoding",
        action="store",
        type="string",
        metavar=_("ENCODING"),
        dest="encoding",
        default=None,
        help=_("set the character encoding used to open files"))

    parser.add_option(
        "--list-encodings",
        action="callback",
        callback=_on_parser_list_encodings,
        help=_("list all available character encodings"))

    parser.add_option(
        "-t", "--translation-file",
        action="store",
        type="string",
        metavar=_("FILE"),
        dest="translation_file",
        default=None,
        help=_("open translation file"))

    parser.add_option(
        "-a", "--align-method",
        action="store",
        type="string",
        metavar=_("METHOD"),
        dest="align_method",
        default="position",
        help=_("method used to align translation subtitles: "
               "'number' or 'position'"))

    parser.add_option(
        "-v", "--video-file",
        action="store",
        type="string",
        metavar=_("FILE"),
        dest="video_file",
        default=None,
        help=_("select video file"))

    opts, args = parser.parse_args(args)
    # Translate encoding if an alias given.
    if not opts.encoding in (None, "auto"):
        try:
            opts.encoding = aeidon.encodings.translate_code(opts.encoding)
        except ValueError:
            raise SystemExit("Unrecognized encoding: {}"
                             .format(repr(opts.encoding)))

    # Parse row to jump to.
    opts.jump = None
    re_jump = re.compile(r"\+\d*")
    for arg in filter(re_jump.match, args):
        opts.jump = (max(0, int(arg[1:])-1) if arg[1:] else -1)
        args.remove(arg)
    return opts, args