coercer.core.loader

 1#!/usr/bin/env python3
 2# -*- coding: utf-8 -*-
 3# File name          : loader.py
 4# Author             : Podalirius (@podalirius_)
 5# Date created       : 18 Sep 2022
 6
 7
 8import os
 9import sys
10from importlib import import_module
11from coercer.structures.MethodType import MethodType
12
13
14def find_and_load_coerce_methods(debug=False):
15    """
16    Function find_and_load_coerce_methods()
17
18    Parameters:
19        bool:debug Enable or disable debug output
20
21    Returns:
22        list:coerce_methods
23    """
24    coerce_methods = {}
25    search_dir = os.path.dirname(__file__) + os.path.sep + ".." + os.path.sep + "methods"
26    if debug:
27        print("[loader] Loading coerce methods from %s ..." % search_dir)
28    sys.path.extend([search_dir])
29    for _dir in os.listdir(search_dir):
30        _dirpath = search_dir + os.path.sep + _dir
31        if os.path.isdir(_dirpath) and _dir not in ["__pycache__"]:
32            if debug:
33                print("[loader] Loading methods for category %s ..." % _dir)
34            for _file in os.listdir(_dirpath):
35                _filepath = _dirpath + os.path.sep + _file
36                if _file.endswith('.py'):
37                    if os.path.isfile(_filepath) and _file not in ["__init__.py"]:
38                        try:
39                            module = import_module('coercer.methods.%s.%s' % (_dir, _file[:-3]))
40                            method_class = module.__getattribute__(_file[:-3])
41                            if all([kw in dir(method_class) for kw in ["method_type"]]):
42                                if method_class.method_type not in coerce_methods.keys():
43                                    coerce_methods[method_class.method_type] = {}
44                                # Handling Microsoft Network protocols methods
45                                if method_class.method_type == MethodType.MICROSOFT_PROTOCOL:
46                                    if method_class.protocol["shortname"] not in coerce_methods[method_class.method_type].keys():
47                                        coerce_methods[method_class.method_type][method_class.protocol["shortname"]] = {}
48                                    if method_class.function["name"] not in coerce_methods[method_class.method_type][method_class.protocol["shortname"]].keys():
49                                        coerce_methods[method_class.method_type][method_class.protocol["shortname"]][method_class.function["name"]] = {
50                                            "class": method_class
51                                        }
52                                    if debug:
53                                        print("[loader]   └──> Loaded Remote Procedure Call %s (opnum %d)" % (method_class.function["name"], method_class.function["opnum"]))
54                                # Handling other methods
55                                elif method_class.method_type == MethodType.OTHER:
56                                    pass
57                            else:
58                                if debug:
59                                    print("[loader] '%s' does not match the template." % _file)
60                        except AttributeError as e:
61                            pass
62    if debug:
63        print("[loader] coerce_methods:", coerce_methods)
64    return coerce_methods
def find_and_load_coerce_methods(debug=False):
15def find_and_load_coerce_methods(debug=False):
16    """
17    Function find_and_load_coerce_methods()
18
19    Parameters:
20        bool:debug Enable or disable debug output
21
22    Returns:
23        list:coerce_methods
24    """
25    coerce_methods = {}
26    search_dir = os.path.dirname(__file__) + os.path.sep + ".." + os.path.sep + "methods"
27    if debug:
28        print("[loader] Loading coerce methods from %s ..." % search_dir)
29    sys.path.extend([search_dir])
30    for _dir in os.listdir(search_dir):
31        _dirpath = search_dir + os.path.sep + _dir
32        if os.path.isdir(_dirpath) and _dir not in ["__pycache__"]:
33            if debug:
34                print("[loader] Loading methods for category %s ..." % _dir)
35            for _file in os.listdir(_dirpath):
36                _filepath = _dirpath + os.path.sep + _file
37                if _file.endswith('.py'):
38                    if os.path.isfile(_filepath) and _file not in ["__init__.py"]:
39                        try:
40                            module = import_module('coercer.methods.%s.%s' % (_dir, _file[:-3]))
41                            method_class = module.__getattribute__(_file[:-3])
42                            if all([kw in dir(method_class) for kw in ["method_type"]]):
43                                if method_class.method_type not in coerce_methods.keys():
44                                    coerce_methods[method_class.method_type] = {}
45                                # Handling Microsoft Network protocols methods
46                                if method_class.method_type == MethodType.MICROSOFT_PROTOCOL:
47                                    if method_class.protocol["shortname"] not in coerce_methods[method_class.method_type].keys():
48                                        coerce_methods[method_class.method_type][method_class.protocol["shortname"]] = {}
49                                    if method_class.function["name"] not in coerce_methods[method_class.method_type][method_class.protocol["shortname"]].keys():
50                                        coerce_methods[method_class.method_type][method_class.protocol["shortname"]][method_class.function["name"]] = {
51                                            "class": method_class
52                                        }
53                                    if debug:
54                                        print("[loader]   └──> Loaded Remote Procedure Call %s (opnum %d)" % (method_class.function["name"], method_class.function["opnum"]))
55                                # Handling other methods
56                                elif method_class.method_type == MethodType.OTHER:
57                                    pass
58                            else:
59                                if debug:
60                                    print("[loader] '%s' does not match the template." % _file)
61                        except AttributeError as e:
62                            pass
63    if debug:
64        print("[loader] coerce_methods:", coerce_methods)
65    return coerce_methods

Function find_and_load_coerce_methods()

Parameters: bool:debug Enable or disable debug output

Returns: list:coerce_methods