Demo entry 1468847

sprays addition

   

Submitted by anonymous on Mar 15, 2015 at 18:43
Language: Python. Code size: 4.3 kB.

from __future__ import with_statement
import Live
from _Framework.ControlSurface import ControlSurface

#import os #для startfile и определения пути до файла
from os import path as os_path
from subprocess import Popen, PIPE
from threading import Thread
from Queue import Queue, Empty

FOC_BROWSER = 1
SHOW_BROWSER, SHOW_DETAIL, SHOW_CLIP = range (2, 4 + 1)
_SHOW_FOC_BROWSER, _SHOW_FOC_DETAIL, _SHOW_FOC_DEVCH, _SHOW_DC_FOC_BR = range (5, 8 + 1)
IS_VISIBLE_CLIP = 2
DISCONNECT = 127

class Sprays_Addition (ControlSurface):
    
    __module__ = __name__
    __doc__ = "Sprays Addition"
    
    def __init__ (self, c_instance):
        ControlSurface.__init__(self, c_instance)
        with self.component_guard ():
            
            # попытка сделать слушатель в отдельном потоке
            # возникают глюки с активностью окон
            #os.startfile (self.filedir + '\\Sprays_Addition.au3')
            #self.server = Thread (target = self.autoit_listener)
            #self.server.daemon = True
            #self.server.start ()
            
            filedir = os_path.abspath (os_path.dirname (__file__))
            autoit_path = 'h:\\Program Files\\AutoIt3\\AutoIt3.exe'
            au_script_path = filedir + '\\Sprays_Addition.au3'
            
            self.autoit = Popen ([autoit_path, au_script_path], stdout = PIPE, stdin = PIPE)
            self.queue = Queue ()
            self.threadout = Thread (target = enqueue_output, args = (self.autoit.stdout, self.queue))
            self.threadout.daemon = True
            self.threadout.start ()
            
            view = self.application().view
            self.focus_view = view.focus_view
            self.show_view = view.show_view
            self.is_view_visible = view.is_view_visible
            
            self.schedule_message (1, self.autoit_listener)
    
    def disconnect (self):
        ControlSurface.disconnect (self)
        self.autoit.stdin.write (chr (DISCONNECT))
        self.autoit.stdin.flush ()
        self.autoit.kill ()
    
    def autoit_listener (self): # без цикла, назначенными заданиями
        try:
            data = self.queue.get_nowait ()
        except Empty:
            pass
        else:
            value = ord (data)  # работаем с числовым представлением
            msg = value % 16 # остаток от деления
            question = value // 16
            #view = self.application().view
            
            if msg:
                if msg == FOC_BROWSER:
                    self.focus_view ('Browser')
                #########################
                elif msg == SHOW_BROWSER:
                    self.show_view ('Browser')
                elif msg == SHOW_DETAIL:
                    self.show_view ('Detail')
                elif msg == SHOW_CLIP:
                    self.show_view ('Detail/Clip')
                #########################
                elif msg == _SHOW_FOC_BROWSER:
                    self.show_view ('Browser')
                    self.focus_view ('Browser')
                elif msg == _SHOW_FOC_DETAIL:
                    self.show_view ('Detail')
                    self.focus_view ('Detail')
                elif msg == _SHOW_FOC_DEVCH:
                    self.show_view ('Detail/DeviceChain')
                    self.focus_view ('Detail')      
                elif msg == _SHOW_DC_FOC_BR:
                    self.show_view ('Detail/DeviceChain')
                    self.focus_view ('Browser')
            
            if question:
                if question == IS_VISIBLE_CLIP:
                    answer = '1' if self.is_view_visible('Detail/Clip') else '0'
                else: answer = data # по умолчанию возвращает полученный символ
                self.autoit.stdin.write (answer)
                self.autoit.stdin.flush ()
            
        self.schedule_message (1, self.autoit_listener)
    
    #def start_autoit (self): # для запуска в отдельном потоке
    #    os.startfile (self.filedir + '\\Sprays_Addition.au3')

def enqueue_output (out, queue):
    def read (): return out.read (1)
    for byte in iter (read, ''):
        queue.put (byte)
    out.close ()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).