Completed
Pull Request — master (#161)
by Thomas
01:21
created

LinphoneRecorder   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 64
Duplicated Lines 32.81 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 21
loc 64
rs 10
c 1
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 14 4
A last_record_filename() 0 2 1
A stop() 0 6 2
A __init__() 21 21 3
A record_filename() 0 2 1
A destroy() 0 4 2
A parsed_record_filename() 0 2 1

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
from doorpi import DoorPi
11
from doorpi.sipphone.AbstractBaseClass import RecorderAbstractBaseClass, SIPPHONE_SECTION
12
13
class LinphoneRecorder(RecorderAbstractBaseClass):
14
15
    __record_filename = ''
16
    __last_record_filename = ''
17
18
    @property
19
    def record_filename(self): return self.__record_filename
20
21
    @property
22
    def parsed_record_filename(self): return DoorPi().parse_string(self.__record_filename)
23
24
    @property
25
    def last_record_filename(self): return self.__last_record_filename
26
27 View Code Duplication
    def __init__(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
28
        self.__record_filename = DoorPi().config.get(SIPPHONE_SECTION, 'records',
29
                                                     '!BASEPATH!/records/%Y-%m-%d_%H-%M-%S.wav')
30
        if self.__record_filename is '':
31
            logger.debug('no recorder found in config at section DoorPi and key records')
32
            return
33
34
        DoorPi().event_handler.register_action('OnSipPhoneDestroy', self.destroy)
35
36
        DoorPi().event_handler.register_event('OnRecorderStarted', __name__)
37
        DoorPi().event_handler.register_event('OnRecorderStopped', __name__)
38
        DoorPi().event_handler.register_event('OnRecorderCreated', __name__)
39
40
        if DoorPi().config.get_bool(SIPPHONE_SECTION, 'record_while_dialing', 'False') is True:
41
            DoorPi().event_handler.register_action('OnSipPhoneMakeCall', self.start)
42
        else:
43
            DoorPi().event_handler.register_action('OnCallStateConnect', self.start)
44
45
        DoorPi().event_handler.register_action('OnCallStateDisconnect', self.stop)
46
47
        DoorPi().event_handler('OnRecorderCreated', __name__)
48
49
    def start(self):
50
        if self.__record_filename is '':
51
            return
52
53
        if self.__record_filename is not '':
54
            self.__last_record_filename = self.parsed_record_filename
55
            if not os.path.exists(os.path.dirname(self.__last_record_filename)):
56
                logger.info('Path %s does not exist - creating it now', os.path.dirname(self.__last_record_filename))
57
                os.makedirs(os.path.dirname(self.__last_record_filename))
58
59
            logger.debug('starting recording to %s', self.__last_record_filename)
60
            DoorPi().sipphone.current_call.start_recording()
61
            DoorPi().event_handler('OnRecorderStarted', __name__, {
62
                'last_record_filename': self.__last_record_filename
63
            })
64
65
    def stop(self):
66
        if not DoorPi().sipphone.current_call: return
67
        logger.debug('stopping recording to %s', self.__last_record_filename)
68
        DoorPi().sipphone.current_call.stop_recording()
69
        DoorPi().event_handler('OnRecorderStopped', __name__, {
70
            'last_record_filename': self.__last_record_filename
71
        })
72
73
    def destroy(self):
74
        try: self.stop()
75
        except: pass
76
        DoorPi().event_handler.unregister_source(__name__, True)
77