• 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

44.05
/nixnet/system/_databases.py
1
from collections.abc import Mapping
1✔
2
import typing  # NOQA: F401
1✔
3

1✔
4
import six
5

1✔
6
from nixnet import _funcs
7

1✔
8

×
9
class AliasCollection(Mapping):
NEW
10
    """Alias aliases."""
×
11

1✔
12
    def __init__(self, handle):
13
        # type: (int) -> None
1✔
14
        self._handle = handle
15

1✔
16
    def __repr__(self):
17
        return '{}(handle={})'.format(type(self).__name__, self._handle)
18

1✔
19
    def __eq__(self, other):
20
        if isinstance(other, self.__class__):
21
            return self._handle == typing.cast(AliasCollection, other)._handle
1✔
22
        else:
23
            return NotImplemented
1✔
24

25
    def __ne__(self, other):
1✔
26
        result = self.__eq__(other)
×
27
        if result is NotImplemented:
28
            return result
1✔
29
        else:
×
30
            return not result
×
31

32
    def __hash__(self):
×
33
        return hash(self._handle)
34

1✔
35
    def __len__(self):
×
36
        return self._get_database_len('')
×
37

×
38
    def __iter__(self):
39
        return self.keys()
×
40

41
    def __getitem__(self, index):
1✔
42
        # type: (typing.Text) -> Alias
×
43
        """Return the Alias object associated with the specified alias.
44

1✔
45
            Args:
×
46
                index(str): The value of the index (alias name).
47
        """
1✔
48
        if isinstance(index, six.string_types):
×
49
            for alias, filepath in self._get_database_list(''):
50
                if alias == index:
1✔
51
                    return self._create_item(alias, filepath)
52
            else:
53
                raise KeyError('Alias alias %s not found in the system' % index)
54
        else:
55
            raise TypeError(index)
56

57
    def __delitem__(self, index):
×
58
        # type: (typing.Text) -> None
×
59
        """Delete/Remove a database alias from the system.
×
60

×
61
        This function removes the alias from NI-XNET, but does not affect the
62
        database text file. It just removes the alias association to the
×
63
        database file path.
64

×
65
        This function is supported on Windows only, and the alias is removed
66
        from Windows only (not RT targets). Use 'undeploy' to remove an alias
1✔
67
        from a Real-Time (RT) target.
68

69
        Args:
70
            index(str): The name of the alias to delete.
71
        """
72
        _funcs.nxdb_remove_alias(index)
73

74
    def keys(self):
75
        """Return all keys (alias names) used in the AliasCollection object.
76

77
            Yields:
78
                An iterator to all the keys in the Alias object.
79
        """
80
        for alias, _ in self._get_database_list(''):
81
            yield alias
1✔
82

83
    def values(self):
1✔
84
        """Return all Alias objects in the system.
85

86
            Yields:
87
                An iterator to all the values in the AliasCollection object.
88
        """
89
        for alias, filepath in self._get_database_list(''):
×
90
            yield self._create_item(alias, filepath)
×
91

92
    def items(self):
1✔
93
        """Return all aliases and database objects associated with those aliases in the system.
94

95
            Yields:
96
                An iterator to tuple pairs of alias and database objects in the system.
97
        """
98
        for alias, filepath in self._get_database_list(''):
×
99
            yield alias, self._create_item(alias, filepath)
×
100

101
    def add_alias(self, database_alias, database_filepath, default_baud_rate=None):
1✔
102
        # type: (typing.Text, typing.Text, typing.Optional[int]) -> None
103
        """Add a new alias with baud rate size of up to 64 bits to a database file.
104

105
        NI-XNET uses alias names for database files. The alias names provide a
106
        shorter name for display, allow for changes to the file system without
107
        changing the application.
×
108

×
109
        This function is supported on Windows only.
110

1✔
111
        Args:
112
            database_alias(str): Provides the desired alias name. Unlike the names of
113
                other XNET database objects, the alias name can use special
114
                characters such as space and dash. Commas are not allowed in the
115
                alias name. If the alias name already exists, this function
116
                changes the previous filepath to the specified filepath.
117
            database_filepath(str): Provides the path to the CANdb, FIBEX, or LDF
118
                file. Commas are not allowed in the filepath name.
119
            default_baud_rate(int): Provides the default baud rate, used when
120
                filepath refers to a CANdb database (.dbc) or an NI-CAN database
121
                (.ncd). These database formats are specific to CAN and do not
122
                specify a cluster baud rate. Use this default baud rate to
123
                specify a default CAN baud rate to use with this alias. If
124
                database_filepath refers to a FIBEX database (.xml) or LIN LDF
125
                file, the default_baud_rate parameter is ignored. The FIBEX and
126
                LDF database formats require a valid baud rate for every
127
                cluster, and NI-XNET uses that baud rate as the default.
128
        """
129
        if default_baud_rate is None:
130
            default_baud_rate = 0
131

132
        _funcs.nxdb_add_alias64(database_alias, database_filepath, default_baud_rate)
133

134
    def _create_item(self, database_alias, database_filepath):
135
        # type: (typing.Text, typing.Text) -> Alias
136
        return Alias(database_alias, database_filepath)
137

138
    @staticmethod
1✔
139
    def _get_database_len(ip_address):
×
140
        # type: (typing.Text) -> int
141
        alias_buffer_size, filepath_buffer_size = _funcs.nxdb_get_database_list_sizes(ip_address)
1✔
142
        _, _, number_of_databases = _funcs.nxdb_get_database_list(ip_address, alias_buffer_size, filepath_buffer_size)
143
        return number_of_databases
1✔
144

145
    @staticmethod
×
146
    def _get_database_list(ip_address):
147
        # type: (typing.Text) -> typing.List[typing.Tuple[typing.Text, typing.Text]]
1✔
148
        alias_buffer_size, filepath_buffer_size = _funcs.nxdb_get_database_list_sizes(ip_address)
149
        aliases, filepaths, _ = _funcs.nxdb_get_database_list(ip_address, alias_buffer_size, filepath_buffer_size)
150
        return list(zip(aliases.split(","), filepaths.split(",")))
×
151

×
152

×
153
class Alias(object):
154
    """Alias alias."""
1✔
155

156
    def __init__(
157
            self,
×
158
            database_alias,
×
159
            database_filepath,
×
160
    ):
161
        # type: (typing.Text, typing.Text) -> None
162
        self._database_alias = database_alias
1✔
163
        self._database_filepath = database_filepath
164

165
    def __repr__(self):
1✔
166
        return '{}(alias={}, filepath={})'.format(
167
            type(self).__name__, self._database_alias, self._database_filepath)
168

169
    def __eq__(self, other):
170
        if isinstance(other, self.__class__):
171
            other_db = typing.cast(Alias, other)
×
172
            return self.alias == other_db.alias and self.filepath == other_db.filepath
×
173
        else:
174
            return NotImplemented
1✔
175

×
176
    def __ne__(self, other):
177
        result = self.__eq__(other)
178
        if result is NotImplemented:
1✔
179
            return result
×
180
        else:
×
181
            return not result
×
182

183
    def __hash__(self):
×
184
        return hash(self.alias)
185

1✔
186
    @property
×
187
    def alias(self):
×
188
        return self._database_alias
×
189

190
    @property
×
191
    def filepath(self):
192
        # type: () -> typing.Text
1✔
193
        """str: Get the filepath associated with the Alias object"""
×
194
        return self._database_filepath
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