async def download_file(
    http_session: aiohttp.ClientSession,
    file_spec: DownloadableFileSpec,
    position: int,
) -> None:

    if file_spec.target:
        request = http_session.get(file_spec.target.url)
        afp = aiofile.AIOFile(str(file_spec.target.path), "wb")
        progress = tqdm(
            desc=file_spec.target.path.name,
            total=file_spec.target.size,
            leave=False,
            miniters=1,
            unit="iB",
            unit_scale=True,
            unit_divisor=(2**10),
            position=position,
            file=sys.stdout,
        )
        async with request as response, afp:
            with progress:
                writer = aiofile.Writer(afp)
                async for chunk in response.content.iter_chunked(CHUNK_SIZE):
                    await writer(chunk)
                    progress.update(len(chunk))

    if file_spec.target_md5:
        async with http_session.get(file_spec.target_md5.url) as response:
            content = await response.read()
            file_spec.target_md5.path.write_bytes(content)
Exemplo n.º 2
0
async def writeCommentToLogger(comment):
    ts = time.gmtime()
    timestamp = str(time.strftime("%s", ts))
    async with aiofile.AIOFile("verifier_logger.csv", 'a+') as afp:
        writer = aiofile.Writer(afp)
        await writer(timestamp + "," + comment + "\n")
        await afp.fsync()
Exemplo n.º 3
0
async def wal_writer(path, wal_log):

    async with aiofile.AIOFile(path, 'w+b') as f:
        writer = aiofile.Writer(f)
        count = 0
        while True:
            item = await wal_log.get()
            message_type, record = item
            if message_type == MessageTypes.SHUTDOWN.value:
                wal_log.task_done()
                break

            if message_type == MessageTypes.RECORD.value:
                await writer(write_wal_record_header(record))
                await writer(bytes(record.record, "utf8"))

                # FIXME

            elif message_type == MessageTypes.CHECKPOINT.value:
                log.info("checkpoint %s:%s", record.stream_name, record.id)
                await writer(write_wal_checkpoint())

            wal_log.task_done()

            count += 1

            if count % FLUSH_INTERVAL:
                await f.fsync()

    log.info("wal writer shutdown")
Exemplo n.º 4
0
 async def  __dump_links(self) -> None:
   # We need to dump links into /links
   links_dir = f"{self.__dir}/{LINKS_FOLDER_NAME}/{self.__name}"
   async with aiofile.AIOFile(links_dir, "w+") as file:
     writer = aiofile.Writer(file)
     output = "\n".join(self.__raw_links)
     await writer(output)
Exemplo n.º 5
0
async def main(start_port: int, show_timing: bool = False, container_name: str = "Simple_client"):
    global agent
    global client_name
    genesis = await default_genesis_txns()
    if not genesis:
        print("Error retrieving ledger genesis transactions")
        sys.exit(1)
    try:
        log_status("Provision an agent and wallet, get back configuration details")
        label=container_name
        client_name=label
        agent = ClientAgent(
            label, start_port, start_port + 1, genesis_data=genesis, timing=show_timing
        )
        await agent.listen_webhooks(start_port + 2)
        # await agent.register_did()
        with log_timer("Startup duration:"):
            await agent.start_process()
        log_msg("Admin url is at:", agent.admin_url)
        log_msg("Endpoint url is at:", agent.endpoint)

        async with aiofile.AIOFile("client_logger.csv", 'w') as afp:
            writer = aiofile.Writer(afp)
            await writer("")
            await afp.fsync()

        app = web.Application()
        app.add_routes([
            web.get('/get_client_name', handle_get_client_name),
            web.get('/get_connections', handle_get_connections),
            web.post('/input_invitation', handle_input_invitation),
            web.post('/sign_message', handle_sign_message),
            web.get('/get_signing_did', handle_get_signing_did),
            web.get('/readCommentsFromLogger', readCommentsFromLogger),
        ])

        cors = aiohttp_cors.setup(
            app,
            defaults={
                "*": aiohttp_cors.ResourceOptions(
                    allow_credentials=True,
                    expose_headers="*",
                    allow_headers="*",
                    allow_methods="*",
                )
            },
        )
        for route in app.router.routes():
            cors.add(route)

        return app
    except Exception:
        print("Error when starting to run server!!")
