-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.py
More file actions
54 lines (41 loc) · 1.51 KB
/
Driver.py
File metadata and controls
54 lines (41 loc) · 1.51 KB
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
import os
import base64
from Encoder import Encoder
from Decoder import Decoder
class Driver:
def __init__(self, args):
self.operation = args.operation
self.input = args.input
self.output = args.output
self.use_base64 = args.use_base64
if os.path.exists(self.output):
raise Exception(f"Path {self.output} already exists!")
self.encoder = Encoder()
self.decoder = Decoder()
def start_execution(self):
if self.operation == 'encode':
self.execute_encode_operation()
else:
self.execute_decode_operation()
def execute_decode_operation(self):
decoded = self.decoder.decode(self.input)
if self.use_base64:
mode = 'wb'
data_to_be_written = base64.b64decode(decoded)
else:
mode = 'wb'
try:
data_to_be_written = decoded.encode("utf-8")
except:
mode = 'w'
data_to_be_written = decoded
with open(self.output, mode) as f:
f.write(data_to_be_written)
def execute_encode_operation(self):
with open(self.input, 'rb') as f:
binary_input_data = f.read()
if self.use_base64:
input_data = base64.b64encode(binary_input_data).decode('utf-8')
else:
input_data = binary_input_data.decode('utf-8')
self.encoder.encode(input_data, self.output)