Skip to content

Commit 0b41df3

Browse files
authored
[resotolib][feat] Add optional duration parsing (#1575)
1 parent 5403071 commit 0b41df3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

resotocore/resotocore/infra_apps/local_runtime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from aiostream.core import Stream
1111
from aiostream import stream
1212
from argparse import Namespace
13-
from resotolib.durations import parse_duration
13+
from resotolib.durations import parse_optional_duration
1414
from resotolib.asynchronous.utils import async_lines
1515

1616

@@ -72,7 +72,7 @@ async def perform_search(search: str) -> AsyncIterator[Json]:
7272
async for result in ctx:
7373
yield result
7474

75-
template.globals["parse_duration"] = parse_duration
75+
template.globals["parse_duration"] = parse_optional_duration
7676
template.globals["search"] = perform_search
7777

7878
async for line in async_lines(template.generate_async(config=config, args=kwargs, stdin=stdin)):

resotolib/resotolib/durations.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ def parse_duration(ds: str) -> timedelta:
5656
return timedelta(seconds=duration_parser.parse(ds))
5757

5858

59+
def parse_optional_duration(ds: str) -> Optional[timedelta]:
60+
try:
61+
return parse_duration(ds)
62+
except Exception:
63+
return None
64+
65+
5966
def duration_str(duration: timedelta, precision: Optional[int] = 0, down_to_unit: Optional[str] = None) -> str:
6067
"""
6168
Convert a timedelta to a string representing the duration human-readable short unit syntax.

0 commit comments

Comments
 (0)