-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·93 lines (77 loc) · 2.9 KB
/
console.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
#!/usr/bin/env python3
import logging
import sys
from pathlib import Path
from loader import Loader
from unpacker import Unpacker
# TODO: verbose option should be more versatile
class Console:
FORMAT = '%(message)s'
VERBOSE = 'v'
LOG = 'modlib.log'
WORKING_DIR = Path('.')
PROJECT_SUFFIX = '_unpacked'
def __init__(self, args: tuple):
self.flags = set()
paths = self._parse_args(args)
self._set_up_logger()
modules = self.load_paths(paths)
self.unpack_data(modules)
@staticmethod
def load_paths(paths: list) -> list:
logging.debug('LOADING:')
loaded_modules = []
for path in paths:
if path.is_file():
try:
# TODO: Actually it shouldn't return NoneType
# Must always rise an exception
module = Loader.load_file(path)
if module:
loaded_modules.append(module)
except Loader.ModuleLoaderError:
msg = "Cannot load path: %s" % str(path)
logging.error(msg)
return loaded_modules
def unpack_data(self, modules: list):
logging.debug('UNPACKING:')
for module in modules:
project_path = self.WORKING_DIR / ("%s%s" % (module['filename'],
self.PROJECT_SUFFIX))
sample_path = project_path / 'samples'
try:
project_path.mkdir(exist_ok=True)
sample_path.mkdir(exist_ok=True)
except (IOError, OSError):
msg = "Cannot create folders %s, %s at the working dir %s" % (
project_path.resolve(), sample_path.resolve(),
self.WORKING_DIR.resolve()
)
logging.error(msg)
return
try:
Unpacker.unpack(module, project_path, sample_path)
except Unpacker.ModuleUnpackerError:
msg = "Cannot unpack module: %s" % module.name
logging.error(msg)
def _parse_args(self, args: tuple) -> list:
paths = []
for arg in args:
if arg.startswith('--'):
self.flags.add(arg[2:].upper())
elif arg.startswith('-'):
for letter in arg[1:]:
self.flags.add(letter)
else:
paths.extend(list(self.WORKING_DIR.glob(arg)))
return paths
def _set_up_logger(self):
if self.VERBOSE in self.flags:
logging.basicConfig(level=logging.DEBUG, format=self.FORMAT,
stream=sys.stdout)
else:
logging.basicConfig(filemode='w', level=logging.DEBUG,
format=self.FORMAT, filename=self.LOG)
if __name__ == "__main__":
console = Console(sys.argv[1:])
exit(0)