Exemplo n.º 6
0
async def main(start_port: int, show_timing: bool = False):
    global agent
    genesis = await default_genesis_txns()
    agent = None
    if not genesis:
        print("Error retrieving ledger genesis transactions")
        sys.exit(1)
    try:
        agent = CredentialIssuerAgent(
            start_port, start_port + 1, genesis_data=genesis, timing=show_timing
        )
        await agent.listen_webhooks(start_port + 2)
        await agent.register_did()
        with log_timer("Startup duration:"):
            await agent.start_process()
        log_msg("Admin url is at:", agent.admin_url)
        log_msg("Endpoint url is at:", agent.endpoint)

        async with aiofile.AIOFile("issuer_logger.csv", 'w') as afp:
            writer = aiofile.Writer(afp)
            await writer("")
            await afp.fsync()

        app = web.Application()
        app.add_routes([
            web.get('/create_invitation', handle_create_invitation),
            web.post('/create_schema_cred_def', handle_create_schema_credential_definition),
            web.post('/send_credential_offer', handle_send_credential_offer),
            web.get('/issue_credential', handle_issue_credential),
            web.get('/get_connection_list', handle_get_connection_list),
            web.get('/get_cred_def_list', handle_get_cred_def_list),
            web.get('/readCommentsFromLogger', readCommentsFromLogger),
        ])

        cors = aiohttp_cors.setup(
            app,
            defaults={
                "*": aiohttp_cors.ResourceOptions(
                    allow_credentials=True,
                    expose_headers="*",
                    allow_headers="*",
                    allow_methods="*",
                )
            },
        )
        for route in app.router.routes():
            cors.add(route)

        return app
    except Exception:
        print("Error when starting to run server!!")
Exemplo n.º 7
0
async def token_consumer(in_q: asyncio.Queue):
    async with aiofile.AIOFile(sys.argv[3], 'wb') as f:
        write = aiofile.Writer(f)
        eos = False
        while not eos:
            tokens = deque([await in_q.get()])
            while not in_q.empty():
                token = await in_q.get()
                tokens.append(token)
            if tokens[-1] is OES:
                eos = True
                tokens.pop()
            if tokens:
                transform_tasks = [transform_token(t) for t in tokens]
                transformed = await asyncio.gather(*transform_tasks)
                await write(b''.join(transformed))
            in_q.task_done()
        await f.fsync()
Exemplo n.º 8
0
async def save_messages(filepath, queue):
    async with aiofile.AIOFile(filepath, 'a') as history_file:
        writer = aiofile.Writer(history_file)
        while True:
            message = await queue.get()
            await writer(f'{message}\n')
Exemplo n.º 9
0
async def main(start_port: int, show_timing: bool = False):
    global agent
    genesis = await default_genesis_txns()
    if not genesis:
        print("Error retrieving ledger genesis transactions")
        sys.exit(1)

    try:
        log_status(
            "Provision an agent and wallet, get back configuration details")
        agent = MSPAgent(start_port,
                         start_port + 1,
                         genesis_data=genesis,
                         timing=show_timing)
        await agent.listen_webhooks(start_port + 2)

        with log_timer("Startup duration:"):
            await agent.start_process()
        log_msg("Admin url is at:", agent.admin_url)
        log_msg("Endpoint url is at:", agent.endpoint)

        async with aiofile.AIOFile("verifier_logger.csv", 'w') as afp:
            writer = aiofile.Writer(afp)
            await writer("")
            await afp.fsync()

        app = web.Application()
        app.add_routes([
            web.get('/create_invitation', handle_create_invitation),
            web.post('/verify_signature', handle_verify_signature),
            web.post('/verify_proof', handle_verify_proof),
            web.get('/readCommentsFromLogger', readCommentsFromLogger),
        ])

        cors = aiohttp_cors.setup(
            app,
            defaults={
                "*":
                aiohttp_cors.ResourceOptions(
                    allow_credentials=True,
                    expose_headers="*",
                    allow_headers="*",
                    allow_methods="*",
                )
            },
        )
        for route in app.router.routes():
            cors.add(route)

        setup_aiohttp_apispec(app=app,
                              title="Aries Cloud Agent two",
                              version="v1",
                              swagger_path="/api/doc")
        app.on_startup.append(on_startup)

        return app

    except Exception:
        log_status(
            "Error while provision an agent and wallet, get back configuration details"
        )
Exemplo n.º 10
0
 async def __dump_text(self) -> None:
   # and words into /words
   words_dir = f"{self.__dir}/{WORDS_FOLDER_NAME}/{self.__name}"
   async with aiofile.AIOFile(words_dir, "w+") as file:
     writer = aiofile.Writer(file)
     await writer(self.__raw_text)