コード例 #1
0
ファイル: plot_view.py プロジェクト: yangbiaocn/inkcut
class PlotView(Control):
    hug_width = set_default('ignore')
    hug_height = set_default('ignore')
    proxy = Typed(ProxyPlotView)
    data = d_(ContainerList())
    setup = d_(Callable(lambda graph: None))

    title = d_(Str())
    labels = d_(Dict(Str(), Str()))

    axis_scales = d_(Dict(Str(), Float()))

    #background_color = d_(Str())
    #foreground = d_(Str())

    antialiasing = d_(Bool(True))
    aspect_locked = d_(Bool(True))

    grid = d_(Tuple(item=Bool(), default=(False, False)))
    grid_alpha = d_(FloatRange(low=0.0, high=1.0, value=0.5))

    multi_axis = d_(Bool(True))

    @observe('data', 'title', 'labels', 'multi_axis', 'antialiasing',
             'axis_scales', 'grid', 'grid_alpha')
    def _update_proxy(self, change):
        """ An observer which sends state change to the proxy.
        """
        # The superclass handler implementation is sufficient.
        super(PlotView, self)._update_proxy(change)
コード例 #2
0
class SerialTransport(DeviceTransport):

    #: Default config
    config = Instance(SerialConfig, ())

    #: Connection port
    connection = Instance(SerialPort)

    #: Whether a serial connection spools depends on the device (configuration)
    always_spools = set_default(False)

    #: Wrapper
    _protocol = Instance(InkcutProtocol)

    def connect(self):
        try:
            config = self.config

            #: Save a reference
            self.protocol.transport = self

            #: Make the wrapper
            self._protocol = InkcutProtocol(self, self.protocol)

            self.connection = SerialPort(
                self._protocol,
                config.port,
                reactor,
                baudrate=config.baudrate,
                bytesize=config.bytesize,
                parity=SERIAL_PARITIES[config.parity],
                stopbits=config.stopbits,
                xonxoff=config.xonxoff,
                rtscts=config.rtscts
            )
            log.debug("{} | opened".format(self.config.port))
        except Exception as e:
            #: Make sure to log any issues as these tracebacks can get
            #: squashed by twisted
            log.error("{} | {}".format(
                self.config.port, traceback.format_exc()
            ))
            raise

    def write(self, data):
        if not self.connection:
            raise IOError("Port is not opened")
        log.debug("-> {} | {}".format(self.config.port, data))
        if hasattr(data, 'encode'):
            data = data.encode()
        self._protocol.transport.write(data)

    def disconnect(self):
        if self.connection:
            log.debug("{} | closed".format(self.config.port))
            self.connection.loseConnection()
            self.connection = None

    def __repr__(self):
        return self.config.port
コード例 #3
0
class EthernetTransport(DeviceTransport):
    config = Instance(EthernetConfig, ()).tag(config=True)
    connection = Instance(socket.socket)

    #: Whether a serial connection spools depends on the device (configuration)
    always_spools = set_default(False)

    def connect(self):
        print("SeroIP connected")
        print(self.config.ip)
        print(self.config.port)
        self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connection.connect((self.config.ip, int(self.config.port)))
        self.connected = True

    def write(self, data):
        print("SeroIP printing " + data)
        self.connection.send(data.encode())

    def disconnect(self):
        print("SeroIP disconnected")
        self.connection.close()
        self.connected = False

        # if self.connection:
        #     self.connection.close()

    def repr(self):
        return self.config
コード例 #4
0
ファイル: plugin.py プロジェクト: yairvillarp/inkcut
class FileTransport(DeviceTransport):

    #: Default config
    config = Instance(FileConfig, ()).tag(config=True)

    #: The OS spools file writes
    always_spools = set_default(True)

    #: The output buffer
    file = Instance(object)

    #: Current path
    path = Unicode()

    def _default_path(self):
        config = self.config
        params = dict(time=str(time.time()).split(".")[0],
                      protocol=str(self.protocol.declaration.id).lower())
        return join(config.directory, config.format.format(**params))

    def connect(self):
        config = self.config
        path = self.path = self._default_path()
        if not exists(config.directory):
            os.makedirs(config.directory)
        log.debug("-- File | Writing to '{}'".format(path))
        self.file = open(path, 'wb')
        self.connected = True
        #: Save a reference
        self.protocol.transport = self
        self.protocol.connection_made()

    def write(self, data):
        log.debug("-> File | {}".format(data))

        #: Python 3 is annoying
        if hasattr(data, 'encode'):
            data = data.encode()

        self.file.write(data)

    def read(self, size=None):
        return ""

    def disconnect(self):
        log.debug("-- File | Closed '{}'".format(self.path))
        self.connected = False
        self.protocol.connection_lost()
        if self.file:
            self.file.close()
            self.file = None

    def __repr__(self):
        return self.path
