Display known messages as directories
This commit is contained in:
parent
748b1720ba
commit
6c387338d3
2 changed files with 126 additions and 64 deletions
25
eufs/euph.py
25
eufs/euph.py
|
|
@ -16,9 +16,18 @@ class Message:
|
||||||
self.id = mid
|
self.id = mid
|
||||||
self.parent = mparent
|
self.parent = mparent
|
||||||
self.time = mtime
|
self.time = mtime
|
||||||
self.text = f"{mid} [{mnick}] {mcontent}"
|
self.text = f"[{mnick}] {mcontent}"
|
||||||
self.children = {} # id -> msg
|
self.children = {} # id -> msg
|
||||||
|
|
||||||
|
self.text = self.text.replace("/", "?")[:50]
|
||||||
|
print(mid, mparent)
|
||||||
|
|
||||||
|
|
||||||
|
def find_msg_by_text(msgs, text):
|
||||||
|
for msg in msgs.values():
|
||||||
|
if msg.text == text:
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
class Room:
|
class Room:
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
|
|
@ -32,13 +41,25 @@ class Room:
|
||||||
self.msgs = {} # id -> msg
|
self.msgs = {} # id -> msg
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
t = threading.Thread(target=Room._run, args=(self,))
|
t = threading.Thread(target=Room._run, args=(self,), daemon=True)
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.alive = False
|
self.alive = False
|
||||||
self.ws.close()
|
self.ws.close()
|
||||||
|
|
||||||
|
def find_msg_by_texts(self, texts):
|
||||||
|
msg = None
|
||||||
|
children = {mid: msg for mid, msg in self.msgs.items() if msg.parent == None}
|
||||||
|
|
||||||
|
for text in texts:
|
||||||
|
msg = find_msg_by_text(children, text)
|
||||||
|
if not msg:
|
||||||
|
return
|
||||||
|
children = msg.children
|
||||||
|
|
||||||
|
return msg
|
||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
while self.alive:
|
while self.alive:
|
||||||
self.ws = ws.connect(f"wss://euphoria.leet.nu/room/{self.name}/ws")
|
self.ws = ws.connect(f"wss://euphoria.leet.nu/room/{self.name}/ws")
|
||||||
|
|
|
||||||
165
eufs/main.py
165
eufs/main.py
|
|
@ -1,93 +1,134 @@
|
||||||
import errno
|
import errno
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
|
import threading
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import fuse
|
import fuse
|
||||||
|
|
||||||
|
from . import euph
|
||||||
|
|
||||||
|
|
||||||
fuse.fuse_python_api = (0, 2)
|
fuse.fuse_python_api = (0, 2)
|
||||||
|
|
||||||
|
|
||||||
# For debugging
|
class EuFs(fuse.Fuse):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
fuse.Fuse.__init__(self, *args, **kwargs)
|
||||||
|
|
||||||
f = open("log.txt", "w")
|
self.rooms = {}
|
||||||
|
|
||||||
|
self.join_room("test")
|
||||||
|
|
||||||
def log(str):
|
def join_room(self, name):
|
||||||
f.write(f"{str}\n")
|
# Not threadsafe, but using threading.Lock broke things
|
||||||
f.flush()
|
if name in self.rooms:
|
||||||
|
return
|
||||||
|
|
||||||
|
room = euph.Room(name)
|
||||||
|
room.start()
|
||||||
|
self.rooms[name] = room
|
||||||
|
print(f"Joined &{name}")
|
||||||
|
|
||||||
hello_path = "/hello"
|
def leave_room(self, name):
|
||||||
hello_str = b"Hello World!\n"
|
# Not threadsafe, but using threading.Lock broke things
|
||||||
|
if name not in self.rooms:
|
||||||
|
return
|
||||||
|
|
||||||
|
room = self.rooms[name]
|
||||||
|
room.stop()
|
||||||
|
del self.rooms[name]
|
||||||
|
print(f"Left &{name}")
|
||||||
|
|
||||||
class MyStat(fuse.Stat):
|
|
||||||
def __init__(self):
|
|
||||||
self.st_mode = 0
|
|
||||||
self.st_ino = 0
|
|
||||||
self.st_dev = 0
|
|
||||||
self.st_nlink = 0
|
|
||||||
self.st_uid = 0
|
|
||||||
self.st_gid = 0
|
|
||||||
self.st_size = 0
|
|
||||||
self.st_atime = 0
|
|
||||||
self.st_mtime = 0
|
|
||||||
self.st_ctime = 0
|
|
||||||
|
|
||||||
|
|
||||||
class HelloFS(fuse.Fuse):
|
|
||||||
def getattr(self, path):
|
def getattr(self, path):
|
||||||
log(f"getattr: {path!r}")
|
print(f"getattr: {path!r}")
|
||||||
st = MyStat()
|
|
||||||
if path == "/":
|
parts = Path(path).parts
|
||||||
st.st_mode = stat.S_IFDIR | 0o755
|
if parts == ("/",):
|
||||||
st.st_nlink = 2
|
print(f" is root")
|
||||||
elif path == hello_path:
|
return fuse.Stat(
|
||||||
st.st_mode = stat.S_IFREG | 0o444
|
st_mode=stat.S_IFDIR | 0o755,
|
||||||
st.st_nlink = 1
|
st_nlink=1,
|
||||||
st.st_size = len(hello_str)
|
)
|
||||||
else:
|
print(f" is not root")
|
||||||
|
|
||||||
|
roomname = parts[1]
|
||||||
|
texts = parts[2:]
|
||||||
|
|
||||||
|
room = self.rooms.get(roomname)
|
||||||
|
if not room:
|
||||||
|
print(f" is invalid room")
|
||||||
return -errno.ENOENT
|
return -errno.ENOENT
|
||||||
return st
|
print(f" is valid room")
|
||||||
|
if texts == ():
|
||||||
|
return fuse.Stat(
|
||||||
|
st_mode=stat.S_IFDIR | 0o755,
|
||||||
|
st_nlink=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
msg = room.find_msg_by_texts(texts)
|
||||||
|
if not msg:
|
||||||
|
print(f" is invalid msg")
|
||||||
|
return -errno.ENOENT
|
||||||
|
print(f" is valid msg")
|
||||||
|
|
||||||
|
return fuse.Stat(
|
||||||
|
st_mode=stat.S_IFDIR | 0o755,
|
||||||
|
st_nlink=1,
|
||||||
|
st_ctime=msg.time,
|
||||||
|
st_mtime=msg.time,
|
||||||
|
st_atime=msg.time,
|
||||||
|
)
|
||||||
|
|
||||||
def readdir(self, path, offset):
|
def readdir(self, path, offset):
|
||||||
log(f"readdir: {path!r} {offset!r}")
|
print(f"readdir: {path!r} {offset!r}")
|
||||||
for r in ".", "..", hello_path[1:]:
|
|
||||||
yield fuse.Direntry(r)
|
|
||||||
|
|
||||||
def open(self, path, flags):
|
parts = Path(path).parts
|
||||||
log(f"open: {path!r} {flags!r}")
|
if parts == ("/",):
|
||||||
if path != hello_path:
|
print(f" is root")
|
||||||
return -errno.ENOENT
|
yield fuse.Direntry(".")
|
||||||
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
|
yield fuse.Direntry("..")
|
||||||
if (flags & accmode) != os.O_RDONLY:
|
for room in self.rooms:
|
||||||
return -errno.EACCES
|
yield fuse.Direntry(room)
|
||||||
|
return
|
||||||
|
print(f" is not root")
|
||||||
|
|
||||||
def read(self, path, size, offset):
|
roomname = parts[1]
|
||||||
log(f"read: {path!r} {size!r} {offset!r}")
|
texts = parts[2:]
|
||||||
if path != hello_path:
|
|
||||||
return -errno.ENOENT
|
room = self.rooms.get(roomname)
|
||||||
slen = len(hello_str)
|
if not room:
|
||||||
if offset < slen:
|
print(f" is invalid room")
|
||||||
if offset + size > slen:
|
return
|
||||||
size = slen - offset
|
print(f" is valid room")
|
||||||
buf = hello_str[offset : offset + size]
|
|
||||||
|
if texts == ():
|
||||||
|
print(" children from room")
|
||||||
|
children = {
|
||||||
|
mid: msg for mid, msg in room.msgs.items() if msg.parent == None
|
||||||
|
}
|
||||||
else:
|
else:
|
||||||
buf = b""
|
msg = room.find_msg_by_texts(texts)
|
||||||
return buf
|
if not msg:
|
||||||
|
print(f" is invalid msg")
|
||||||
|
print(f" is valid msg")
|
||||||
|
print(" children from msg")
|
||||||
|
children = msg.children
|
||||||
|
|
||||||
|
print(children)
|
||||||
|
|
||||||
|
children = list(children.values())
|
||||||
|
children.sort(key=lambda m: m.time)
|
||||||
|
for child in children:
|
||||||
|
print(child.text)
|
||||||
|
yield fuse.Direntry(child.text)
|
||||||
|
|
||||||
from . import euph
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# server = HelloFS(version="%prog " + fuse.__version__, dash_s_do="setsingle")
|
server = EuFs(version="%prog " + fuse.__version__, dash_s_do="setsingle")
|
||||||
|
|
||||||
# server.parse(errex=1)
|
server.parse(errex=1)
|
||||||
# server.main()
|
server.main()
|
||||||
|
|
||||||
room = euph.Room("test")
|
|
||||||
room.start()
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue