def main(): usage = "%prog [options] <config file>" opts = optparse.OptionParser(usage) opts.add_option("-i", "--debuginput", dest="debuginput", help="read commands from file instead of from tty port") opts.add_option("-I", "--input-tty", dest="inputtty", default='/tmp/printer', help="input tty name (default is /tmp/printer)") opts.add_option("-l", "--logfile", dest="logfile", help="write log to file instead of stderr") opts.add_option("-v", action="store_true", dest="verbose", help="enable debug messages") opts.add_option("-o", "--debugoutput", dest="debugoutput", help="write output to file instead of to serial port") opts.add_option("-d", "--dictionary", dest="dictionary", type="string", action="callback", callback=arg_dictionary, help="file to read for mcu protocol dictionary") options, args = opts.parse_args() if len(args) != 1: opts.error("Incorrect number of arguments") start_args = {'config_file': args[0], 'start_reason': 'startup'} input_fd = bglogger = None debuglevel = logging.INFO if options.verbose: debuglevel = logging.DEBUG if options.debuginput: start_args['debuginput'] = options.debuginput debuginput = open(options.debuginput, 'rb') input_fd = debuginput.fileno() else: input_fd = util.create_pty(options.inputtty) if options.debugoutput: start_args['debugoutput'] = options.debugoutput start_args.update(options.dictionary) if options.logfile: bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel) else: logging.basicConfig(level=debuglevel) logging.info("Starting Klippy...") start_args['software_version'] = util.get_git_version() if bglogger is not None: versions = "\n".join([ "Args: %s" % (sys.argv, ), "Git version: %s" % (repr(start_args['software_version']), ), "CPU: %s" % (util.get_cpu_info(), ), "Python: %s" % (repr(sys.version), ) ]) logging.info(versions) elif not options.debugoutput: logging.warning("No log file specified!" " Severe timing issues may result!") # Start Printer() class while 1: if bglogger is not None: bglogger.clear_rollover_info() bglogger.set_rollover_info('versions', versions) printer = Printer(input_fd, bglogger, start_args) res = printer.run() if res in ['exit', 'error_exit']: break time.sleep(1.) logging.info("Restarting printer") start_args['start_reason'] = res if bglogger is not None: bglogger.stop() if res == 'error_exit': sys.exit(-1)
def _dispatch(self, eventtime, obj): "Dispatch an incoming message accordingly." if 'showth' in obj: self._subpipe.send({'showth':self._showth(), 'eventtime':eventtime}) return True elif 'open' in obj: if obj['open']['tty'] == True: # create virtual tty fd = HandlerTtyVirtual(util.create_pty(obj['open']['filename'])) elif obj['open']['tty']: tty = obj['open']['tty'] if 'chelper' in tty: #fd = pass else: # open hw tty fd = serial.Serial(obj['open']['filename'], obj['open']['tty']['baud'], obj['open']['tty']['timeout'], obj['open']['tty']['exclusive']).fileno() else: # open file fd = open(obj['open']['filename'], obj['open']['mode']).fileno() if obj['open']['poll']: self._poll.register(fd, select.POLLIN | select.POLLHUP) self.fd[fd] = obj['open'] self._subpipe.send({'open':fd, 'eventtime':eventtime}) return True elif 'info' in obj: fd = obj['info'] self._subpipe.send({'info':self.fd[fd], 'eventtime':eventtime}) return True elif 'seek' in obj: fd = obj['seek'] fd.seek(obj['pos']) return True elif 'truncate' in obj: fd = obj['truncate'] fd.truncate() return True elif 'read' in obj: fd = obj['read'] if 'size' in obj: chunksize = obj['size'] else: chunksize = self.fd[fd]['chunksize'] data = fd.read(chunksize) self._subpipe.send({'eventtime':eventtime, 'fd':fd, 'data':data}) return True elif 'write' in obj: fd = obj['write'] fd.write(obj['data']) return True elif 'close' in obj: fd = obj['close'] if 'poll' in self.fd[fd]: self._poll.unregister(fd) fd.close() self.fd.pop(fd) return True else: self.message_lock.acquire() self.message[len(self.message)] = obj self.message_lock.release() return False
def main(): usage = "%prog [options] <config file>" opts = optparse.OptionParser(usage) opts.add_option("-o", "--debugoutput", dest="outputfile", help="write output to file instead of to serial port") opts.add_option("-i", "--debuginput", dest="inputfile", help="read commands from file instead of from tty port") opts.add_option("-I", "--input-tty", dest="inputtty", default='/tmp/printer', help="input tty name (default is /tmp/printer)") opts.add_option("-l", "--logfile", dest="logfile", help="write log to file instead of stderr") opts.add_option("-v", action="store_true", dest="verbose", help="enable debug messages") opts.add_option("-d", dest="read_dictionary", help="file to read for mcu protocol dictionary") options, args = opts.parse_args() if len(args) != 1: opts.error("Incorrect number of arguments") conffile = args[0] input_fd = debuginput = debugoutput = bglogger = None debuglevel = logging.INFO if options.verbose: debuglevel = logging.DEBUG if options.inputfile: debuginput = open(options.inputfile, 'rb') input_fd = debuginput.fileno() else: input_fd = util.create_pty(options.inputtty) if options.outputfile: debugoutput = open(options.outputfile, 'wb') if options.logfile: bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel) else: logging.basicConfig(level=debuglevel) logging.info("Starting Klippy...") software_version = util.get_git_version() if bglogger is not None: lines = ["Args: %s" % (sys.argv,), "Git version: %s" % (repr(software_version),), "CPU: %s" % (util.get_cpu_info(),), "Python: %s" % (repr(sys.version),)] lines = "\n".join(lines) logging.info(lines) bglogger.set_rollover_info('versions', lines) # Start firmware res = 'startup' while 1: is_fileinput = debuginput is not None printer = Printer( conffile, input_fd, res, is_fileinput, software_version, bglogger) if debugoutput: proto_dict = read_dictionary(options.read_dictionary) printer.set_fileoutput(debugoutput, proto_dict) res = printer.run() if res == 'restart': printer.disconnect() time.sleep(1.) logging.info("Restarting printer") continue elif res == 'firmware_restart': printer.firmware_restart() time.sleep(1.) logging.info("Restarting printer") continue elif res == 'exit_eof': printer.disconnect() break if bglogger is not None: bglogger.stop()
def main(): usage = "%prog [options] <config file>" opts = optparse.OptionParser(usage) opts.add_option("-i", "--debuginput", dest="debuginput", help="read commands from file instead of from tty port") opts.add_option("-I", "--input-tty", dest="inputtty", default='/tmp/printer', help="input tty name (default is /tmp/printer)") opts.add_option("-l", "--logfile", dest="logfile", help="write log to file instead of stderr") opts.add_option("-v", action="store_true", dest="verbose", help="enable debug messages") opts.add_option("-o", "--debugoutput", dest="debugoutput", help="write output to file instead of to serial port") opts.add_option("-d", "--dictionary", dest="dictionary", type="string", action="callback", callback=arg_dictionary, help="file to read for mcu protocol dictionary") options, args = opts.parse_args() if len(args) != 1: opts.error("Incorrect number of arguments") start_args = {'config_file': args[0], 'start_reason': 'startup'} input_fd = bglogger = None debuglevel = logging.INFO if options.verbose: debuglevel = logging.DEBUG if options.debuginput: start_args['debuginput'] = options.debuginput debuginput = open(options.debuginput, 'rb') input_fd = debuginput.fileno() else: input_fd = util.create_pty(options.inputtty) if options.debugoutput: start_args['debugoutput'] = options.debugoutput start_args.update(options.dictionary) if options.logfile: bglogger = queuelogger.setup_bg_logging(options.logfile, debuglevel) else: logging.basicConfig(level=debuglevel) logging.info("Starting Klippy...") start_args['software_version'] = util.get_git_version() if bglogger is not None: versions = "\n".join([ "Args: %s" % (sys.argv,), "Git version: %s" % (repr(start_args['software_version']),), "CPU: %s" % (util.get_cpu_info(),), "Python: %s" % (repr(sys.version),)]) logging.info(versions) # Start Printer() class while 1: if bglogger is not None: bglogger.clear_rollover_info() bglogger.set_rollover_info('versions', versions) printer = Printer(input_fd, bglogger, start_args) res = printer.run() if res in ['exit', 'error_exit']: break time.sleep(1.) logging.info("Restarting printer") start_args['start_reason'] = res if bglogger is not None: bglogger.stop() if res == 'error_exit': sys.exit(-1)