-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlackBox.py
132 lines (118 loc) · 3.93 KB
/
BlackBox.py
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
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
81
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python3
# I use tabs, not spaces. Sorry not sorry.
import os, sys, argparse, textwrap, settings, importlib, Adapters
# Handle commandline arguments
parser = argparse.ArgumentParser(
prog="BlackBox",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent("""\
BlackBox v1.0
by Caleb White
NOTE: Before use, be sure to add and configure adapters with your settings
"""),
epilog=textwrap.dedent("""\
Any flags set at runtime will override the imported settings,
but some settings do not have a corresponding flag.
""")
)
parser.add_argument(
"--verbose",
help="makes the script rather talkative",
required=False,
action='store_true',
dest="verbose")
parser.add_argument(
"--debug",
help="this will show you full, ugly errors when they happen",
required=False,
action='store_true',
dest="debug")
parser.add_argument(
"--log",
help="this will record all output to log files",
required=False,
action="store_true",
dest="log")
parser.add_argument(
"--moo",
help=argparse.SUPPRESS,
required=False,
action='store_true',
dest="moo")
args = parser.parse_args()
# Moo - the most important part of all this nonsense
if args.moo:
print(textwrap.dedent("""\
,= , =.
_ _ /'/ )\\,/,/(_ \\`\\
`//-.| ( ,\\\\)\\//\\)\\/_ ) |
//___\\ `\\\\\\/\\\\/\\/\\\\///' /
,-\"~`-._ `\"--'_ `\"\"\"` _ \\`'\"~-,_
\\ `-. '_`. .'_` \\ ,-\"~`/
`.__.-'`/ (-\\ /-) |-.__,'
|| | \\O) /^\\ (O/ |
`\\\\ | / `\\ /
the \\\\ \\ / `\\ /
cow `\\\\ `-. /' .---.--.\\
says `\\\\/`~(, '() ()
'moo' /(O) \\\\ _,.-.,_)
// \\\\ `\\'` /
/ | || `\"\"~~~\"`
/' |__||
`o """))
exit(0)
# Get default arguments from pipe or settings - whichever is true first
verbose = args.verbose if args.verbose else settings.verbose
debug = args.debug if args.debug else settings.debug
log = args.log if args.log else settings.log
### Adapter Enumeration ###
# Add Adapters directory to path to avoid layered import errors
sys.path.insert(1, os.path.join(sys.path[0], 'Adapters'))
# Set up arrays to receive adapters
adapters, meta_adapters = [], []
# Get all contents of Adapters directory
try:
adapter_list = [f for f in os.listdir(os.path.join(sys.path[0], 'Adapters'))]
except:
# This isn't great, but it should only ever happen once...
print("Failed to get adapters. Have you set any up?")
if debug:
raise
# Iterate through the list of files
for adapter_name in adapter_list:
# Split file into name and extension
adapter_name = adapter_name.split(".")
try:
# Ignore the template, directories, and any non-python files
if os.path.isdir(adapter_name[0]) or adapter_name[1] != "py" or adapter_name[0] == "Template":
continue
# Import the adapter
try:
adapter = importlib.import_module(adapter_name[0], "Adapters.{0}".format(adapter_name[0]))
except: # Still not sure why this is necessary
adapter = importlib.import_module(adapter_name[0], "Adapters")
# Import and initialize the adapter's class
adapter = getattr(adapter, adapter_name[0])
try:
adapter = adapter(verbose, debug, log)
adapter.log("Module loaded!", "success")
# Drop it in the proper array
if adapter.type == "Meta":
meta_adapters.append(adapter)
else:
adapters.append(adapter)
except:
raise # This doesn't throw an error, it just raises it to the next except block
except:
print("\033[93m\033[1mERROR: Failed to load adapter: {0}\033[0m".format(".".join(adapter_name))) # ANSI escape codes for bold yellow text
if debug:
raise
continue
### Adapter Execution ###
if meta_adapters == []:
print("\033[91m\033[1mWARNING: No meta adapter found! Adapters will receive no input and no output will be stored!\033[0m") # ANSI escape codes for bold red text
# Iterate through available adapters
for adapter in adapters:
# Feed modules to meta adapter(s)
for meta in meta_adapters:
meta.main(adapter)