libs.initialize   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 60
dl 0
loc 118
rs 10
c 0
b 0
f 0

3 Methods

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