Passed
Push — master ( 72d98f...4e0ed0 )
by Randy
04:14 queued 03:02
created

libs.initialize.Initialize.__config_checker()   B

Complexity

Conditions 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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