tools.py 3.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
'''
Modules containing Tools classes for Encryption
'''
import json
from importlib import import_module

from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_file('./ui/encryption/tools.kv')


class ToolBox(BoxLayout):
    '''
    ToolBox class to present available tools
    '''
    pass


class BackToToolsMenu(BoxLayout):
    '''
    BackToToolsMenu class presents back button and tool title
    '''
    pass


class Tools(BoxLayout):
    '''
    Tools class to present Tools menu of Encryption
    '''
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    def __init__(self, **kwargs):
        super(Tools, self).__init__(**kwargs)

        self.all_tools = {}
        self.removed_tools_menu = None

        with open('data/root.json') as data:
            json_data = json.loads(data.read())
            self.encryption_data = json_data[4]["encryption"]

        for key, value in self.encryption_data[1]["tools"][0].items():
            value['title'] = key
            tool_key = '_'.join(key.lower().split())
            self.all_tools[tool_key] = value

        self.populate_tools_menu()

    def populate_tools_menu(self):
        '''
        populate_tools_menu gathers the information about the tools and populates it on the tools
        menu in GUI
        '''
        difficulty_palette = {
            'Beginner': (0.015, 0.588, 1, 1),
            'Intermediate': (0.2, 0.792, 0.498, 1),
            'Advance': (0.890, 0.090, 0.039, 1),
        }

        for key, value in self.all_tools.items():
            tool_title = value['title']
            tool_difficulty = value["difficulty"]
            tool_color = difficulty_palette[tool_difficulty]
            tool_box_widget = ToolBox()
            tool_box_widget.ids['tool_button'].bind(on_press=self.open_tool)
            tool_box_widget.id = key
            tool_box_widget.canvas.before.children[0].rgba = tool_color
            tool_box_widget.ids['tool_box_title'].text = tool_title
            tool_box_widget.ids['tool_box_difficulty'].text = tool_difficulty
            self.ids['tools_menu'].add_widget(tool_box_widget)
            self.import_tool_modules(key)

    def import_tool_modules(self, tool_to_import):
        '''
        import_tool_modules try to import tool_to_import named module and also the class
        ToolToImport
        '''
        module_name = tool_to_import
        class_name = ''.join(tool_to_import.title().split('_'))
81
        tool_module = import_module('modules.encryption.tools.all_tools.' + module_name)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        tool_class = getattr(tool_module, class_name)
        self.all_tools[tool_to_import]['module'] = tool_module
        self.all_tools[tool_to_import]['class'] = tool_class

    def open_tool(self, button_object):
        '''
        open_tool creates a new widget with option to get back to the tools menu and also
        imports the selected tool module as the children widget
        '''
        tool_id = button_object.parent.parent.id
        self.removed_tools_menu = self.children[0]
        self.remove_widget(self.children[0])

        box_layout = BoxLayout()
        box_layout.id = 'tools_menu'
        box_layout.orientation = 'vertical'

        back_to_tool_menu = BackToToolsMenu()
        back_to_tool_menu.ids['tool_title'].text = self.all_tools[tool_id]['title']
        back_to_tool_menu.ids['tools_menu_button'].bind(on_press=self.back_to_tools_menu)

        box_layout.add_widget(back_to_tool_menu)
        box_layout.add_widget(self.all_tools[tool_id]['class']())
        self.add_widget(box_layout)

    def back_to_tools_menu(self, button_object):
        '''
        back_to_tools_menu removed the tool widget and replaces tools menu
        '''
        self.remove_widget(button_object)
        self.remove_widget(self.children[0])
        self.add_widget(self.removed_tools_menu)