1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

make pylint happy

This commit is contained in:
Falk Werner 2023-02-04 08:58:15 +01:00
parent 7559bebb05
commit a83fcd7260
2 changed files with 66 additions and 44 deletions

View File

@ -0,0 +1,17 @@
[BASIC]
good-names=i,j,k,ex,Run,_,f,fd
[MESSAGES CONTROL]
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead,
missing-function-docstring

View File

@ -1,11 +1,13 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Example webfuse provider written in python."""
import asyncio import asyncio
import os import os
import stat import stat
import websockets
import errno import errno
import getpass import getpass
import websockets
INVALID_FD = 0xffffffffffffffff INVALID_FD = 0xffffffffffffffff
@ -90,6 +92,8 @@ O_TMPFILE = 0o20200000
O_TRUNC = 0o00001000 O_TRUNC = 0o00001000
class MessageReader: class MessageReader:
"""Reads webfuse messages from buffer."""
def __init__(self, buffer): def __init__(self, buffer):
self.buffer = buffer self.buffer = buffer
self.offset = 0 self.offset = 0
@ -103,7 +107,8 @@ class MessageReader:
return self.read_u8() == 1 return self.read_u8() == 1
def read_u32(self): def read_u32(self):
value = (self.buffer[self.offset] << 24) + (self.buffer[self.offset + 1] << 16) + (self.buffer[self.offset + 2] << 8) + self.buffer[self.offset + 3] value = (self.buffer[self.offset] << 24) + (self.buffer[self.offset + 1] << 16) + \
(self.buffer[self.offset + 2] << 8) + self.buffer[self.offset + 3]
self.offset += 4 self.offset += 4
return value return value
@ -152,7 +157,7 @@ class MessageReader:
mode |= stat.S_IFDIR if MODE_DIR == (value & MODE_DIR ) else 0 mode |= stat.S_IFDIR if MODE_DIR == (value & MODE_DIR ) else 0
mode |= stat.S_IFCHR if MODE_CHR == (value & MODE_CHR ) else 0 mode |= stat.S_IFCHR if MODE_CHR == (value & MODE_CHR ) else 0
mode |= stat.S_IFBLK if MODE_BLK == (value & MODE_BLK ) else 0 mode |= stat.S_IFBLK if MODE_BLK == (value & MODE_BLK ) else 0
mode |= stat.S_IFFIFO if MODE_FIFO == (value & MODE_FIFO) else 0 mode |= stat.S_IFIFO if MODE_FIFO == (value & MODE_FIFO) else 0
mode |= stat.S_IFLNK if MODE_LNK == (value & MODE_LNK ) else 0 mode |= stat.S_IFLNK if MODE_LNK == (value & MODE_LNK ) else 0
mode |= stat.S_IFSOCK if MODE_SOCK == (value & MODE_SOCK) else 0 mode |= stat.S_IFSOCK if MODE_SOCK == (value & MODE_SOCK) else 0
return mode return mode
@ -187,6 +192,8 @@ class MessageReader:
class MessageWriter: class MessageWriter:
""""Writes webfuse messages into buffer."""
def __init__(self, message_id, message_type): def __init__(self, message_id, message_type):
self.buffer = [] self.buffer = []
self.write_u32(message_id) self.write_u32(message_id)
@ -220,8 +227,7 @@ class MessageWriter:
def write_result(self, value): def write_result(self, value):
if 0 > value: if 0 > value:
if value in ERRNO: value = ERRNO.get(value, value)
value = ERRNO[value]
self.write_i32(value) self.write_i32(value)
def write_str(self, value): def write_str(self, value):
@ -242,8 +248,10 @@ class MessageWriter:
def get_bytes(self): def get_bytes(self):
return bytearray(self.buffer) return bytearray(self.buffer)
# pylint: disable=too-many-public-methods
class FilesystemProvider: class FilesystemProvider:
"""Webfuse filesystem provider."""
def __init__(self, path, url): def __init__(self, path, url):
self.root = os.path.abspath(path) self.root = os.path.abspath(path)
self.url = url self.url = url
@ -275,6 +283,7 @@ class FilesystemProvider:
async def run(self): async def run(self):
extra_headers = [("X-Auth-Token", "user:bob;token=foo")] extra_headers = [("X-Auth-Token", "user:bob;token=foo")]
# pylint: disable-next=no-member
async with websockets.connect(self.url, extra_headers=extra_headers) as connection: async with websockets.connect(self.url, extra_headers=extra_headers) as connection:
while True: while True:
request = await connection.recv() request = await connection.recv()
@ -286,7 +295,7 @@ class FilesystemProvider:
method = self.commands[message_type] method = self.commands[message_type]
method(self, reader, writer) method(self, reader, writer)
else: else:
print("unknown message type: %d" % message_type) print(f"unknown message type: {message_type}")
response = writer.get_bytes() response = writer.get_bytes()
await connection.send(response) await connection.send(response)
@ -295,7 +304,7 @@ class FilesystemProvider:
mode = reader.read_access_mode() mode = reader.read_access_mode()
result = -errno.EACCES result = -errno.EACCES
try: try:
if os.access(path, mode) == True: if os.access(path, mode) is True:
result = 0 result = 0
except OSError as ex: except OSError as ex:
result = -ex.errno result = -ex.errno
@ -407,7 +416,7 @@ class FilesystemProvider:
writer.write_result(result) writer.write_result(result)
def fsync(self, reader, writer): def fsync(self, reader, writer):
path = reader.read_path(self.root) _ = reader.read_path(self.root)
_ = reader.read_bool() _ = reader.read_bool()
fd = reader.read_u64() fd = reader.read_u64()
result = 0 result = 0
@ -423,7 +432,6 @@ class FilesystemProvider:
atime_ns = reader.read_u32() atime_ns = reader.read_u32()
mtime = reader.read_u64() mtime = reader.read_u64()
mtime_ns = reader.read_u32() mtime_ns = reader.read_u32()
fd = reader.read_u64()
result = 0 result = 0
try: try:
os.utime(path, (atime, mtime), ns = (atime_ns, mtime_ns)) os.utime(path, (atime, mtime), ns = (atime_ns, mtime_ns))
@ -431,7 +439,6 @@ class FilesystemProvider:
result = -ex.errno result = -ex.errno
writer.write_result(result) writer.write_result(result)
def open(self, reader, writer): def open(self, reader, writer):
path = reader.read_path(self.root) path = reader.read_path(self.root)
flags = reader.read_openflags() flags = reader.read_openflags()
@ -443,7 +450,6 @@ class FilesystemProvider:
writer.write_result(0) writer.write_result(0)
writer.write_u64(fd) writer.write_u64(fd)
def mknod(self, reader, writer): def mknod(self, reader, writer):
path = reader.read_path(self.root) path = reader.read_path(self.root)
mode = reader.read_mode() mode = reader.read_mode()
@ -466,10 +472,9 @@ class FilesystemProvider:
return return
writer.write_result(0) writer.write_result(0)
writer.write_u64(fd) writer.write_u64(fd)
pass
def release(self, reader, writer): def release(self, reader, writer):
path = reader.read_path(self.root) _ = reader.read_path(self.root)
fd = reader.read_u64() fd = reader.read_u64()
result = 0 result = 0
try: try:
@ -534,8 +539,8 @@ class FilesystemProvider:
path = reader.read_path(self.root) path = reader.read_path(self.root)
names = [] names = []
try: try:
with os.scandir(path) as it: with os.scandir(path) as entries:
for entry in it: for entry in entries:
names.append(entry.name) names.append(entry.name)
except OSError as ex: except OSError as ex:
writer.write_result(-ex.errno) writer.write_result(-ex.errno)
@ -553,7 +558,7 @@ class FilesystemProvider:
writer.write_result(result) writer.write_result(result)
def statfs(self, reader, writer): def statfs(self, reader, writer):
path = self.get_path(reader) path = reader.read_path(self.root)
try: try:
buffer = os.statvfs(path) buffer = os.statvfs(path)
except OSError as ex: except OSError as ex:
@ -569,7 +574,7 @@ class FilesystemProvider:
writer.write_u64(buffer.f_ffree) writer.write_u64(buffer.f_ffree)
writer.write_u64(buffer.f_namemax) writer.write_u64(buffer.f_namemax)
def getcreds(self, reader, writer): def getcreds(self, _, writer):
credentials = getpass.getpass(prompt="credentials: ") credentials = getpass.getpass(prompt="credentials: ")
writer.write_str(credentials) writer.write_str(credentials)