-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesys.py
67 lines (51 loc) · 1.34 KB
/
filesys.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
import threading
import hashlib
import tempfile
import os
lan = __import__("lan$")
class Async(threading.Thread):
def __init__(self, hook, *args):
threading.Thread.__init__(self)
self.hook = hook
self.args = args
self.start()
def run(self):
self.hook(*self.args)
class WriteHandle(object):
def __init__(self, name):
self.name = name
self.fullname = os.path.join(os.path.dirname(__file__), "data", self.name)
self.handle = open(self.fullname, 'w')
def write(self, buf):
self.handle.write(buf)
def close(self):
self.handle.flush()
self.handle.close()
# def __():
lan.local_update()
lan.add(self.fullname)
# Async(__)
class ReadHandle(object):
def __init__(self, name):
self.name = name
self.fullname = os.path.join(os.path.dirname(__file__), "data", self.name)
self.flag = threading.Event();
self.handle = None
def __():
lan.get(self.name)
self.flag.set()
Async(__)
def read(self, *args):
if not self.flag.is_set():
self.flag.wait()
if not self.handle:
self.handle = open(self.fullname, 'r')
return self.handle.read(*args)
def close(self):
if self.handle:
self.handle.close()
def open_(name, mode='r'):
assert mode == 'w' or mode == 'r'
if mode == 'w':
return WriteHandle(name)
return ReadHandle(name)