Ejemplo n.º 1
0
def self_desc(cls):
    '''Method to get self description, used at init.'''
    desc_list = [f'{get_class_name(cls)}:']
    for k, v in get_class_attr(cls).items():
        if k == 'spec':
            desc_v = v['name']
        elif ps.is_dict(v) or ps.is_dict(ps.head(v)):
            desc_v = to_json(v)
        else:
            desc_v = v
        desc_list.append(f'- {k} = {desc_v}')
    desc = '\n'.join(desc_list)
    return desc
Ejemplo n.º 2
0
def describe(cls):
    desc_list = [f'{get_cls_name(cls)}:']
    for k, v in get_cls_attr(cls).items():
        if k == '_config':
            try:
                desc_v = v['name']
            except:
                continue
        elif ps.is_dict(v) or ps.is_dict(ps.head(v)):
            desc_v = pformat(v)
        else:
            desc_v = v
        desc_list.append(f'- {k} = {desc_v}')
    desc = '\n'.join(desc_list)
    return desc
Ejemplo n.º 3
0
def self_desc(cls, omit=None):
    '''Method to get self description, used at init.'''
    desc_list = [f'{get_class_name(cls)}:']
    omit_list = ps.compact(cast_list(omit))
    for k, v in get_class_attr(cls).items():
        if k in omit_list:
            continue
        if k == 'spec':  # spec components are described at their object level; for session, only desc spec.meta
            desc_v = pformat(v['meta'])
        elif ps.is_dict(v) or ps.is_dict(ps.head(v)):
            desc_v = pformat(v)
        else:
            desc_v = v
        desc_list.append(f'- {k} = {desc_v}')
    desc = '\n'.join(desc_list)
    return desc
Ejemplo n.º 4
0
def flatten_dict(obj, delim='.'):
    '''Missing pydash method to flatten dict'''
    nobj = {}
    for key, val in obj.items():
        if ps.is_dict(val) and not ps.is_empty(val):
            strip = flatten_dict(val, delim)
            for k, v in strip.items():
                nobj[key + delim + k] = v
        elif ps.is_list(val) and not ps.is_empty(val) and ps.is_dict(val[0]):
            for idx, v in enumerate(val):
                nobj[key + delim + str(idx)] = v
                if ps.is_object(v):
                    nobj = flatten_dict(nobj, delim)
        else:
            nobj[key] = val
    return nobj
Ejemplo n.º 5
0
def is_sub_dict(sub_dict, super_dict):
    '''
    Check if sub_dict is a congruent subset of super_dict
    @param {dict} sub_dict The sub dictionary
    @param {dict} super_dict The super dictionary
    @returns {bool}
    @example

    sub_dict = {'a': 1, 'b': 2}
    super_dict = {'a': 0, 'b': 0, 'c': 0}
    util.is_sub_dict(sub_dict, super_dict)
    # => True

    nested_sub_dict = {'a': {'b': 1}, 'c': 2}
    nested_super_dict = {'a': {'b': 0}, 'c': 0, 'd': 0}
    util.is_sub_dict(nested_sub_dict, nested_super_dict)
    # => True

    incon_nested_super_dict = {'a': {'b': 0}, 'c': {'d': 0}}
    util.is_sub_dict(nested_sub_dict, incon_nested_super_dict)
    # => False
    '''
    for sub_k, sub_v in sub_dict.items():
        if sub_k not in super_dict:
            return False
        super_v = super_dict[sub_k]
        if type(sub_v) != type(super_v):
            return False
        if ps.is_dict(sub_v):
            if not is_sub_dict(sub_v, super_v):
                return False
        else:
            if sub_k not in super_dict:
                return False
    return True
Ejemplo n.º 6
0
 def __init__(self, spec, aeb_space, global_nets=None):
     self.spec = spec
     self.aeb_space = aeb_space
     aeb_space.agent_space = self
     self.info_space = aeb_space.info_space
     self.aeb_shape = aeb_space.aeb_shape
     assert not ps.is_dict(
         global_nets
     ), f'multi agent global_nets must be a list of dicts, got {global_nets}'
     assert ps.is_list(self.spec['agent'])
     self.agents = []
     for a in range(len(self.spec['agent'])):
         body_a = self.aeb_space.body_space.get(a=a)
         if global_nets is not None:
             agent_global_nets = global_nets[a]
         else:
             agent_global_nets = None
         agent = Agent(self.spec,
                       self.info_space,
                       body=body_a,
                       a=a,
                       agent_space=self,
                       global_nets=agent_global_nets)
         self.agents.append(agent)
     logger.info(util.self_desc(self))
Ejemplo n.º 7
0
    def send_message(self,
                     topic,
                     msg={},
                     retain=False,
                     qos=0,
                     dup=False,
                     timeout=DEFAULT_TIMEOUT):
        future = asyncio.Future(loop=self.loop)
        if (_.is_dict(msg) and _.get(msg, '__head__') is None):
            head = wrap_data(None, None, self.name, self.version)['__head__']
            _.set_(msg, '__head__', head)
        message = json.dumps(msg)

        _mid = None

        def on_publish(client, userdata, mid):
            if (mid == _mid):
                if (future.done() == False):
                    future.set_result('done')

        def on_timeout():
            if (future.done() == False):
                future.set_exception(Exception(f'timeout {timeout}ms'))

        self.client.on_publish = self.safe(on_publish)
        (qos, _mid) = self.client.publish(topic,
                                          payload=message,
                                          qos=qos,
                                          retain=retain)
        set_timeout(self.safe(on_timeout), timeout)

        return future
