s+/#17: better conversion framework code

This commit is contained in:
Mark Joshwel 2023-09-02 04:22:19 +00:00
parent 3b136c30ba
commit fd66a53c11

View file

@ -39,10 +39,10 @@ from typing import (
Final, Final,
Generic, Generic,
NamedTuple, NamedTuple,
Sequence,
TextIO, TextIO,
TypeAlias, TypeAlias,
TypeVar, TypeVar,
Sequence,
) )
from geopy import Location as _geopy_Location # type: ignore from geopy import Location as _geopy_Location # type: ignore
@ -128,20 +128,13 @@ class IncompletePlusCodeError(Exception):
# data structures # data structures
class SurplusQueryTypes(Enum): class ConversionResultTypeEnum(Enum):
"""enum representing the mode of surplus""" """enum representing what the result type of conversion should be"""
PLUS_CODE = "pluscode" PLUS_CODE = "pluscode"
LOCAL_CODE = "localcode" LOCAL_CODE = "localcode"
LATLONG = "latlong" LATLONG = "latlong"
STRING = "string" SHAREABLE_TEXT = "shareabletext"
class SurplusOperationMode(Enum):
"""enum representing the mode of surplus"""
GENERATE_TEXT = "generate"
CONVERT_TYPES = "convert"
ResultType = TypeVar("ResultType") ResultType = TypeVar("ResultType")
@ -456,7 +449,7 @@ def default_reverser(latlong: Latlong) -> dict[str, Any]:
class Behaviour(NamedTuple): class Behaviour(NamedTuple):
""" """
typing.NamedTuple representing program behaviour typing.NamedTuple representing expected behaviour of surplus
arguments arguments
query: list[str] query: list[str]
@ -473,11 +466,10 @@ class Behaviour(NamedTuple):
TextIO-like object representing a writeable file. defaults to sys.stdout. TextIO-like object representing a writeable file. defaults to sys.stdout.
debug: bool = False debug: bool = False
whether to print debug information to stderr whether to print debug information to stderr
operation_mode: SurplusOperationMode = SurplusOperationMode.GENERATE_TEXT version_header: bool = False
surplus operation mode enum value whether to print version information and exit
convert_to_type: SurplusQueryTypes | None = None convert_to_type: ConversionResultTypeEnum = ConversionResultTypeEnum.SHAREABLE_TEXT
surplus query type enum value for when what type to convert query to
operation_mode = SurplusOperationMode.CONVERT_TYPES
""" """
query: list[str] query: list[str]
@ -487,8 +479,7 @@ class Behaviour(NamedTuple):
stdout: TextIO = stdout stdout: TextIO = stdout
debug: bool = False debug: bool = False
version_header: bool = False version_header: bool = False
operation_mode: SurplusOperationMode = SurplusOperationMode.GENERATE_TEXT convert_to_type: ConversionResultTypeEnum = ConversionResultTypeEnum.SHAREABLE_TEXT
convert_to_type: SurplusQueryTypes | None = None
# functions # functions
@ -698,23 +689,12 @@ def handle_args() -> Behaviour:
"-c", "-c",
"--convert-to", "--convert-to",
type=str, type=str,
choices=[str(v.value) for v in SurplusQueryTypes], choices=[str(v.value) for v in ConversionResultTypeEnum],
help="converts query to another type", help="converts query to another type",
default="", default="",
) )
args = parser.parse_args() args = parser.parse_args()
convert_to_type: SurplusQueryTypes | None = None
if args.convert_to != "":
convert_to_type = SurplusQueryTypes(args.convert_to)
operation_mode: SurplusOperationMode = (
SurplusOperationMode.GENERATE_TEXT
if convert_to_type is None
else SurplusOperationMode.CONVERT_TYPES
)
behaviour = Behaviour( behaviour = Behaviour(
query=args.query, query=args.query,
geocoder=default_geocoder, geocoder=default_geocoder,
@ -723,8 +703,7 @@ def handle_args() -> Behaviour:
stdout=stdout, stdout=stdout,
debug=args.debug, debug=args.debug,
version_header=args.version, version_header=args.version,
operation_mode=operation_mode, convert_to_type=ConversionResultTypeEnum(args.convert_to),
convert_to_type=convert_to_type,
) )
return behaviour return behaviour
@ -768,16 +747,16 @@ def surplus(
# operate on query # operate on query
text: str = "" text: str = ""
if behaviour.operation_mode == SurplusOperationMode.GENERATE_TEXT: match behaviour.convert_to_type:
case ConversionResultTypeEnum.SHAREABLE_TEXT:
# TODO # TODO
return Result[str]("", error="not fully implemented yet") return Result[str]("", error="TODO")
elif behaviour.operation_mode == SurplusOperationMode.CONVERT_TYPES: case _:
# TODO: https://github.com/markjoshwel/surplus/issues/18 # TODO: https://github.com/markjoshwel/surplus/issues/18
return Result[str]("", error="conversion functionality is not implemented yet") return Result[str](
"", error="conversion functionality is not implemented yet"
else: )
return Result[str]("", error="unknown operation mode")
# command-line entry # command-line entry