Completed
Push — master ( d042c7...36d81b )
by Kyle
01:15
created

_cdata()   B

Complexity

Conditions 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 5
c 3
b 0
f 1
dl 0
loc 11
rs 8.5454
1
"""This module focuses on improvements to the Python libtcod API.
2
"""
3
from __future__ import absolute_import as _
4
5
import os as _os
0 ignored issues
show
Unused Code introduced by
Unused os imported as _os
Loading history...
6
import sys as _sys
7
8
import platform as _platform
0 ignored issues
show
Unused Code introduced by
Unused platform imported as _platform
Loading history...
9
import weakref as _weakref
10
import functools as _functools
11
12
def _unpack_char_p(char_p):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
13
    if char_p == ffi.NULL:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
14
        return ''
15
    return ffi.string(char_p).decode()
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named string.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
16
17
def _int(int_or_str):
18
    'return an integer where a single character string may be expected'
19
    if isinstance(int_or_str, str):
20
        return ord(int_or_str)
21
    if isinstance(int_or_str, bytes):
22
        return int_or_str[0]
23
    return int(int_or_str) # check for __count__
24
25
def _cdata(cdata):
26
    """covert value into a cffi.CData instance"""
27
    try: # first check for _CDataWrapper
28
        cdata = cdata.cdata
29
    except AttributeError: # assume cdata is valid
0 ignored issues
show
Unused Code introduced by
This except handler seems to be unused and could be removed.

Except handlers which only contain pass and do not have an else clause can usually simply be removed:

try:
    raises_exception()
except:  # Could be removed
    pass
Loading history...
30
        pass
31
    except KeyError:
0 ignored issues
show
Unused Code introduced by
This except handler seems to be unused and could be removed.

Except handlers which only contain pass and do not have an else clause can usually simply be removed:

try:
    raises_exception()
except:  # Could be removed
    pass
Loading history...
32
        pass
33
    if cdata is None or cdata == 0: # convert None to NULL
34
        cdata = ffi.NULL
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
35
    return cdata
