Completed
Push — master ( e26f88...641e43 )
by Thomas
02:22
created

LinphoneRecorder.reset_last_record_filename()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
dl 3
loc 3
rs 10
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 reset_last_record_filename(self):
0 ignored issues
show
Duplication introduced by Thomas
This code seems to be duplicated in your project.
Loading history...
28
        self.__last_record_filename = self.parsed_record_filename
29
        return self.__last_record_filename
30
31
    def __init__(self):
32
        self.__record_filename = DoorPi().config.get(SIPPHONE_SECTION, 'records',
33
                                                     '!BASEPATH!/records/%Y-%m-%d_%H-%M-%S.wav')
34
        if self.__record_filename is '':
35
            logger.debug('no recorder found in config at section DoorPi and key records')
36
            return
37
38
        DoorPi().event_handler.register_action('OnSipPhoneDestroy', self.destroy)
39
40
        DoorPi().event_handler.register_event('OnRecorderStarted', __name__)
41
        DoorPi().event_handler.register_event('OnRecorderStopped', __name__)
42
        DoorPi().event_handler.register_event('OnRecorderCreated', __name__)
43
44
        if DoorPi().config.get_bool(SIPPHONE_SECTION, 'record_while_dialing', 'False') is True:
45
            DoorPi().event_handler.register_action('OnSipPhoneMakeCall', self.start)
46
        else:
47
            DoorPi().event_handler.register_action('OnCallStateConnect', self.start)
48
49
        DoorPi().event_handler.register_action('OnCallStateDisconnect', self.stop)
50
51
        DoorPi().event_handler('OnRecorderCreated', __name__)
52
53
    def start(self):
54
        if self.__record_filename is '':
55
            return
56
57
        if self.__record_filename is not '':
58
            if not os.path.exists(os.path.dirname(self.__last_record_filename)):
59
                logger.info('Path %s does not exist - creating it now', os.path.dirname(self.__last_record_filename))
60
                os.makedirs(os.path.dirname(self.__last_record_filename))
61
62
            logger.debug('starting recording to %s', self.__last_record_filename)
63
            DoorPi().sipphone.current_call.start_recording()
64
            DoorPi().event_handler('OnRecorderStarted', __name__, {
65
                'last_record_filename': self.__last_record_filename
66
            })
67
68
    def stop(self):
69
        if not DoorPi().sipphone.current_call: return
70
        logger.debug('stopping recording to %s', self.__last_record_filename)
71
        DoorPi().sipphone.current_call.stop_recording()
72
        DoorPi().event_handler('OnRecorderStopped', __name__, {
73
            'last_record_filename': self.__last_record_filename
74
        })
75
76
    def destroy(self):
77
        try: self.stop()
78
        except: pass
79
        DoorPi().event_handler.unregister_source(__name__, True)
80