示例#1
0
    async def change_bot_state(request: "web.Request") -> "web.Response":
        bot = get_bot(request)
        action = request.match_info["action"]

        if action.lower() == "pause":
            bot.pause()
            raise web.HTTPAccepted()
        elif action.lower() == "resume":
            bot.resume()
            raise web.HTTPAccepted()
        else:
            raise web.HTTPBadRequest()
示例#2
0
    async def change_bot_state(request: 'web.Request') -> 'web.Response':
        bot = get_bot(request)
        action = request.match_info['action']

        if action.lower() == 'pause':
            bot.pause()
            raise web.HTTPAccepted()
        elif action.lower() == 'resume':
            bot.resume()
            raise web.HTTPAccepted()
        else:
            raise web.HTTPBadRequest()
示例#3
0
async def datasource_start(request: web.Request):
    """Start the datasource"""
    source_id = request.match_info['id']
    if source_id in request.app['datasources']:
        raise web.HTTPBadRequest(reason='Datasource is already running')
    path = os.path.join(request.app['settings'].DATASOURCE_DIR, source_id)
    if not os.path.exists(path):
        raise web.HTTPBadRequest(
            reason=f'datasource with id {source_id} does not exists')
    with open(path, 'r') as f:
        kwargs = json.loads(f.read())
    topic = f'{request.app["topic_counter"]:04}'
    request.app['topic_counter'] += 1
    request.app['datasources'].set_source(
        source_id=source_id,
        addr=tuple(kwargs['addr']),
        topic=topic,
        input_byte_format=kwargs['input_byte_format'],
        input_names=kwargs['input_names'],
        output_refs=kwargs['output_refs'],
        time_index=kwargs['time_index'],
    )
    request.app['topics'][topic] = {
        'url':
        '/datasources/' + source_id,
        'output_names':
        request.app['datasources'].get_source(source_id).output_names,
        'byte_format':
        request.app['datasources'].get_source(source_id).byte_format,
    }
    return web.HTTPAccepted()
示例#4
0
async def create_project(request: web.Request):
    r = await request.post()
    data = r['file']  # data is the file

    headers = request.headers
    content_length = int(headers['Content-length'])
    projectName = headers["projectName"]

    os.makedirs(request.app['settings'].PROJECT_DIR + "/" + projectName,
                exist_ok=True)

    # Write ".FMM" to disc
    if (".fmm" in data.filename):
        # request.app cannot be correct?
        fmmPath = request.app[
            'settings'].PROJECT_DIR + "/" + projectName + "/" + data.filename
        with open(fmmPath, 'w', newline='\n') as file:
            file.write(data.file.read(content_length).decode(
                'ascii'))  # writes .fmm to file

    print(request.app['settings'].PROJECT_DIR, projectName, data.filename)

    # Write ".FMU" to disc
    if (".fmu" in data.filename):
        fmuPath = request.app[
            'settings'].PROJECT_DIR + "/" + projectName + "/" + data.filename
        with open(fmuPath, 'wb') as file:
            file.write(data.file.read(content_length))  # writes .fmu to file
        thread = threading.Thread(target=processFMUfile,
                                  args=(request, fmuPath))
        #await processFMUfile(request, fmuPath)
        thread.daemon = True  # Daemonize thread
        thread.start()

    return web.HTTPAccepted()
示例#5
0
async def processor_inputs_update(request: web.Request):
    """
    Update the processor inputs

        Post params:

        - input_ref: reference values to the inputs to be used
        - measurement_ref: reference values to the measurement inputs to be used for the inputs.
          Must be in the same order as input_ref.
        - measurement_proportion: scale to be used on measurement values before inputting them.
          Must be in the same order as input_ref.
    """

    post = await request.post()
    processor_id = request.match_info['id']
    if processor_id not in request.app['processors']:
        return web.HTTPNotFound()
    input_refs = await try_get_all(post, 'input_ref', int)
    measurement_refs = await try_get_all(post, 'measurement_ref', int)
    measurement_proportions = await try_get_all(post, 'measurement_proportion',
                                                float)
    processor = request.app['processors'][processor_id]
    try:
        input_names = processor.set_inputs(input_refs, measurement_refs,
                                           measurement_proportions)
    except:
        raise web.HTTPUnprocessableEntity(
            reason='The selected processor does not seem to be running.')
    request.app['topics'][processor.topic]['input_names'] = input_names
    raise web.HTTPAccepted()