36
37
if _sys.version_info[0] == 2: # Python 2
38
    def _bytes(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
39
        if isinstance(string, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
40
            return string.encode()
41
        return string
42
43
    def _unicode(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
44
        if not isinstance(string, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'unicode'
Loading history...
45
            return string.decode()
46
        return string
47
48
else: # Python 3
49
    def _bytes(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
50
        if isinstance(string, str):
51
            return string.encode()
52
        return string
53
54
    def _unicode(string):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
55
        if isinstance(string, bytes):
56
            return string.decode()
57
        return string
58
59
class _PropagateException():
60
    """ context manager designed to propagate exceptions outside of a cffi
61
    callback context.  normally cffi suppresses the exception
62
63
    when propagate is called this class will hold onto the error until the
64
    control flow leaves the context, then the error will be raised
65
66
    with _PropagateException as propagate:
67
    # give propagate as onerror parameter for ffi.def_extern
68
    """
69
70
    def __init__(self):
71
        self.exc_info = None # (exception, exc_value, traceback)
72
73
    def propagate(self, *exc_info):
74
        """ set an exception to be raised once this context exits
75
76
        if multiple errors are caught, only keep the first exception raised
77
        """
78
        if not self.exc_info:
79
            self.exc_info = exc_info
80
81
    def __enter__(self):
82
        """ once in context, only the propagate call is needed to use this
83
        class effectively
84
        """
85
        return self.propagate
86
87
    def __exit__(self, type, value, traceback):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in type.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
88
        """ if we're holding on to an exception, raise it now
89
90
        prefers our held exception over any current raising error
91
92
        self.exc_info is reset now in case of nested manager shenanigans
93
        """
94
        if self.exc_info:
95
            type, value, traceback = self.exc_info
0 ignored issues
show
Bug introduced by
The tuple unpacking with sequence seems to be unbalanced; 3 value(s) for 0 label(s)

This happens when the amount of values does not equal the amount of labels:

a, b = ("a", "b", "c")  # only 2 labels for 3 values
Loading history...
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 71.
Loading history...
Bug Best Practice introduced by
It seems like you are trying to unpack a non-sequence, which was defined at line 96.
Loading history...
96
            self.exc_info = None
97
        if type:
98
            # Python 2/3 compatible throw
99
            exception = type(value)
100
            exception.__traceback__ = traceback
101
            raise exception
102
103
class _CDataWrapper(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
104
105
    def __init__(self, *args, **kargs):
106
        self.cdata = self._get_cdata_from_args(*args, **kargs)
107
        if self.cdata == None:
108
            self.cdata = ffi.NULL
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
109
        super(_CDataWrapper, self).__init__()
110
111
    def _get_cdata_from_args(self, *args, **kargs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
112
        if len(args) == 1 and isinstance(args[0], ffi.CData) and not kargs:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named CData.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
113
            return args[0]
114
        else:
115
            return None
116
117
118
    def __hash__(self):
119
        return hash(self.cdata)
120
121
    def __eq__(self, other):
122
        try:
123
            return self.cdata == other.cdata
124
        except AttributeError:
125
            return NotImplemented
126
127
    def __getattr__(self, attr):
128
        if 'cdata' in self.__dict__:
129
            return getattr(self.__dict__['cdata'], attr)
130
        raise AttributeError(attr)
131
132
    def __setattr__(self, attr, value):
133
        if hasattr(self, 'cdata') and hasattr(self.cdata, attr):
134
            setattr(self.cdata, attr, value)
135
        else:
136
            super(_CDataWrapper, self).__setattr__(attr, value)
137
138
def _assert_cdata_is_not_null(func):
139
    """Any BSP methods which use a cdata object in a TCOD call need to have
140
    a sanity check, otherwise it may end up passing a NULL pointer"""
141
    if __debug__:
142
        @_functools.wraps(func)
143
        def check_sanity(*args, **kargs):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The variable check_sanity seems to be unused.
Loading history...
144
            assert self.cdata != ffi.NULL and self.cdata is not None, \
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'self'
Loading history...
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
145
                   'cannot use function, cdata is %r' % self.cdata
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'self'
Loading history...
146
            return func(*args, **kargs)
147
    return func
148
149
class BSP(_CDataWrapper):
150
    """
151
152
    .. attribute:: x
153
    .. attribute:: y
154
    .. attribute:: w
155
    .. attribute:: h
156
157
    :param int x: rectangle left coordinate
158
    :param int y: rectangle top coordinate
159
    :param int w: rectangle width
160
    :param int h: rectangle height
161
162
    .. versionchanged:: 2.0
163
       You can create BSP's with this class contructor instead of using
164
       :any:`bsp_new_with_size`.
165
166
    """
167
168
    def __init__(self, *args, **kargs):
169
        self._reference = None # to prevent garbage collection
170
        self._children = _weakref.WeakSet() # used by _invalidate_children
171
        super(BSP, self).__init__(*args, **kargs)
172
        if self._get_cdata_from_args(*args, **kargs) is None:
173
            self._init(*args, **kargs)
174
175
    def _init(self, x, y, w, h):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
176
        self.cdata = ffi.gc(lib.TCOD_bsp_new_with_size(x, y, w, h),
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named gc.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_new_with_size.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
177
                             lib.TCOD_bsp_delete)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_delete.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
178
179
    def _pass_reference(self, reference):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
180
        self._reference = reference
181
        self._reference._children.add(self)
182
        return self
183
184
    def _invalidate_children(self):
185
        """Invalidates BSP instances known to be based off of this one."""
186
        for child in self._children:
187
            child._reference = None
188
            child._invalidate_children()
189
            child.cdata = ffi.NULL
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
190
        self._children.clear()
191
192
    def __repr__(self):
193
        """Provide a useful readout when printed."""
194
        if not self.cdata:
195
            return '<%s NULL!>' % self.__class__.__name__
196
197
        status = 'leaf'
198
        if not self.is_leaf():
199
            status = ('split at dicision=%i,orientation=%r' %
200
                      (self.get_division(), self.get_orientation()))
201
202
        return ('<%s(x=%i,y=%i,w=%i,h=%i)depth=%i,%s>' %
203
                (self.__class__.__name__,
204
                 self.x, self.y, self.w, self.h, self.get_depth(), status))
205
206
    def get_depth(self):
207
        """Return the depth of this node.
208
209
        :rtype: int
210
211
        .. versionadded:: 2.0
212
        """
213
        return self.cdata.level
214
215
    def get_division(self):
216
        """Return the point where this node was divided into parts.
217
218
        :rtype: :any:`int` or :any:`None`
219
220
        .. versionadded:: 2.0
221
        """
222
        if self.is_leaf():
223
            return None
224
        return self.cdata.position
225
226
    def get_orientation(self):
227
        """
228
229
        :rtype: str
230
231
        .. versionadded:: 2.0
232
        """
233
        if self.is_leaf():
234
            return ''
235
        elif self.cdata.horizontal:
236
            return 'horizontal'
237
        else:
238
            return 'vertical'
239
240
    @_assert_cdata_is_not_null
241
    def split_once(self, orientation, position):
242
        """
243
244
        :rtype: tuple
245
246
        .. versionadded:: 2.0
247
        """
248
        # orientation = horz
249
        if orientation[:1].lower() == 'h':
250
            lib.TCOD_bsp_split_once(self.cdata, True, position)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_split_once.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
251
        elif orientation[:1].lower() == 'v':
252
            lib.TCOD_bsp_split_once(self.cdata, False, position)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_split_once.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
253
        else:
254
            raise ValueError("orientation must be 'horizontal' or 'vertical'"
255
                             "\nNot %r" % orientation)
256
        return self.get_children()
257
258
    @_assert_cdata_is_not_null
259
    def split_recursive(self, depth, min_width, min_height,
260
                        max_horz_ratio, max_vert_raito, random=None):
261
        """
262
263
        :rtype: iter
264
265
        .. versionadded:: 2.0
266
        """
267
        lib.TCOD_bsp_split_recursive(self.cdata, random or ffi.NULL,
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_split_recursive.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
268
                                      depth, min_width, min_height,
269
                                      max_horz_ratio, max_vert_raito)
270
        return self.walk()
271
272
    @_assert_cdata_is_not_null
273
    def resize(self, x, y, w, h):
274
        """Resize this BSP to the provided rectangle.
275
276
        :param int x: rectangle left coordinate
277
        :param int y: rectangle top coordinate
278
        :param int w: rectangle width
279
        :param int h: rectangle height
280
281
        .. versionadded:: 2.0
282
        """
283
        lib.TCOD_bsp_resize(self.cdata, x, y, w, h)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_resize.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
284
285
    @_assert_cdata_is_not_null
286
    def get_left(self):
287
        """Return this BSP's 'left' child.
288
289
        Returns None if this BSP is a leaf node.
290
291
        :return: BSP's left/top child or None.
292
        :rtype: :any:`BSP` or :any:`None`
293
294
        .. versionadded:: 2.0
295
        """
296
        if self.is_leaf():
297
            return None
298
        return BSP(lib.TCOD_bsp_left(self.cdata))._pass_reference(self)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_left.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
299
300
    @_assert_cdata_is_not_null
301
    def get_right(self):
302
        """Return this BSP's 'right' child.
303
304
        Returns None if this BSP is a leaf node.
305
306
        :return: BSP's right/bottom child or None.
307
        :rtype: :any:`BSP` or :any:`None`
308
309
        .. versionadded:: 2.0
310
        """
311
        if self.is_leaf():
312
            return None
313
        return BSP(lib.TCOD_bsp_right(self.cdata))._pass_reference(self)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_right.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
314
315
    @_assert_cdata_is_not_null
316
    def get_parent(self):
317
        """Return this BSP's parent node.
318
319
        :return: Returns the parent node as a BSP instance.
320
                 Returns None if this BSP has no parent.
321
        :rtype: :any:`BSP` or :any:`None`
322
323
        .. versionadded:: 2.0
324
        """
325
        node = BSP(lib.TCOD_bsp_father(self.cdata))._pass_reference(self)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_father.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
326
        if node.cdata == ffi.NULL:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
327
            return None
328
        return node
329
330
    @_assert_cdata_is_not_null
331
    def get_children(self):
332
        """Return as a tuple, this instances immediate children, if any.
333
334
        :return: Returns a tuple of (left, right) BSP instances.
335
                 The returned tuple is empty if this BSP has no children.
336
        :rtype: tuple
337
338
        .. versionadded:: 2.0
339
        """
340
        if self.is_leaf():
341
            return ()
342
        return (BSP(lib.TCOD_bsp_left(self.cdata))._pass_reference(self),
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_left.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
343
                BSP(lib.TCOD_bsp_right(self.cdata))._pass_reference(self))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_right.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
344
345
    @_assert_cdata_is_not_null
346
    def walk(self):
347
        """Iterate over this BSP's hieracrhy.
348
349
        The iterator will include the instance which called it.
350
        It will traverse its own children and grandchildren, in no particular
351
        order.
352
353
        :return: Returns an iterator of BSP instances.
354
        :rtype: iter
355
356
        .. versionadded:: 2.0
357
        """
358
        for child in self.get_children():
359
            for grandchild in child.walk():
360
                yield grandchild
361
        yield self
362
363
    @_assert_cdata_is_not_null
364
    def is_leaf(self):
365
        """Returns True if this node is a leaf.  False when this node has children.
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (83/79).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
366
367
        :rtype: bool
368
369
        .. versionadded:: 2.0
370
        """
371
        return bool(lib.TCOD_bsp_is_leaf(self.cdata))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_is_leaf.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
372
373
    @_assert_cdata_is_not_null
374
    def contains(self, x, y):
375
        """Returns True if this node contains these coordinates.
376
377
        :rtype: bool
378
379
        .. versionadded:: 2.0
380
        """
381
        return bool(lib.TCOD_bsp_contains(self.cdata, x, y))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_contains.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
382
383
    @_assert_cdata_is_not_null
384
    def find_node(self, x, y):
385
        """Return the deepest node which contains these coordinates.
386
387
        :rtype: :any:`BSP` or :any:`None`
388
389
        .. versionadded:: 2.0
390
        """
391
        node = BSP(lib.TCOD_bsp_find_node(self.cdata,
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_bsp_find_node.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
392
                                           x, y))._pass_reference(self)
393
        if node.cdata == ffi.NULL:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
394
            node = None
395
        return node
396
397
class HeightMap(_CDataWrapper):
398
    """libtcod HeightMap instance
399
    """
400
401
    def __init__(self, *args, **kargs):
402
        super(HeightMap, self).__init__(*args, **kargs)
403
        if not self.cdata:
404
            self._init(*args, **kargs)
405
406
    def _init(self, width, height):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
407
        self.cdata = ffi.gc(lib.TCOD_heightmap_new(width, height),
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named gc.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
Bug introduced by
The Instance of object does not seem to have a member named TCOD_heightmap_new.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
408
                             lib.TCOD_heightmap_delete)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_heightmap_delete.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
409
410
411
class Color(dict):
412
    """list-like behaviour could change in the future"""
413
414
    def __init__(self, r=0, g=0, b=0):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class dict is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
415
        self['r'] = r
416
        self['g'] = g
417
        self['b'] = b
418
419
    def __getattr__(self, attr):
420
        return self.__getitem__(attr)
421
422
    def __setattr__(self, attr, value):
423
        self.__setitem__(attr, value)
424
425
    @classmethod
426
    def from_cdata(cls, tcod_color):
427
        """new in libtcod-cffi"""
428
        return cls(tcod_color.r, tcod_color.g, tcod_color.b)
429
430
431
    @classmethod
432
    def from_int(cls, integer):
433
        """a TDL int color: 0xRRGGBB
434
435
        new in libtcod-cffi"""
436
        return cls(lib.TDL_color_from_int(integer))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TDL_color_from_int.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
437
438
    def __eq__(self, other):
439
        return (isinstance(other, (Color)) and
440
                lib.TCOD_color_equals(self, other))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_color_equals.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
441
442
    def __mul__(self, other):
443
        if isinstance(other, (Color, list, tuple)):
444
            return Color.from_cdata(lib.TCOD_color_multiply(self,
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_color_multiply.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
445
                                                 other))
446
        else:
447
            return Color.from_cdata(lib.TCOD_color_multiply_scalar(self,
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_color_multiply_scalar.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
448
                                                        other))
449
450
    def __add__(self, other):
451
        return Color.from_cdata(lib.TCOD_color_add(self, other))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_color_add.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
452
453
    def __sub__(self, other):
454
        return Color.from_cdata(lib.TCOD_color_subtract(self, other))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_color_subtract.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
455
456
    def __repr__(self):
457
        return "<%s(%i,%i,%i)>" % (self.__class__.__name__,
458
                                   self.r, self.g, self.b)
459
460
    def __iter__(self):
461
        return iter((self.r, self.g, self.b))
462
463
    def __int__(self):
464
        # new in libtcod-cffi
465
        return lib.TDL_color_RGB(*self)
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TDL_color_RGB.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
466
467
class Key(_CDataWrapper):
468
    """Key Event instance
469
    """
470
471
    def __init__(self, *args):
472
        super(Key, self).__init__(*args)
473
        if self.cdata == ffi.NULL:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
474
            self.cdata = ffi.new('TCOD_key_t*')
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named new.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
475
476
    def __getattr__(self, attr):
477
        if attr == 'c':
478
            return ord(getattr(self.cdata, attr))
479
        else:
480
            return super(Key, self).__getattr__(attr)
481
482
class Mouse(_CDataWrapper):
483
    """Mouse event instance
484
    """
485
486
    def __init__(self, *args):
487
        super(Mouse, self).__init__(*args)
488
        if self.cdata == ffi.NULL:
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named NULL.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
489
            self.cdata = ffi.new('TCOD_mouse_t*')
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named new.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
490
491
492
def clipboard_set(string):
493
    """Set the clipboard contents to string.
494
495
    .. versionadded:: 2.0
496
    """
497
    lib.TCOD_sys_clipboard_set(_bytes(string))
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_sys_clipboard_set.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
498
499
def clipboard_get():
500
    """Return the current contents of the clipboard.
501
502
    .. versionadded:: 2.0
503
    """
504
    return _unpack_char_p(lib.TCOD_sys_clipboard_get())
0 ignored issues
show
Bug introduced by
The Instance of object does not seem to have a member named TCOD_sys_clipboard_get.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
505
506
from tcod.libtcod import *
0 ignored issues
show
Coding Style introduced by
The usage of wildcard imports like tcod.libtcod should generally be avoided.
Loading history...
Unused Code introduced by
NOISE_DEFAULT_HURST was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BKGND_DEFAULT was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BKGND_SET was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
FONT_LAYOUT_ASCII_INCOL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
NOISE_DEFAULT_LACUNARITY was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BKGND_ADDALPHA was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
RENDERER_SDL was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
KEY_RELEASED was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
BKGND_ALPHA was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
NOISE_DEFAULT was imported with wildcard, but is not used.
Loading history...
Unused Code introduced by
FOV_PERMISSIVE was imported with wildcard, but is not used.
Loading history...
507
508
@ffi.def_extern()
509
def _pycall_bsp_callback(node, handle):
510
    """static bool _pycall_bsp_callback(TCOD_bsp_t *node, void *userData);"""
511
    func, userData, propagate = ffi.from_handle(handle)
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named from_handle.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
512
    try:
513
        return func(BSP(node), userData)
514
    except BaseException:
515
        propagate(*_sys.exc_info())
516
        return False
517
518
519
__all__ = [_name for _name in list(globals()) if _name[0] != '_']
520