my.time.tz.via_location: provide default (empty) config if user doesn't have time config defined

This commit is contained in:
Dima Gerasimov 2022-05-22 15:21:08 +01:00 committed by karlicoss
parent 76a497f2bb
commit eae0e1a614
2 changed files with 24 additions and 3 deletions

View file

@ -80,8 +80,11 @@ class location:
accuracy: float
from my.core.compat import Literal
class time:
class tz:
policy: Literal['keep', 'convert', 'throw']
class via_location:
fast: bool
sort_locations: bool

View file

@ -7,12 +7,30 @@ REQUIRES = [
]
from my.config import time
## user might not have tz config section, so makes sense to be more defensive about it
# todo might be useful to extract a helper for this
try:
from my.config import time
except ImportError as ie:
if ie.name != 'time':
raise ie
else:
try:
user_config = time.tz.via_location
except AttributeError as ae:
if not ("'tz'" in str(ae) or "'via_location'"):
raise ae
# deliberately dynamic to prevent confusing mypy
if 'user_config' not in globals():
globals()['user_config'] = object
##
from my.core import dataclass
@dataclass
class config(time.tz.via_location):
class config(user_config):
# less precise, but faster
fast: bool = True