From f58217d0cbe78f7c6b35767b0245f6deda5260dd Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Sun, 5 Feb 2017 16:28:40 +0100 Subject: [PATCH 1/2] Add missing async in UnixConnection in nyasync --- grzegorz/nyasync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grzegorz/nyasync.py b/grzegorz/nyasync.py index 6d2de56..516b496 100644 --- a/grzegorz/nyasync.py +++ b/grzegorz/nyasync.py @@ -38,7 +38,7 @@ class Condition: self.monitor.notify_all() class UnixConnection: - def __init__(self, path): + async def __init__(self, path): (self.reader, self.writer) = await asyncio.open_unix_connection(path) def __aiter__(self): From bc54eb3f6b245e4d51def6bcfe61be8540560ac4 Mon Sep 17 00:00:00 2001 From: Aleksander Wasaznik Date: Sun, 5 Feb 2017 16:30:18 +0100 Subject: [PATCH 2/2] Add singleton playlist manager Currently implemented is queing, dequening, and automatic title fetch using metadatafetch. --- grzegorz/__init__.py | 2 ++ grzegorz/playlistmanage.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 grzegorz/playlistmanage.py diff --git a/grzegorz/__init__.py b/grzegorz/__init__.py index 640a5fc..c2c0a7c 100644 --- a/grzegorz/__init__.py +++ b/grzegorz/__init__.py @@ -1,7 +1,9 @@ import asyncio +from . import playlistmanage async def entry(): await asyncio.wait([ + playlistmanage.metadatafetch_loop(), ]) def main(): diff --git a/grzegorz/playlistmanage.py b/grzegorz/playlistmanage.py new file mode 100644 index 0000000..081831e --- /dev/null +++ b/grzegorz/playlistmanage.py @@ -0,0 +1,34 @@ +import asyncio + +from . import metadatafetch +from . import nyasync + +metadatafetch_queue = nyasync.Queue() +async def metadatafetch_loop(): + async for item in metadatafetch_queue: + title = await metadatafetch.title(item.url) + item.title = title + metadatafetch_queue.task_done() + +class PlaylistItem: + def __init__(self, url): + self.url = url + self.title = None + +class Playlist: + def __init__(self): + self.playlist = [] + self.nonempty = nyasync.Condition(lambda: self.playlist) + self.change = nyasync.Event() + + def queue(self, url): + item = PlaylistItem(url) + self.playlist.append(item) + metadatafetch_queue.put_nowait(item) + self.nonempty.notify() + self.change.notify() + + async def dequeue(self) -> PlaylistItem: + await self.nonempty + self.change.notify() + return self.playlist.pop(0)