コード例 #1
0
 def __init__(self, seconds=60, max_emails=24, max_email_time=60 * 60 * 4):
     self.seconds = seconds
     self.max_emails = max_emails
     self.max_email_time = max_email_time
     self.errorno = 0
     self.errortime = None
     self.mail_times = []
     RepeatTimer.__init__(self, seconds, self.do_bot_check)
コード例 #2
0
ファイル: bot.py プロジェクト: AlexUlrich/digsby
 def __init__(self, seconds=60, max_emails=24, max_email_time=60*60*4):
     self.seconds = seconds
     self.max_emails = max_emails
     self.max_email_time = max_email_time
     self.errorno = 0
     self.errortime = None
     self.mail_times = []
     RepeatTimer.__init__(self, seconds, self.do_bot_check)
コード例 #3
0
ファイル: UpdateMixin.py プロジェクト: AlexUlrich/digsby
    def set_enabled(self, value):
        # the first time "enabled" is set, create a timer
        has_been_set = hasattr(self, '_enabled')
        if not has_been_set:
            self.change_reason(self.Reasons.NONE)

            if self.update_mixin_timer:
                info('%s creating a timer with update frequency %s', self,self.updatefreq)
                self.timer = RepeatTimer(int(self.updatefreq), self.update_now)
                self.timer.start(); self.timer.stop()
                if get(self, 'on_connect', None) is not None and self.update_now not in self.on_connect:
                    self.on_connect += self.update_now

        # when enabled, start the timer.
        self._enabled = value
        if value:
            info('enabling timer for %s', self)
            if self.OFFLINE and getattr(self, '_needs_connect', True):
                self._needs_connect = False
                get(self, 'Connect', nothing)()
            if self.update_mixin_timer:
                self.timer.reset(int(self.updatefreq))
        # when disabled, stop the timer.
        else:
            if has_been_set:
                if not self.OFFLINE:
                    get(self, 'Disconnect',nothing)()
                self._needs_connect = True

                if self.update_mixin_timer:
                    info('stopping timer for %s', self)
                    self.timer.stop()

                get(self, 'on_disable', nothing)()
コード例 #4
0
    def set_enabled(self, value):
        # the first time "enabled" is set, create a timer
        has_been_set = hasattr(self, '_enabled')
        if not has_been_set:
            self.change_reason(self.Reasons.NONE)

            if self.update_mixin_timer:
                info('%s creating a timer with update frequency %s', self,
                     self.updatefreq)
                self.timer = RepeatTimer(int(self.updatefreq), self.update_now)
                self.timer.start()
                self.timer.stop()
                if get(
                        self, 'on_connect', None
                ) is not None and self.update_now not in self.on_connect:
                    self.on_connect += self.update_now

        # when enabled, start the timer.
        self._enabled = value
        if value:
            info('enabling timer for %s', self)
            if self.OFFLINE and getattr(self, '_needs_connect', True):
                self._needs_connect = False
                get(self, 'Connect', nothing)()
            if self.update_mixin_timer:
                self.timer.reset(int(self.updatefreq))
        # when disabled, stop the timer.
        else:
            if has_been_set:
                if not self.OFFLINE:
                    get(self, 'Disconnect', nothing)()
                self._needs_connect = True

                if self.update_mixin_timer:
                    info('stopping timer for %s', self)
                    self.timer.stop()

                get(self, 'on_disable', nothing)()
コード例 #5
0
    def _info_changed(self, timer_name = 'network_timer', update_frequency = UPDATE_FREQ_SECS):
        '''
        Called when information in the buddylist store object changes.

        It resets a timer, and every so many seconds after no changes have occurred,
        the changes are synced with the server.
        '''

        t = getattr(self, timer_name, None)
        if t is None:
            t = RepeatTimer(update_frequency, lambda: self._on_timer(timer_name))
            setattr(self, timer_name, t)
            t.start()
        else:
            t.reset()
コード例 #6
0
ファイル: nowplaying.py プロジェクト: sgricci/digsby
    def on_before_status_change(self, status):
        '''
        Invoked when the profile's status message changes.
        '''
        log.info('on_status_change')

        is_music_status = isinstance(status, NowPlayingStatus)
        timer           = getattr(self, 'timer', None)
        timer_alive     = timer is not None and self.timer.isAlive()

        # If the status is a "music status" and our timer isn't running, start it.
        if is_music_status and not timer_alive:
            self.link()
            log.debug('starting now playing timer')
            if timer is None:
                self.timer = timer = RepeatTimer(5, lambda: wx.CallAfter(self.on_timer))
            timer.start()
            # call it now to look responsive! yay
        # Otherwise if the status is not a "music status" and the timer IS running, stop it.
        elif not is_music_status and timer_alive:
            log.debug('stopping now playing timer')
            self.song = (None, 0)
            self.unlink()
            self.timer.stop()
            releaseAll()
        if is_music_status:
            s = status.status
            if pref(NOWPLAYING_STATUS_PREF, type = str, default = 'available') != s:
                profile.prefs.__setitem__(NOWPLAYING_STATUS_PREF, s.lower())

        if not is_music_status:
            return status

        try:
            if status is not self.status:
                self.check_song(force=True, status=status)
            return self.status
        except Exception:
            traceback.print_exc()
            return status
