s+: add reading from stdin support (#22)

This commit is contained in:
Mark Joshwel 2023-09-04 16:20:02 +00:00
parent b371a7c4df
commit 37c54019ad

View file

@ -33,7 +33,7 @@ from argparse import ArgumentParser
from collections import OrderedDict
from datetime import datetime, timedelta, timezone
from enum import Enum
from sys import stderr, stdout
from sys import stderr, stdout, stdin
from typing import (
Any,
Callable,
@ -544,10 +544,12 @@ class Behaviour(NamedTuple):
dict. keys found in SHAREABLE_TEXT_LINE_*_KEYS used to access address details
are placed top-level in the dict, exceptions are handled by the caller.
see the playground notebook for example output
stderr: TextIO = stderr
stderr: TextIO = sys.stderr
TextIO-like object representing a writeable file. defaults to sys.stderr
stdout: TextIO = stdout
stdout: TextIO = sys.stdout
TextIO-like object representing a writeable file. defaults to sys.stdout
stderr: TextIO = sys.stdin
TextIO-like object representing a readable file. defaults to sts.stdin
debug: bool = False
whether to print debug information to stderr
version_header: bool = False
@ -561,6 +563,7 @@ class Behaviour(NamedTuple):
reverser: Callable[[Latlong], dict[str, Any]] = default_reverser
stderr: TextIO = stderr
stdout: TextIO = stdout
stdin: TextIO = stdin
debug: bool = False
version_header: bool = False
convert_to_type: ConversionResultTypeEnum = ConversionResultTypeEnum.SHAREABLE_TEXT
@ -774,7 +777,8 @@ def handle_args() -> Behaviour:
"full-length Plus Code (6PH58QMF+FX), "
"shortened Plus Code/'local code' (8QMF+FX Singapore), "
"latlong (1.3336875, 103.7749375), "
"or string query (e.g., 'Wisma Atria')"
"string query (e.g., 'Wisma Atria'), "
"or '-' to read from stdin"
),
nargs="*",
)
@ -805,8 +809,22 @@ def handle_args() -> Behaviour:
)
args = parser.parse_args()
query: str | list[str] = ""
if args.query == ["-"]:
stdin_query: list[str] = []
for line in stdin:
stdin_query.append(line.strip())
query = "\n".join(stdin_query)
else:
query: list[str] = args.query
behaviour = Behaviour(
query=args.query,
query=query,
geocoder=default_geocoder,
reverser=default_reverser,
stderr=stderr,