-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.py
56 lines (54 loc) · 1.67 KB
/
filesystem.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
import os
from daemon import EventDaemon
from service import Service
class Directory:
def __init__(self,events,directory=None):
directory = directory or "."
self.directory = directory
def process(self,event):
if event['mode']=='read':
if event.get('format','data')=='raw':
self.read_raw(event)
elif event.get('format','data')=='info':
self.send_meta(event)
else:
self.read_data(event)
elif event['mode']=='write':
if event.get('format','data')=='raw':
self.write_raw(event)
else:
self.write_data(event)
def get_filename(self,event):
base_filename = event['filename']
base_directory = os.path.abspath(self.directory)
return os.path.join(base_directory,base_filename)
def open_file(self,event):
rw = 'r' if event.get('mode','read')=='read' else 'w'
return open(self.get_filename(event),rw)
def read_data(self,event):
with self.open_file(event) as f:
data = json.load(f)
def read_raw(self,event):
with self.open_file(event) as f:
data = f.read()
def send_meta(self,event):
filename = self.get_filename(event)
def write_data(self,event):
with self.open_file(event) as f:
pass
def write_raw(self,event):
with self.open_file(event) as f:
pass
class DirectoryService(Service):
def __init__(self,events):
super().__init__(events,'directory_service')
self.open_directories = {}
def respond(self,addr,event):
if event['directory'] not in self.open_directories:
self.open_directories = self.spawn_directory(event)
directory = self.open_directories[event['directory']]
result = directory.process(event)
if result:
self.emits.broadcast(result)
def spawn_directory(self,event):
return Directory(event['directory'])