コード例 #7
0
ファイル: UpdateMixin.py プロジェクト: AlexUlrich/digsby
class UpdateMixin(StateMixin):
    updatefreq = 300 # in seconds
    update_mixin_timer = True

    def __init__(self, updatefreq=None, *a, **k):
        StateMixin.__init__(self, *a, **k)
        if updatefreq is not None:
            try:
                updatefreq = int(updatefreq)
            except ValueError:
                pass
            else:
                if updatefreq < 1:
                    updatefreq = 60
                self.updatefreq =  max(updatefreq, 15)
        self.on_connect = Delegate()
        self.on_disable = Delegate()

    def disconnect(self):
        if self.update_mixin_timer:
            self.timer.stop()

    def update_now(self):
        raise NotImplementedError

    def get_options(self):
        options = dict((a, getattr(self, a)) for a in ('enabled', 'updatefreq'))
        if self.alias is not None:
            options['alias'] = self.alias
        return options

    def get_enabled(self):
        return get(self, '_enabled', False)

    def set_enabled(self, value):
        # the first time "enabled" is set, create a timer
        has_been_set = hasattr(self, '_enabled')
        if not has_been_set:
            self.change_reason(self.Reasons.NONE)

            if self.update_mixin_timer:
                info('%s creating a timer with update frequency %s', self,self.updatefreq)
                self.timer = RepeatTimer(int(self.updatefreq), self.update_now)
                self.timer.start(); self.timer.stop()
                if get(self, 'on_connect', None) is not None and self.update_now not in self.on_connect:
                    self.on_connect += self.update_now

        # when enabled, start the timer.
        self._enabled = value
        if value:
            info('enabling timer for %s', self)
            if self.OFFLINE and getattr(self, '_needs_connect', True):
                self._needs_connect = False
                get(self, 'Connect', nothing)()
            if self.update_mixin_timer:
                self.timer.reset(int(self.updatefreq))
        # when disabled, stop the timer.
        else:
            if has_been_set:
                if not self.OFFLINE:
                    get(self, 'Disconnect',nothing)()
                self._needs_connect = True

                if self.update_mixin_timer:
                    info('stopping timer for %s', self)
                    self.timer.stop()

                get(self, 'on_disable', nothing)()

    enabled = property(get_enabled, set_enabled)

    def pause_timer(self):
        with self.timer._cv:
            if not self.timer.paused:
                self.timer.pause()

    def start_timer(self):
        with self.timer._cv:
            if self.timer.paused:
                self.timer.start()
コード例 #8
0
ファイル: diagnostic.py プロジェクト: AlexUlrich/digsby
 def start(self):
     INFO('Diagnostic Scheduled')
     RepeatTimer.start(self)
     self._on_timer() # do one run right away
コード例 #9
0
ファイル: diagnostic.py プロジェクト: AlexUlrich/digsby
 def __init__(self, interval):
     RepeatTimer.__init__(self, interval, self._on_timer)
     self.diagnostic = None
     self._diaglock = threading.Lock()
コード例 #10
0
ファイル: nowplaying.py プロジェクト: sgricci/digsby
 def setup(self):
     self.links = []
     self.timer = RepeatTimer(5, lambda: wx.CallAfter(self.on_timer))
     self.song = (None, 0)
     self.status = None
     self.on_before_status_change(profile.status)