示例#6
0
async def processor_outputs_update(request: web.Request):
    """
    Update the processor outputs

        Post params:

        - output_ref: reference values to the outputs to be used
    """

    post = await request.post()
    processor_id = request.match_info['id']
    if processor_id not in request.app['processors']:
        return web.HTTPNotFound()
    processor: Processor = request.app['processors'][processor_id]
    if post.get('output_ref') == 'all':
        output_names = processor.set_outputs('all')
    else:
        output_refs = await try_get_all(post, 'output_ref', int)
        try:
            output_names = processor.set_outputs(output_refs)
        except:
            raise web.HTTPUnprocessableEntity(
                reason='The selected processor does not seem to be running.')
    request.app['topics'][
        processor.topic]['byte_format'] = processor.byte_format
    request.app['topics'][processor.topic]['output_names'] = output_names
    raise web.HTTPAccepted()
示例#7
0
文件: site.py 项目: witlox/dcron
    async def toggle_job(self, request):
        data = await request.post()

        self.logger.debug("received toggle request {0}".format(data))

        if 'command' not in data or \
                'minute' not in data or \
                'hour' not in data or \
                'dom' not in data or \
                'month' not in data or \
                'dow' not in data:
            return web.Response(status=500,
                                text='not all mandatory fields submitted')

        cron_item = self.generate_cron_item(data)

        if cron_item not in self.storage.cluster_jobs:
            raise web.HTTPConflict(text='job not found on cluster')

        self.logger.debug("broadcasting run result")

        broadcast(self.udp_port,
                  UdpSerializer.dump(Toggle(cron_item), self.hash_key))

        raise web.HTTPAccepted()
示例#8
0
async def delete_expansion(data_store, amqp, node_id):
    expansion = utils.delete_object(data_store, amqp, node_id,
                                    utils.KEY_EXPANSION)
    expansion.status = "Deleting"
    expansion.deleting = True
    data_store.save(expansion)
    await send_delete_expansion(data_store, amqp, expansion)
    raise web.HTTPAccepted()
示例#9
0
async def unsubscribe(request: web.Request):
    """Unsubscribe to the given topic"""
    topic = request.match_info['id']
    client = await get_client(request)
    if topic not in request.app['topics']:
        raise web.HTTPNotFound()
    request.app['subscribers'][topic].discard(client)
    raise web.HTTPAccepted()
async def delete_vpn_connection(data_store, amqp, node_id):
    vpn_connection = utils.delete_object(data_store, amqp, node_id,
                                         utils.KEY_CONNECTION)
    vpn_connection.status = "Deleting"
    vpn_connection.deleting = True
    data_store.save(vpn_connection)
    await send_delete_connection(data_store, amqp, vpn_connection)
    raise web.HTTPAccepted()
示例#11
0
async def delete_mptcp_proxy(data_store, amqp, node_id):
    mptcp_proxy = utils.delete_object(data_store, amqp, node_id,
                                      utils.KEY_MPTCP_PROXY)
    mptcp_proxy.status = "Deleting"
    mptcp_proxy.deleting = True
    data_store.save(mptcp_proxy)
    await send_delete_proxy(data_store, amqp, mptcp_proxy)
    raise web.HTTPAccepted()
示例#12
0
async def delete_l2_tunnel(data_store, amqp, node_id):
    l2_tunnel = utils.delete_object(data_store, amqp, node_id,
                                    utils.KEY_L2_TUNNEL)
    l2_tunnel.status = "Deleting"
    l2_tunnel.deleting = True
    data_store.save(l2_tunnel)
    await send_delete_tunnel(data_store, amqp, l2_tunnel)
    raise web.HTTPAccepted()
示例#13
0
    async def handle_message(self, request):
        """ Put to message queue and return 202 to client.
        """
        if not request.app['agent'].initialized:
            raise web.HTTPUnauthorized()

        msg = await request.read()
        await self.msg_queue.put(msg)
        raise web.HTTPAccepted()
示例#14
0
    async def __call__(self, request):
        kw = None
        if self._has_var_kw_arg or self._has_named_kw_args or self._has_request_arg:
            if request.method == 'POST':
                if not request.content_type:
                    return web.HTTPBadRequest('Missing Content-Type.')
                ct = request.content_type.lower()
                if ct.startswith('application/json'):
                    params = await request.json()
                    if not isinstance(params, dict):
                        return web.HTTPAccepted('JSON body must be object.')
                    kw = params
                elif ct.startswith('application/x-www-form-urlencoded'
                                   ) or ct.startswith('multipart/form-data'):
                    params = await request.post()
                    kw = dict(**params)
                else:
                    return web.HTTPBadRequest('Unsupported Content-Type: %s' %
                                              request.content_type)
            if request.method == 'GET':
                qs = request.query_string
                if qs:
                    kw = dict()
                    for k, v in parse.parse_qs(qs, True).items():
                        kw[k] = v[0]
        if kw is None:
            kw = dict(**request.match_info)
        else:
            if not self._has_var_kw_arg and self._named_kw_args:
                #remove all unamed kw:
                copy = dict()
                for name in self._named_kw_args:
                    if name in kw:
                        copy[name] = kw[name]
                kw = copy


