Ejemplo n.º 1
0
def test_get_fankit_exists(tmpdir):
    tempdir_root = tmpdir.mkdir('test_get_fankit_exists')
    fake_tempdir = tempdir_root.mkdir('tempdir')
    fake_output_dir = tempdir_root.mkdir('output_dir')
    fake_output_dir.mkdir(fankit.FANKIT_DIR_NAME)

    fankit.get_fankit(fake_tempdir.realpath(), fake_output_dir.realpath(), True)

    assert fake_tempdir.join(fankit.FANKIT_DIR_NAME).check(dir=1)
Ejemplo n.º 2
0
def test_get_fankit_download_fail(tmpdir, requests_mock, capsys):
    requests_mock.get('mock://foo', status_code=500)

    tempdir_root = tmpdir.mkdir('test_get_fankit_download')
    fake_tempdir = tempdir_root.mkdir('tempdir')
    fake_output_dir = tempdir_root.mkdir('output_dir')

    fankit.get_fankit(fake_tempdir.realpath(), fake_output_dir.realpath(), True)

    assert fake_tempdir.join(fankit.FANKIT_DIR_NAME).check(dir=0)
Ejemplo n.º 3
0
def test_get_fankit_download_no_redirect(tmpdir, requests_mock, capsys):
    requests_mock.get(fankit.FANKIT_URL, status_code=200)

    tempdir_root = tmpdir.mkdir('test_get_fankit_download')
    fake_tempdir = tempdir_root.mkdir('tempdir')
    fake_output_dir = tempdir_root.mkdir('output_dir')

    fankit.get_fankit(fake_tempdir.realpath(), fake_output_dir.realpath(), True)

    #assert False
    assert fake_tempdir.join(fankit.FANKIT_DIR_NAME).check(dir=0)
Ejemplo n.º 4
0
def test_get_fankit_download_not_happen_when_flag_false(tmpdir):
    test_fankit_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fankit.zip')

    with open(test_fankit_path, mode='rb') as file:
        test_fankit_content = file.read()

    tempdir_root = tmpdir.mkdir('test_get_fankit_download')
    fake_tempdir = tempdir_root.mkdir('tempdir')
    fake_output_dir = tempdir_root.mkdir('output_dir')

    fankit.get_fankit(fake_tempdir.realpath(), fake_output_dir.realpath(), False)

    assert fake_tempdir.join(fankit.FANKIT_DIR_NAME).check(dir=0)
Ejemplo n.º 5
0
def test_get_fankit_download(tmpdir, requests_mock):
    test_fankit_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fankit.zip')

    with open(test_fankit_path, mode='rb') as file:
        test_fankit_content = file.read()

    redirect_headers = {
        'Location': fankit.FANKIT_URL
    }
    requests_mock.get(fankit.FANKIT_URL, headers=redirect_headers, content=test_fankit_content, status_code=200)

    tempdir_root = tmpdir.mkdir('test_get_fankit_download')
    fake_tempdir = tempdir_root.mkdir('tempdir')
    fake_output_dir = tempdir_root.mkdir('output_dir')

    fankit.get_fankit(fake_tempdir.realpath(), fake_output_dir.realpath(), True)

    assert fake_tempdir.join(fankit.FANKIT_DIR_NAME).check(dir=1)
