Example #1
0
 async def __handle_proxy_status(self):
     async with contextlib.aclosing(
             self.proxy_mpd.idle(
                 "player",
                 "playlist",
                 "options",
                 "update",
                 initial_trigger=True,
                 split=False,
             )) as iter_changed:
         async for changed in iter_changed:
             changed = frozenset(changed)
             assert changed <= {"player", "playlist", "options", "update"
                                }, f"Got {changed}"
             status = await self.update_status_from_proxy()
             if self.play_state != PlayState.stop:
                 state = status[b"state"]
                 assert state in (b"stop", b"play", b"pause")
                 if state == b"stop":
                     await self.proxy_mpd.command_returning_nothing(b"play")
                     logger.info("Taken proxy mpd out of 'stop' state")
             if 'player' in changed:
                 self.trigger_cast_state_update.set()
             for c in changed:
                 self.notify_idle(c)
Example #2
0
 async def run(self):
     async with contextlib.aclosing(super().run()) as iter_x:
         async for x in iter_x:
             yield x
     self.partition.current_time = self.parse_args()['time']
     self.partition.notify_idle('player')
     self.partition.trigger_cast_state_update.set()
Example #3
0
 async def run(self):
     async with contextlib.aclosing(super().run()) as iter_x:
         async for x in iter_x:
             yield x
     if self.partition.current_output_id is not None:
         self.partition.play_state = PlayState.play
         self.partition.notify_idle('player')
         self.partition.trigger_cast_state_update.set()
Example #4
0
async def async_assets(
    dandiset: RemoteDandiset, ds: Dataset, config: Config, tracker: AssetTracker
) -> Report:
    done_flag = trio.Event()
    total_report = Report()
    async with aclosing(  # type: ignore[type-var]
        aiterassets(dandiset, done_flag)
    ) as aia:
        while not done_flag.is_set():
            async with await open_git_annex(
                "addurl",
                "--batch",
                "--with-files",
                "--jobs",
                str(config.jobs),
                "--json",
                "--json-error-messages",
                "--json-progress",
                "--raw",
                path=ds.pathobj,
            ) as p:
                async with httpx.AsyncClient() as s3client:
                    async with Downloader(
                        dandiset.identifier, p, ds.pathobj, config, tracker, s3client
                    ) as dm:
                        try:
                            async with trio.open_nursery() as nursery:
                                dm.nursery = nursery
                                nursery.start_soon(dm.asset_loop, aia)
                                nursery.start_soon(dm.feed_addurl)
                                nursery.start_soon(dm.read_addurl)
                        finally:
                            tracker.dump()
            if dandiset.version_id == "draft":
                if dm.report.registered or dm.report.downloaded:
                    log.info(
                        "%s registered, %s downloaded for this version segment;"
                        " committing",
                        quantify(dm.report.registered, "asset"),
                        quantify(dm.report.downloaded, "asset"),
                    )
                    if any(
                        r["state"] != "clean" for r in ds.status(result_renderer=None)
                    ):
                        log.info("Commiting changes")
                        assert dm.last_timestamp is not None
                        with custom_commit_date(dm.last_timestamp):
                            ds.save(message=dm.report.get_commit_message())
                        total_report.commits += 1
                else:
                    log.info(
                        "No assets downloaded for this version segment; not committing"
                    )
            total_report.update(dm.report)
    return total_report
Example #5
0
 async def background():
     try:
         async with self.proxy_mpd:
             async with contextlib.aclosing(
                     self.proxy_mpd.idle("database")) as iter_idle:
                 async for _ in iter_idle:
                     pass
     except (ConnectionError, anyio.BrokenResourceError):
         logger.info("Client lost connection with proxy mpd:",
                     exc_info=True)
         tg.cancel_scope.cancel()
Example #6
0
    async def test_aclosing(self):
        state = []

        class C:
            async def aclose(self):
                state.append(1)

        x = C()
        self.assertEqual(state, [])
        async with aclosing(x) as y:
            self.assertEqual(x, y)
        self.assertEqual(state, [1])
Example #7
0
 async def run(self):
     songid = self.partition.status_from_proxy.get(b"songid")
     try:
         async with contextlib.aclosing(
                 super().run()) as iter_lines:
             async for line in iter_lines:
                 yield line
     finally:
         await self.partition.update_status_from_proxy()
         if songid != self.partition.status_from_proxy.get(
                 b"songid"):
             self.partition.current_time = 0
Example #8
0
    async def test_aclosing_error(self):
        state = []

        class C:
            async def aclose(self):
                state.append(1)

        x = C()
        self.assertEqual(state, [])
        with self.assertRaises(ZeroDivisionError):
            async with aclosing(x) as y:
                self.assertEqual(x, y)
                1 / 0
        self.assertEqual(state, [1])
