def run(): """feeluown entry point""" args = setup_argparse().parse_args() config = create_config() # we are trying to run some commands if args.cmd is not None: if args.cmd == 'genicon': return run_cli(args) # if daemon is started, we send commands to daemon directly # we simple think the daemon is started as long as # the port 23333 or 23334 is used if is_port_used(23333) or is_port_used(23334): return run_cli(args) # If daemon is not started, (some) commands can be meaningless, # such as `status`, `toggle`, `next`, etc. However, some other # commands can still be usefule. For instance, when people # want to fetch a song's playable url or see the lyric of a song, # they may run `fuo show fuo://xxx/songs/12345`. When people # want to make an audition of some music, they run # `fuo play fuo://xxx/songs/12345`. Under these circumstances, # we should try to make feeluown work as they expected to. # Currently, we have three such commands: cmds = ('show', 'play', 'search') if args.cmd in cmds: return run_once(args, config) raise SystemExit("can't connect to fuo daemon.") return run_forever(args, config)
def run_forever(args, config): is_daemon_started = is_port_used(23333) or is_port_used(23334) if config.MODE & App.DaemonMode and is_daemon_started: print_error('Fuo daemon is already started.') sys.exit(1) if config.MODE & App.GuiMode: prepare_gui() app = setup_app(args, config) if sys.platform.lower() == 'darwin': enable_mac_hotkey() loop = asyncio.get_event_loop() try: if not config.MODE & App.GuiMode: if config.MODE & App.DaemonMode: # when started with daemon mode, show some message to tell user # that the program is running. print('Fuo daemon running on 0.0.0.0:23333 (tcp)') else: print('Fuo running with no daemon and no window') loop.run_forever() except KeyboardInterrupt: # NOTE: gracefully shutdown? pass finally: loop.stop() app.shutdown() loop.close()
def init(args, config): """ return value: 1: run cli 2: run once (run simple cmd) 3: run loop """ # 根据命令行参数来更新配置 # 注:用户在 rcfile 文件中也可以配置这些选项的值 # 所以我们不能这样实现 config.DEBUG = args.debug,而是像下面这样 config.DEBUG = args.debug or config.DEBUG config.MPV_AUDIO_DEVICE = args.mpv_audio_device or config.MPV_AUDIO_DEVICE config.LOG_TO_FILE = bool(args.log_to_file or config.LOG_TO_FILE) if args.cmd is not None: is_daemon_started = is_port_used(23333) or is_port_used(23334) if is_daemon_started: return 1 # run_cli cli_cmds = ('show', 'play', 'search') if args.cmd not in cli_cmds: print_error('Fuo daemon not started.') sys.exit(1) return 2 # run_once if not args.no_window: try: import PyQt5 # noqa except ImportError: logger.warning('PyQt5 is not installed, can only use daemon mode.') else: config.MODE |= App.GuiMode if not args.no_server: config.MODE |= App.DaemonMode return 3
def check_ports(): if is_port_used(23333) or is_port_used(23334): print('\033[0;31m', end='') print( 'Port(23333 or 23334) is used, maybe another feeluown is running?') print('\033[0m', end='') sys.exit(1)
def main(): parser = setup_argparse() args = parser.parse_args() if args.version: print('feeluown {}, fuocore {}'.format(feeluown_version, fuocore_version)) return if is_port_used(23333) or is_port_used(23334): print('\033[0;31m', end='') print( 'Port(23333 or 23334) is used, maybe another feeluown is running?') print('\033[0m', end='') sys.exit(1) debug = args.debug mpv_audio_device = args.mpv_audio_device cli_only = args.no_window logger_config(debug, to_file=args.log_to_file) load_rcfile() player_kwargs = dict(audio_device=bytes(mpv_audio_device, 'utf-8')) # 设置 exception hook sys.excepthook = excepthook if not cli_only: try: import PyQt5 # noqa except ImportError: logger.warning('PyQt5 is not installed,can only use CLI mode.') cli_only = True if not cli_only: from PyQt5.QtWidgets import QApplication from quamash import QEventLoop q_app = QApplication(sys.argv) q_app.setQuitOnLastWindowClosed(True) q_app.setApplicationName('FeelUOwn') app_event_loop = QEventLoop(q_app) asyncio.set_event_loop(app_event_loop) mode = App.CliMode if cli_only else App.GuiMode app = create_app(mode, **player_kwargs) bind_signals(app) event_loop = asyncio.get_event_loop() try: event_loop.run_forever() logger.info('Event loop stopped.') except KeyboardInterrupt: # NOTE: gracefully shutdown? app.shutdown() finally: event_loop.close()
def run_cli(args, config): is_daemon_started = is_port_used(23333) or is_port_used(23334) if is_daemon_started: climain(args) sys.exit(0) cli_cmds = ('show', 'play', 'search') # 如果服务端已经启动,则将命令发送给服务端处理 if args.cmd not in cli_cmds: print_error('Fuo daemon not started.') sys.exit(1) run_once(args, config)
def run_forever(args, config): is_daemon_started = is_port_used(23333) or is_port_used(23334) if config.MODE & App.DaemonMode and is_daemon_started: print_error('Fuo daemon is already started.') sys.exit(1) if config.MODE & App.GuiMode: prepare_gui() app = setup_app(args, config) if sys.platform.lower() == 'darwin': enable_mac_hotkey(force=config.FORCE_MAC_HOTKEY) loop = asyncio.get_event_loop() try: loop.run_forever() except KeyboardInterrupt: # NOTE: gracefully shutdown? pass finally: loop.stop() app.shutdown() loop.close()
def ensure_daemon_not_started(): if is_port_used(23333) or is_port_used(23334): raise SystemExit("Fuo daemon has already started.")