Python sys.stdin() Examples
The following are 30
code examples of sys.stdin().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.

Example #1
Source File: inst.py From kaldi-python-io with Apache License 2.0 | 11 votes |
def _fopen(fname, mode): """ Extend file open function, to support 1) "-", which means stdin/stdout 2) "$cmd |" which means pipe.stdout """ if mode not in ["w", "r", "wb", "rb"]: raise ValueError("Unknown open mode: {mode}".format(mode=mode)) if not fname: return None fname = fname.rstrip() if fname == "-": if mode in ["w", "wb"]: return sys.stdout.buffer if mode == "wb" else sys.stdout else: return sys.stdin.buffer if mode == "rb" else sys.stdin elif fname[-1] == "|": pin = pipe_fopen(fname[:-1], mode, background=(mode == "rb")) return pin if mode == "rb" else TextIOWrapper(pin) else: if mode in ["r", "rb"] and not os.path.exists(fname): raise FileNotFoundError( "Could not find common file: {}".format(fname)) return open(fname, mode)
Example #2
Source File: run_containers.py From container-agent with Apache License 2.0 | 10 votes |
def main(): if len(sys.argv) > 2: Fatal('usage: %s [containers.yaml]' % sys.argv[0]) if len(sys.argv) == 2: with open(sys.argv[1], 'r') as fp: config = yaml.load(fp) else: config = yaml.load(sys.stdin) syslog.openlog(PROGNAME) LogInfo('processing container manifest') CheckVersion(config) all_volumes = LoadVolumes(config.get('volumes', [])) user_containers = LoadUserContainers(config.get('containers', []), all_volumes) CheckGroupWideConflicts(user_containers) if user_containers: infra_containers = LoadInfraContainers(user_containers) RunContainers(infra_containers + user_containers)
Example #3
Source File: __init__.py From supervisor-logging with Apache License 2.0 | 6 votes |
def supervisor_events(stdin, stdout): """ An event stream from Supervisor. """ while True: stdout.write('READY\n') stdout.flush() line = stdin.readline() headers = get_headers(line) payload = stdin.read(int(headers['len'])) event_headers, event_data = eventdata(payload) yield event_headers, event_data stdout.write('RESULT 2\nOK') stdout.flush()
Example #4
Source File: batch.py From aegea with Apache License 2.0 | 6 votes |
def add_command_args(parser): group = parser.add_mutually_exclusive_group() group.add_argument("--watch", action="store_true", help="Monitor submitted job, stream log until job completes") group.add_argument("--wait", action="store_true", help="Block on job. Exit with code 0 if job succeeded, 1 if failed") group = parser.add_mutually_exclusive_group() group.add_argument("--command", nargs="+", help="Run these commands as the job (using " + BOLD("bash -c") + ")") group.add_argument("--execute", type=argparse.FileType("rb"), metavar="EXECUTABLE", help="Read this executable file and run it as the job") group.add_argument("--wdl", type=argparse.FileType("rb"), metavar="WDL_WORKFLOW", help="Read this WDL workflow file and run it as the job") parser.add_argument("--wdl-input", type=argparse.FileType("r"), metavar="WDL_INPUT_JSON", default=sys.stdin, help="With --wdl, use this JSON file as the WDL job input (default: stdin)") parser.add_argument("--environment", nargs="+", metavar="NAME=VALUE", type=lambda x: dict(zip(["name", "value"], x.split("=", 1))), default=[]) parser.add_argument("--staging-s3-bucket", help=argparse.SUPPRESS)
Example #5
Source File: client.py From iSDX with Apache License 2.0 | 6 votes |
def _sender(conn,stdin): # Warning: when the parent dies we are seeing continual # newlines, so we only access so many before stopping counter = 0 while True: try: line = stdin.readline().strip() if line == "": counter += 1 if counter > 100: break continue counter = 0 conn.send(line) sendLogger.debug(line) except: pass
Example #6
Source File: build_lemma_cache.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
def build(args): words={} for line in sys.stdin: line=line.strip() if not line or line.startswith("#"): continue cols=line.split("\t") word=(cols[FORM], cols[UPOS], cols[XPOS], cols[FEATS], cols[LEMMA]) if word not in words: words[word]=0 words[word]+=1 for word, count in sorted(words.items(), key=lambda x: x[1], reverse=True): if count>args.cutoff: w="\t".join(word) if len(w.strip().split("\t"))!=5: # make sure there is no empty columns print("Skipping weird line", w, file=sys.stderr) continue print(w) else: break
Example #7
Source File: cmds.py From knob with MIT License | 6 votes |
def hciCallback(self, record): hcipkt, orig_len, inc_len, flags, drops, recvtime = record dummy = "\x00\x00\x00" # TODO: Figure out purpose of these fields direction = p8(flags & 0x01) packet = dummy + direction + hcipkt.getRaw() length = len(packet) ts_sec = recvtime.second #+ timestamp.minute*60 + timestamp.hour*60*60 #FIXME timestamp not set ts_usec = recvtime.microsecond pcap_packet = struct.pack('@ I I I I', ts_sec, ts_usec, length, length) + packet try: self.wireshark_process.stdin.write(pcap_packet) self.wireshark_process.stdin.flush() log.debug("HciMonitorController._callback: done") except IOError as e: log.warn("HciMonitorController._callback: broken pipe. terminate.") self.killMonitor()
Example #8
Source File: cmds.py From knob with MIT License | 6 votes |
def lmpCallback(self, lmp_packet, sendByOwnDevice, src, dest, timestamp): eth_header = dest + src + "\xff\xf0" meta_data = "\x00"*6 if sendByOwnDevice else "\x01\x00\x00\x00\x00\x00" packet_header = "\x19\x00\x00" + p8(len(lmp_packet)<<3 | 7) packet = eth_header + meta_data + packet_header + lmp_packet packet += "\x00\x00" # CRC length = len(packet) ts_sec = timestamp.second + timestamp.minute*60 + timestamp.hour*60*60 ts_usec = timestamp.microsecond pcap_packet = struct.pack('@ I I I I', ts_sec, ts_usec, length, length) + packet try: self.wireshark_process.stdin.write(pcap_packet) self.wireshark_process.stdin.flush() log.debug("LmpMonitorController._callback: done") except IOError as e: log.warn("LmpMonitorController._callback: broken pipe. terminate.") self.killMonitor()
Example #9
Source File: insightfinder.py From InsightAgent with Apache License 2.0 | 6 votes |
def main(): # Initialize the logger logging.basicConfig() # Intialize from our arguments insightfinder = InsightfinderStore(*sys.argv[1:]) current_chunk = 0 # Get all the inputs for line in sys.stdin: if insightfinder.filter_string not in line: continue map_size = len(bytearray(json.dumps(insightfinder.metrics_map))) if map_size >= insightfinder.flush_kb * 1000: insightfinder.logger.debug("Flushing chunk number: " + str(current_chunk)) insightfinder.send_metrics() current_chunk += 1 insightfinder.append(line.strip()) insightfinder.logger.debug("Flushing chunk number: " + str(current_chunk)) insightfinder.send_metrics() insightfinder.save_grouping() insightfinder.logger.debug("Finished sending all chunks to InsightFinder")
Example #10
Source File: ksp_compiler.py From SublimeKSP with GNU General Public License v3.0 | 6 votes |
def __call__(self, string): # the special argument "-" means sys.std{in,out} if string == '-': if 'r' in self._mode: return sys.stdin elif 'w' in self._mode: return sys.stdout else: msg = _('argument "-" with mode %r') % self._mode raise ValueError(msg) # all other arguments are used as file names try: return open(string, self._mode, self._bufsize, self._encoding, self._errors) except OSError as e: message = _("can't open '%s': %s") raise ArgumentTypeError(message % (string, e))
Example #11
Source File: files.py From udapi-python with GNU General Public License v3.0 | 6 votes |
def _token_to_filenames(token): if token[0] == '!': pattern = token[1:] filenames = glob.glob(pattern) if not filenames: raise RuntimeError('No filenames matched "%s" pattern' % pattern) elif token[0] == '@': filelist_name = sys.stdin if token == '@-' else token[1:] with open(filelist_name) as filelist: filenames = [line.rstrip('\n') for line in filelist] directory = os.path.dirname(token[1:]) if directory != '.': filenames = [f if f[0] != '/' else directory + '/' + f for f in filenames] else: filenames = [token] return filenames
Example #12
Source File: files.py From udapi-python with GNU General Public License v3.0 | 6 votes |
def next_filehandle(self): """Go to the next file and retrun its filehandle or None (meaning no more files).""" filename = self.next_filename() if filename is None: fhandle = None elif filename == '-': fhandle = io.TextIOWrapper(sys.stdin.buffer, encoding=self.encoding) elif filename == '<filehandle_input>': fhandle = self.filehandle else: filename_extension = filename.split('.')[-1] if filename_extension == 'gz': myopen = gzip.open elif filename_extension == 'xz': myopen = lzma.open elif filename_extension == 'bz2': myopen = bz2.open else: myopen = open fhandle = myopen(filename, 'rt', encoding=self.encoding) self.filehandle = fhandle return fhandle
Example #13
Source File: chardetect.py From jawfish with MIT License | 6 votes |
def description_of(lines, name='stdin'): """ Return a string describing the probable encoding of a file or list of strings. :param lines: The lines to get the encoding of. :type lines: Iterable of bytes :param name: Name of file or collection of lines :type name: str """ u = UniversalDetector() for line in lines: u.feed(line) u.close() result = u.result if result['encoding']: return '{0}: {1} with confidence {2}'.format(name, result['encoding'], result['confidence']) else: return '{0}: no result'.format(name)
Example #14
Source File: cmd2plus.py From OpenTrader with GNU Lesser General Public License v3.0 | 6 votes |
def redirect_output(self, statement): if statement.parsed.pipeTo: self.kept_state = Statekeeper(self, ('stdout',)) self.kept_sys = Statekeeper(sys, ('stdout',)) self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) sys.stdout = self.stdout = self.redirect.stdin elif statement.parsed.output: if (not statement.parsed.outputTo) and (not can_clip): raise EnvironmentError('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable') self.kept_state = Statekeeper(self, ('stdout',)) self.kept_sys = Statekeeper(sys, ('stdout',)) if statement.parsed.outputTo: mode = 'w' if statement.parsed.output == 2 * self.redirector: mode = 'a' sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) else: sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+") if statement.parsed.output == '>>': self.stdout.write(get_paste_buffer())
Example #15
Source File: cmd2plus.py From OpenTrader with GNU Lesser General Public License v3.0 | 6 votes |
def do_py(self, arg): ''' py <command>: Executes a Python command. py: Enters interactive Python mode. End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, ``exit()``. Non-python commands can be issued with ``cmd("your command")``. Run python code from external files with ``run("filename.py")`` ''' self.pystate['self'] = self arg = arg.parsed.raw[2:].strip() localvars = (self.locals_in_py and self.pystate) or {} interp = InteractiveConsole(locals=localvars) interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') if arg.strip(): interp.runcode(arg) else: def quit(): raise EmbeddedConsoleExit def onecmd_plus_hooks(arg): return self.onecmd_plus_hooks(arg + '\n') def run(arg): try: file = open(arg) interp.runcode(file.read()) file.close() except IOError as e: self.perror(e) self.pystate['quit'] = quit self.pystate['exit'] = quit self.pystate['cmd'] = onecmd_plus_hooks self.pystate['run'] = run try: cprt = 'Type "help", "copyright", "credits" or "license" for more information.' keepstate = Statekeeper(sys, ('stdin','stdout')) sys.stdout = self.stdout sys.stdin = self.stdin interp.interact(banner= "Python %s on %s\n%s\n(%s)\n%s" % (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) except EmbeddedConsoleExit: pass keepstate.restore()
Example #16
Source File: cmd2plus.py From OpenTrader with GNU Lesser General Public License v3.0 | 6 votes |
def do_load(self, arg=None): """Runs script of command(s) from a file or URL.""" if arg is None: targetname = self.default_file_name else: arg = arg.split(None, 1) targetname, args = arg[0], (arg[1:] or [''])[0].strip() try: target = self.read_file_or_url(targetname) except IOError as e: self.perror('Problem accessing script from %s: \n%s' % (targetname, e)) return keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt', 'continuation_prompt','current_script_dir')) self.stdin = target self.use_rawinput = False self.prompt = self.continuation_prompt = '' self.current_script_dir = os.path.split(targetname)[0] stop = self._cmdloop() self.stdin.close() keepstate.restore() self.lastcmd = '' return stop and (stop != self._STOP_SCRIPT_NO_EXIT)
Example #17
Source File: __init__.py From razzy-spinner with GNU General Public License v3.0 | 6 votes |
def in_idle(): """ @rtype: C{boolean} @return: true if this function is run within idle. Tkinter programs that are run in idle should never call L{Tk.mainloop}; so this function should be used to gate all calls to C{Tk.mainloop}. @warning: This function works by checking C{sys.stdin}. If the user has modified C{sys.stdin}, then it may return incorrect results. """ import sys, types return (type(sys.stdin) == types.InstanceType and \ sys.stdin.__class__.__name__ == 'PyShell') ##////////////////////////////////////////////////////// ## Test code. ##//////////////////////////////////////////////////////
Example #18
Source File: util.py From razzy-spinner with GNU General Public License v3.0 | 6 votes |
def in_idle(): """ Return True if this function is run within idle. Tkinter programs that are run in idle should never call ``Tk.mainloop``; so this function should be used to gate all calls to ``Tk.mainloop``. :warning: This function works by checking ``sys.stdin``. If the user has modified ``sys.stdin``, then it may return incorrect results. :rtype: bool """ import sys return sys.stdin.__class__.__name__ in ('PyShell', 'RPCProxy') ########################################################################## # PRETTY PRINTING ##########################################################################
Example #19
Source File: flow.py From pwnypack with MIT License | 6 votes |
def interact(self): """ Interact with the socket. This will send all keyboard input to the socket and input from the socket to the console until an EOF occurs. """ sockets = [sys.stdin, self.channel] while True: ready = select.select(sockets, [], [])[0] if sys.stdin in ready: line = sys.stdin.readline().encode('latin1') if not line: break self.write(line) if self.channel in ready: self.read(1, echo=True)
Example #20
Source File: firefox_decrypt.py From firefox_decrypt with GNU General Public License v3.0 | 6 votes |
def ask_password(profile, interactive): """ Prompt for profile password """ if not PY3: profile = profile.encode(SYS_ENCODING) passmsg = "\nMaster Password for profile {0}: ".format(profile) if sys.stdin.isatty() and interactive: passwd = getpass(passmsg) else: # Ability to read the password from stdin (echo "pass" | ./firefox_...) if sys.stdin in select.select([sys.stdin], [], [], 0)[0]: passwd = sys.stdin.readline().rstrip("\n") else: LOG.warning("Master Password not provided, continuing with blank password") passwd = "" return py2_decode(passwd)
Example #21
Source File: search_command.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def process(self, argv=sys.argv, ifile=sys.stdin, ofile=sys.stdout): """ Process data. :param argv: Command line arguments. :type argv: list or tuple :param ifile: Input data file. :type ifile: file :param ofile: Output data file. :type ofile: file :return: :const:`None` :rtype: NoneType """ if len(argv) > 1: self._process_protocol_v1(argv, ifile, ofile) else: self._process_protocol_v2(argv, ifile, ofile)
Example #22
Source File: search_command.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def process(self, argv=sys.argv, ifile=sys.stdin, ofile=sys.stdout): """ Process data. :param argv: Command line arguments. :type argv: list or tuple :param ifile: Input data file. :type ifile: file :param ofile: Output data file. :type ofile: file :return: :const:`None` :rtype: NoneType """ if len(argv) > 1: self._process_protocol_v1(argv, ifile, ofile) else: self._process_protocol_v2(argv, ifile, ofile)
Example #23
Source File: __init__.py From supervisor-logging with Apache License 2.0 | 5 votes |
def main(): """ Main application loop. """ env = os.environ try: host = env['SYSLOG_SERVER'] port = int(env['SYSLOG_PORT']) socktype = socket.SOCK_DGRAM if env['SYSLOG_PROTO'] == 'udp' \ else socket.SOCK_STREAM except KeyError: sys.exit("SYSLOG_SERVER, SYSLOG_PORT and SYSLOG_PROTO are required.") handler = SysLogHandler( address=(host, port), socktype=socktype, ) handler.setFormatter(PalletFormatter()) for event_headers, event_data in supervisor_events(sys.stdin, sys.stdout): event = logging.LogRecord( name=event_headers['processname'], level=logging.INFO, pathname=None, lineno=0, msg=event_data, args=(), exc_info=None, ) event.process = int(event_headers['pid']) handler.handle(event)
Example #24
Source File: standard_input_stream.py From clikit with MIT License | 5 votes |
def __init__(self): # type: () -> None super(StandardInputStream, self).__init__(sys.stdin)
Example #25
Source File: image_tags.py From drydock with Apache License 2.0 | 5 votes |
def main(): config = read_config(sys.stdin, os.environ) tags = build_tags(config) for tag in tags: print(tag)
Example #26
Source File: inst.py From kaldi-python-io with Apache License 2.0 | 5 votes |
def _fclose(fname, fd): """ Extend file close function, to support 1) "-", which means stdin/stdout 2) "$cmd |" which means pipe.stdout 3) None type """ if fname != "-" and fd and fname[-1] != "|": fd.close()
Example #27
Source File: inst.py From kaldi-python-io with Apache License 2.0 | 5 votes |
def parse_scps(scp_path, value_processor=lambda x: x, num_tokens=2, restrict=True): """ Parse kaldi's script(.scp) file with supported for stdin WARN: last line of scripts could not be None and with "\n" end """ scp_dict = dict() line = 0 with ext_open(scp_path, "r") as f: for raw_line in f: scp_tokens = raw_line.strip().split() line += 1 if scp_tokens[-1] == "|": key, value = scp_tokens[0], " ".join(scp_tokens[1:]) else: token_len = len(scp_tokens) if num_tokens >= 2 and token_len != num_tokens or restrict and token_len < 2: raise RuntimeError(f"For {scp_path}, format error " + f"in line[{line:d}]: {raw_line}") if num_tokens == 2: key, value = scp_tokens else: key, value = scp_tokens[0], scp_tokens[1:] if key in scp_dict: raise ValueError( f"Duplicate key \'{key}\' exists in {scp_path}") scp_dict[key] = value_processor(value) return scp_dict
Example #28
Source File: __init__.py From csv2json with Apache License 2.0 | 5 votes |
def convert(csv, json, **kwargs): '''Convert csv to json. csv: filename or file-like object json: filename or file-like object if csv is '-' or None: stdin is used for input if json is '-' or None: stdout is used for output ''' csv_local, json_local = None, None try: if csv == '-' or csv is None: csv = sys.stdin elif isinstance(csv, str): csv = csv_local = open(csv, 'r') if json == '-' or json is None: json = sys.stdout elif isinstance(json, str): json = json_local = open(json, 'w') data = load_csv(csv, **kwargs) save_json(data, json, **kwargs) finally: if csv_local is not None: csv_local.close() if json_local is not None: json_local.close()
Example #29
Source File: anonymizer.py From CAMISIM with Apache License 2.0 | 5 votes |
def anonymize_sequences( self, mapping, input_stream=sys.stdin, output_stream=sys.stdout, sequence_prefix='', file_format="fasta"): """ Anonymize sequences and write a mapping file @attention: only 'fasta' and 'fastq' supported @param mapping: output stream for sequence id mapping @type mapping: file | io.FileIO | StringIO.StringIO @param input_stream: Input stream of fasta format data @type input_stream: file | io.FileIO | StringIO.StringIO @param output_stream: Output stream of anonymous fasta format data @type output_stream: file | io.FileIO | StringIO.StringIO @param sequence_prefix: Prefix of the anonymous sequence id. @type sequence_prefix: basestring @param file_format: Fasta format of input and output. Either 'fasta' or 'fastq'. @type file_format: basestring @return: None @rtype: None """ assert self.is_stream(input_stream) assert self.is_stream(output_stream) assert self.is_stream(mapping) assert isinstance(sequence_prefix, basestring) assert isinstance(file_format, basestring) file_format = file_format.lower() assert file_format in self._legal_formats sequence_counter = 0 for seq_record in SeqIO.parse(input_stream, file_format): anonymous_id = "{pre}{ct}".format(pre=sequence_prefix, ct=sequence_counter) map_line = "{oldid}\t{newid}\n".format(oldid=seq_record.id, newid=anonymous_id) mapping.write(map_line) seq_record.id = anonymous_id seq_record.description = '' output_stream.write(seq_record.format(file_format)) sequence_counter += 1
Example #30
Source File: ref_seq_gen.py From CAMISIM with Apache License 2.0 | 5 votes |
def findPlasmids(outPlasmidFilePath): """ Read sequence descriptions from a DBK files (stdin), output sequence ids (record.id) if the corresponding description contain "plasmid". Plasmids can be also within the sequences! """ # append to a file if it already exists if os.path.isfile(outPlasmidFilePath): outFileMode = 'a' else: outFileMode = 'w' outBuffer = csv.OutFileBuffer(outPlasmidFilePath, bufferText=False, fileOpenMode=outFileMode) recordCount = 0 plasmidCount = 0 for record in SeqIO.parse(sys.stdin, "genbank"): recordCount += 1 if string.find(record.description, 'plasmid') != -1: outBuffer.writeText(str(str(record.id) + '\n')) plasmidCount += 1 outBuffer.close() print 'file, records, plasmids:', outPlasmidFilePath, recordCount, plasmidCount #for feature in record.features: # if feature.type == "source":