コード例 #5
0
ファイル: plugin.py プロジェクト: grany/inkcut
class PrinterTransport(DeviceTransport):

    #: Default config
    config = Instance(PrinterConfig).tag(config=True)

    #: Delegate to the implementation based on the current platform
    connection = Instance(PrinterConnection)

    #: The OS printing subsystem will take care of spooling
    always_spools = set_default(True)

    def _default_config(self):
        if sys.platform == 'win32':
            return Win32PrinterConfig()
        else:
            return CupsPrinterConfig()

    def _default_connection(self):
        if sys.platform == 'win32':
            return Win32PrinterConnection()
        else:
            return LPRPrinterConnection()

    def connect(self):
        try:
            # Always create a new connection
            self.connection = self._default_connection()

            # Save a reference
            self.protocol.transport = self
            self.connection.transport = self
            self.connection.open()
            log.debug("{} | opened".format(self.config.printer))
        except Exception as e:
            # Make sure to log any issues as these tracebacks can get
            # squashed by twisted
            log.error("{} | {}".format(
                self.config.printer, traceback.format_exc()
            ))
            raise

    def write(self, data):
        if not self.connection:
            raise IOError("Port is not opened")
        self.connection.write(data)

    def disconnect(self):
        if self.connection:
            self.connection.close()
            log.debug("{} | closed".format(self.config.printer))
            self.connection = None

    def __repr__(self):
        return self.config.printer
コード例 #6
0
ファイル: plugin.py プロジェクト: yangbiaocn/inkcut
class SerialTransport(RawFdTransport):

    #: Default config
    config = Instance(SerialConfig, ()).tag(config=True)

    #: Connection port
    connection = Instance(SerialPort)

    #: Whether a serial connection spools depends on the device (configuration)
    always_spools = set_default(False)

    def connect(self):
        config = self.config
        self.device_path = config.port
        try:
            #: Save a reference
            self.protocol.transport = self

            #: Make the wrapper
            self._protocol = RawFdProtocol(self, self.protocol)

            self.connection = SerialPort(
                self._protocol,
                config.port,
                reactor,
                baudrate=config.baudrate,
                bytesize=config.bytesize,
                parity=SERIAL_PARITIES[config.parity],
                stopbits=config.stopbits,
                xonxoff=config.xonxoff,
                rtscts=config.rtscts
            )

            # Twisted is missing this
            if config.dsrdtr:
                try:
                    self.connection._serial.dsrdtr = True
                except AttributeError as e:
                    log.warning("{} | dsrdtr is not supported {}".format(
                        config.port, e))

            log.debug("{} | opened".format(config.port))
        except Exception as e:
            #: Make sure to log any issues as these tracebacks can get
            #: squashed by twisted
            log.error("{} | {}".format(config.port, traceback.format_exc()))
            raise
コード例 #7
0
class PlotArea(Container):
    hug_width = set_default('ignore')
    hug_height = set_default('ignore')
    proxy = Typed(ProxyPlotArea)
    setup = d_(Callable(lambda graph: None))
コード例 #8
0
class QtSerialTransport(DeviceTransport):

    #: Default config
    config = Instance(QtSerialConfig, ()).tag(config=True)
    #: Current path
    device_path = Str()
    #: Connection port
    connection = Instance(QSerialPort)

    #: Whether a serial connection spools depends on the device (configuration)
    always_spools = set_default(False)

    def open_serial_port(self, config):
        try:
            serial_port = QSerialPort()
            serial_port.setPortName(config.port)
            #Setting the AllDirections flag is supported on all platforms. Windows supports only this mode.
            serial_port.setBaudRate(config.baudrate, QSerialPort.AllDirections)
            serial_port.setParity(config.parity)
            serial_port.setStopBits(config.stopbits)
            serial_port.setDataBits(config.bytesize)
            serial_port.setFlowControl(config.flowcontrol)
            serial_port.open(QSerialPort.ReadWrite)
            return serial_port
        except Exception as e:
            log.error("{}".format(traceback.format_exc()))
            return None

    def connect(self):
        config = self.config
        #self.device_path = config.port
        device_path = self.device_path = config.port
        try:
            #: Save a reference
            self.protocol.transport = self

            self.connection = self.open_serial_port(config)
            self.connected = True
            log.debug("{} | opened".format(config.port))
            self.protocol.connection_made()

        except Exception as e:
            #: Make sure to log any issues
            log.error("{} | {}".format(config.port, traceback.format_exc()))
            raise

    def write(self, data):
        if not self.connection:
            raise IOError("{} is not opened".format(self.device_path))
        log.debug("-> {} | {}".format(self.device_path, data))
        if hasattr(data, 'encode'):
            data = data.encode()
        self.last_write = data
        self.connection.write(data)

    def disconnect(self):
        if self.connection:
            log.debug("-- {} | closed by request".format(self.device_path))
            self.connected = False
            self.connection.close()
            self.connection = None

    def __repr__(self):
        return self.device_path