<commit_before># coding: utf-8
import threading
import webbrowser
from gi.repository import GLib, Gtk, GdkPixbuf
from .base import Protocol as BaseProtocol
from ..utils import res, download, notify, add_tag


class Protocol(BaseProtocol):
    def __init__(self):
        BaseProtocol.__init__(self)
        self.widgets = {}
        self.builder = Gtk.Builder()
        self.builder.add_from_file(res.glade)
        self.builder.connect_signals(self)
        self.builder.get_object('window-player').show_all()
        self.init_indicator()
        self.init_kbps()

    def connectionMade(self):
        BaseProtocol.connectionMade(self)
        self.transport.write(
            'state\nvolume\nkbps\nchannels\nchannel\nuser\nsong')

    def on_channel(self, channel_id):
        BaseProtocol.on_channel(self, channel_id)
        self.channel_id = int(channel_id)
        self.widget_channels[self.channel_id].set_active(True)

    def on_user(self, user):
        BaseProtocol.on_user(self, user)
        self.user = user
        self.set_login_state()

    def on_login_failed(self, message):
        BaseProtocol.on_login_failed(self, message)
        self.alert(Gtk.MessageType.WARNING, '登录失败', message)

    def set_login_state(self):
        if self.user:
            label = '注销'
        else:
            label = '登录'
        self.get_widget('menu-item-login').set_label(label)
        self.get_widget('menu-item-popup-login').set_label(label)

    def on_kbps(self, kbps):
        BaseProtocol.on_kbps(self, kbps)
        self.kbps = kbps
        self.widget_kbps[kbps].set_active(True)

    def init_kbps(self):
        group = Gtk.RadioMenuItem()
        self.widget_kbps = {}
        for kbps in [64, 128, 192]:
            item = Gtk.RadioMenuItem(kbps, visible=True, group=group)
            item.connect('activate', self.set_kbps, kbps)
            self.get_widget('menu-kbps').append(item)
            self.widget_kbps[kbps] = item

    def on_channels(self, channels):
        BaseProtocol.on_channels(self, channels)
        group = Gtk.RadioMenuItem()
        self.widget_channels = {}
        for channel in channels:
            item = Gtk.RadioMenuItem(channel['name'], visible=True, group=group)
            item.connect('activate', self.select_channel, int(channel['channel_id']))
            self.get_widget('menu-channels').append(item)
            self.widget_channels[int(channel['channel_id'])] = item

    def playback(self, widget):
        if self.get_widget('button-playback').get_tooltip_text() == '播放':
            self.transport.write('resume')
        else:
            self.transport.write('pause')

    def on_state(self, state):
        BaseProtocol.on_state(self, state)
        if state == 'paused':
            self.on_pause()
        else:
            self.on_resume()

    def rate(self, widget):
        if not self.rate_flag:
            if self.song['like']:
                self.transport.write('unlike')
            else:
                self.transport.write('like')

    def skip(self, widget):
        self.transport.write('skip')

    def on_skip(self):
        BaseProtocol.on_skip(self)

    def on_play(self, song):
        BaseProtocol.on_play(self, song)

    def on_song(self, song):
        BaseProtocol.on_song(self, song)
        self.song = song
        self.get_widget('image-album-cover').set_from_pixbuf(
            GdkPixbuf.Pixbuf.new_from_file_at_scale(
                song['picture_file'], 240, -1, True))
        self.get_widget('image-album-cover').set_tooltip_text(
            '标题：%s\n艺术家：%s\n专辑：%s' % (
                song['title'],
                song['artist'],
                song['albumtitle']))
        self.get_widget('menu-item-title').set_label(
            self.song['artist'] + ' - ' + self.song['title'])

        if song['like']:
            self.set_like()
        else:
            self.set_unlike()

    def on_like(self):
        BaseProtocol.on_like(self)
        self.set_like()

    def on_unlike(self):
        BaseProtocol.on_unlike(self)
        self.set_unlike()

    def set_like(self):
        self.song['like'] = True
        self.get_widget('button-rate').set_tooltip_text('取消喜欢')
        self.get_widget('menu-item-rate').set_label('取消喜欢')
        self.rate_flag = True
        self.get_widget('button-rate').set_active(True)
        self.rate_flag = False

    def set_unlike(self):
        self.song['like'] = False
        self.get_widget('button-rate').set_tooltip_text('喜欢')
        self.get_widget('menu-item-rate').set_label('喜欢')
        self.rate_flag = True
        self.get_widget('button-rate').set_active(False)
        self.rate_flag = False

    def on_pause(self):
        BaseProtocol.on_pause(self)
        self.get_widget('button-playback').set_image(
            self.get_widget('image-play'))
        self.get_widget('button-playback').set_tooltip_text('播放')
        self.get_widget('menu-item-playback').set_label('播放')

    def on_volume(self, volume):
        BaseProtocol.on_volume(self, volume)
        self.get_widget('volume-button').set_value(float(volume))

    def set_volume(self, widget, value):
        self.transport.write('volume %s' % value)

    def on_resume(self):
        BaseProtocol.on_resume(self)
        self.get_widget('button-playback').set_image(
            self.get_widget('image-pause'))
        self.get_widget('button-playback').set_tooltip_text('暂停')
        self.get_widget('menu-item-playback').set_label('暂停')

    def select_channel(self, widget, channel_id):
        if widget.get_active() and not self.channel_id == channel_id:
            self.transport.write('channel %s' % channel_id)

    def set_kbps(self, widget, kbps):
        if widget.get_active() and not self.kbps == kbps:
            self.transport.write('kbps %s' % kbps)

    def open_album(self, widget):
        webbrowser.open('http://music.douban.com' + self.song['album'])