# check named arg:
            for k, v in request.match_info.items():
                if k in kw:
                    logging.warning(
                        'Duplicate arg name in named arg and kw args: %s' % k)
                kw[k] = v
        if self._has_request_arg:
            kw['request'] = request
        # check required kw:
        if self._required_kw_args:
            for name in self._required_kw_args:
                if not name in kw:
                    return web.HTTPBadRequest('Missing argument: %s' % name)
        logging.info('call with args: %s' % str(kw))
        try:
            r = await self._func(**kw)
            return r
        except Exception as e:
            #return dict(error=e.error, data=e.data, message=e.message)
            pass
示例#15
0
async def subscribe(request: web.Request):
    """Subscribe to the given topic"""
    topic = request.match_info['id']
    client = await get_client(request)
    if topic not in request.app['topics']:
        raise web.HTTPNotFound()
    request.app['subscribers'][topic].add(client)
    print("client: ", client, "added to topic ", topic)
    raise web.HTTPAccepted()
示例#16
0
    async def handle_message(self, request):
        """ Put connection offer onto the mssage queue and return 202.
        """
        if not request.app['agent'].initialized:
            raise web.HTTPUnauthorized()

        msg = await request.read()
        await self.msg_queue.put(msg)
        raise web.HTTPAccepted()
示例#17
0
    async def handle(request):
        """aiohttp handle POST."""
        response = []
        with channel_manager.reply_handler(response.append):
            await channel_manager.handle(await request.read())

        if response:
            return web.Response(body=response.pop())

        raise web.HTTPAccepted()
示例#18
0
    async def post_handle(request):
        """Handle posted messages."""
        response = []
        with conn.session(response.append) as session:
            await session.handle(await request.read())

        if response:
            return web.Response(text=response.pop())

        raise web.HTTPAccepted()
示例#19
0
    async def handle(request):
        """aiohttp handle POST."""
        response = []
        with suite.reply(response.append):
            await suite.handle(await request.read())

        if response:
            return web.Response(body=response.pop())

        raise web.HTTPAccepted()
示例#20
0
    async def handle(request):
        """aiohttp handle POST."""
        response = []
        with conn.reply_handler(response.append):
            await conn.handle(await request.read())

        if response:
            return web.Response(text=response.pop())

        raise web.HTTPAccepted()
示例#21
0
    async def handle(request):
        """aiohttp handle POST."""
        response = []
        with conn.session(response.append) as session:
            await conn.handle(await request.read(), session)

        if response:
            return web.Response(body=response.pop())

        raise web.HTTPAccepted()
示例#22
0
async def datasource_delete(request: web.Request):
    """Delete the datasource"""
    source_id = request.match_info['id']
    if source_id in request.app['datasources']:
        raise web.HTTPBadRequest(reason='Datasource must be stopped first')
    path = os.path.join(request.app['settings'].DATASOURCE_DIR, source_id)
    if not os.path.exists(path):
        raise web.HTTPBadRequest(
            reason=f'datasource with id {source_id} does not exists')
    os.remove(path)
    return web.HTTPAccepted()
示例#23
0
async def unlock_key(request):
    """Unlock a key via a POST request.
    POST request takes the form:
    \{"type": "pgp", "private": "path/to/file.sec", "passphrase": "pass", "expire": "30/MAR/18 08:00:00"\}
    """
    key_info = await request.json()
    LOG.debug(f'Admin unlocking: {key_info}')
    if all(k in key_info for k in ("path", "passphrase", "expire")):
        await activate_key(key_info['type'], key_info)
        return web.HTTPAccepted()
    else:
        return web.HTTPBadRequest()
示例#24
0
async def unlock_key(request):
    """Unlock a key via a POST request.

    POST request takes the form:
    {"private": "path/to/file.sec", "passphrase": "pass", "expire": "30/MAR/18 08:00:00"}
    """
    key_info = await request.json()
    LOG.debug(f'Admin unlocking: {key_info}')
    if all(k in key_info for k in ("path", "passphrase", "expire")):
        _unlock_key('whichname?', **key_info)
        return web.HTTPAccepted()
    else:
        return web.HTTPBadRequest()
示例#25
0
async def post_handle(request):
    msg = await request.read()
    conn = HTTPConnection(msg)
    await request.app['connection_queue'].put(conn)

    try:
        await asyncio.wait_for(conn.wait(), 5)
    except asyncio.TimeoutError:
        await conn.close()

    if conn.new_msg:
        return web.Response(body=conn.new_msg)

    raise web.HTTPAccepted()
