Python click.get_text_stream() Examples
The following are 30
code examples of click.get_text_stream().
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
click
, or try the search function
.
Example #1
Source File: callbacks.py From notifiers with MIT License | 6 votes |
def _notify(p, **data): """The callback func that will be hooked to the ``notify`` command""" message = data.get("message") if not message and not sys.stdin.isatty(): message = click.get_text_stream("stdin").read() data["message"] = message data = clean_data(data) ctx = click.get_current_context() if ctx.obj.get("env_prefix"): data["env_prefix"] = ctx.obj["env_prefix"] rsp = p.notify(**data) rsp.raise_on_errors() click.secho(f"Succesfully sent a notification to {p.name}!", fg="green")
Example #2
Source File: cli.py From pygeobuf with ISC License | 6 votes |
def encode(precision, with_z): """Given GeoJSON on stdin, writes a geobuf file to stdout.""" logger = logging.getLogger('geobuf') stdin = click.get_text_stream('stdin') sink = click.get_binary_stream('stdout') try: data = json.load(stdin) pbf = geobuf.encode( data, precision if precision >= 0 else 6, 3 if with_z else 2) sink.write(pbf) sys.exit(0) except Exception: logger.exception("Failed. Exception caught") sys.exit(1)
Example #3
Source File: gateway_client.py From threema-msgapi-sdk-python with MIT License | 6 votes |
def send_simple(ctx, **arguments): # Read message from stdin text = click.get_text_stream('stdin').read().strip() # Create connection with Connection(arguments['from'], arguments['secret'], **ctx.obj) as connection: # Create message message = simple.TextMessage( connection=connection, to_id=arguments['to'], text=text ) # Send message click.echo() click.echo((yield from message.send()))
Example #4
Source File: helper.py From twtxt with MIT License | 6 votes |
def validate_text(ctx, param, value): conf = click.get_current_context().obj["conf"] if isinstance(value, tuple): value = " ".join(value) if not value and not sys.stdin.isatty(): value = click.get_text_stream("stdin").read() if value: value = value.strip() if conf.character_warning and len(value) > conf.character_warning: click.confirm("✂ Warning: Tweet is longer than {0} characters. Are you sure?".format( conf.character_warning), abort=True) return value else: raise click.BadArgumentUsage("Text can’t be empty.")
Example #5
Source File: gateway_client.py From threema-msgapi-sdk-python with MIT License | 6 votes |
def decrypt(private_key, public_key, nonce): # Get key instances private_key = util.read_key_or_key_file(private_key, Key.Type.private) public_key = util.read_key_or_key_file(public_key, Key.Type.public) # Convert nonce to bytes nonce = binascii.unhexlify(nonce) # Read message from stdin and convert to bytes message = click.get_text_stream('stdin').read() message = binascii.unhexlify(message) # Unpack message connection = _MockConnection(private_key, public_key) parameters = {'from_id': '', 'message_id': '', 'date': ''} message = yield from e2e.Message.receive(connection, parameters, nonce, message) # Ensure that this is a text message if message.type is not e2e.Message.Type.text_message: raise TypeError('Cannot decrypt message type {} in CLI'.format(message.type)) # Print text click.echo() click.echo(message.text)
Example #6
Source File: cli.py From Lintly with MIT License | 6 votes |
def main(**options): """Slurp up linter output and send it to a GitHub PR review.""" configure_logging(log_all=options.get('log')) stdin_stream = click.get_text_stream('stdin') stdin_text = stdin_stream.read() click.echo(stdin_text) config = Config(options) build = LintlyBuild(config, stdin_text) try: build.execute() except NotPullRequestException: logger.info('Not a PR. Lintly is exiting.') sys.exit(0) exit_code = 0 # Exit with the number of files that have violations if not options['exit_zero']: exit_code = len(build.violations) sys.exit(exit_code)
Example #7
Source File: cli.py From trailscraper with Apache License 2.0 | 5 votes |
def generate(): """Generates a policy that allows the events passed in through STDIN""" stdin = click.get_text_stream('stdin') records = parse_records(json.load(stdin)['Records']) policy = policy_generator.generate_policy(records) click.echo(policy.to_json())
Example #8
Source File: cli.py From gTTS with MIT License | 5 votes |
def tts_cli(text, file, output, slow, tld, lang, nocheck): """ Read <text> to mp3 format using Google Translate's Text-to-Speech API (set <text> or --file <file> to - for standard input) """ # stdin for <text> if text == '-': text = click.get_text_stream('stdin').read() # stdout (when no <output>) if not output: output = click.get_binary_stream('stdout') # <file> input (stdin on '-' is handled by click.File) if file: try: text = file.read() except UnicodeDecodeError as e: # pragma: no cover log.debug(str(e), exc_info=True) raise click.FileError( file.name, "<file> must be encoded using '%s'." % sys_encoding()) # TTS try: tts = gTTS( text=text, lang=lang, slow=slow, tld=tld, lang_check=not nocheck) tts.write_to_fp(output) except (ValueError, AssertionError) as e: raise click.UsageError(str(e)) except gTTSError as e: raise click.ClickException(str(e))
Example #9
Source File: cli.py From trailscraper with Apache License 2.0 | 5 votes |
def guess(only): """Extend a policy passed in through STDIN by guessing related actions""" stdin = click.get_text_stream('stdin') policy = parse_policy_document(stdin) allowed_prefixes = [s.title() for s in only] policy = guess_statements(policy, allowed_prefixes) click.echo(policy.to_json())
Example #10
Source File: cli.py From followthemoney with MIT License | 5 votes |
def pretty(infile): stdout = click.get_text_stream('stdout') try: while True: entity = read_entity(infile) if entity is None: break data = json.dumps(entity.to_dict(), indent=2) stdout.write(data + '\n') except BrokenPipeError: raise click.Abort()
Example #11
Source File: test_prompt_utils.py From athenacli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_confirm_destructive_query_notty(): stdin = click.get_text_stream('stdin') assert stdin.isatty() is False sql = 'drop database foo;' assert confirm_destructive_query(sql) is None
Example #12
Source File: login.py From aws-adfs with MIT License | 5 votes |
def _stdin_user_credentials(): stdin = click.get_text_stream('stdin').read() stdin_lines = stdin.strip().splitlines() try: username, password = stdin_lines[:2] except ValueError: print('Failed to read newline separated username and password from stdin.') username = None password = None return username, password
Example #13
Source File: gateway_client.py From threema-msgapi-sdk-python with MIT License | 5 votes |
def encrypt(private_key, public_key): # Get key instances private_key = util.read_key_or_key_file(private_key, Key.Type.private) public_key = util.read_key_or_key_file(public_key, Key.Type.public) # Read text from stdin text = click.get_text_stream('stdin').read() # Print nonce and message as hex connection = _MockConnection(private_key, public_key) message = e2e.TextMessage(connection, text=text, to_id='') nonce, message = yield from message.send(get_data_only=True) click.echo() click.echo(binascii.hexlify(nonce)) click.echo(binascii.hexlify(message))
Example #14
Source File: gateway_client.py From threema-msgapi-sdk-python with MIT License | 5 votes |
def send_e2e(ctx, **arguments): # Get key instances private_key = util.read_key_or_key_file(arguments['private_key'], Key.Type.private) if arguments['public_key'] is not None: public_key = util.read_key_or_key_file(arguments['public_key'], Key.Type.public) else: public_key = None # Read message from stdin text = click.get_text_stream('stdin').read().strip() # Create connection connection = Connection( identity=arguments['from'], secret=arguments['secret'], key=private_key, **ctx.obj ) with connection: # Create message message = e2e.TextMessage( connection=connection, to_id=arguments['to'], key=public_key, text=text ) # Send message click.echo() click.echo((yield from message.send()))
Example #15
Source File: util.py From planet-client-python with Apache License 2.0 | 5 votes |
def echo_json_response(response, pretty, limit=None, ndjson=False): '''Wrapper to echo JSON with optional 'pretty' printing. If pretty is not provided explicity and stdout is a terminal (and not redirected or piped), the default will be to indent and sort keys''' indent = None sort_keys = False nl = False if not ndjson and (pretty or (pretty is None and sys.stdout.isatty())): indent = 2 sort_keys = True nl = True try: if ndjson and hasattr(response, 'items_iter'): items = response.items_iter(limit) for item in items: click.echo(json.dumps(item)) elif not ndjson and hasattr(response, 'json_encode'): response.json_encode(click.get_text_stream('stdout'), limit=limit, indent=indent, sort_keys=sort_keys) else: res = response.get_raw() if len(res) == 0: # if the body is empty, just return the status click.echo("status: {}".format(response.response.status_code)) else: res = json.dumps(json.loads(res), indent=indent, sort_keys=sort_keys) click.echo(res) if nl: click.echo() except IOError as ioe: # hide scary looking broken pipe stack traces raise click.ClickException(str(ioe))
Example #16
Source File: dns.py From gandi.cli with GNU General Public License v3.0 | 5 votes |
def update(gandi, fqdn, name, type, value, ttl, file): """Update record entry for a domain. --file option will ignore other parameters and overwrite current zone content with provided file content. """ domains = gandi.dns.list() domains = [domain['fqdn'] for domain in domains] if fqdn not in domains: gandi.echo('Sorry domain %s does not exist' % fqdn) gandi.echo('Please use one of the following: %s' % ', '.join(domains)) return content = '' if file: content = file.read() elif not sys.stdin.isatty(): content = click.get_text_stream('stdin').read() content = content.strip() if not content and not name and not type and not value: click.echo('Cannot find parameters for zone content to update.') return if name and type and not value: click.echo('You must provide one or more value parameter.') return result = gandi.dns.update_record(fqdn, name, type, value, ttl, content) gandi.echo(result['message'])
Example #17
Source File: cli.py From catt with BSD 2-Clause "Simplified" License | 5 votes |
def process_url(ctx, param, value): if value == "-": stdin_text = click.get_text_stream("stdin") if not stdin_text.isatty(): value = stdin_text.read().strip() else: raise CliError("No input received from stdin") if "://" not in value: if ctx.info_name != "cast": raise CliError("Local file not allowed as argument to this command") if not Path(value).is_file(): raise CliError("The chosen file does not exist") return value
Example #18
Source File: test_prompt_utils.py From litecli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_confirm_destructive_query_notty(): stdin = click.get_text_stream("stdin") assert stdin.isatty() is False sql = "drop database foo;" assert confirm_destructive_query(sql) is None
Example #19
Source File: utils.py From textkit with MIT License | 5 votes |
def write_csv(rows, delim): writer = csv.writer(click.get_text_stream('stdout'), delimiter=delim, lineterminator='\n') try: [writer.writerow(row) for row in rows] except (OSError, IOError): sys.stderr.close()
Example #20
Source File: cli.py From easse with GNU General Public License v3.0 | 5 votes |
def get_sys_sents(test_set, sys_sents_path=None): # Get system sentences to be evaluated if sys_sents_path is not None: return read_lines(sys_sents_path) else: # read the system output with click.get_text_stream('stdin', encoding='utf-8') as system_output_file: return system_output_file.read().splitlines()
Example #21
Source File: indexing.py From ms_deisotope with Apache License 2.0 | 5 votes |
def msms_intervals(paths, processes=4, time_radius=5, mz_lower=2., mz_higher=3., output=None): '''Construct an interval tree spanning time and m/z domains where MSn spectra were acquired in the LC-MS map. The interval tree is serialized to JSON. ''' interval_extraction = _MSMSIntervalTask(time_radius, mz_lower, mz_higher) interval_set = [] total_work_items = len(paths) * processes * 4 def _run(): for path in paths: reader = MSFileLoader(path) chunk_out_of_order = quick_index.run_task_in_chunks( reader, processes, processes * 4, task=interval_extraction) for chunk in chunk_out_of_order: interval_set.extend(chunk) yield 0 work_iterator = _run() with click.progressbar(work_iterator, length=total_work_items, label='Extracting Intervals') as g: for _ in g: pass tree = scan_interval_tree.ScanIntervalTree(scan_interval_tree.make_rt_tree(interval_set)) if output is not None: with open(output, 'wt') as fh: tree.serialize(fh) else: stream = click.get_text_stream('stdout') tree.serialize(stream) stream.flush()
Example #22
Source File: utils.py From ms_deisotope with Apache License 2.0 | 5 votes |
def __init__(self, iterable=None, length=None, label=None, item_show_func=None, interval=None, file=None, **kwargs): if iterable is not None: try: length = len(iterable) except TypeError: pass if interval is None: if length is None: interval = 1000 else: interval = int(length * 0.05) if interval == 0: interval = 1 if label is None: label = '' if file is None: file = click.get_text_stream('stderr') self.iterable = iterable self.length = length self.item_show_func = item_show_func self.current_item = None self.count = 0 self.interval = interval self.last_update = 0 self.label = label self.file = file
Example #23
Source File: utils.py From ms_deisotope with Apache License 2.0 | 5 votes |
def __init__(self, stream=None, symbols=None, title=None): if symbols is None: symbols = list(self._default_symbols) if stream is None: stream = click.get_text_stream('stdout') self.symbol_cycle = itertools.cycle(symbols) self.stream = stream self.title = title
Example #24
Source File: cli.py From chinese-support-redux with GNU General Public License v3.0 | 5 votes |
def tts_cli(text, file, output, slow, lang, nocheck): """ Read <text> to mp3 format using Google Translate's Text-to-Speech API (set <text> or --file <file> to - for standard input) """ # stdin for <text> if text == '-': text = click.get_text_stream('stdin').read() # stdout (when no <output>) if not output: output = click.get_binary_stream('stdout') # <file> input (stdin on '-' is handled by click.File) if file: try: text = file.read() except UnicodeDecodeError as e: # pragma: no cover log.debug(str(e), exc_info=True) raise click.FileError( file.name, "<file> must be encoded using '%s'." % sys_encoding()) # TTS try: tts = gTTS( text=text, lang=lang, slow=slow, lang_check=not nocheck) tts.write_to_fp(output) except (ValueError, AssertionError) as e: raise click.UsageError(str(e)) except gTTSError as e: raise click.ClickException(str(e))
Example #25
Source File: cli.py From pygeobuf with ISC License | 5 votes |
def decode(): """Given a Geobuf byte string on stdin, write a GeoJSON feature collection to stdout.""" logger = logging.getLogger('geobuf') stdin = click.get_binary_stream('stdin') sink = click.get_text_stream('stdout') try: pbf = stdin.read() data = geobuf.decode(pbf) json.dump(data, sink) sys.exit(0) except Exception: logger.exception("Failed. Exception caught") sys.exit(1)
Example #26
Source File: cli.py From sacremoses with MIT License | 5 votes |
def process_pipeline(processors, encoding, **kwargs): with click.get_text_stream("stdin", encoding=encoding) as fin: iterator = fin # Initialize fin as the first iterator. for proc in processors: iterator = proc(list(iterator), **kwargs) if iterator: for item in iterator: click.echo(item)
Example #27
Source File: test_prompt_utils.py From pgcli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_confirm_destructive_query_notty(): stdin = click.get_text_stream("stdin") if not stdin.isatty(): sql = "drop database foo;" assert confirm_destructive_query(sql) is None
Example #28
Source File: message.py From uptick with MIT License | 5 votes |
def verify(ctx, file, account): """ Verify a signed message """ if not file: print_message("Prompting for message. Terminate with CTRL-D", "info") file = click.get_text_stream("stdin") m = Message(file.read(), bitshares_instance=ctx.bitshares) try: if m.verify(): print_message("Verified", "success") else: print_message("not verified", "error") except InvalidMessageSignature: print_message("Signature INVALID!", "error")
Example #29
Source File: message.py From uptick with MIT License | 5 votes |
def sign(ctx, file, account): """ Sign a message with an account """ if not file: print_message("Prompting for message. Terminate with CTRL-D", "info") file = click.get_text_stream("stdin") m = Message(file.read(), bitshares_instance=ctx.bitshares) print_message(m.sign(account), "info")
Example #30
Source File: cli.py From osm-wikidata with GNU General Public License v3.0 | 4 votes |
def check_saved_edits(): app.config.from_object('config.default') database.init_app(app) q = ChangesetEdit.query.order_by(ChangesetEdit.saved.desc()) total = q.count() report_timestamp = datetime.now() reject_count = 0 stdout = click.get_text_stream('stdout') for num, edit in enumerate(q): ret = matcher.check_item_candidate(edit.candidate) item = edit.candidate.item if num % 100 == 0: status = f'{num:6,d}/{total:6,d} {num/total:5.1%}' status += f' bad: {reject_count:3d} {item.qid:10s} {item.label()}' click.echo(status) stdout.flush() if 'reject' not in ret: continue for f in 'matching_tags', 'place_names': if f in ret and isinstance(ret[f], set): ret[f] = list(ret[f]) try: reject = EditMatchReject(edit=edit, report_timestamp=report_timestamp, matcher_result=ret) database.session.add(reject) database.session.commit() except sqlalchemy.exc.StatementError: pprint(ret) raise reject_count += 1 continue if len(ret) == 1: print((item.qid, item.label(), ret['reject'])) else: print(item.qid, item.label()) if 'place_names' in ret: print('place names:', ret.pop('place_names')) pprint(ret) if 'osm_tags' not in ret: pprint(edit.candidate.tags)