From 0e08434af416ee51b0b7851217d63c6d05e34767 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Fri, 31 Jan 2020 21:44:07 -0700 Subject: [PATCH] Closes #105: Moved parameter handling to argparse Doing this because it sucks way less to do this in pure Python than in GTK. --- sublime/__main__.py | 19 ++++++++++++++++++- sublime/app.py | 46 +++------------------------------------------ 2 files changed, 21 insertions(+), 44 deletions(-) diff --git a/sublime/__main__.py b/sublime/__main__.py index f7b3c79..fe610f0 100644 --- a/sublime/__main__.py +++ b/sublime/__main__.py @@ -1,4 +1,5 @@ #! /usr/bin/env python3 +import os import argparse import logging @@ -29,6 +30,12 @@ def main(): help='the minium level of logging to do', default='WARNING', ) + parser.add_argument( + '-c', + '--config', + help='specify a configuration file. Defaults to ' + '~/.config/sublime-music/config.json', + ) args, unknown_args = parser.parse_known_args() if args.version: @@ -46,5 +53,15 @@ def main(): format='%(asctime)s:%(levelname)s:%(name)s:%(module)s:%(message)s', ) - app = SublimeMusicApp() + # Config File + config_file = args.config + if not config_file: + # Default to ~/.config/sublime-music. + config_folder = ( + os.environ.get('XDG_CONFIG_HOME') or os.environ.get('APPDATA') + or os.path.join(os.environ.get('HOME'), '.config')) + config_folder = os.path.join(config_folder, 'sublime-music') + config_file = os.path.join(config_folder, 'config.json') + + app = SublimeMusicApp(config_file) app.run(unknown_args) diff --git a/sublime/app.py b/sublime/app.py index 2299e0c..4fe97bc 100644 --- a/sublime/app.py +++ b/sublime/app.py @@ -3,8 +3,6 @@ import logging import math import random -from os import environ - import gi gi.require_version('Gtk', '3.0') gi.require_version('Notify', '0.7') @@ -22,53 +20,15 @@ from .ui.common.players import PlayerEvent, MPVPlayer, ChromecastPlayer class SublimeMusicApp(Gtk.Application): - def __init__(self, *args, **kwargs): - super().__init__( - *args, - application_id="com.sumnerevans.sublimemusic", - flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, - **kwargs, - ) + def __init__(self, config_file): + super().__init__(application_id="com.sumnerevans.sublimemusic") Notify.init('Sublime Music') self.window = None self.state = ApplicationState() - - # Specify Command Line Options - self.add_main_option( - 'config', - ord('c'), - GLib.OptionFlags.NONE, - GLib.OptionArg.FILENAME, - 'Specify a configuration file. Defaults to ' - '~/.config/sublime-music/config.json', - None, - ) - - self.connect('shutdown', self.on_app_shutdown) - - # Handle command line option parsing. - def do_command_line(self, command_line): - options = command_line.get_options_dict() - - # Config File - config_file = options.lookup_value('config') - if config_file: - config_file = config_file.get_bytestring().decode('utf-8') - else: - # Default to ~/.config/sublime-music. - config_folder = ( - environ.get('XDG_CONFIG_HOME') or environ.get('APPDATA') - or os.path.join(environ.get('HOME'), '.config')) - config_folder = os.path.join(config_folder, 'sublime-music') - config_file = os.path.join(config_folder, 'config.json') - self.state.config_file = config_file - # Have to do this or else the application doesn't work. Not entirely - # sure why, but C-bindings... - self.activate() - return 0 + self.connect('shutdown', self.on_app_shutdown) def do_startup(self): Gtk.Application.do_startup(self)