-
Notifications
You must be signed in to change notification settings - Fork 4
/
commands.py
43 lines (32 loc) · 873 Bytes
/
commands.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
import os
def get_path(filename):
return os.path.abspath(os.path.join(os.path.dirname(__file__), filename))
def read(data):
path = get_path(data['path'])
if data.get('options', {}).get('binary'):
return list(bytearray(open(path, 'rb').read()))
return open(path, 'r').read()
def write(data):
path = get_path(data['path'])
content = data.get('data')
if content:
if type(content) is list:
content = bytearray(content)
if type(content) is bytearray:
with open(path, 'wb') as out:
out.write(content)
else:
with open(path, 'w') as out:
out.write(content.encode('utf-8'))
print ('wrote {}'.format(path))
else:
raise Exception("Unable to write to '{}'".format(path))
commands = {
'read': read,
'write': write,
}
def main(data):
command = data.get('command')
function = commands.get(command)
if function:
return function(data)