Passed
Push — master ( 1e26bf...eae794 )
by Dave
01:31
created

backend.fcmservice.Telegram.sendphoto()   A

Complexity

Conditions 4

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import os
2
from helpers.telegramhelper import sendalert, sendphoto
3
4
class ServiceName:
5
    '''names of infrastructure services'''
6
    messagebus = 'rabbit'
7
    cache = 'redis'
8
    database = 'mysql'
9
    email = 'gmail'
10
    telegram = 'telegram'
11
12
class InfrastructureService:
13
    '''configuration for a dependency'''
14
    def __init__(self, name, connection, host, port, user, password):
15
        self.name = name
16
        self.connection = connection
17
        self.host = host
18
        self.port = port
19
        self.user = user
20
        self.password = password
21
22
class Configuration(object):
23
    def __init__(self, config):
24
        self.__config = config
25
26
    def get(self, key):
27
        if not key in self.__config:
28
            return None
29
        return self.__config[key]
30
31
    def is_enabled(self, key):
32
        lookupkey = '{0}.enabled'.format(key)
33
        if not lookupkey in self.__config:
34
            return False
35
        value = self.__config[lookupkey]
36
        if isinstance(value, str):
37
            return value == 'true' or value == 'True'
38
        return value
39
40
class Telegram(object):
41
    def __init__(self, config, service):
42
        self.configuration = config
43
        self.service = service
44
45
    def sendmessage(self, message):
46
        if self.configuration.is_enabled('telegram'):
47
            sendalert(message, self.service)
48
49
    def sendphoto(self, file_name):
50
        if os.path.isfile(file_name) and os.path.getsize(file_name) > 0:
51
            if self.configuration.is_enabled('telegram'):
52
                sendphoto(file_name, self.service)
53
54