コード例 #11
0
ファイル: nowplaying.py プロジェクト: sgricci/digsby
class NowPlaying(Plugin):

    edit_toggle = False
    SONG_THRESHOLD = 1
    snipr_cache = SniprSongLRU(20)
    inited = False

    def setup(self):
        self.links = []
        self.timer = RepeatTimer(5, lambda: wx.CallAfter(self.on_timer))
        self.song = (None, 0)
        self.status = None
        self.on_before_status_change(profile.status)

    def link(self):
        self.links.append(profile.prefs.link('plugins.nowplaying.format', self.format_string_change, callnow=False))
        self.links.append(profile.prefs.link('plugins.nowplaying.backup_format', self.format_string_change, callnow=False))

    def unlink(self):
        while self.links:
            link = self.links.pop(0)
            link.unlink()

    def format_string_change(self, value):
        log.info('Format string changed to %r', value)
        self.check_song(force=True)

    def on_before_status_change(self, status):
        '''
        Invoked when the profile's status message changes.
        '''
        log.info('on_status_change')

        is_music_status = isinstance(status, NowPlayingStatus)
        timer           = getattr(self, 'timer', None)
        timer_alive     = timer is not None and self.timer.isAlive()

        # If the status is a "music status" and our timer isn't running, start it.
        if is_music_status and not timer_alive:
            self.link()
            log.debug('starting now playing timer')
            if timer is None:
                self.timer = timer = RepeatTimer(5, lambda: wx.CallAfter(self.on_timer))
            timer.start()
            # call it now to look responsive! yay
        # Otherwise if the status is not a "music status" and the timer IS running, stop it.
        elif not is_music_status and timer_alive:
            log.debug('stopping now playing timer')
            self.song = (None, 0)
            self.unlink()
            self.timer.stop()
            releaseAll()
        if is_music_status:
            s = status.status
            if pref(NOWPLAYING_STATUS_PREF, type = str, default = 'available') != s:
                profile.prefs.__setitem__(NOWPLAYING_STATUS_PREF, s.lower())

        if not is_music_status:
            return status

        try:
            if status is not self.status:
                self.check_song(force=True, status=status)
            return self.status
        except Exception:
            traceback.print_exc()
            return status

    def on_timer(self):
        'Invoked when the song check timer goes off. Which happens a lot.'
        status = profile.status
        if not isinstance(status, NowPlayingStatus):
            # early exit for when the status message is not a music status
            if hasattr(self, 'timer'):
                self.timer.stop()
            return

        self.check_song()
        try:
            new_message = self.status.message
        except Exception:
            traceback.print_exc()
            return

        if new_message != profile.status.message:
            log.info('current message: %r', profile.status.message)
            log.info('setting new now playing status: message=%r, cursong=%r', new_message, profile.status.message)
            profile.set_status(self.status)

    def check_song(self, force=False, status=None):
        cursong = currentSong()
        song = cursong.format_string % cursong.format_args
        new_message = ' '.join([NOTES_SYMBOL, song])

        oldsong, songcount = self.song

        def got_new_song():
            # on a new song, store the name, and the "count" of times we've
            # seen it
            self.snipr_cache[amazon_format(cursong.format_args)]
            self.song = (cursong, 0)

        def set_new_song(status = status):
            if status is None:
                status = profile.status
            self.status = status.copy(message = new_message, **cursong)

        if oldsong is None:
            got_new_song()
            set_new_song()

        elif cursong != oldsong:
            got_new_song()
            if force:
                set_new_song()
        else:
            # only set the profile status after a certain period of time passed
            # with the same song present
            songcount += 1
            self.song = (cursong, songcount)

            # only set the song after (N * five) seconds of play time
            if force or (songcount >= self.SONG_THRESHOLD):
                set_new_song()
コード例 #12
0
class UpdateMixin(StateMixin):
    updatefreq = 300  # in seconds
    update_mixin_timer = True

    def __init__(self, updatefreq=None, *a, **k):
        StateMixin.__init__(self, *a, **k)
        if updatefreq is not None:
            try:
                updatefreq = int(updatefreq)
            except ValueError:
                pass
            else:
                if updatefreq < 1:
                    updatefreq = 60
                self.updatefreq = max(updatefreq, 15)
        self.on_connect = Delegate()
        self.on_disable = Delegate()

    def disconnect(self):
        if self.update_mixin_timer:
            self.timer.stop()

    def update_now(self):
        raise NotImplementedError

    def get_options(self):
        options = dict(
            (a, getattr(self, a)) for a in ('enabled', 'updatefreq'))
        if self.alias is not None:
            options['alias'] = self.alias
        return options

    def get_enabled(self):
        return get(self, '_enabled', False)

    def set_enabled(self, value):
        # the first time "enabled" is set, create a timer
        has_been_set = hasattr(self, '_enabled')
        if not has_been_set:
            self.change_reason(self.Reasons.NONE)

            if self.update_mixin_timer:
                info('%s creating a timer with update frequency %s', self,
                     self.updatefreq)
                self.timer = RepeatTimer(int(self.updatefreq), self.update_now)
                self.timer.start()
                self.timer.stop()
                if get(
                        self, 'on_connect', None
                ) is not None and self.update_now not in self.on_connect:
                    self.on_connect += self.update_now

        # when enabled, start the timer.
        self._enabled = value
        if value:
            info('enabling timer for %s', self)
            if self.OFFLINE and getattr(self, '_needs_connect', True):
                self._needs_connect = False
                get(self, 'Connect', nothing)()
            if self.update_mixin_timer:
                self.timer.reset(int(self.updatefreq))
        # when disabled, stop the timer.
        else:
            if has_been_set:
                if not self.OFFLINE:
                    get(self, 'Disconnect', nothing)()
                self._needs_connect = True

                if self.update_mixin_timer:
                    info('stopping timer for %s', self)
                    self.timer.stop()

                get(self, 'on_disable', nothing)()

    enabled = property(get_enabled, set_enabled)

    def pause_timer(self):
        with self.timer._cv:
            if not self.timer.paused:
                self.timer.pause()

    def start_timer(self):
        with self.timer._cv:
            if self.timer.paused:
                self.timer.start()