add cleaner memoization
This commit is contained in:
parent
e78b4c7b72
commit
ee81cf93af
6 changed files with 37 additions and 29 deletions
|
@ -1,23 +0,0 @@
|
|||
aiofiles==0.6.0
|
||||
aiohttp==3.7.4.post0
|
||||
aiohttp-socks==0.6.0
|
||||
async-timeout==3.0.1
|
||||
attrs==21.2.0
|
||||
chardet==4.0.0
|
||||
future==0.18.2
|
||||
h11==0.12.0
|
||||
h2==4.0.0
|
||||
hpack==4.0.0
|
||||
hyperframe==6.0.1
|
||||
idna==3.2
|
||||
jsonschema==3.2.0
|
||||
Logbook==1.5.3
|
||||
matrix-nio==0.18.7
|
||||
multidict==5.1.0
|
||||
pycryptodome==3.10.4
|
||||
pyrsistent==0.18.0
|
||||
python-socks==1.2.4
|
||||
six==1.16.0
|
||||
typing-extensions==3.10.0.2
|
||||
unpaddedbase64==2.1.0
|
||||
yarl==1.6.3
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
matrix-nio==0.18.7
|
1
requirements_dev.txt
Normal file
1
requirements_dev.txt
Normal file
|
@ -0,0 +1 @@
|
|||
python-dotenv==0.19.0
|
|
@ -12,3 +12,10 @@ python_requires = >=3.9.2
|
|||
package_dir = =src
|
||||
install_requires =
|
||||
matrix-nio>=0.18.7
|
||||
|
||||
[options.extras_require]
|
||||
testing =
|
||||
python-dotenv>=0.19.0
|
||||
|
||||
[options.package_data]
|
||||
matrix_bot = py.typed
|
||||
|
|
|
@ -27,6 +27,8 @@ class Client:
|
|||
"""
|
||||
|
||||
__client: nio.AsyncClient
|
||||
__rooms_by_aliases: dict[RoomAlias, Room]
|
||||
__rooms_by_id: dict[RoomId, Room]
|
||||
allowed_rooms: Optional[dict[RoomId, Room]]
|
||||
|
||||
def __init__(
|
||||
|
@ -50,6 +52,8 @@ class Client:
|
|||
homeserver,
|
||||
username
|
||||
)
|
||||
self.__rooms_by_aliases = {}
|
||||
self.__rooms_by_id = {}
|
||||
resp = loop.run_until_complete(self.__client.login(password))
|
||||
if isinstance(resp, nio.responses.LoginError):
|
||||
raise RuntimeError(f"Fail to connect: {resp.message}")
|
||||
|
@ -60,10 +64,7 @@ class Client:
|
|||
self.allowed_rooms = {}
|
||||
for room_name in allowed_rooms_names:
|
||||
room = loop.run_until_complete(self.resolve_room(room_name))
|
||||
if room.id in self.allowed_rooms:
|
||||
self.allowed_rooms[room.id].aliases.update(room.aliases)
|
||||
else:
|
||||
self.allowed_rooms[room.id] = room
|
||||
self.allowed_rooms[room.id] = room # room uniqueness is handled by self.resolve_room
|
||||
|
||||
|
||||
async def resolve_room(
|
||||
|
@ -72,16 +73,37 @@ class Client:
|
|||
)->Room:
|
||||
"""
|
||||
Lookup a room from its id or alias.
|
||||
If the name has already been resolved by this client, the
|
||||
room is return directly without querying the server.
|
||||
"""
|
||||
# If the room_name is empty:
|
||||
if len(room_name) == 0:
|
||||
raise ValueError(f"Invalid room_name: {room_name}")
|
||||
if room_name[0] == '!':
|
||||
# If it is a known room id:
|
||||
if room_name[0] == '!' and room_name in self.__rooms_by_id:
|
||||
return self.__rooms_by_id[room_name]
|
||||
# If it is a unknown room id:
|
||||
elif room_name[0] == '!':
|
||||
return Room(id=room_name)
|
||||
# If it is not a room id nor a room alias:
|
||||
elif room_name[0] != '#':
|
||||
raise ValueError(f"Invalid room_name: {room_name}")
|
||||
# If it is a known room alias:
|
||||
elif room_name in self.__rooms_by_aliases:
|
||||
return self.__rooms_by_aliases[room_name]
|
||||
# If it is an unknown room alias:
|
||||
else:
|
||||
resp = await self.__client.room_resolve_alias(room_name)
|
||||
if isinstance(resp, nio.responses.RoomResolveAliasError):
|
||||
raise RuntimeError(f"Error while resolving alias: {resp.message}")
|
||||
return Room(id=resp.room_id,aliases={room_name})
|
||||
# If the room is already known:
|
||||
if resp.room_id in self.__rooms_by_id:
|
||||
room = self.__rooms_by_id[resp.room_id]
|
||||
room.aliases.add(room_name)
|
||||
# If the room is unknwon:
|
||||
else:
|
||||
room = Room(id=resp.room_id,aliases={room_name})
|
||||
self.__rooms_by_id[resp.room_id] = room
|
||||
self.__rooms_by_aliases[room_name] = room
|
||||
return room
|
||||
|
||||
|
|
0
src/matrix_bot/py.typed
Normal file
0
src/matrix_bot/py.typed
Normal file
Loading…
Reference in a new issue