• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ni / nixnet-python / #631999863

08 Apr 2025 07:22AM UTC coverage: 67.546% (-0.005%) from 67.551%
#631999863

Pull #302

travis-ci

Pull Request #302: Fix `tox -e mypy` errors with `mypy 1.14.1`

22 of 66 new or added lines in 15 files covered. (33.33%)

10 existing lines in 1 file now uncovered.

4712 of 6976 relevant lines covered (67.55%)

0.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.18
/nixnet/database/database.py
1
import typing  # NOQA: F401
1✔
2
import warnings
1✔
3

1✔
4
from nixnet import _cconsts
5
from nixnet import _funcs
1✔
6
from nixnet import _props
1✔
7
from nixnet import constants
8
from nixnet import errors
1✔
9

1✔
10
from nixnet.database import _collection
1✔
11
from nixnet.database import _database_object
1✔
12
from nixnet.database import _find_object
1✔
13

14

1✔
15
class Database(_database_object.DatabaseObject):
1✔
16
    """Opens a database file.
1✔
17

18
    When an already open database is opened,
19
    this class grants access to the same database and increases an internal reference counter.
1✔
20
    A multiple referenced (open) database must be closed as many times as it has been opened.
21
    Until it is completely closed, the access to this database remains granted,
22
    and the database uses computer resources (memory and handles).
23
    For more information, refer to :any:`Database.close`.
24

25
    Args:
26
        database_name(str): The database alias or file pathname to open.
27
    """
28
    def __init__(self, database_name):
29
        # type: (typing.Text) -> None
30
        self._handle = None  # To satisfy `__del__` in case nxdb_open_database throws
31
        self._handle = _funcs.nxdb_open_database(database_name)
32

1✔
33
        from nixnet.database import _cluster
34
        self._clusters = _collection.DbCollection(
1✔
35
            self._handle, constants.ObjectClass.CLUSTER, _cconsts.NX_PROP_DATABASE_CLST_REFS, _cluster.Cluster)
1✔
36

37
    def __del__(self):
1✔
38
        if self._handle is not None:
1✔
39
            warnings.warn(
40
                'Database was not explicitly closed before it was destructed. '
41
                'Resources on the device may still be reserved.',
1✔
42
                errors.XnetResourceWarning)
1✔
43

×
44
    def __enter__(self):
45
        return self
46

47
    def __exit__(self, exception_type, exception_value, traceback):
48
        self.close()
1✔
49

1✔
50
    def __eq__(self, other):
51
        if isinstance(other, self.__class__):
1✔
52
            return self._handle == other._handle
1✔
53
        else:
54
            return NotImplemented
1✔
55

×
56
    def __ne__(self, other):
×
57
        result = self.__eq__(other)
58
        if result is NotImplemented:
×
59
            return result
60
        else:
1✔
61
            return not result
×
62

×
63
    def __hash__(self):
×
64
        return hash(self._handle)
65

×
66
    def __repr__(self):
67
        return '{}(handle={})'.format(type(self).__name__, self._handle)
1✔
68

×
69
    def close(self, close_all_refs=False):
70
        # type: (bool) -> None
1✔
71
        """Closes the database.
×
72

73
        For the case that different threads of an application are using the same database,
1✔
74
        :any:`Database` and :any:`Database.close`
75
        maintain a reference counter indicating how many times the database is open.
76
        Every thread can open the database, work with it,
77
        and close the database independently using ``close_all_refs`` set to ``False``.
78
        Only the last call to :any:`Database.close` actually closes access to the database.
79

80
        .. note:: ``Database.__exit__`` calls :any:`Database.close` with ``close_all_refs`` set to ``False``.
81
            See examples of this in :ref:`can_dynamic_database_creation_label`
82
            and :ref:`lin_dynamic_database_creation_label`.
83

84
        Another option is that only one thread executes :any:`Database.close` once,
85
        using ``close_all_refs`` set to ``True``, which closes access for all other threads.
86
        This may be convenient when, for example,
87
        the main program needs to stop all running threads
88
        and be sure the database is closed properly,
89
        even if some threads could not execute :any:`Database.close`.
90

91
        Args:
92
            close_all_refs(bool): Indicates that a database open multiple times
93
                (refer to :any:`Database`) should be closed completely
94
                (``close_all_refs`` is ``True``),
95
                or just the reference counter should be decremented
96
                (``close_all_refs`` is ``False``),
97
                and the database remains open.
98
                When the database is closed completely,
99
                all references to objects in this database become invalid.
100
        """