示例#26
0
async def data(request):
    if request.body_exists and request.content_type == 'application/json':
        try:
            data = await request.json()
        except json.decoder.JSONDecodeError:
            raise web.HTTPBadRequest(text='bad or empty json\n')
        print("data recieved:", data)
        #await write_into_db(data)
        async with request.app['db'].acquire() as conn:
            result, err = await save_json(conn, data)  #save data into db
            if result:
                raise web.HTTPAccepted(text='json recieved\n')
            else:
                raise web.HTTPBadRequest(text=err + '\n')
    else:
        raise web.HTTPForbidden(text='only json allowed\n')
示例#27
0
    async def process_single_rpc(self, rpc_data: dict) -> dict:
        try:
            protocol_version = rpc_data["jsonrpc"]
            method_name = rpc_data["method"]
            params = rpc_data.get("params", [])
            rid = rpc_data.get("id", None)
        except KeyError:
            raise JsonRpcError(Errors.INVALID_REQUEST)

        if protocol_version != PROTOCOL_VERSION:
            raise JsonRpcError(
                Errors.INVALID_REQUEST,
                detail=f"Unsupported protocol version {protocol_version}",
            )

        if method_name not in self.methods:
            raise JsonRpcError(Errors.METHOD_NOT_FOUND, rid=rid)

        if not isinstance(params, (list, dict, None)):
            raise JsonRpcError(Errors.INVALID_PARAMS, rid=rid)

        try:
            if isinstance(params, list):
                result = await self.methods[method_name](*params)
            else:
                result = await self.methods[method_name](**params)
        except JsonRpcError as exc:
            exc.set_context(rid=rid)
            raise exc
        except TypeError as exc:
            new_exp = JsonRpcError(Errors.INVALID_PARAMS,
                                   rid=rid,
                                   detail=exc_message(exc))
            raise new_exp.with_traceback() if self.debug else new_exp
        except Exception:  # pylint: disable=broad-except
            exp_class, exc, trace = sys.exc_info()
            logger = logging.getLogger("aiohttp.server")
            logger.error("".join(format_exception(exp_class, exc, trace)))
            new_exc = JsonRpcError(detail=exc_message(exc), rid=rid)
            if self.debug:
                raise new_exc.with_traceback()
            raise new_exc

        if not rid:
            raise web.HTTPAccepted()

        return {"jsonrpc": protocol_version, "result": result, "id": rid}
示例#28
0
async def get_file(request: web.Request) -> FileResponse:
    """For worker to pull file for calculating file hash task

    request headers:

    * X-DISTRIBUTED-WORKER-NAME: worker's name, worker should been registered
                                 already. required

    responses:

    * 200 OK: file response
    * 202 Accepted: wait for processing files timeout and make request again
    * 204 No Content: no more files
    * 400 Bad Request: missing worker name header
    * 400 Bad Request: worker not registered yet
    """
    master = request.app["master"]

    worker_name = _get_worker_name(request)
    _ensure_worker_registered(master, worker_name)

    try:
        file_path = master.files_queue.get_nowait()
    except asyncio.QueueEmpty:
        # try to get timeout task
        timeout_file = master.get_earlist_timeout_file()
        if timeout_file:
            _worker_name, file_path, _ = timeout_file
            # remove old processing file
            master.remove_current_processing_file(_worker_name, file_path)
        else:
            # try to find out any processing files
            if not master.has_processing_files():
                # no more files
                raise web.HTTPNoContent()
            else:
                # tell workers to wait until processing files timeout
                raise web.HTTPAccepted(
                    text=("wait for processing files timeout and make request"
                          " again"))

    master.new_processing_file(worker_name, file_path)
    master.files_queue.task_done()

    full_path = master.home.get_full_path(file_path)
    headers = {constants.HTTP_HEADER_X_FILE_PATH: file_path}
    return FileResponse(full_path, headers=headers)
示例#29
0
文件: site.py 项目: witlox/dcron
    async def re_balance(self, request):
        self.logger.debug("rebalance request received")

        self.scheduler.re_balance()

        jobs = self.storage.cluster_jobs.copy()

        broadcast(
            self.udp_port,
            UdpSerializer.dump(ReBalance(timestamp=datetime.now()),
                               self.hash_key))

        time.sleep(5)
        for job in jobs:
            broadcast(self.udp_port, UdpSerializer.dump(job, self.hash_key))

        raise web.HTTPAccepted()
示例#30
0
async def upload_datafile(request: web.Request):
    r = await request.post()
    data = r['file']

    headers = request.headers
    content_length = int(headers['Content-length'])
    project_name = headers["projectName"]
    os.makedirs(request.app['settings'].PROJECT_DIR + "/" + project_name + "/files", exist_ok=True)

    # Write ".FMU" to disc
    if ".csv" in data.filename or ".xlsx" in data.filename:
        path = request.app['settings'].PROJECT_DIR + "/" + project_name + "/files/" + data.filename
        with open(path, 'wb') as file:
            file.write(data.file.read(content_length))
        return web.HTTPAccepted()
    else:
        return web.HTTPBadRequest()