my.time.tz.via_location: provide default (empty) config if user doesn't have time config defined
This commit is contained in:
parent
76a497f2bb
commit
eae0e1a614
2 changed files with 24 additions and 3 deletions
|
@ -80,8 +80,11 @@ class location:
|
||||||
accuracy: float
|
accuracy: float
|
||||||
|
|
||||||
|
|
||||||
|
from my.core.compat import Literal
|
||||||
class time:
|
class time:
|
||||||
class tz:
|
class tz:
|
||||||
|
policy: Literal['keep', 'convert', 'throw']
|
||||||
|
|
||||||
class via_location:
|
class via_location:
|
||||||
fast: bool
|
fast: bool
|
||||||
sort_locations: bool
|
sort_locations: bool
|
||||||
|
|
|
@ -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
|
from my.core import dataclass
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class config(time.tz.via_location):
|
class config(user_config):
|
||||||
# less precise, but faster
|
# less precise, but faster
|
||||||
fast: bool = True
|
fast: bool = True
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue