示例#1
0
 def _create_timer(self):
     timer = NSTimer.alloc() \
         .initWithFireDate_interval_target_selector_userInfo_repeats_(
         start_time, 1, self, 'tick:', None, True)
     NSRunLoop.currentRunLoop().addTimer_forMode_(timer,
                                                  NSDefaultRunLoopMode)
     timer.fire()
示例#2
0
def gevent_timer(deleg):
    timer = NSTimer.alloc(
    ).initWithFireDate_interval_target_selector_userInfo_repeats_(
        NSDate.date(), 0.1, deleg, 'gevent:', None, True)
    NSRunLoop.currentRunLoop().addTimer_forMode_(timer, NSDefaultRunLoopMode)
    timer.fire()
    print "started gevent timer"
示例#3
0
文件: tray.py 项目: jone/unison-tray
 def _create_timer(self):
     timer = NSTimer.alloc() \
         .initWithFireDate_interval_target_selector_userInfo_repeats_(
         start_time, 1, self, 'tick:', None, True)
     NSRunLoop.currentRunLoop().addTimer_forMode_(
         timer, NSDefaultRunLoopMode)
     timer.fire()
示例#4
0
 def setFlash_(self, on):
     if on and self.flash_timer is None:
         self.flashing_state = True
         self.flash_timer = NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats_(0.5, self, self.updateStatusImage_, ('flash',), True)
         self.flash_timer.fire()
         NSRunLoop.currentRunLoop().addTimer_forMode_(self.flash_timer, NSDefaultRunLoopMode)
     elif not on and self.flashing_state is not None:
         if self.flash_timer is not None:
             self.flash_timer.invalidate()
             self.flash_timer = None
             self.flashing_state = None
             self.setNeedsDisplay_(1)
示例#5
0
    def finishLaunching(self):
        # Get current track info
        curr_track = get_current_track()

        self.lyrics = get_lyrics(curr_track['curr_artist'],
                                 curr_track['curr_song']).split('<br>')

        # Remove empty lines
        self.lyrics = [x for x in self.lyrics if x]

        # Cache current track to avoid unnecessary requests
        self.curr_track_hash = abs(hash(curr_track['curr_track_full'])) % (10**
                                                                           8)

        # Create the status & menu bar
        statusBar = NSStatusBar.systemStatusBar()
        self.statusItem = statusBar.statusItemWithLength_(-1)
        self.statusItem.setTitle_(curr_track['curr_track_full'])

        self.menuBar = NSMenu.alloc().init()

        # Lyrics block
        for i, row in enumerate(self.lyrics):
            row = re.sub('<[^<]+?>', '', row).strip()
            setattr(
                self, 'row_{}'.format(i),
                NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
                    row, 'doNothing:', ''))
            self.menuBar.addItem_(getattr(self, 'row_{}'.format(i)))

        self.SEPERATOR = NSMenuItem.separatorItem()
        self.menuBar.addItem_(self.SEPERATOR)

        # Quit option
        self.QUIT = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
            'Quit', 'terminate:', '')
        self.SEPERATOR = NSMenuItem.separatorItem()
        self.menuBar.addItem_(self.SEPERATOR)
        self.menuBar.addItem_(self.QUIT)

        # Add menu to status bar
        self.statusItem.setMenu_(self.menuBar)

        # Create our timer for song title updates
        self.timer = NSTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(
            1, self, 'update:', '', True)

        # Add our timer to the runloop
        NSRunLoop.currentRunLoop().addTimer_forMode_(
            self.timer, NSEventTrackingRunLoopMode)
示例#6
0
 def updateIcon_(self, icon_state):
     target_image = self.ICON_MAP.get(icon_state)
     assert target_image is not None, 'invalid icon state'
     if target_image in ('busy', 'cam'):
         self.busy_timer_image = target_image
         if self.busy_timer is None:
             self.busy_timer_state = 5
             self.busy_timer = NSTimer.timerWithTimeInterval_target_selector_userInfo_repeats_(0.4, self, self.updateStatusImage_, ('busy',), True)
             self.busy_timer.fire()
             NSRunLoop.currentRunLoop().addTimer_forMode_(self.busy_timer, NSDefaultRunLoopMode)
     else:
         if self.busy_timer is not None:
             self.busy_timer.invalidate()
             self.busy_timer = None
         self.updateStatusImage_((target_image,))
示例#7
0
    def watchForProxyChanges(self):
        """ install a watcher for proxy changes """
        SCDynamicStoreSetNotificationKeys(self.store, None,
                                          ['State:/Network/Global/Proxies'])

        source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0)
        loop = NSRunLoop.currentRunLoop().getCFRunLoop()
        CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)
示例#8
0
    def change_icon(self, name):
        if name == 'syncing':
            if self._last_icon is not None and \
                    self._last_icon.startswith('syncing'):
                return

            self.change_icon('syncing1')

            self._syncing_icon_timer = NSTimer.alloc() \
                .initWithFireDate_interval_target_selector_userInfo_repeats_(
                start_time, 1.0, self, 'animateSyncingIcon:', None, True)
            NSRunLoop.currentRunLoop().addTimer_forMode_(
                self._syncing_icon_timer, NSDefaultRunLoopMode)
            self._syncing_icon_timer.fire()
            return

        self._last_icon = name
        self.trayicon.setImage_(ICONS[name])