Ejemplo n.º 8
0
    def userWebhook(event):
        message = get(event, 'body')
        body = get(message, 'body')
        defaultResponse = result(
            'user WebHook replied', 200, {
                'headers': {
                    'validation-token':
                    get(event, 'headers.validation-token')
                    or get(event, 'headers.Validation-Token')
                }
            })

        if not is_dict(body):
            print('body not dict')
            return defaultResponse

        userId = get(body, 'extensionId') or get(message, 'ownerId')
        eventType = get(message, 'event')
        user = getUser(userId)
        isRenewEvent = eventType == subscribeIntervalText

        if not isinstance(user, User):
            return defaultResponse

        if isRenewEvent:
            user.renewWebHooks(event)
            user.refresh()
            return defaultResponse

        else:
            conf.userEventAction(user, eventType, event, getBot, dbAction)
            return defaultResponse
Ejemplo n.º 9
0
 async def put_plugin(self, receiver, prop, val, timeout=DEFAULT_TIMEOUT):
     if (not _.is_dict(val)):
         msg = {}
         _.set_(msg, prop, val)
         val = msg
     result = await self.call_action(receiver, prop, val, 'put', timeout)
     return result
Ejemplo n.º 10
0
    def __init__(self, spec, info_space, global_nets=None):
        self.spec = spec
        self.info_space = info_space
        self.index = self.info_space.get('session')
        util.set_session_logger(self.spec, self.info_space, logger)
        self.data = None

        self.aeb_space = AEBSpace(self.spec, self.info_space)
        self.env_space = EnvSpace(self.spec, self.aeb_space)
        self.aeb_space.init_body_space()
        util.set_rand_seed(self.info_space.get_random_seed(), self.env_space)
        util.try_set_cuda_id(self.spec, self.info_space)
        assert not ps.is_dict(
            global_nets
        ), f'multi agent global_nets must be a list of dicts, got {global_nets}'
        self.agent_space = AgentSpace(self.spec, self.aeb_space, global_nets)

        logger.info(util.self_desc(self))
        logger.info(f'Initialized session {self.index}')
Ejemplo n.º 11
0
def test_s_get(test_agent):
    spec = util.s_get(test_agent, 'aeb_space.spec')
    assert _.is_dict(spec)
    spec = util.s_get(test_agent, 'aeb_space').spec
    assert _.is_dict(spec)
Ejemplo n.º 12
0
def test_is_dict(case, expected):
    assert _.is_dict(case) == expected
Ejemplo n.º 13
0
def test_s_get(test_agent):
    spec = util.s_get(test_agent, 'aeb_space.spec')
    assert ps.is_dict(spec)
    spec = util.s_get(test_agent, 'aeb_space').spec
    assert ps.is_dict(spec)
Ejemplo n.º 14
0
  def botWebhook(event):
    message = get(event, 'body')
    body = get(message, 'body')
    defaultResponse = result('bot WebHook replied', 200, {
      'headers': {
        'validation-token': get(event, 'headers.validation-token') or get(event, 'headers.Validation-Token')
      }
    })
    if not is_dict(body) :
      return defaultResponse

    botId = get(message, 'ownerId')
    eventType = get(body, 'eventType')
    msgType = get(body, 'type')
    groupId = get(body, 'groupId') or get(body, 'id')
    bot = getBot(botId)
    creatorId = get(body, 'creatorId')
    if not isinstance(bot, Bot):
      return defaultResponse

    user = getUser(creatorId)
    if user == False:
      user = User()
    if eventType == 'GroupJoined':
      conf.botJoinPrivateChatAction(bot, groupId, user, dbAction)

    elif eventType == 'PostAdded' and msgType == 'TextMessage':
      # for bot self post, ignore
      if creatorId == botId:
        return defaultResponse
      text = get(body, 'text') or ''
      if hiddenCmd(bot, groupId, text, event):
        return defaultResponse
      handledByExtension = runExtensionFunction(
        extensions,
        'botGotPostAddAction',
        bot,
        groupId,
        creatorId,
        user,
        text,
        dbAction,
        event
      )
      conf.botGotPostAddAction(
        bot,
        groupId,
        creatorId,
        user,
        text,
        dbAction,
        handledByExtension,
        event
      )

    elif eventType == 'Delete':
      conf.botDeleteAction(
        bot,
        message,
        dbAction
      )

    elif eventType == 'GroupLeft':
      conf.botGroupLeftAction(
        bot,
        message,
        dbAction
      )

    else:
      text = get(body, 'text') or ''
      handledByExtension = runExtensionFunction(
        extensions,
        'defaultEventHandler',
        bot,
        groupId,
        creatorId,
        user,
        text,
        dbAction,
        event
      )
      conf.defaultEventHandler(
        bot,
        groupId,
        creatorId,
        user,
        text,
        dbAction,
        handledByExtension,
        event
      )

    return defaultResponse