def test_output_with_generator_and_output_json(self):
        def generate_data():
            for i in range(10):
                yield {'href': 'test%s' % i}

        data = generate_data()
        output._output = MagicMock()

        output.output(data, output_json=True)
        self.assertEqual(output._output.call_count, 1)
        output._output.assert_called_with(
            list(generate_data()), output_json=True, show_headers=False,
            show_links=False)
    def test_output_with_generator(self):
        def generate_data():
            for i in range(10):
                yield {'href': 'test%s' % i}

        data = generate_data()
        output._output = MagicMock()

        output.output(data)
        self.assertEqual(output._output.call_count, 10)
        calls = []
        for i in range(10):
            calls.append(call(
                {'href': 'test%s' % i}, output_json=False,
                show_headers=False, show_links=False))
        output._output.assert_has_calls(calls)
Example #3
0
def main():
    arguments = docopt(__doc__)
    action = arguments.get('<action>')
    resource = arguments.get('<resource>')

    log = setup_output(arguments.get('--verbose'))

    arguments.update(get_context_dict())
    arguments, resource, action = find_non_dash_arguments_and_default_action(
        arguments, resource, action)
    arguments = properly_support_boolean_values(arguments)
    arguments = check_primary_identifier_without_flags(arguments, resource,
                                                       action)

    if not action:
        log.error(__doc__.strip('\n'))
        return -1

    if action == 'help':
        log.error(__doc__.strip('\n'))
        return -1

    if action not in AVAILABLE_ACTIONS:
        log.error(
            "Unknown action '{}'. See 'stormpath --help' for list of available actions."
            .format(action))
        return -1

    if action in LOCAL_ACTIONS:
        return 0 if AVAILABLE_ACTIONS[action](arguments) else -1

    if not resource and action != STATUS_ACTION:
        if action == SET_ACTION:
            log.error(
                "A resource type is required. Available resources for the set command are: application, directory. Please see 'stormpath --help'"
            )
            return -1

        log.error(
            "A resource type is required. Available resources: {}. Please see 'stormpath --help'"
            .format(', '.join(sorted(AVAILABLE_RESOURCES.keys()))))
        return -1

    if resource not in AVAILABLE_RESOURCES and action != STATUS_ACTION:
        log.error(
            "Unknown resource type '{}'. See 'stormpath --help' for list of available resource types."
            .format(resource))
        return -1

    try:
        auth_args = init_auth(arguments)
        client = Client(user_agent=USER_AGENT, **auth_args)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    if action == STATUS_ACTION:
        return 0 if AVAILABLE_ACTIONS[action](client, arguments) else -1

    try:
        res = AVAILABLE_RESOURCES[resource](client, arguments)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    act = AVAILABLE_ACTIONS[action]

    try:
        result = act(res, arguments)
    except (StormpathError, ValueError) as ex:
        log.error(str(ex))
        return -1

    if result is not None and (isinstance(result, list) or isinstance(
            result, dict) or isinstance(result, types.GeneratorType)):
        output(result,
               show_links=arguments.get('--show-links', False),
               show_headers=arguments.get('--show-headers', False),
               output_json=arguments.get('--output-json', False))
    return 0
Example #4
0
def main():
    arguments = docopt(__doc__)
    action = arguments.get('<action>')
    resource = arguments.get('<resource>')

    log = setup_output(arguments.get('--verbose'))

    arguments.update(get_context_dict())
    arguments = strip_equal_sign(arguments)

    if resource and resource.find('=') != -1:
        # Workaround for when list command is not specified
        # and non-dash attributes are used
        arguments['<attributes>'].append(resource)
        arguments['<resource>'] = None
        resource = None

    if action in AVAILABLE_RESOURCES and not resource:
        resource = action
        action = DEFAULT_ACTION

    if not action:
        log.error(__doc__.strip('\n'))
        return -1

    if action not in AVAILABLE_ACTIONS:
        log.error("Unknown action '%s'. See 'stormpath --help' for list of "
            "available actions." % action)
        return -1

    if action in LOCAL_ACTIONS:
        return 0 if AVAILABLE_ACTIONS[action](arguments) else -1

    if not resource:
        log.error("A resource type is required. Available resources: %s. "
            "Please see 'stormpath --help'" % ", ".join(sorted(AVAILABLE_RESOURCES.keys())))
        return -1

    if resource not in AVAILABLE_RESOURCES:
        log.error("Unknown resource type '%s'. See 'stormpath --help' for "
            "list of available resource types." % resource)
        return -1

    try:
        auth_args = init_auth(arguments)
        client = Client(user_agent=USER_AGENT, **auth_args)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    try:
        res = AVAILABLE_RESOURCES[resource](client, arguments)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    act = AVAILABLE_ACTIONS[action]

    try:
        result = act(res, arguments)
    except (StormpathError, ValueError) as ex:
        log.error(str(ex))
        return -1

    if result is not None and (isinstance(result, list) or isinstance(result, dict)):
        output(result,
            show_links=arguments.get('--show-links', False))
    return 0
