-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutors.py
More file actions
61 lines (52 loc) · 1.89 KB
/
executors.py
File metadata and controls
61 lines (52 loc) · 1.89 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
55
56
57
58
59
60
61
from subprocess import Popen, PIPE
from parsers import BaseParser, HumanParser, InodeParser
import json
class BaseExecutor(object):
'''class to make a command'''
def __init__(self, cmd='df', *args):
self.cmd = cmd
self.args = args
self.parser = BaseParser
def command_maker(self):
'''make command from arguments'''
tmp = []
tmp.append(self.cmd)
tmp.extend([arg for arg in self.args])
return tmp
def execute(self):
'''execute command in shell'''
with Popen(self.command_maker(), stdout=PIPE, stderr=PIPE) as proc:
data, error = proc.communicate()
return_code = proc.returncode
if return_code == 0:
result_dict = self.parser(data.decode(
"utf-8")).make_dict_from_string()
return self.result(result_dict, str(error.decode("utf-8")),
return_code)
else:
return self.result(None, str(error.decode("utf-8")),
return_code)
def result(self, result_dict, error, return_code):
"""Serialize output to a json format"""
if return_code:
status = 'failure'
else:
status = 'success'
as_dict = {
'status': status,
'error': error,
'result': result_dict
}
json_data = json.dumps(as_dict, sort_keys=False,
indent=4, separators=(',', ': '))
return json_data
class HumanExecutor(BaseExecutor):
'''class to execute command with argument -h'''
def __init__(self):
super().__init__('df', '-h')
self.parser = HumanParser
class InodeExecutor(BaseExecutor):
'''class to execute command with argument -i'''
def __init__(self):
super().__init__('df', '-i')
self.parser = InodeParser