Fixed the root cause of the failure on the time module.

The previous commit prompted a new error:

```
2024-06-01 10:54:08,310|ERROR|platypush:plugin:bluetooth|module 'platypush.entities.time' has no attribute 'time'
Traceback (most recent call last):
  File "/usr/lib/python3.9/dist-packages/platypush/plugins/__init__.py", line 247, in _runner
    self.main()
  File "/usr/lib/python3.9/dist-packages/platypush/plugins/bluetooth/__init__.py", line 590, in main
    self._refresh_cache()
  File "/usr/lib/python3.9/dist-packages/platypush/plugins/bluetooth/__init__.py", line 146, in _refresh_cache
    get_entities_engine().wait_start()
  File "/usr/lib/python3.9/dist-packages/platypush/entities/__init__.py", line 48, in get_entities_engine
    time_start = time.time()
AttributeError: module 'platypush.entities.time' has no attribute 'time'
```

Which explains even the previous error: `import time` in that module
won't use the `time` module from the Python library, but the `.time`
module within the same directory.

This error only happens when the current directory is part of PYTHONPATH
(and usually it shouldn't), but for sake of keeping things safe I've
replaced `time()` with `utcnow().timestamp()`, with `utcnow` imported
from `platypush.utils`.
This commit is contained in:
Fabio Manganiello 2024-06-01 10:56:02 +02:00
parent b067430cd5
commit 5fc9c1199b

View file

@ -1,8 +1,9 @@
import logging
import time
from threading import Event
from typing import Collection, Optional
from platypush.utils import utcnow
from ._base import (
Entity,
EntityKey,
@ -45,8 +46,8 @@ def get_entities_engine(timeout: Optional[float] = None) -> EntitiesEngine:
:param timeout: Timeout in seconds (default: None).
"""
time_start = time.time()
while not timeout or (time.time() - time_start < timeout):
time_start = utcnow().timestamp()
while not timeout or (utcnow().timestamp() - time_start < timeout):
if _engine:
break