PjsuaPlayer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 41.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 25
loc 60
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A player_filename() 0 2 1
B __init__() 25 25 5
A start() 0 14 2
A stop() 0 9 2

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 os
9
10
import doorpi
11
from doorpi.media.CreateDialTone import generate_dial_tone
12
from doorpi.sipphone.AbstractBaseClass import PlayerAbstractBaseClass, SIPPHONE_SECTION
13
14
class PjsuaPlayer(PlayerAbstractBaseClass):
15
16
    __player_id = None
17
    __slot_id = None
18
19
    __player_filename = ''
20
21
    @property
22
    def player_filename(self): return self.__player_filename
23
24 View Code Duplication
    def __init__(self):
0 ignored issues
show
Duplication introduced by Thomas Meissner
This code seems to be duplicated in your project.
Loading history...
25
        self.__player_filename = doorpi.DoorPi().config.get_string_parsed(SIPPHONE_SECTION, 'dialtone',
26
                                                                          '!BASEPATH!/media/ShortDialTone.wav')
27
        if self.__player_filename is '':
28
            logger.debug('no player found in config at section DoorPi and key dialtone')
29
            return
30
31
        self.__player_filename = doorpi.DoorPi().parse_string(self.__player_filename)
32
        if not os.path.exists(os.path.dirname(self.__player_filename)):
33
            logger.info('Path %s does not exist - creating it now', os.path.dirname(self.__player_filename))
34
            os.makedirs(os.path.dirname(self.__player_filename))
35
        dialtone_renew_every_start = doorpi.DoorPi().config.get_bool(SIPPHONE_SECTION, 'dialtone_renew_every_start', False)
36
        if not os.path.isfile(self.__player_filename) or dialtone_renew_every_start:
37
            logger.info('DialTone %s does not exist - creating it now', self.__player_filename)
38
            dialtone_volume = doorpi.DoorPi().config.get_int(SIPPHONE_SECTION, 'dialtone_volume', 35)
39
            generate_dial_tone(self.__player_filename, dialtone_volume)
40
        doorpi.DoorPi().event_handler.register_event('OnPlayerStarted', __name__)
41
        doorpi.DoorPi().event_handler.register_event('OnPlayerStopped', __name__)
42
        doorpi.DoorPi().event_handler.register_event('OnPlayerCreated', __name__)
43
44
        doorpi.DoorPi().event_handler.register_action('OnSipPhoneMakeCall', self.start)
45
        doorpi.DoorPi().event_handler.register_action('OnCallStateConnect', self.stop)
46
        doorpi.DoorPi().event_handler.register_action('OnCallStateDisconnect', self.stop)
47
48
        doorpi.DoorPi().event_handler('OnPlayerCreated', __name__)
49
50
    def start(self):
51
        if self.__player_id is not None:
52
            logger.trace('player already created as player_id %s and playing %s', self.__player_id, self.player_filename)
53
            return
54
55
        doorpi.DoorPi().sipphone.lib.thread_register('PjsuaPlayer_start_thread')
56
57
        self.__player_filename = doorpi.DoorPi().parse_string(self.__player_filename)
58
        logger.debug('starting player from %s', self.__player_filename)
59
        self.__player_id = doorpi.DoorPi().sipphone.lib.create_player(self.__player_filename, True)
60
        doorpi.DoorPi().sipphone.lib.player_set_pos(self.__player_id, 0)
61
        self.__slot_id = doorpi.DoorPi().sipphone.lib.player_get_slot(self.__player_id)
62
        doorpi.DoorPi().sipphone.lib.conf_connect(self.__slot_id, 0)
63
        doorpi.DoorPi().event_handler('OnPlayerStarted', __name__)
64
65
    def stop(self):
66
        if self.__player_id is not None:
67
            doorpi.DoorPi().sipphone.lib.thread_register('PjsuaPlayer_stop_thread')
68
            logger.debug('stopping player from %s', self.__player_filename)
69
            doorpi.DoorPi().sipphone.lib.conf_disconnect(0, self.__slot_id)
70
            doorpi.DoorPi().sipphone.lib.player_destroy(self.__player_id)
71
            self.__player_id = None
72
            self.__slot_id = None
73
            doorpi.DoorPi().event_handler('OnPlayerStopped', __name__)
74