Passed
Push — master ( c8d2fb...72a28d )
by Dave
01:11
created

backend.fcmservice.Configuration.get()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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
from domain.mining import Miner, Pool
4
from messaging.schema import MinerSchema, PoolSchema
5
6
class ServiceName:
7
    '''names of infrastructure services'''
8
    messagebus = 'rabbit'
9
    cache = 'redis'
10
    database = 'mysql'
11
    email = 'gmail'
12
    telegram = 'telegram'
13
14
class InfrastructureService:
15
    '''configuration for a dependency'''
16
    def __init__(self, name, connection, host, port, user, password):
17
        self.name = name
18
        self.connection = connection
19
        self.host = host
20
        self.port = port
21
        self.user = user
22
        self.password = password
23
24
class BaseService():
25
    def __init__(self):
26
        self.homedirectory = os.path.dirname(__file__)
27
28
    def getconfigfilename(self, configfilename):
29
        '''get the contents of a config file'''
30
        return os.path.join(self.homedirectory, configfilename)
31
    def serialize(self, entity):
32
        '''serialize any entity
33
        only need schema, message class not needed
34
        '''
35
        if isinstance(entity, Miner):
36
            schema = MinerSchema()
37
            return schema.dumps(entity).data
38
39
        if isinstance(entity, Pool):
40
            schema = PoolSchema()
41
            return schema.dumps(entity).data
42
        return None
43
44
45
class Configuration(object):
46
    def __init__(self, config):
47
        self.__config = config
48
49
    def get(self, key):
50
        if not key in self.__config:
51
            return None
52
        return self.__config[key]
53
54
    def is_enabled(self, key):
55
        lookupkey = '{0}.enabled'.format(key)
56
        if not lookupkey in self.__config:
57
            return False
58
        value = self.__config[lookupkey]
59
        if isinstance(value, str):
60
            return value == 'true' or value == 'True'
61
        return value
62
63
class Telegram(object):
64
    def __init__(self, config, service):
65
        self.configuration = config
66
        self.service = service
67
68
    def sendmessage(self, message):
69
        if self.configuration.is_enabled('telegram'):
70
            sendalert(message, self.service)
71
72
    def sendphoto(self, file_name):
73
        if os.path.isfile(file_name) and os.path.getsize(file_name) > 0:
74
            if self.configuration.is_enabled('telegram'):
75
                sendphoto(file_name, self.service)
76