101
        if self._handle is None:
102
            warnings.warn(
103
                'Attempting to close NI-XNET database but database was already '
104
                'closed', errors.XnetResourceWarning)
105
            return
1✔
106

×
107
        _funcs.nxdb_close_database(self._handle, close_all_refs)
108
        self._handle = None
109

×
110
    def find(
111
            self,
1✔
112
            object_class,  # type: typing.Type[_database_object.DatabaseObject]
1✔
113
            object_name,  # type: typing.Text
114
    ):
1✔
115
        # type: (...) -> _database_object.DatabaseObject
116
        """Finds an object in the database.
117

118
        This function finds a database object relative to this parent object.
119
        This object may be a grandparent or great-grandparent.
120

121
        If this object is a direct parent
122
        (for example, :any:`Frame<_frame.Frame>` for :any:`Signal<_signal.Signal>`),
123
        the ``object_name`` to search for can be short, and the search proceeds quickly.
124

125
        If this object is not a direct parent
126
        (for example, :any:`Database` for :any:`Signal<_signal.Signal>`),
127
        the ``object_name`` to search for must be qualified such
128
        that it is unique within the scope of this object.
129

130
        For example, if the class of this object is :any:`Cluster`,
131
        and ``object_class`` is :any:`Signal<_signal.Signal>`,
132
        you can specify ``object_name`` of ``mySignal``,
133
        assuming that signal name is unique to the cluster.
134
        If not, you must include the :any:`Frame<_frame.Frame>` name as a prefix,
135
        such as ``myFrameA.mySignal``.
136

137
        NI-XNET supports the following subclasses of ``DatabaseObject`` as arguments for ``object_class``:
138

139
        *   :any:`nixnet.database.Cluster<Cluster>`
140
        *   :any:`nixnet.database.Frame<_frame.Frame>`
141
        *   :any:`nixnet.database.Pdu<Pdu>`
142
        *   :any:`nixnet.database.Signal<_signal.Signal>`
143
        *   :any:`nixnet.database.SubFrame<SubFrame>`
144
        *   :any:`nixnet.database.Ecu<Ecu>`
145
        *   :any:`nixnet.database.LinSched<LinSched>`
146
        *   :any:`nixnet.database.LinSchedEntry<LinSchedEntry>`
147

148
        Args:
149
            object_class(``DatabaseObject``): The class of the object to find.
150
            object_name(str): The name of the object to find.
151
        Returns:
152
            An instance of the found object.
153
        Raises:
154
            ValueError: Unsupported value provided for argument ``object_class``.
155
            :any:`XnetError`: The object is not found.
156
        """
157
        return _find_object.find_object(self._handle, object_class, object_name)  # type: ignore
158

159
    def save(self, db_filepath=""):
160
        # type: (typing.Text) -> None
NEW
161
        """Saves the open database to a FIBEX 3.1.0 file.
×
162

163
        The file extension must be .xml. If the target file exists, it is overwritten.
1✔
164

165
        XNET saves to the FIBEX file only features that XNET sessions use to communicate on the network.
166
        If the original file was created using non-XNET software,
167
        the target file may be missing details from the original file.
168
        For example, NI-XNET supports only linear scaling.
169
        If the original FIBEX file used a rational equation that cannot be expressed as a linear scaling,
170
        XNET converts this to a linear scaling with factor 1.0 and offset 0.0.
171

172
        If ``db_filepath`` is empty, the file is saved to the same FIBEX file specified when opened.
173
        If opened as a file path, it uses that file path.
174
        If opened as an alias, it uses the file path registered for that alias.
175

176
        Saving a database is not supported under Real-Time (RT),
177
        but you can deploy and use a database saved on Windows on a Real-Time (RT) target (refer to `Database.deploy`).
178

179
        Args:
180
            db_filepath(str): Contains the pathname to the database file or is
181
                empty (saves to the original filepath).
182
        """
