diff options
| author | 2020-01-27 11:57:29 +0000 | |
|---|---|---|
| committer | 2020-01-27 12:13:10 +0000 | |
| commit | d7cc7781bdb83682596c253a67889ffaeb6f60ef (patch) | |
| tree | 4f020f84cc131d62f527eb76fa21fb1b62267810 /src/utils/parse | |
| parent | add "additional word" (`aword`) command arument spec type (diff) | |
| signature | ||
add 'int' command arg spec type
Diffstat (limited to 'src/utils/parse')
| -rw-r--r-- | src/utils/parse/__init__.py | 9 | ||||
| -rw-r--r-- | src/utils/parse/spec.py | 9 | ||||
| -rw-r--r-- | src/utils/parse/types.py | 8 |
3 files changed, 18 insertions, 8 deletions
diff --git a/src/utils/parse/__init__.py b/src/utils/parse/__init__.py index bc3b8647..cf32d8f5 100644 --- a/src/utils/parse/__init__.py +++ b/src/utils/parse/__init__.py @@ -1,8 +1,9 @@ import decimal, io, typing from src.utils import datetime, errors -from .time import duration from .spec import * +from .time import duration +from .types import try_int COMMENT_TYPES = ["#", "//"] def hashflags(filename: str @@ -78,12 +79,6 @@ def keyvalue(s: str, delimiter: str=" " items[key] = None return items -def try_int(s: str) -> typing.Optional[int]: - try: - return int(s) - except ValueError: - return None - def line_normalise(s: str) -> str: lines = list(filter(None, [line.strip() for line in s.split("\n")])) return " ".join(line.replace(" ", " ") for line in lines) diff --git a/src/utils/parse/spec.py b/src/utils/parse/spec.py index 0fb25aca..a52dc969 100644 --- a/src/utils/parse/spec.py +++ b/src/utils/parse/spec.py @@ -1,6 +1,6 @@ import enum, typing from .time import duration -from . import try_int +from .types import try_int class SpecArgumentContext(enum.IntFlag): CHANNEL = 1 @@ -49,6 +49,12 @@ class SpecArgumentTypeTrimString(SpecArgumentTypeString): def simple(self, args: typing.List[str]): return SpecArgumentTypeString.simple(self, list(filter(None, args))) +class SpecArgumentTypeInt(SpecArgumentType): + def simple(self, args): + if args: + return try_int(args[0]), 1 + return None, 1 + class SpecArgumentTypeDuration(SpecArgumentType): def name(self): return "+%s" % (SpecArgumentType.name(self) or "duration") @@ -68,6 +74,7 @@ SPEC_ARGUMENT_TYPES = { "wordlower": SpecArgumentTypeWordLower, "string": SpecArgumentTypeString, "tstring": SpecArgumentTypeTrimString, + "int": SpecArgumentTypeInt, "duration": SpecArgumentTypeDuration } diff --git a/src/utils/parse/types.py b/src/utils/parse/types.py new file mode 100644 index 00000000..62730847 --- /dev/null +++ b/src/utils/parse/types.py @@ -0,0 +1,8 @@ +import typing + +def try_int(s: str) -> typing.Optional[int]: + try: + return int(s) + except ValueError: + return None + |
