diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e85906a23..e340b2508 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,12 +33,12 @@ jobs: ] fail-fast: false steps: - - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # 6.0.0 with: fetch-depth: 0 persist-credentials: false - name: Install uv - uses: astral-sh/setup-uv@eb1897b8dc4b5d5bfe39a428a8f2304605e0983c # 7.0.0 + uses: astral-sh/setup-uv@1e862dfacbd1d6d858c55d9b792c756523627244 # 7.1.4 - name: Install tox run: uv tool install tox --with tox-uv - name: Setup SocketCAN @@ -55,7 +55,7 @@ jobs: # See: https://github.com/pypy/pypy/issues/3808 TEST_SOCKETCAN: "${{ matrix.os == 'ubuntu-latest' && ! startsWith(matrix.env, 'pypy' ) }}" - name: Coveralls Parallel - uses: coverallsapp/github-action@648a8eb78e6d50909eff900e4ec85cab4524a45b # 2.3.6 + uses: coverallsapp/github-action@5cbfd81b66ca5d10c19b062c04de0199c215fb6e # 2.3.7 with: github-token: ${{ secrets.github_token }} flag-name: Unittests-${{ matrix.os }}-${{ matrix.env }} @@ -66,12 +66,12 @@ jobs: needs: test runs-on: ubuntu-latest steps: - - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # 6.0.0 with: fetch-depth: 0 persist-credentials: false - name: Coveralls Finished - uses: coverallsapp/github-action@648a8eb78e6d50909eff900e4ec85cab4524a45b # 2.3.6 + uses: coverallsapp/github-action@5cbfd81b66ca5d10c19b062c04de0199c215fb6e # 2.3.7 with: github-token: ${{ secrets.github_token }} parallel-finished: true @@ -79,12 +79,12 @@ jobs: static-code-analysis: runs-on: ubuntu-latest steps: - - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # 6.0.0 with: fetch-depth: 0 persist-credentials: false - name: Install uv - uses: astral-sh/setup-uv@eb1897b8dc4b5d5bfe39a428a8f2304605e0983c # 7.0.0 + uses: astral-sh/setup-uv@1e862dfacbd1d6d858c55d9b792c756523627244 # 7.1.4 - name: Install tox run: uv tool install tox --with tox-uv - name: Run linters @@ -97,12 +97,12 @@ jobs: docs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # 6.0.0 with: fetch-depth: 0 persist-credentials: false - name: Install uv - uses: astral-sh/setup-uv@eb1897b8dc4b5d5bfe39a428a8f2304605e0983c # 7.0.0 + uses: astral-sh/setup-uv@1e862dfacbd1d6d858c55d9b792c756523627244 # 7.1.4 - name: Install tox run: uv tool install tox --with tox-uv - name: Build documentation @@ -113,18 +113,18 @@ jobs: name: Packaging runs-on: ubuntu-latest steps: - - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 + - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # 6.0.0 with: fetch-depth: 0 persist-credentials: false - name: Install uv - uses: astral-sh/setup-uv@eb1897b8dc4b5d5bfe39a428a8f2304605e0983c # 7.0.0 + uses: astral-sh/setup-uv@1e862dfacbd1d6d858c55d9b792c756523627244 # 7.1.4 - name: Build wheel and sdist run: uv build - name: Check build artifacts run: uvx twine check --strict dist/* - name: Save artifacts - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # 4.6.2 + uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # 5.0.0 with: name: release path: ./dist @@ -140,7 +140,7 @@ jobs: # upload to PyPI only on release if: github.event.release && github.event.action == 'published' steps: - - uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # 5.0.0 + - uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # 6.0.0 with: path: dist merge-multiple: true diff --git a/can/io/asc.py b/can/io/asc.py index 2c80458c4..fcf8fc5e4 100644 --- a/can/io/asc.py +++ b/can/io/asc.py @@ -116,41 +116,50 @@ def _extract_header(self) -> None: @staticmethod def _datetime_to_timestamp(datetime_string: str) -> float: - # ugly locale independent solution month_map = { - "Jan": 1, - "Feb": 2, - "Mar": 3, - "Apr": 4, - "May": 5, - "Jun": 6, - "Jul": 7, - "Aug": 8, - "Sep": 9, - "Oct": 10, - "Nov": 11, - "Dec": 12, - "Mär": 3, - "Mai": 5, - "Okt": 10, - "Dez": 12, + "jan": 1, + "feb": 2, + "mar": 3, + "apr": 4, + "may": 5, + "jun": 6, + "jul": 7, + "aug": 8, + "sep": 9, + "oct": 10, + "nov": 11, + "dec": 12, + "mär": 3, + "mai": 5, + "okt": 10, + "dez": 12, } - for name, number in month_map.items(): - datetime_string = datetime_string.replace(name, str(number).zfill(2)) datetime_formats = ( "%m %d %I:%M:%S.%f %p %Y", "%m %d %I:%M:%S %p %Y", "%m %d %H:%M:%S.%f %Y", "%m %d %H:%M:%S %Y", + "%m %d %H:%M:%S.%f %p %Y", + "%m %d %H:%M:%S %p %Y", ) + + datetime_string_parts = datetime_string.split(" ", 1) + month = datetime_string_parts[0].strip().lower() + + try: + datetime_string_parts[0] = f"{month_map[month]:02d}" + except KeyError: + raise ValueError(f"Unsupported month abbreviation: {month}") from None + datetime_string = " ".join(datetime_string_parts) + for format_str in datetime_formats: try: return datetime.strptime(datetime_string, format_str).timestamp() except ValueError: continue - raise ValueError(f"Incompatible datetime string {datetime_string}") + raise ValueError(f"Unsupported datetime format: '{datetime_string}'") def _extract_can_id(self, str_can_id: str, msg_kwargs: dict[str, Any]) -> None: if str_can_id[-1:].lower() == "x": diff --git a/can/thread_safe_bus.py b/can/thread_safe_bus.py index 71d6a5536..518604364 100644 --- a/can/thread_safe_bus.py +++ b/can/thread_safe_bus.py @@ -1,12 +1,14 @@ from contextlib import nullcontext from threading import RLock -from typing import Any - -from can import typechecking -from can.bus import BusABC, BusState, CanProtocol -from can.message import Message +from typing import TYPE_CHECKING, Any, cast +from . import typechecking +from .bus import BusState, CanProtocol from .interface import Bus +from .message import Message + +if TYPE_CHECKING: + from .bus import BusABC try: # Only raise an exception on instantiation but allow module @@ -15,11 +17,13 @@ import_exc = None except ImportError as exc: - ObjectProxy = object + ObjectProxy = None # type: ignore[misc,assignment] import_exc = exc -class ThreadSafeBus(ObjectProxy): # pylint: disable=abstract-method +class ThreadSafeBus( + ObjectProxy +): # pylint: disable=abstract-method # type: ignore[assignment] """ Contains a thread safe :class:`can.BusABC` implementation that wraps around an existing interface instance. All public methods @@ -36,8 +40,6 @@ class ThreadSafeBus(ObjectProxy): # pylint: disable=abstract-method instead of :meth:`~can.BusABC.recv` directly. """ - __wrapped__: BusABC - def __init__( self, channel: typechecking.Channel | None = None, @@ -59,58 +61,61 @@ def __init__( ) ) + # store wrapped bus as a proxy-local attribute. Name it with the + # `_self_` prefix so wrapt won't forward it onto the wrapped object. + self._self_wrapped = cast( + "BusABC", object.__getattribute__(self, "__wrapped__") + ) + # now, BusABC.send_periodic() does not need a lock anymore, but the # implementation still requires a context manager - self.__wrapped__._lock_send_periodic = nullcontext() # type: ignore[assignment] + self._self_wrapped._lock_send_periodic = nullcontext() # type: ignore[assignment] # init locks for sending and receiving separately - self._lock_send = RLock() - self._lock_recv = RLock() + self._self_lock_send = RLock() + self._self_lock_recv = RLock() def recv(self, timeout: float | None = None) -> Message | None: - with self._lock_recv: - return self.__wrapped__.recv(timeout=timeout) + with self._self_lock_recv: + return self._self_wrapped.recv(timeout=timeout) def send(self, msg: Message, timeout: float | None = None) -> None: - with self._lock_send: - return self.__wrapped__.send(msg=msg, timeout=timeout) - - # send_periodic does not need a lock, since the underlying - # `send` method is already synchronized + with self._self_lock_send: + return self._self_wrapped.send(msg=msg, timeout=timeout) @property def filters(self) -> typechecking.CanFilters | None: - with self._lock_recv: - return self.__wrapped__.filters + with self._self_lock_recv: + return self._self_wrapped.filters @filters.setter def filters(self, filters: typechecking.CanFilters | None) -> None: - with self._lock_recv: - self.__wrapped__.filters = filters + with self._self_lock_recv: + self._self_wrapped.filters = filters def set_filters(self, filters: typechecking.CanFilters | None = None) -> None: - with self._lock_recv: - return self.__wrapped__.set_filters(filters=filters) + with self._self_lock_recv: + return self._self_wrapped.set_filters(filters=filters) def flush_tx_buffer(self) -> None: - with self._lock_send: - return self.__wrapped__.flush_tx_buffer() + with self._self_lock_send: + return self._self_wrapped.flush_tx_buffer() def shutdown(self) -> None: - with self._lock_send, self._lock_recv: - return self.__wrapped__.shutdown() + with self._self_lock_send, self._self_lock_recv: + return self._self_wrapped.shutdown() @property def state(self) -> BusState: - with self._lock_send, self._lock_recv: - return self.__wrapped__.state + with self._self_lock_send, self._self_lock_recv: + return self._self_wrapped.state @state.setter def state(self, new_state: BusState) -> None: - with self._lock_send, self._lock_recv: - self.__wrapped__.state = new_state + with self._self_lock_send, self._self_lock_recv: + self._self_wrapped.state = new_state @property def protocol(self) -> CanProtocol: - with self._lock_send, self._lock_recv: - return self.__wrapped__.protocol + with self._self_lock_send, self._self_lock_recv: + return self._self_wrapped.protocol diff --git a/doc/changelog.d/2009.changed.md b/doc/changelog.d/2009.changed.md new file mode 100644 index 000000000..6e68198a1 --- /dev/null +++ b/doc/changelog.d/2009.changed.md @@ -0,0 +1 @@ +Improved datetime parsing and added support for “double-defined” datetime strings (such as, e.g., `"30 15:06:13.191 pm 2017"`) for ASCReader class. \ No newline at end of file diff --git a/doc/plugin-interface.rst b/doc/plugin-interface.rst index 2f295b678..612148033 100644 --- a/doc/plugin-interface.rst +++ b/doc/plugin-interface.rst @@ -81,6 +81,10 @@ The table below lists interface drivers that can be added by installing addition +----------------------------+----------------------------------------------------------+ | `python-can-coe`_ | A CAN-over-Ethernet interface for Technische Alternative | +----------------------------+----------------------------------------------------------+ +| `RP1210`_ | CAN channels in RP1210 Vehicle Diagnostic Adapters | ++----------------------------+----------------------------------------------------------+ +| `python-can-damiao`_ | Interface for Damiao USB-CAN adapters | ++----------------------------+----------------------------------------------------------+ .. _python-can-canine: https://github.com/tinymovr/python-can-canine .. _python-can-cvector: https://github.com/zariiii9003/python-can-cvector @@ -90,4 +94,6 @@ The table below lists interface drivers that can be added by installing addition .. _python-can-cando: https://github.com/belliriccardo/python-can-cando .. _python-can-candle: https://github.com/BIRLab/python-can-candle .. _python-can-coe: https://c0d3.sh/smarthome/python-can-coe +.. _RP1210: https://github.com/dfieschko/RP1210 +.. _python-can-damiao: https://github.com/gaoyichuan/python-can-damiao diff --git a/pyproject.toml b/pyproject.toml index 3c263579f..94bf60823 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,6 +72,8 @@ sontheim = ["python-can-sontheim>=0.1.2"] canine = ["python-can-canine>=0.2.2"] zlgcan = ["zlgcan"] candle = ["python-can-candle>=1.2.2"] +rp1210 = ["rp1210>=1.0.1"] +damiao = ["python-can-damiao"] viewer = [ "windows-curses; platform_system == 'Windows' and platform_python_implementation=='CPython'" ] @@ -87,18 +89,18 @@ docs = [ "furo", ] lint = [ - "pylint==3.3.*", + "pylint==4.0.*", "ruff==0.14.*", - "black==25.9.*", - "mypy==1.18.*", + "black==25.11.*", + "mypy==1.19.*", ] test = [ - "pytest==8.4.*", + "pytest==9.0.*", "pytest-timeout==2.4.*", "pytest-modern==0.7.*;platform_system!='Windows'", "coveralls==4.0.*", "pytest-cov==7.0.*", - "coverage==7.10.*", + "coverage==7.12.*", "hypothesis==6.*", "parameterized==0.9.*", ] diff --git a/test/logformats_test.py b/test/logformats_test.py index f4bd1191f..f8a8de91d 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -680,6 +680,40 @@ def test_write(self): self.assertEqual(expected_file.read_text(), actual_file.read_text()) + @parameterized.expand( + [ + ( + "May 27 04:09:35.000 pm 2014", + datetime(2014, 5, 27, 16, 9, 35, 0).timestamp(), + ), + ( + "Mai 27 04:09:35.000 pm 2014", + datetime(2014, 5, 27, 16, 9, 35, 0).timestamp(), + ), + ( + "Apr 28 10:44:52.480 2022", + datetime(2022, 4, 28, 10, 44, 52, 480000).timestamp(), + ), + ( + "Sep 30 15:06:13.191 2017", + datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(), + ), + ( + "Sep 30 15:06:13.191 pm 2017", + datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(), + ), + ( + "Sep 30 15:06:13.191 am 2017", + datetime(2017, 9, 30, 15, 6, 13, 191000).timestamp(), + ), + ] + ) + def test_datetime_to_timestamp( + self, datetime_string: str, expected_timestamp: float + ): + timestamp = can.ASCReader._datetime_to_timestamp(datetime_string) + self.assertAlmostEqual(timestamp, expected_timestamp) + class TestBlfFileFormat(ReaderWriterTest): """Tests can.BLFWriter and can.BLFReader.