Example #1
0
def register_api_resource(api, resource):
  if funcy.is_seqcoll(resource):
    cls, url, endpoint = (
        funcy.first(resource),
        funcy.second(resource),
        funcy.nth(2, resource),
      )
    api.add_resource(cls, url, endpoint=endpoint)
Example #2
0
    def auth(self, source, target, args):
        """Authorize a Remote Plugin

        Syntax: AUTH <plugin> <password>
        """

        if not args:
            yield "No plugin specified."
            return

        tokens = args.split(" ", 2)
        plugin = first(tokens)
        password = second(tokens)

        data = self.parent.data.rplugins
        config = self.parent.config["rplugins"]

        if password != config["password"]:
            yield "Authorization failed."
            return

        if plugin in data["pending"]:
            url = data["pending"][plugin]
            del data["pending"][plugin]
            data["allowed"][plugin] = True

            value = yield self.call(
                task(
                    verify_plugin,
                    url,
                    config["path"],
                    data["allowed"],
                ),
                "workerprocesses"
            )

            allowed, plugin = value.value
            if allowed:
                msg = log(
                    "Remote Plugin {0:s} ({1:s}) successfully authorized.",
                    url, plugin
                )
                yield msg
            else:
                del data["allowed"][plugin]

                msg = log(
                    "Remote Plugin {0:s} ({1:s}) failed authorization.",
                    url, plugin
                )
                yield msg
        else:
            yield log("Remote Plugin {0:s} not found.", plugin)
Example #3
0
    def part(self, source, target, args):
        """Leave the specified channel

        Syntax: PART <channel> [<message>]
        """

        if not args:
            return "No channel specified."

        tokens = args.split(" ", 1)
        channel, message = first(tokens), second(tokens) or "Leaving"

        self.fire(PART(channel, message), "bot")
Example #4
0
def part2(grid):
    changeset = [None]

    while len(changeset) > 0:
        changeset.clear()

        for c, r in grid.coords():
            val = grid.get(c, r)
            if val == "L" or val == "#":
                splits = grid.starsplit(c, r, match=["L", "#"])
                occupied = sum(first(second(split)) == "#" for split in splits)
                if val == "L" and occupied == 0:
                    changeset.append((c, r, "#"))
                if val == "#" and occupied >= 5:
                    changeset.append((c, r, "L"))

        for change in changeset:
            grid.set(*change)

    return grid.count("#")
Example #5
0
File: rss.py Project: prologic/kdb
    def radd(self, source, target, args):
        """Add a new RSS feed to be checked at the given interval.

        Intervan is in minutes.

        Syntax: RADD <url> [<interval>]
        """

        if not args:
            yield "No URL specified."

        tokens = args.split(" ", 2)
        url = first(tokens)
        interval = second(tokens) or "60"

        try:
            interval = int(interval)
        except Exception, error:
            log("ERROR: {0:s}\n{1:s}", error, format_exc())
            yield "Invalid interval specified."
Example #6
0
    def greetings(self, source, target, args):
        """Manage greetings

        Syntax: GREETINGS <sub-command>

        See: COMMANDS greetings
        """

        if not args:
            yield "No command specified."

        tokens = args.split(" ", 1)
        command, args = first(tokens), (second(tokens) or "")

        event = cmd.create(command, source, target, args)

        try:
            yield self.call(event, "commands:greetings")
        except Exception as error:
            yield "ERROR: {0:s}".format(error)
Example #7
0
    def rplugins(self, source, target, args):
        """Manage Remote Plugins

        Syntax: RPLUGINS <command>

        See: COMMANDS rplugins
        """

        if not args:
            yield "No command specified."

        tokens = args.split(" ", 1)
        command, args = first(tokens), (second(tokens) or "")
        command = command.encode("utf-8")

        event = cmd.create(command, source, target, args)

        try:
            yield (yield self.call(event, "commands:rplugins"))
        except Exception as error:
            yield "ERROR: {0:s}".format(error)
Example #8
0
    def channels(self, source, target, args):
        """Manage channel startup join list

        Syntax: CHANNELS <sub-command>

        See: COMMANDS channels
        """

        if not args:
            yield "No command specified."
            raise StopIteration()

        tokens = args.split(" ", 1)
        command, args = first(tokens), (second(tokens) or "")
        command = command.encode("utf-8")

        event = cmd.create(command, source, target, args)

        try:
            yield (yield self.call(event, "commands:channels"))
        except Exception as error:
            yield "ERROR: {0:s}".format(error)