Example #9
0
 async def __handle_proxy_output(self):
     async with contextlib.aclosing(
             self.proxy_mpd.idle("output",
                                 initial_trigger=True)) as iter_subsystems:
         async for subsystem in iter_subsystems:
             assert subsystem == "output", f"Got subsystem {subsystem}"
             for output in await self.proxy_mpd.command_returning_raw_objects(
                     b"outputs", delimiter=b"outputid"):
                 outputenabled = output[b"outputenabled"]
                 assert outputenabled in (b"0", b"1")
                 if output[b"outputenabled"] == b"1":
                     await self.proxy_mpd.command_returning_nothing(
                         b"disableoutput " + output[b"outputid"])
                     logger.info("Disabled output of proxy mpd")
Example #10
0
 async def __forward_idle(self):
     async with contextlib.aclosing(
             self.proxy_mpd.idle(
                 "database",
                 "stored_playlist",
                 "sticker",
                 "partition",
                 "subscription",
                 "message",
                 "mixer",
                 initial_trigger=True,
             )) as iter_subsystems:
         async for subsystem in iter_subsystems:
             self.notify_idle(subsystem)
Example #11
0
    async def test_aclosing_bpo41229(self):
        state = []

        class Resource:
            def __del__(self):
                state.append(1)

        async def agenfunc():
            r = Resource()
            yield -1
            yield -2

        x = agenfunc()
        self.assertEqual(state, [])
        with self.assertRaises(ZeroDivisionError):
            async with aclosing(x) as y:
                self.assertEqual(x, y)
                self.assertEqual(-1, await x.__anext__())
                1 / 0
        self.assertEqual(state, [1])
Example #12
0
    async def test_aclosing_bpo41229(self):
        state = []

        @contextmanager
        def sync_resource():
            try:
                yield
            finally:
                state.append(1)

        async def agenfunc():
            with sync_resource():
                yield -1
                yield -2

        x = agenfunc()
        self.assertEqual(state, [])
        with self.assertRaises(ZeroDivisionError):
            async with aclosing(x) as y:
                self.assertEqual(x, y)
                self.assertEqual(-1, await x.__anext__())
                1 / 0
        self.assertEqual(state, [1])
Example #13
0
 async def read_addurl(self) -> None:
     async with aclosing(aiter(self.addurl)) as lineiter:  # type: ignore[type-var]
         async for line in lineiter:
             data = json.loads(line)
             if "byte-progress" in data:
                 # Progress message
                 log.info(
                     "%s: Downloaded %d / %s bytes (%s)",
                     data["action"]["file"],
                     data["byte-progress"],
                     data.get("total-size", "???"),
                     data.get("percent-progress", "??.??%"),
                 )
             elif not data["success"]:
                 log.error(
                     "%s: download failed:%s",
                     data["file"],
                     format_errors(data["error-messages"]),
                 )
                 self.in_progress.pop(data["file"])
                 self.report.failed += 1
             else:
                 path = data["file"]
                 key = data.get("key")
                 log.info("%s: Finished downloading (key = %s)", path, key)
                 self.report.downloaded += 1
                 dl = self.in_progress.pop(path)
                 self.tracker.finish_asset(dl.path)
                 assert self.nursery is not None
                 self.nursery.start_soon(
                     self.check_unannexed_hash,
                     dl.path,
                     dl.sha256_digest,
                     name=f"check_unannexed_hash:{dl.path}",
                 )
     log.debug("Done reading from addurl")
Example #14
0
 async def discover_chromecasts(self):
     async with contextlib.aclosing(discover_chromecasts(
             self.zconf)) as iter_changes:
         async for change, cast_service in iter_changes:
             if change in {'+', '-+'}:
                 try:
                     oid = self.output_id_by_uuid[cast_service.uuid]
                 except KeyError:
                     oid = self.output_id_by_uuid[cast_service.uuid] = len(
                         self.output_id_by_uuid)
                 self.outputs[oid] = cast_service
                 logger.info('Found chromecast {}',
                             cast_service.friendly_name)
             elif change == '-':
                 try:
                     oid = self.output_id_by_uuid[cast_service.uuid]
                     del self.outputs[oid]
                 except KeyError:
                     pass
                 logger.info('Lost chromecast {}',
                             cast_service.friendly_name)
             else:
                 raise AssertionError
             self.server.notify_idle('output')
Example #15
0
 def test_instance_docs(self):
     cm_docstring = aclosing.__doc__
     obj = aclosing(None)
     self.assertEqual(obj.__doc__, cm_docstring)