<commit_msg>修正 gtk 调整音量时重复设置<commit_after># coding: utf-8
import threading
import webbrowser
from gi.repository import GLib, Gtk, GdkPixbuf
from .base import Protocol as BaseProtocol
from ..utils import res, download, notify, add_tag


class Protocol(BaseProtocol):
    def __init__(self):
        BaseProtocol.__init__(self)
        self.widgets = {}
        self.builder = Gtk.Builder()
        self.builder.add_from_file(res.glade)
        self.builder.connect_signals(self)
        self.builder.get_object('window-player').show_all()
        self.init_indicator()
        self.init_kbps()

    def connectionMade(self):
        BaseProtocol.connectionMade(self)
        self.transport.write(
            'state\nvolume\nkbps\nchannels\nchannel\nuser\nsong')

    def on_channel(self, channel_id):
        BaseProtocol.on_channel(self, channel_id)
        self.channel_id = int(channel_id)
        self.widget_channels[self.channel_id].set_active(True)

    def on_user(self, user):
        BaseProtocol.on_user(self, user)
        self.user = user
        self.set_login_state()

    def on_login_failed(self, message):
        BaseProtocol.on_login_failed(self, message)
        self.alert(Gtk.MessageType.WARNING, '登录失败', message)

    def set_login_state(self):
        if self.user:
            label = '注销'
        else:
            label = '登录'
        self.get_widget('menu-item-login').set_label(label)
        self.get_widget('menu-item-popup-login').set_label(label)

    def on_kbps(self, kbps):
        BaseProtocol.on_kbps(self, kbps)
        self.kbps = kbps
        self.widget_kbps[kbps].set_active(True)

    def init_kbps(self):
        group = Gtk.RadioMenuItem()
        self.widget_kbps = {}
        for kbps in [64, 128, 192]:
            item = Gtk.RadioMenuItem(kbps, visible=True, group=group)
            item.connect('activate', self.set_kbps, kbps)
            self.get_widget('menu-kbps').append(item)
            self.widget_kbps[kbps] = item

    def on_channels(self, channels):
        BaseProtocol.on_channels(self, channels)
        group = Gtk.RadioMenuItem()
        self.widget_channels = {}
        for channel in channels:
            item = Gtk.RadioMenuItem(channel['name'], visible=True, group=group)
            item.connect('activate', self.select_channel, int(channel['channel_id']))
            self.get_widget('menu-channels').append(item)
            self.widget_channels[int(channel['channel_id'])] = item

    def playback(self, widget):
        if self.get_widget('button-playback').get_tooltip_text() == '播放':
            self.transport.write('resume')
        else:
            self.transport.write('pause')

    def on_state(self, state):
        BaseProtocol.on_state(self, state)
        if state == 'paused':
            self.on_pause()
        else:
            self.on_resume()

    def rate(self, widget):
        if not self.rate_flag:
            if self.song['like']:
                self.transport.write('unlike')
            else:
                self.transport.write('like')

    def skip(self, widget):
        self.transport.write('skip')

    def on_skip(self):
        BaseProtocol.on_skip(self)

    def on_play(self, song):
        BaseProtocol.on_play(self, song)

    def on_song(self, song):
        BaseProtocol.on_song(self, song)
        self.song = song
        self.get_widget('image-album-cover').set_from_pixbuf(
            GdkPixbuf.Pixbuf.new_from_file_at_scale(
                song['picture_file'], 240, -1, True))
        self.get_widget('image-album-cover').set_tooltip_text(
            '标题：%s\n艺术家：%s\n专辑：%s' % (
                song['title'],
                song['artist'],
                song['albumtitle']))
        self.get_widget('menu-item-title').set_label(
            self.song['artist'] + ' - ' + self.song['title'])

        if song['like']:
            self.set_like()
        else:
            self.set_unlike()

    def on_like(self):
        BaseProtocol.on_like(self)
        self.set_like()

    def on_unlike(self):
        BaseProtocol.on_unlike(self)
        self.set_unlike()

    def set_like(self):
        self.song['like'] = True
        self.get_widget('button-rate').set_tooltip_text('取消喜欢')
        self.get_widget('menu-item-rate').set_label('取消喜欢')
        self.rate_flag = True
        self.get_widget('button-rate').set_active(True)
        self.rate_flag = False

    def set_unlike(self):
        self.song['like'] = False
        self.get_widget('button-rate').set_tooltip_text('喜欢')
        self.get_widget('menu-item-rate').set_label('喜欢')
        self.rate_flag = True
        self.get_widget('button-rate').set_active(False)
        self.rate_flag = False

    def on_pause(self):
        BaseProtocol.on_pause(self)
        self.get_widget('button-playback').set_image(
            self.get_widget('image-play'))
        self.get_widget('button-playback').set_tooltip_text('播放')
        self.get_widget('menu-item-playback').set_label('播放')

    def on_volume(self, volume):
        BaseProtocol.on_volume(self, volume)
        self.volume = float(volume)
        self.get_widget('volume-button').set_value(self.volume)

    def set_volume(self, widget, value):
        if self.volume != value:
            self.transport.write('volume %s' % value)

    def on_resume(self):
        BaseProtocol.on_resume(self)
        self.get_widget('button-playback').set_image(
            self.get_widget('image-pause'))
        self.get_widget('button-playback').set_tooltip_text('暂停')
        self.get_widget('menu-item-playback').set_label('暂停')

    def select_channel(self, widget, channel_id):
        if widget.get_active() and not self.channel_id == channel_id:
            self.transport.write('channel %s' % channel_id)

    def set_kbps(self, widget, kbps):
        if widget.get_active() and not self.kbps == kbps:
            self.transport.write('kbps %s' % kbps)

    def open_album(self, widget):
        webbrowser.open('http://music.douban.com' + self.song['album'])

{'fraction_non_alphanumeric': 0.13055508971993962, 'fraction_numerical': 0.0021801106825423446, 'mean_word_length': 2.4945795487840607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5287381', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3817, 'n_words': 581}