Passed
Push — master ( 4b6ab6...72d98f )
by Randy
02:02
created

libs.initialize.Initialize.__config_checker()   A

Complexity

Conditions 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.3333
c 0
b 0
f 0
cc 5
nop 1
1
import mysql.connector as sql_client
2
3
from .tools import Tools
4
5
"""
6
    Copyright (c) 2020 Star Inc.(https://starinc.xyz)
7
8
    This Source Code Form is subject to the terms of the Mozilla Public
9
    License, v. 2.0. If a copy of the MPL was not distributed with this
10
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
"""
12
13
14
class Initialize:
15
    print("""
16
    Phishing Blocker Project - Analytics
17
    
18
    Copyright (c) 2020 Star Inc.(https://starinc.xyz)
19
    
20
    Now: {}
21
    ===
22
        This OSS is licensed under the Mozilla Public License, v. 2.0.
23
        Source Code: https://github.com/star-inc/pbp-analytics
24
    ===
25
    """.format(Tools.get_time()))
26
27
    default_configs = {
28
        "WebCapture": {
29
            "capture_type": "2",
30
            "cache_path": "./cache/",
31
            "capture_browser": "firefox"
32
        },
33
        "MySQL": {
34
            "host": "localhost",
35
            "user": None,
36
            "passwd": None,
37
            "database": None
38
        },
39
        "Google Safe Browsing": {
40
            "google_api_key": None
41
        },
42
        "PhishTank": {
43
            "username": None,
44
            "api_key": None
45
        }
46
    }
47
48
    mysql_tables = [
49
        "blacklist",
50
        "result_cache",
51
        "trustlist",
52
        "trust_domain",
53
        "warnlist"
54
    ]
55
56
    def __init__(self, pbp_handle):
57
        """
58
        Load configs and databases from config.ini
59
60
        :param pbp_handle: Analytics object
61
        """
62
        self.handle = pbp_handle
63
        self.__config_checker()
64
65
    def __config_checker(self):
66
        """
67
        Check configs
68
69
        :return:
70
        """
71
        self.handle.cfg = self.default_configs
72
        for item in self.default_configs:
73
            for item_ in self.default_configs.get(item):
74
                if item in self.handle.config and item_ in self.handle.config[item]:
75
                    self.handle.cfg[item][item_] = self.handle.config[item][item_]
76
                else:
77
                    self.handle.cfg[item][item_] = self.default_configs[item][item_]
78
                assert self.handle.cfg[item][item_] is not None, \
79
                    "{}/{} is not found in config.ini".format(item, item_)
80
        return self.__mysql_checker()
81
82
    def __mysql_checker(self):
83
        """
84
        Check databases simply
85
86
        :return:
87
        """
88
        client = sql_client.connect(**self.handle.cfg["MySQL"])
89
90
        def check_table_exists(table: str):
91
            """
92
            To check table whether exists in database
93
94
            :param table: name of table
95
            :return: bool
96
            """
97
            cursor = client.cursor(dictionary=True)
98
            cursor.execute("SHOW TABLES LIKE %s", (table,))
99
            result = cursor.fetchall()
100
            client.commit()
101
            cursor.close()
102
            if result:
103
                return True
104
            return False
105
106
        for item in self.mysql_tables:
107
            assert check_table_exists(item), \
108
                "Table {} does not exists in database".format(item)
109