示例#9
0
文件: tray.py 项目: jone/unison-tray
    def change_icon(self, name):
        if name == 'syncing':
            if self._last_icon is not None and \
                    self._last_icon.startswith('syncing'):
                return

            self.change_icon('syncing1')

            self._syncing_icon_timer = NSTimer.alloc() \
                .initWithFireDate_interval_target_selector_userInfo_repeats_(
                start_time, 1.0, self, 'animateSyncingIcon:', None, True)
            NSRunLoop.currentRunLoop().addTimer_forMode_(
                self._syncing_icon_timer, NSDefaultRunLoopMode)
            self._syncing_icon_timer.fire()
            return

        self._last_icon = name
        self.trayicon.setImage_(ICONS[name])
示例#10
0
    def present(self):
        """Present the Alert, giving up after configured time..

        Returns: Int result code, based on PyObjC enums. See NSAlert
            Class reference, but result should be one of:
                User clicked the cancel button:
                    NSAlertFirstButtonReturn = 1000
                Alert timed out:
                    NSRunAbortedResponse = -1001
        """
        if self.timer:
            NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSModalPanelRunLoopMode)
        # Start a Cocoa application by getting the shared app object.
        # Make the python app the active app so alert is noticed.
        app = NSApplication.sharedApplication()
        app.activateIgnoringOtherApps_(True)
        if self.alert_sound:
            sound = NSSound.soundNamed_(self.alert_sound).play()
        result = self.runModal()  # pylint: disable=no-member
        print result
        return result
示例#11
0
    def present(self):
        """Present the Alert, giving up after configured time..

        Returns: Int result code, based on PyObjC enums. See NSAlert
            Class reference, but result should be one of:
                User clicked the cancel button:
                    NSAlertFirstButtonReturn = 1000
                Alert timed out:
                    NSRunAbortedResponse = -1001
        """
        if self.timer:
            NSRunLoop.currentRunLoop().addTimer_forMode_(
                self.timer, NSModalPanelRunLoopMode)
        # Start a Cocoa application by getting the shared app object.
        # Make the python app the active app so alert is noticed.
        app = NSApplication.sharedApplication()
        app.activateIgnoringOtherApps_(True)
        if self.alert_sound:
            sound = NSSound.soundNamed_(self.alert_sound).play()
        result = self.runModal()  # pylint: disable=no-member
        print result
        return result
示例#12
0
文件: app.py 项目: Werniman/client
def gevent_timer(deleg):
    timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(
        NSDate.date(), 0.1, deleg, 'gevent:', None, True)
    NSRunLoop.currentRunLoop().addTimer_forMode_(timer, NSDefaultRunLoopMode)
    timer.fire()
    print "started gevent timer"
示例#13
0
    def instances_(self, _n):
        self.loop_func = sys._getframe(0).f_code.co_name
        try:
            x = subprocess.check_output(["/usr/local/bin/docker", "ps", "--format", "{{.Names}}"])
        except subprocess.CalledProcessError as e:
            #print(e)
            self.statusitem.setImage_(self.image)
            # Keep looping, but don't do anything
            NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
            return

        containers = x.splitlines()

        self.statusitem.setTitle_(str(len(containers)))

        new_menu = NSMenu.alloc().init()
        shellmenu = NSMenu.alloc().init()
        logmenu = NSMenu.alloc().init()

        i = NSMenuItem.alloc().init()
        i.setTitle_("Shell")
        i.setSubmenu_(shellmenu)
        new_menu.addItem_(i)

        i = NSMenuItem.alloc().init()
        i.setTitle_("Logs")
        i.setSubmenu_(logmenu)
        new_menu.addItem_(i)

        new_menu.addItem_(NSMenuItem.separatorItem())

        for container in containers:
            shellmenuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(container, 'shellinto:', '')
            shellmenu.addItem_(shellmenuitem)
            logmenuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(container, 'loginto:', '')
            logmenu.addItem_(logmenuitem)
            menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(container, 'killinstance:', '')
            menuitem.setImage_(self.toggle_images['kill' if self.kill else 'refresh'])
            # pprint(dir(menuitem))
            new_menu.addItem_(menuitem)

        new_menu.addItem_(NSMenuItem.separatorItem())

        i = NSMenuItem.alloc().init()
        configmenu = NSMenu.alloc().init()
        i.setTitle_("Settings")
        i.setSubmenu_(configmenu)
        new_menu.addItem_(i)


        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Kill', 'killornot:', '')
        menuitem.setState_(NSOnState if self.kill else NSOffState)
        configmenu.addItem_(menuitem)
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Restart', 'killornot:', '')
        menuitem.setState_(NSOnState if not self.kill else NSOffState)
        configmenu.addItem_(menuitem)
        configmenu.addItem_(NSMenuItem.separatorItem())
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('iTerm', 'itermtoggle:', '')
        menuitem.setState_(NSOnState if self.which_terminal == 'iTerm' else NSOffState)
        configmenu.addItem_(menuitem)
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Terminal', 'itermtoggle:', '')
        menuitem.setState_(NSOnState if not self.which_terminal == 'iTerm' else NSOffState)
        configmenu.addItem_(menuitem)

        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Quit', 'terminate:', '')
        new_menu.addItem_(menuitem)

        self.menu = new_menu
        self.statusitem.setMenu_(self.menu)

        if self.state == 'setup':
            self.state = 'running'
        if self.state == 'running':
            NSRunLoop.currentRunLoop().addTimer_forMode_(self.timer, NSDefaultRunLoopMode)
示例#14
0
 def watchForProxyOrIpChanges(self):
     """ install a watcher for proxy and Ip changes """
     SCDynamicStoreSetNotificationKeys(self.store, None, ['State:/Network/Global/Proxies', 'State:/Network/Global/IPv4'])
     source = SCDynamicStoreCreateRunLoopSource(None, self.store, 0)
     loop = NSRunLoop.currentRunLoop().getCFRunLoop()
     CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes)