1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import logging
4import os
5import sys
6
7from gooey import Gooey, GooeyParser
8
9from .constants import (
10    RELEASE_URL,
11    WEBSITE,
12    DEV_WEBSITE,
13    DESCRIPTION,
14    LONG_DESCRIPTION,
15    PROJECT_NAME,
16    PROJECT_LICENSE,
17    COPYRIGHT_YEAR,
18    SUBSYNC_RESOURCES_ENV_MAGIC,
19)
20# set the env magic so that we look for resources in the right place
21if SUBSYNC_RESOURCES_ENV_MAGIC not in os.environ:
22    os.environ[SUBSYNC_RESOURCES_ENV_MAGIC] = getattr(sys, '_MEIPASS', '')
23from .ffsubsync import run, add_cli_only_args
24from .version import get_version, update_available
25
26logging.basicConfig(level=logging.INFO)
27logger = logging.getLogger(__name__)
28
29
30_menu = [
31    {
32        'name': 'File',
33        'items': [
34            {
35                'type': 'AboutDialog',
36                'menuTitle': 'About',
37                'name': PROJECT_NAME,
38                'description': LONG_DESCRIPTION,
39                'version': get_version(),
40                'copyright': COPYRIGHT_YEAR,
41                'website': WEBSITE,
42                'developer': DEV_WEBSITE,
43                'license': PROJECT_LICENSE,
44            },
45            {
46                'type': 'Link',
47                'menuTitle': 'Download latest release',
48                'url': RELEASE_URL,
49            }
50        ]
51    }
52]
53
54
55@Gooey(
56    program_name=PROJECT_NAME,
57    image_dir=os.path.join(os.environ[SUBSYNC_RESOURCES_ENV_MAGIC], 'img'),
58    menu=_menu,
59    tabbed_groups=True,
60    progress_regex=r"(\d+)%",
61    hide_progress_msg=True
62)
63def make_parser():
64    description = DESCRIPTION
65    if update_available():
66        description += '\nUpdate available! Please go to "File" -> "Download latest release" to update FFsubsync.'
67    parser = GooeyParser(description=description)
68    main_group = parser.add_argument_group('Basic')
69    main_group.add_argument(
70        'reference',
71        help='Reference (video or subtitles file) to which to synchronize input subtitles.',
72        widget='FileChooser'
73    )
74    main_group.add_argument('srtin', help='Input subtitles file', widget='FileChooser')
75    main_group.add_argument('-o', '--srtout',
76                            help='Output subtitles file (default=${srtin}.synced.srt).',
77                            widget='FileSaver')
78    advanced_group = parser.add_argument_group('Advanced')
79
80    # TODO: these are shared between gui and cli; don't duplicate this code
81    advanced_group.add_argument('--merge-with-reference', '--merge', action='store_true',
82                                help='Merge reference subtitles with synced output subtitles.')
83    advanced_group.add_argument('--make-test-case', '--create-test-case', action='store_true',
84                                help='If specified, create a test archive a few KiB in size '
85                                     'to send to the developer as a debugging aid.')
86    advanced_group.add_argument(
87        '--reference-stream', '--refstream', '--reference-track', '--reftrack', default=None,
88        help='Which stream/track in the video file to use as reference, '
89             'formatted according to ffmpeg conventions. For example, s:0 '
90             'uses the first subtitle track; a:3 would use the fourth audio track.'
91    )
92    return parser
93
94
95def main():
96    parser = make_parser()
97    _ = parser.parse_args()  # Fool Gooey into presenting the simpler menu
98    add_cli_only_args(parser)
99    args = parser.parse_args()
100    args.gui_mode = True
101    return run(args)
102
103
104if __name__ == "__main__":
105    sys.exit(main())
106