Completed
Push — oop-api ( ea9bd3 )
by Kyle
01:48
created

Color.b()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
1
"""This module handles loading of the libtcod cffi API.
2
"""
3
from __future__ import absolute_import as _
4
5
import sys as _sys
6
import os as _os
7
8
import platform as _platform
9
10
from tcod import __path__
11
12
# add Windows dll's to PATH
13
if _sys.platform == 'win32':
14
    _bits, _linkage = _platform.architecture()
15
    _os.environ['PATH'] += (';' +
16
        _os.path.join(__path__[0], 'x86/' if _bits == '32bit' else 'x64'))
17
18
19
def _import_library_functions(lib):
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...
Comprehensibility Bug introduced by
lib is re-defining a name which is already available in the outer-scope (previously defined on line 54).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
20
    # imports libtcod namespace into thie module
21
    # does not override existing names
22
    g = globals()
23
    for name in dir(lib):
24
        if name[:5] == 'TCOD_':
25
            if (isinstance(getattr(lib, name), ffi.CData) and
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...
26
                ffi.typeof(getattr(lib, name)) == ffi.typeof('TCOD_color_t')):
0 ignored issues
show
Bug introduced by
The Instance of _MockFFI does not seem to have a member named typeof.

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...
27
                g[name[5:]] = Color._new_from_cdata(getattr(lib, name))
28
            elif name.isupper():
29
                g[name[5:]] = getattr(lib, name) # const names
30
            #else:
31
            #    g[name[5:]] = getattr(lib, name) # function names
32
        elif name[:6] == 'TCODK_': # key name
33
            g['KEY_' + name[6:]] = getattr(lib, name)
34
35
NOISE_DEFAULT_HURST = 0.5
36
NOISE_DEFAULT_LACUNARITY = 2.0
37
38
def FOV_PERMISSIVE(p) :
0 ignored issues
show
Coding Style introduced by
No space allowed before :
def FOV_PERMISSIVE(p) :
^
Loading history...
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
    return FOV_PERMISSIVE_0+p
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'FOV_PERMISSIVE_0'
Loading history...
40
41
def BKGND_ALPHA(a):
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...
42
    return BKGND_ALPH | (int(a * 255) << 8)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'BKGND_ALPH'
Loading history...
43
44
def BKGND_ADDALPHA(a):
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...
45
    return BKGND_ADDA | (int(a * 255) << 8)
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'BKGND_ADDA'
Loading history...
46
47
class _MockFFI(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...
48
    def def_extern(self):
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...
49
        return lambda func:func
0 ignored issues
show
Coding Style introduced by
Exactly one space required after :
return lambda func:func
^
Loading history...
50
51
if _os.environ.get('READTHEDOCS'):
52
    # Mock the lib and ffi objects needed to compile docs for readthedocs.io
53
    # Allows an import without building the cffi module first.
54
    lib = object()
55
    ffi = _MockFFI()
56
    RENDERER_SDL = 2
57
    FONT_LAYOUT_ASCII_INCOL = 1
58
    BKGND_SET = 1
59
    BKGND_DEFAULT = 13
60
    KEY_RELEASED = 2
61
    NOISE_DEFAULT = 0
62
else:
63
    from tcod._libtcod import lib, ffi
0 ignored issues
show
Bug introduced by
The name _libtcod does not seem to exist in module tcod.
Loading history...
Configuration introduced by
The import tcod._libtcod could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
64
65
class Color(list):
66
    """
67
68
    Args:
69
        r (int): Red value, from 0 to 255.
70
        g (int): Green value, from 0 to 255.
71
        b (int): Blue value, from 0 to 255.
72
    """
73
74
    def __init__(self, r=0, g=0, b=0):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class list 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...
75
        self[:] = (r & 0xff, g & 0xff, b & 0xff)
76
77
    @property
78
    def r(self):
79
        """int: Red value, always normalised to 0-255."""
80
        return self[0]
81
82
    @r.setter
83
    def r(self, value):
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...
84
        self[0] = value & 0xff
85
86
    @property
87
    def g(self):
88
        """int: Green value, always normalised to 0-255."""
89
        return self[1]
90
    @g.setter
91
    def g(self, value):
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...
92
        self[1] = value & 0xff
93
94
    @property
95
    def b(self):
96
        """int: Blue value, always normalised to 0-255."""
97
        return self[2]
98
    @b.setter
99
    def b(self, value):
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...
100
        self[2] = value & 0xff
101
102
    @classmethod
103
    def _new_from_cdata(cls, cdata):
104
        """new in libtcod-cffi"""
105
        return cls(cdata.r, cdata.g, cdata.b)
106
107
108
    @classmethod
109
    def _new_from_int(cls, integer):
110
        """a TDL int color: 0xRRGGBB
111
112
        new in libtcod-cffi"""
113
        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...
114
115
    def __eq__(self, other):
116
        """Compare equality between colors."""
117
        return (isinstance(other, (Color)) and
118
                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...
119
120
    def __add__(self, other):
121
        """Add two colors together."""
122
        return Color._new_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...
123
124
    def __sub__(self, other):
125
        """Subtract one color from another."""
126
        return Color._new_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...
127
128
    def __mul__(self, other):
129
        """Multiply with a scaler or another color."""
130
        if isinstance(other, (Color, list, tuple)):
131
            return Color._new_from_cdata(lib.TCOD_color_multiply(self, other))
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...
132
        else:
133
            return Color._new_from_cdata(
134
                lib.TCOD_color_multiply_scalar(self, other))
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...
135
136
    def __bytes__(self):
137
        """Return this color in a format suited for color control."""
138
        return b'%c%c%c' % tuple(self)
139
140
    def __str__(self):
141
        """Return this color in a format suited for color control."""
142
        return '%c%c%c' % tuple(self)
143
144
    def __repr__(self):
145
        """Return a printable representation of the current color."""
146
        return "%s(%i,%i,%i)" % (self.__class__.__name__,
147
                                 self.r, self.g, self.b)
148
149
    def __int__(self):
150
        """Return this color as an integer in 0xRRGGBB format."""
151
        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...
152
153
154
_import_library_functions(lib)
155
156
__all__ = [_name for _name in list(globals()) if _name[0] != '_']
157