183
        _funcs.nxdb_save_database(self._handle, db_filepath)  # type: ignore
184

185
    @property
186
    def name(self):
NEW
187
        # type: () -> typing.Text
×
188
        return _props.get_database_name(self._handle)  # type: ignore
189

1✔
190
    @property
191
    def clusters(self):
NEW
192
        # type: () -> _collection.DbCollection
×
193
        """:any:`DbCollection`: Returns a collection of :any:`Cluster` objects in this database.
194

1✔
195
        A cluster is assigned to a database when the cluster object is created.
196
        You cannot change this assignment afterwards.
197

198
        FIBEX and AUTOSAR files can contain any number of clusters,
199
        and each cluster uses a unique name.
200

201
        For CANdb (.dbc), LDF (.ldf), or NI-CAN (.ncd) files,
202
        the file contains only one cluster, and no cluster name is stored in the file.
203
        For these database formats, NI-XNET uses the name Cluster for the single cluster.
204
        """
205
        return self._clusters
206

207
    @property
208
    def show_invalid_from_open(self):
209
        # type: () -> bool
1✔
210
        """bool: Show or hide :any:`Frame<_frame.Frame>` and :any:`Signal<_signal.Signal>` objects that are invalid.
211

1✔
212
        After opening a database, this property always is set to ``False``,
213
        meaning that invalid :any:`Cluster`, :any:`Frame<_frame.Frame>`,
214
        and :any:`Signal<_signal.Signal>` objects
215
        are not returned in properties that return a :any:`DbCollection` for the database
216
        (for example, :any:`Cluster.frames` and :any:`Frame.mux_static_signals`).
217
        Invalid :any:`Cluster`, :any:`Frame<_frame.Frame>`,
218
        and :any:`Signal<_signal.Signal>` objects are incorrectly defined
219
        and therefore cannot be used in the bus communication.
220
        The ``False`` setting is recommended when you use the database to create XNET sessions.
221

222
        In case the database was opened to correct invalid configuration
223
        (for example, in a database editor),
224
        you must set the property to ``True`` prior to reading properties that return
225
        a :any:`DbCollection` for the database
226
        (for example, :any:`Cluster.frames` and :any:`Frame.mux_static_signals`).
227

228
        For invalid objects,
229
        the :any:`Cluster.check_config_status`,
230
        :any:`Frame.check_config_status`,
231
        and :any:`Signal.check_config_status` methods raise an exception if there is a problem.
232
        For valid objects, no error is raised.
233

234
        :any:`Cluster`, :any:`Frame<_frame.Frame>`, and :any:`Signal<_signal.Signal>` objects that became
235
        invalid after the database is opened are still returned from the
236
        :any:`Database.clusters`, :any:`Cluster.frames`, and :any:`Frame.mux_static_signals`,
237
        even if :any:`Database.show_invalid_from_open` is ``False``
238
        and Configuration Status returns an error code.
239
        For example, if you open a :any:`Frame<_frame.Frame>` with valid properties,
240
        then you set :any:`Signal.start_bit` beyond the :any:`Frame.payload_len`,
241
        :any:`Frame.check_config_status` raises an exception,
242
        but the frame is returned from :any:`Cluster.frames`.
243
        """
244
        return _props.get_database_show_invalid_from_open(self._handle)  # type: ignore
245

246
    @show_invalid_from_open.setter
247
    def show_invalid_from_open(self, value):
NEW
248
        # type: (bool) -> None
×
249
        _props.set_database_show_invalid_from_open(self._handle, value)  # type: ignore
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc