File: __init__.py

package info (click to toggle)
python-dlt 2.18.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: python: 3,449; makefile: 55
file content (77 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright (C) 2017. BMW Car IT GmbH. All rights reserved.
"""Basic ctypes binding to the DLT library"""
import ctypes
import os

from dlt.core.core_base import *  # noqa: F403


API_VER = None


def get_version(loaded_lib):
    """Return the API version of the loaded libdlt.so library"""
    global API_VER  # pylint: disable=global-statement
    if API_VER is None:
        buf = ctypes.create_string_buffer(255)
        loaded_lib.dlt_get_version(ctypes.byref(buf), 255)
        # buf would be something like:
        # DLT Package Version: X.XX.X STABLE, Package Revision: vX.XX.XX build on Jul XX XXXX XX:XX:XX
        # -SYSTEMD -SYSTEMD_WATCHDOG -TEST -SHM
        buf_split = buf.value.decode().split()

        API_VER = buf_split[3]

    return API_VER


def get_api_specific_file(version):
    """Return specific version api filename, if not found fallback to first major version release"""
    version_tuple = [int(num) for num in version.split(".")]
    name = "core_{}.py".format("".join((str(num) for num in version_tuple)))
    if os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), name)):
        return name

    # The minor version does not exist, try to truncate
    if version_tuple[-1] != 0:
        version_tuple = version_tuple[:-1] + [0]
        name = "core_{}.py".format("".join((str(num) for num in version_tuple)))
        if not os.path.exists(os.path.join(os.path.dirname(os.path.abspath(__file__)), name)):
            raise ImportError("No module file: {}".format(name))

    return name


def check_libdlt_version(api_ver):
    """Check the version compatibility.

    python-dlt now only supports to run libdlt 2.18.5 or above.
    """
    ver_info = tuple(int(num) for num in api_ver.split("."))
    if ver_info < (2, 18, 5):
        raise ImportError(
            "python-dlt only supports libdlt \
        v2.18.5 (33fbad18c814e13bd7ba2053525d8959fee437d1) or above"
        )


API_VER = get_version(dltlib)  # noqa: F405
check_libdlt_version(API_VER)

# Load version specific definitions, if such a file exists, possibly
# overriding above definitions
#
# The intent is to have version specific implementations to be able to
# provide declarations *incrementally*.
#
# For instance if version 2.17.0 introduces new changes in addition to
# retaining all changes from 2.16.0, then core_2170.py would import
# core_2160.py and declare only version specific changes/overrides. The
# loading logic here below should not require changes.
#
# This allows the implementation below to import just one final module
# (as opposed to loading multiple implementations in a specific order)
# to provide new/overriding implementations.
api_specific_file = get_api_specific_file(API_VER)
overrides = __import__("dlt.core.{}".format(api_specific_file[:-3]), globals(), locals(), ["*"])
locals().update(overrides.__dict__)