Example #5
0
def main():
    arguments = docopt(__doc__)
    action = arguments.get('<action>')
    resource = arguments.get('<resource>')

    log = setup_output(arguments.get('--verbose'))

    arguments.update(get_context_dict())
    arguments, resource, action = find_non_dash_arguments_and_default_action(arguments, resource, action)
    arguments = properly_support_boolean_values(arguments)
    arguments = check_primary_identifier_without_flags(arguments, resource, action)

    if not action:
        log.error(__doc__.strip('\n'))
        return -1

    if action == 'help':
        log.error(__doc__.strip('\n'))
        return -1

    if action not in AVAILABLE_ACTIONS:
        log.error("Unknown action '{}'. See 'stormpath --help' for list of available actions.".format(action))
        return -1

    if action in LOCAL_ACTIONS:
        return 0 if AVAILABLE_ACTIONS[action](arguments) else -1

    if not resource and action != STATUS_ACTION:
        if action == SET_ACTION:
            log.error("A resource type is required. Available resources for the set command are: application, directory. Please see 'stormpath --help'")
            return -1

        log.error("A resource type is required. Available resources: {}. Please see 'stormpath --help'".format(', '.join(sorted(AVAILABLE_RESOURCES.keys()))))
        return -1

    if resource not in AVAILABLE_RESOURCES and action != STATUS_ACTION:
        log.error("Unknown resource type '{}'. See 'stormpath --help' for list of available resource types.".format(resource))
        return -1

    try:
        auth_args = init_auth(arguments)
        client = Client(user_agent=USER_AGENT, **auth_args)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    if action == STATUS_ACTION:
        return 0 if AVAILABLE_ACTIONS[action](client, arguments) else -1

    try:
        res = AVAILABLE_RESOURCES[resource](client, arguments)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    act = AVAILABLE_ACTIONS[action]

    try:
        result = act(res, arguments)
    except (StormpathError, ValueError) as ex:
        log.error(str(ex))
        return -1

    if result is not None and (
            isinstance(result, list) or
            isinstance(result, dict) or
            isinstance(result, types.GeneratorType)):
        output(
            result, show_links=arguments.get('--show-links', False),
            show_headers=arguments.get('--show-headers', False),
            output_json=arguments.get('--output-json', False))
    return 0
Example #6
0
def main():
    arguments = docopt(__doc__)
    action = arguments.get('<action>')
    resource = arguments.get('<resource>')

    log = setup_output(arguments.get('--verbose'))

    arguments.update(get_context_dict())
    arguments = strip_equal_sign(arguments)

    if resource and resource.find('=') != -1:
        # Workaround for when list command is not specified
        # and non-dash attributes are used
        arguments['<attributes>'].append(resource)
        arguments['<resource>'] = None
        resource = None

    if action in AVAILABLE_RESOURCES and not resource:
        resource = action
        action = DEFAULT_ACTION

    if not action:
        log.error(__doc__.strip('\n'))
        return -1

    if action not in AVAILABLE_ACTIONS:
        log.error("Unknown action '%s'. See 'stormpath --help' for list of "
                  "available actions." % action)
        return -1

    if action in LOCAL_ACTIONS:
        return 0 if AVAILABLE_ACTIONS[action](arguments) else -1

    if not resource:
        log.error("A resource type is required. Available resources: %s. "
                  "Please see 'stormpath --help'" %
                  ", ".join(sorted(AVAILABLE_RESOURCES.keys())))
        return -1

    if resource not in AVAILABLE_RESOURCES:
        log.error("Unknown resource type '%s'. See 'stormpath --help' for "
                  "list of available resource types." % resource)
        return -1

    try:
        auth_args = init_auth(arguments)
        client = Client(user_agent=USER_AGENT, **auth_args)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    try:
        res = AVAILABLE_RESOURCES[resource](client, arguments)
    except ValueError as ex:
        log.error(str(ex))
        return -1

    act = AVAILABLE_ACTIONS[action]

    try:
        result = act(res, arguments)
    except (StormpathError, ValueError) as ex:
        log.error(str(ex))
        return -1

    if result is not None and (isinstance(result, list)
                               or isinstance(result, dict)):
        output(result, show_links=arguments.get('--show-links', False))
    return 0