Example #9
0
    def timer(self, source, target, args):
        """Create a new time with the given length and message

        Syntax: TIMER <duration> [<message>]
        """

        if not args:
            return "No duration specified."

        tokens = args.split(" ", 1)
        duration = first(tokens)
        message = second(tokens)

        try:
            duration = int(duration)
        except ValueError:
            return "Invalid duration specified!"

        message = message or "{0:d}s timer expiered!"

        event = PRIVMSG(target, message)
        Timer(duration, event, channel="bot").register(self)

        return "Timer set for {0:d}s".format(duration)
Example #10
0
def parse_operation(op):
    """ Update all relevant collections that this op impacts. """
    op_type = op['type']

    update_accounts_light = set()
    update_accounts_full = set()
    update_comments = set()

    def construct_identifier():
        return '@%s/%s' % (
            op.get('author', op.get('comment_author')),
            op.get('permlink', op.get('comment_permlink')),
        )

    def account_from_auths():
        return first(op.get('required_auths', op.get('required_posting_auths')))

    if op_type in ['account_create',
                   'account_create_with_delegation']:
        update_accounts_light.add(op['creator'])
        update_accounts_full.add(op['new_account_name'])

    elif op_type in ['account_update',
                     'withdraw_vesting',
                     'claim_reward_balance',
                     'return_vesting_delegation',
                     'account_witness_vote']:
        update_accounts_light.add(op['account'])

    elif op_type == 'account_witness_proxy':
        update_accounts_light.add(op['account'])
        update_accounts_light.add(op['proxy'])

    elif op_type in ['author_reward', 'comment']:
        update_accounts_light.add(op['author'])
        update_comments.add(construct_identifier())

    elif op_type == 'cancel_transfer_from_savings':
        update_accounts_light.add(op['from'])

    elif op_type == 'change_recovery_account':
        update_accounts_light.add(op['account_to_recover'])

    elif op_type == 'comment_benefactor_reward':
        update_accounts_light.add(op['benefactor'])

    elif op_type == ['convert',
                     'fill_convert_request',
                     'interest',
                     'limit_order_cancel',
                     'limit_order_create',
                     'shutdown_witness',
                     'witness_update']:
        update_accounts_light.add(op['owner'])

    elif op_type == 'curation_reward':
        update_accounts_light.add(op['curator'])

    elif op_type in ['custom', 'custom_json']:
        update_accounts_light.add(account_from_auths())

    elif op_type == 'delegate_vesting_shares':
        update_accounts_light.add(op['delegator'])
        update_accounts_light.add(op['delegatee'])

    elif op_type == 'delete_comment':
        update_accounts_light.add(op['author'])

    elif op_type in ['escrow_approve',
                     'escrow_dispute',
                     'escrow_release',
                     'escrow_transfer']:
        accs = keep_in_dict(op, ['agent', 'from', 'to', 'who', 'receiver']).values()
        update_accounts_light.update(accs)

    elif op_type == 'feed_publish':
        update_accounts_light.add(op['publisher'])

    elif op_type in ['fill_order']:
        update_accounts_light.add(op['open_owner'])
        update_accounts_light.add(op['current_owner'])

    elif op_type in ['fill_vesting_withdraw']:
        update_accounts_light.add(op['to_account'])
        update_accounts_light.add(op['from_account'])

    elif op_type == 'pow2':
        acc = op['work'][1]['input']['worker_account']
        update_accounts_light.add(acc)

    elif op_type in ['recover_account',
                     'request_account_recovery']:
        update_accounts_light.add(op['account_to_recover'])

    elif op_type == 'set_withdraw_vesting_route':
        update_accounts_light.add(op['from_account'])
        # update_accounts_light.add(op['to_account'])
    elif op_type in ['transfer',
                     'transfer_from_savings',
                     'transfer_to_savings',
                     'transfer_to_vesting']:
        accs = keep_in_dict(op, ['agent', 'from', 'to', 'who', 'receiver']).values()
        update_accounts_light.update(accs)

    elif op_type == 'vote':
        update_accounts_light.add(op['voter'])
        update_comments.add(construct_identifier())

    # handle followers
    if op_type == 'custom_json':
        with suppress(ValueError):
            cmd, op_json = json.loads(op['json'])  # ['follow', {data...}]
            if cmd == 'follow':
                accs = keep_in_dict(op_json, ['follower', 'following']).values()
                update_accounts_light.discard(first(accs))
                update_accounts_light.discard(second(accs))
                update_accounts_full.update(accs)

    return {
        'accounts': list(update_accounts_full),
        'accounts_light': list(update_accounts_light),
        'comments': list(update_comments),
    }
Example #11
0
def es(setup_env):
    return second(setup_env)
Example #12
0
 def _to_references(value):
     return dissoc(
         second(value), 'reference_list', 'source_topic_key',
         'sort_order_category', 'subtopic_key', 'category_key', 'topic_key',
         'sort_order_subtopic')