Ejemplo n.º 6
0
def build_dashboard(config):  # pragma: no coverage
    """Compile and render clan dashboard."""

    logger.debug('crtools version v{}'.format(__version__))
    logger.debug('pyroyale version v{}'.format(pyroyale.__version__))
    logger.debug(config)

    # Create temporary directory. All file writes, until the very end,
    # will happen in this directory, so that no matter what we do, it
    # won't hose existing stuff.
    tempdir = tempfile.mkdtemp(config['paths']['temp_dir_name'])

    # Putting everything in a `try`...`finally` to ensure `tempdir` is removed
    # when we're done. We don't want to pollute the user's disk.
    try:
        output_path = os.path.expanduser(config['paths']['out'])

        # Get clan data and war log from API.
        api = pyroyale.ClashRoyaleAPI(config['api']['api_key'],
                                      config['api']['clan_id'])
        clan = api.clan.clan_info()
        warlog = api.clan.warlog()
        current_war = api.clan.current_war()

        # process data from API
        current_war_processed = process_current_war(config, current_war)
        clan_processed = process_clan(config, clan, current_war)
        member_history = history.get_member_history(
            clan['memberList'], io.get_previous_history(output_path),
            current_war)
        members_processed = process_members(config, clan, warlog, current_war,
                                            member_history)
        recent_wars = process_recent_wars(config, warlog)
        former_members = process_absent_members(config,
                                                member_history['members'])

        io.parse_templates(
            config, member_history, tempdir, clan_processed, members_processed,
            former_members, current_war_processed, recent_wars,
            get_suggestions(config, members_processed, clan_processed),
            get_scoring_rules(config))

        if (config['crtools']['debug'] == True):
            # archive outputs of API for debugging
            io.dump_debug_logs(
                tempdir, {
                    'clan': clan,
                    'warlog': warlog,
                    'currentwar': current_war,
                    'clan-processed': clan_processed,
                    'members-processed': members_processed,
                    'currentwar-processed': current_war_processed,
                    'recentwars-processed': recent_wars
                })

        if config['paths']['use_fankit']:
            fankit.get_fankit(tempdir, output_path)

        io.copy_static_assets(tempdir, config['paths']['clan_logo'],
                              config['paths']['favicon'])

        io.move_temp_to_output_dir(tempdir, output_path)

    except pyroyale.ClashRoyaleAPIAuthenticationError as e:
        msg = 'developer.clashroyale.com authentication error: {}'.format(e)
        if not config['api']['api_key']:
            msg += '\n - API key not provided'
        else:
            msg += '\n - API key not valid'
        logger.error(msg)

    except pyroyale.ClashRoyaleAPIClanNotFound as e:
        logger.error('developer.clashroyale.com: {}'.format(e))

    except pyroyale.ClashRoyaleAPIError as e:
        logger.error('developer.clashroyale.com error: {}'.format(e))

    except pyroyale.ClashRoyaleAPIMissingFieldsError as e:
        logger.error('error: {}'.format(e))

    finally:
        # Ensure that temporary directory gets deleted no matter what
        shutil.rmtree(tempdir)
Ejemplo n.º 7
0
def build_dashboard(config):  # pragma: no coverage
    """Compile and render clan dashboard."""

    logger.debug('crtools version v{}'.format(__version__))
    #logger.debug('pyroyale version v{}'.format(pyroyale.__version__))
    logger.debug(config)

    # Create temporary directory. All file writes, until the very end,
    # will happen in this directory, so that no matter what we do, it
    # won't hose existing stuff.
    tempdir = tempfile.mkdtemp(config['paths']['temp_dir_name'])

    # get API instance
    configuration = pyroyale.Configuration()
    configuration.api_key['authorization'] = config['api']['api_key']
    api = pyroyale.ClansApi(pyroyale.ApiClient(configuration))

    # Putting everything in a `try`...`finally` to ensure `tempdir` is removed
    # when we're done. We don't want to pollute the user's disk.
    try:
        output_path = os.path.expanduser(config['paths']['out'])

        # Get clan data and war log from API.
        clan = api.get_clan(config['api']['clan_id']).to_dict()
        warlog = api.get_clan_war_log(config['api']['clan_id']).to_dict()
        current_war = api.get_current_war(config['api']['clan_id']).to_dict()

        # process data from API
        current_war_processed = process_current_war(config, current_war)
        clan_processed = process_clan(config, clan, current_war)
        member_history = history.get_member_history(
            clan['member_list'], io.get_previous_history(output_path),
            current_war)
        members_processed = process_members(config, clan, warlog, current_war,
                                            member_history)
        recent_wars = process_recent_wars(config, warlog)
        former_members = process_absent_members(config,
                                                member_history['members'])

        io.parse_templates(
            config, member_history, tempdir, clan_processed, members_processed,
            former_members, current_war_processed, recent_wars,
            get_suggestions(config, members_processed, clan_processed),
            get_scoring_rules(config))

        if (config['crtools']['debug'] == True):
            # archive outputs of API for debugging
            io.dump_debug_logs(
                tempdir, {
                    'clan': clan,
                    'warlog': warlog,
                    'currentwar': current_war,
                    'clan-processed': clan_processed,
                    'members-processed': members_processed,
                    'currentwar-processed': current_war_processed,
                    'recentwars-processed': recent_wars
                })

        # if fankit is previously downloaded, it will copy fankit. Otherwise,
        # if fankit is enabled, it will download it.
        fankit.get_fankit(tempdir, output_path, config['paths']['use_fankit'])

        io.copy_static_assets(tempdir, config['paths']['clan_logo'],
                              config['paths']['favicon'])

        io.move_temp_to_output_dir(tempdir, output_path)

    except ApiException as e:
        logger.error('error: {}'.format(e))

    finally:
        # Ensure that temporary directory gets deleted no matter what
        shutil.rmtree(tempdir)
