sonist-gtk/usr/lib/sonist/sonist.py

110 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
import gi, sys, os, re, json
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from utils import run_async
from window import SonistWindow
from about_app import AboutWindow
from preferences import PreferencesWindow
from mpd import MPDClient
APP_ID = 'fun.wkit.sonist'
home_dir = os.getenv('HOME')
def get_music_dir():
mpd_config = f'{home_dir}/.mpd/mpd.conf'
if os.path.isfile(mpd_config):
with open(f'{home_dir}/.mpd/mpd.conf', 'r') as f:
data = f.read()
matches = re.search('music_directory\s*"(.*)"', data).groups()
if len(matches) > 0:
return matches[0]
return '/data/music'
class Application(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self, application_id = APP_ID)
self.music_dir = get_music_dir()
self.album_cache_dir = f"{home_dir}/.cache/sonist/album"
self.lyric_cache_dir = f"{home_dir}/.cache/sonist/lyric"
self.config_dir = f"{home_dir}/.config/sonist"
self.config_file = os.path.join(self.config_dir, 'config.json')
os.makedirs(self.album_cache_dir, exist_ok = True)
os.makedirs(self.lyric_cache_dir, exist_ok = True)
os.makedirs(self.config_dir, exist_ok = True)
self.config_data = {
"host": '127.0.0.1',
"port": 6600,
"music_directory": self.music_dir
}
if os.path.isfile(self.config_file):
with open(self.config_file, 'r') as f:
buff = f.read()
self.config_data = json.loads(buff)
else:
with open(self.config_file, 'w') as f:
buff = json.dumps(self.config_data)
f.write(buff)
self.mpd = MPDClient(self.config_data['host'], self.config_data['port'])
self.connect('window-removed', self.on_window_removed)
@run_async
def connect_mpd(self):
self.mpd.start()
def do_activate(self):
print('hello mpc')
self.set_app_menu(None)
self.set_menubar(None)
self.window = SonistWindow(self)
self.about = AboutWindow()
self.preferences = PreferencesWindow(self, self.mpd)
self.add_window(self.window)
self.window.show_all()
self.connect_mpd()
def on_window_removed(self, app, win):
if len(self.get_windows()) == 0:
self.mpd.destroy()
print('朕要休息了~~~')
def quit_all(self):
self.remove_window(self.window)
if __name__ == "__main__":
app = Application()
app.run(sys.argv)
python + gtk3开发的基于mpd后端的音乐播放器
Python 99.1%
Shell 0.9%