Skip to content

Commit

Permalink
feat: rename search_queries to search_requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JurgenR committed Sep 25, 2023
1 parent c7a5930 commit d29de14
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ aioslsk

aioslsk is a Python library for the SoulSeek protocol built on top of asyncio.

Supported Python versions are currently 3.8 - 3.11

.. contents::

Installation
Expand Down
2 changes: 0 additions & 2 deletions docs/source/MESSAGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1180,8 +1180,6 @@ ChatMessageUsers (Code 149)
2. **string**: message




.. _ChatEnablePublic:

ChatEnablePublic (Code 150)
Expand Down
30 changes: 23 additions & 7 deletions src/aioslsk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .network.network import Network
from .peer import PeerManager
from .server import ServerManager
from .search import SearchRequest
from .search import SearchRequest, SearchResult
from .state import State
from .settings import Settings
from .transfer.manager import TransferManager
Expand Down Expand Up @@ -122,6 +122,11 @@ async def login(self):
)

async def start_shares_manager(self, scan=True):
"""Reads the shares cache and loads the shared directories from the
settings
:param scan: Boolean to indicate whether to start and initial scan or not
"""
self.shares_manager.read_cache()
self.shares_manager.load_from_settings()
if scan:
Expand All @@ -139,8 +144,6 @@ async def stop(self):

await self.network.disconnect()

logger.debug(f"tasks after disconnect : {asyncio.all_tasks()}")

self.peer_manager.stop()
self.transfer_manager.stop()
self.shares_manager.write_cache()
Expand Down Expand Up @@ -278,12 +281,25 @@ async def search(self, query: str) -> SearchRequest:
logger.info(f"Starting search for query: {query}")
return await self.server_manager.search(query)

def get_search_results_by_ticket(self, ticket: int):
async def search_user(self, query: str, user: Union[str, User]) -> SearchRequest:
username = user.name if isinstance(user, User) else user
return await self.server_manager.search_user(username, query)

async def search_room(self, query: str, room: Union[str, Room]) -> SearchRequest:
room_name = room.name if isinstance(room, Room) else room
return await self.server_manager.search_room(room_name, query)

def get_search_request_by_ticket(self, ticket: int) -> SearchRequest:
"""Returns a search request with given ticket"""
return self.state.search_requests[ticket]

def get_search_results_by_ticket(self, ticket: int) -> List[SearchResult]:
"""Returns all search results for given ticket"""
return self.state.search_queries[ticket]
return self.state.search_requests[ticket].results

def remove_search_results_by_ticket(self, ticket: int):
return self.state.search_queries.pop(ticket)
def remove_search_request_by_ticket(self, ticket: int) -> SearchRequest:
"""Removes a search request for given ticket"""
return self.state.search_requests.pop(ticket)

async def get_user_stats(self, user: Union[str, User]):
username = user.name if isinstance(user, User) else user
Expand Down
2 changes: 1 addition & 1 deletion src/aioslsk/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ async def _on_peer_search_reply(self, message: PeerSearchReply.Request, connecti
locked_results=message.locked_results
)
try:
query = self._state.search_queries[message.ticket]
query = self._state.search_requests[message.ticket]
except KeyError:
logger.warning(f"search reply ticket does not match any search query : {message.ticket}")
else:
Expand Down
18 changes: 9 additions & 9 deletions src/aioslsk/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ async def search_room(self, room: str, query: str) -> SearchRequest:
await self._network.send_server_messages(
ChatRoomSearch.Request(room, ticket, query)
)
self._state.search_queries[ticket] = SearchRequest(
self._state.search_requests[ticket] = SearchRequest(
ticket=ticket,
query=query,
search_type=SearchType.ROOM,
room=room
)
return self._state.search_queries[ticket]
return self._state.search_requests[ticket]

async def search_user(self, username: str, query: str) -> SearchRequest:
"""Performs a search query on a user"""
Expand All @@ -252,13 +252,13 @@ async def search_user(self, username: str, query: str) -> SearchRequest:
await self._network.send_server_messages(
UserSearch.Request(username, ticket, query)
)
self._state.search_queries[ticket] = SearchRequest(
self._state.search_requests[ticket] = SearchRequest(
ticket=ticket,
query=query,
search_type=SearchType.USER,
username=username
)
return self._state.search_queries[ticket]
return self._state.search_requests[ticket]

async def search(self, query: str) -> SearchRequest:
"""Performs a global search query"""
Expand All @@ -267,12 +267,12 @@ async def search(self, query: str) -> SearchRequest:
await self._network.send_server_messages(
FileSearch.Request(ticket, query)
)
self._state.search_queries[ticket] = SearchRequest(
self._state.search_requests[ticket] = SearchRequest(
ticket=ticket,
query=query,
search_type=SearchType.NETWORK
)
return self._state.search_queries[ticket]
return self._state.search_requests[ticket]

async def get_user_stats(self, username: str): # pragma: no cover
await self._network.send_server_messages(GetUserStats.Request(username))
Expand Down Expand Up @@ -848,8 +848,8 @@ async def _wishlist_job(self, interval: int):
items = self._settings.get('search.wishlist')

# Remove all current wishlist searches
self._state.search_queries = {
ticket: qry for ticket, qry in self._state.search_queries.items()
self._state.search_requests = {
ticket: qry for ticket, qry in self._state.search_requests.items()
if qry.search_type != SearchType.WISHLIST
}

Expand All @@ -860,7 +860,7 @@ async def _wishlist_job(self, interval: int):
continue

ticket = next(self._ticket_generator)
self._state.search_queries[ticket] = SearchRequest(
self._state.search_requests[ticket] = SearchRequest(
ticket,
item['query'],
search_type=SearchType.WISHLIST
Expand Down
2 changes: 1 addition & 1 deletion src/aioslsk/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class State:

# Search related
received_searches: Deque[ReceivedSearch] = field(default_factory=lambda: deque(list(), 500))
search_queries: Dict[int, SearchRequest] = field(default_factory=dict)
search_requests: Dict[int, SearchRequest] = field(default_factory=dict)

def get_joined_rooms(self) -> List[Room]:
return [room for room in self.rooms.values() if room.joined]
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/test_peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async def test_onPeerSearchReply_shouldStoreResultsAndEmit(self):
TICKET = 1234
connection = AsyncMock()

manager._state.search_queries[TICKET] = SearchRequest(
manager._state.search_requests[TICKET] = SearchRequest(
TICKET, 'search', SearchType.NETWORK)

reply_message = PeerSearchReply.Request(
Expand All @@ -179,14 +179,14 @@ async def test_onPeerSearchReply_shouldStoreResultsAndEmit(self):
)
await manager._on_peer_search_reply(reply_message, connection)

assert 1 == len(manager._state.search_queries[TICKET].results)
assert 1 == len(manager._state.search_requests[TICKET].results)

manager._event_bus.emit.assert_has_awaits(
[
call(
SearchResultEvent(
manager._state.search_queries[TICKET],
manager._state.search_queries[TICKET].results[0]
manager._state.search_requests[TICKET],
manager._state.search_requests[TICKET].results[0]
)
),
call(UserInfoEvent(manager._state.get_or_create_user('user0')))
Expand Down

0 comments on commit d29de14

Please sign in to comment.