def build_dashboard(config):  # pragma: no coverage
    """Compile and render clan dashboard."""

    print('- requesting info for clan id: {}'.format(config['api']['clan_id']))

    clan, warlog, current_war = get_data_from_api(config)

    # Create temporary directory. All file writes, until the very end,
    # will happen in this directory, so that no matter what we do, it
    # won't hose existing stuff.
    tempdir = tempfile.mkdtemp(config['paths']['temp_dir_name'])

    # Putting everything in a `try`...`finally` to ensure `tempdir` is removed
    # when we're done. We don't want to pollute the user's disk.
    try:
        output_path = os.path.expanduser(config['paths']['out'])

        # process data from API
        current_war_processed = ProcessedCurrentWar(current_war, config)
        clan_processed = ProcessedClan(clan, current_war_processed, config)

        member_history = history.get_member_history(
            clan.member_list, config['crtools']['timestamp'],
            io.get_previous_history(output_path), current_war_processed)

        members_processed = process_members(config, clan, warlog,
                                            current_war_processed,
                                            member_history)
        recent_wars = process_recent_wars(config, warlog)
        former_members = process_absent_members(config,
                                                member_history['members'])

        io.parse_templates(
            config, member_history, tempdir, clan_processed, members_processed,
            former_members, current_war_processed, recent_wars,
            get_suggestions(config, members_processed,
                            clan_processed.required_trophies),
            get_scoring_rules(config))

        if (config['crtools']['debug'] == True):
            # archive outputs of API for debugging
            io.dump_debug_logs(
                tempdir, {
                    'clan':
                    clan.to_dict(),
                    'warlog':
                    warlog.to_dict(),
                    'current_war':
                    current_war.to_dict(),
                    'clan-processed':
                    clan_processed,
                    'members-processed':
                    members_processed,
                    'current_war-processed':
                    current_war_processed,
                    'recentwars-processed':
                    list(map(lambda war: war.to_dict(), recent_wars))
                })

        # if fankit is previously downloaded, it will copy fankit. Otherwise,
        # if fankit is enabled, it will download it.
        fankit.get_fankit(tempdir, output_path, config['paths']['use_fankit'])

        io.copy_static_assets(tempdir, config['paths']['clan_logo'],
                              config['paths']['favicon'])

        io.move_temp_to_output_dir(tempdir, output_path)

        discord.trigger_webhooks(config, current_war, members_processed)

    except Exception as e:
        logger.error('error: {}'.format(e))

    finally:
        # Ensure that temporary directory gets deleted no matter what
        shutil.rmtree(tempdir)