PiFace   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 77
Duplicated Lines 20.78 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 16
loc 77
rs 10
c 1
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A event_detect() 0 9 4
B __init__() 0 27 3
A destroy() 0 14 3
A status_input() 0 5 2
B set_output() 16 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
logger = logging.getLogger(__name__)
6
logger.debug("%s loaded", __name__)
7
8
import pifacedigitalio as p  # basic for PiFce control
9
from doorpi.keyboard.AbstractBaseClass import KeyboardAbstractBaseClass, HIGH_LEVEL, LOW_LEVEL
10
import doorpi
11
12
13
def get(**kwargs): return PiFace(**kwargs)
14
15
16
class PiFace(KeyboardAbstractBaseClass):
17
18
    def __init__(self, input_pins, output_pins, keyboard_name, bouncetime,
0 ignored issues
show
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
Unused Code introduced by
The argument args seems to be unused.
Loading history...
19
                 polarity=0, pressed_on_key_down=True, *args, **kwargs):
20
        logger.debug("__init__(input_pins = %s, output_pins = %s, polarity = %s)",
21
                     input_pins, output_pins, polarity)
22
        self.keyboard_name = keyboard_name
23
        self._polarity = polarity
24
        self._InputPins = map(int, input_pins)
25
        self._OutputPins = map(int, output_pins)
26
        self._pressed_on_key_down = pressed_on_key_down
27
28
        p.init()
29
        self.__listener = p.InputEventListener()
30
        for input_pin in self._InputPins:
31
            self.__listener.register(
32
                pin_num=input_pin,
33
                direction=p.IODIR_BOTH,
34
                callback=self.event_detect,
35
                settle_time=bouncetime / 1000  # from milliseconds to seconds
36
            )
37
            self._register_EVENTS_for_pin(input_pin, __name__)
38
        self.__listener.activate()
39
40
        # use set_output to register status @ dict self.__OutputStatus
41
        for output_pin in self._OutputPins:
42
            self.set_output(output_pin, 0, False)
43
44
        self.register_destroy_action()
45
46
    def destroy(self):
47
        if self.is_destroyed:
48
            return
49
        logger.debug("destroy")
50
        
51
        # shutdown listener
52
        self.__listener.deactivate()
53
        
54
        # shutdown all output-pins
55
        for output_pin in self._OutputPins:
56
            self.set_output(output_pin, 0, False)
57
        p.deinit()
58
        doorpi.DoorPi().event_handler.unregister_source(__name__, True)
59
        self.__destroyed = True
60
61
    def event_detect(self, event):
62
        if self.status_input(event.pin_num):
63
            self._fire_OnKeyDown(event.pin_num, __name__)
64
            if self._pressed_on_key_down:  # issue 134
65
                self._fire_OnKeyPressed(event.pin_num, __name__)
66
        else:
67
            self._fire_OnKeyUp(event.pin_num, __name__)
68
            if not self._pressed_on_key_down:  # issue 134
69
                self._fire_OnKeyPressed(event.pin_num, __name__)
70
71
    def status_input(self, pin):
72
        if self._polarity is 0:
73
            return str(p.digital_read(int(pin))).lower() in HIGH_LEVEL
74
        else:
75
            return str(p.digital_read(int(pin))).lower() in LOW_LEVEL
76
77 View Code Duplication
    def set_output(self, pin, value, log_output = True):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
        parsed_pin = doorpi.DoorPi().parse_string("!"+str(pin)+"!")
79
        if parsed_pin != "!"+str(pin)+"!":
80
            pin = parsed_pin
81
82
        pin = int(pin)
83
        value = str(value).lower() in HIGH_LEVEL
84
        if self._polarity is 1: value = not value
85
        log_output = str(log_output).lower() in HIGH_LEVEL
86
87
        if not pin in self._OutputPins: return False
88
        if log_output: logger.debug("out(pin = %s, value = %s, log_output = %s)", pin, value, log_output)
89
90
        p.digital_write(pin, value)
91
        self._OutputStatus[pin] = value
92
        return True
93