add 'int' command arg spec type

This commit is contained in:
jesopo 2020-01-27 11:57:29 +00:00
parent d438b6dbc9
commit d7cc7781bd
3 changed files with 18 additions and 8 deletions

View file

@ -1,8 +1,9 @@
import decimal, io, typing import decimal, io, typing
from src.utils import datetime, errors from src.utils import datetime, errors
from .time import duration
from .spec import * from .spec import *
from .time import duration
from .types import try_int
COMMENT_TYPES = ["#", "//"] COMMENT_TYPES = ["#", "//"]
def hashflags(filename: str def hashflags(filename: str
@ -78,12 +79,6 @@ def keyvalue(s: str, delimiter: str=" "
items[key] = None items[key] = None
return items return items
def try_int(s: str) -> typing.Optional[int]:
try:
return int(s)
except ValueError:
return None
def line_normalise(s: str) -> str: def line_normalise(s: str) -> str:
lines = list(filter(None, [line.strip() for line in s.split("\n")])) lines = list(filter(None, [line.strip() for line in s.split("\n")]))
return " ".join(line.replace(" ", " ") for line in lines) return " ".join(line.replace(" ", " ") for line in lines)

View file

@ -1,6 +1,6 @@
import enum, typing import enum, typing
from .time import duration from .time import duration
from . import try_int from .types import try_int
class SpecArgumentContext(enum.IntFlag): class SpecArgumentContext(enum.IntFlag):
CHANNEL = 1 CHANNEL = 1
@ -49,6 +49,12 @@ class SpecArgumentTypeTrimString(SpecArgumentTypeString):
def simple(self, args: typing.List[str]): def simple(self, args: typing.List[str]):
return SpecArgumentTypeString.simple(self, list(filter(None, args))) 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): class SpecArgumentTypeDuration(SpecArgumentType):
def name(self): def name(self):
return "+%s" % (SpecArgumentType.name(self) or "duration") return "+%s" % (SpecArgumentType.name(self) or "duration")
@ -68,6 +74,7 @@ SPEC_ARGUMENT_TYPES = {
"wordlower": SpecArgumentTypeWordLower, "wordlower": SpecArgumentTypeWordLower,
"string": SpecArgumentTypeString, "string": SpecArgumentTypeString,
"tstring": SpecArgumentTypeTrimString, "tstring": SpecArgumentTypeTrimString,
"int": SpecArgumentTypeInt,
"duration": SpecArgumentTypeDuration "duration": SpecArgumentTypeDuration
} }

8
src/utils/parse/types.py Normal file
View file

@ -0,0 +1,8 @@
import typing
def try_int(s: str) -> typing.Optional[int]:
try:
return int(s)
except ValueError:
return None