示例#1
0
def retrieve_user_info(usernames):
    while usernames:
        try:
            with open(os.devnull, 'w') as devnull:
                with contextlib.redirect_stdout(devnull):
                    users = twapi.UsersLookup(
                            screen_name=usernames[:USERS_PER_REQ],
                            include_entities=False)
        except Exception:
            cl.error('Error: %s' % get_exc_line())
            continue

        for user in users:
            yield {
                'id': user.id_str,
                'screen_name': user.screen_name,
                'name': merge_whitespaces(user.name),
                'favourites_count': user.favourites_count,
                'followers_count': user.followers_count,
                'friends_count': user.friends_count,
                'listed_count': user.listed_count,
                'statuses_count': user.statuses_count,
                'verified': user.verified,
                'created_at': user.created_at,
                'lang': user.lang,
                'location': merge_whitespaces(user.location),
                'avatar': user.profile_image_url_https,
                'description': merge_whitespaces(user.description)
            }

        usernames = usernames[USERS_PER_REQ:]
示例#2
0
def die(msg):
    cl.error(msg)
    if threading.active_count() > 1:
        # We need os._exit() to terminate all threads.
        os._exit(1)  # pylint: disable=protected-access
    else:
        sys.exit(1)
示例#3
0
def retry_until_success(func, *args, **kwargs):
    MAX_RETRY = 2
    retry = 0
    ret = None

    while True:
        try:
            try:
                ret = func(*args, **kwargs)
            except KeyboardInterrupt:
                raise
            except Exception:
                if retry >= MAX_RETRY:
                    cl.error('Error: %sMax retries exceeded, terminating.' %
                             traceback.format_exc())
                    raise

                cl.error('Error: %sRetrying...' % traceback.format_exc())
                retry += 1
            else:
                return ret
        except KeyboardInterrupt:
            cl.warning('User hit Ctrl-C, terminating function %r' %
                       func.__name__)
            raise
示例#4
0
def demo2():
    cl.section('Demo 2')

    username = ''
    while not username:
        username = cl.input('Username: '******''
    while not password:
        password = cl.password('Password: '******'Successfully logged in.')

    with cl.progress('Checking for update...', mode=cl.PROGRESS_SPIN):
        time.sleep(3)

    choice = ''
    while choice.lower() not in {'y', 'n'}:
        choice = cl.question(
            'A new version is present, would you like to update? (Y/N)').strip(
            )

    if choice.lower() == 'y':
        with cl.progress('Downloading ', mode=cl.PROGRESS_DETERMINATE) as p:
            time.sleep(1)
            p.update(0.2, ' 20% (1MB/5MB) ETA 4s')
            time.sleep(2)
            p.update(0.4, ' 40% (2MB/5MB) ETA 3s')

        cl.error('Failed to download package. SSL handshake error.')
    else:
        cl.warning('Update delayed!')
示例#5
0
def demo1():
    cl.section('Demo 1')

    cl.info('Test program started.')

    with cl.progress('Running test case 1...', cl.PROGRESS_SPIN, erase=True):
        time.sleep(3)
    cl.success('Test case 1: Passed')

    with cl.progress('Running test case 2...', cl.PROGRESS_SPIN, erase=True):
        time.sleep(3)
    cl.success('Test case 2: Passed')

    with cl.progress('Running test case 3...', cl.PROGRESS_SPIN, erase=True):
        time.sleep(3)
    cl.success('Test case 3: Passed')

    with cl.progress('Running test case 4...', cl.PROGRESS_SPIN, erase=True):
        time.sleep(3)
    cl.error('Test case 4: Failed')

    cl.info('Input: 1111')
    cl.info('Expected output: 2222')
    cl.info('Got: 3333')

    cl.section('Test Result')
    cl.info('3 out of 4 test cases passed.')
    cl.info('Pass rate: 75%')
示例#6
0
def run_linter(linter: Linter) -> bool:
    linter_name = linter['name']
    cl.progress('Running linter {}'.format(linter_name))
    result = subprocess.call(linter['command'])  # nosec
    if result == 0:
        cl.success('Linter {} success'.format(linter_name))
        return True
    cl.error('Linter {} failed'.format(linter_name))
    return False
示例#7
0
def overview():
    cl.section('Overview of Labels')
    cl.success('Good job! All test cases passed!')
    cl.warning('Warning! Security update delayed!')
    cl.error('Error! Failed to write file!')
    cl.info('Server listening on port 8888.')
    cl.progress('Downloading package, please wait...')
    cl.plain('Nothing interesting.')
    cl.question('A new version is present, would you like to update? (Y/N)')
示例#8
0
def get_callback():
    while True:
        callback = get_input('Callback: ')
        try:
            global_check(callback)
            check_identifier(callback)
        except ValidationError:
            cl.error('Invalid callback')
            continue
        return callback
示例#9
0
def get_level_number():
    while True:
        level_number = get_input('Level: ')
        try:
            global_check(level_number)
            level_number = int(level_number)
            check(0 <= level_number <= NUM_LEVELS)
        except (ValidationError, ValueError):
            cl.error('Invalid level number')
            continue
        return level_number
示例#10
0
def data_retriever(data_source,
                   query,
                   save_filename,
                   *,
                   lang='',
                   proxy=None,
                   remove_duplicates=False,
                   twapi_max=None,
                   twapi_sleep_time=0,
                   twscrape_poolsize=20,
                   twscrape_begindate=None,
                   ghapi_org=None,
                   ghapi_since=None,
                   soapi_begindate=None):
    cl.section('Data Retriever')
    cl.info('Starting to retrieve query: %s, or org: %s' % (query, ghapi_org))
    cl.info('From data source: %s' % data_source)
    cl.info('Using proxy: %s' % proxy)
    cl.info('Remove duplicates: %s' % remove_duplicates)

    if proxy:
        os.environ['HTTP_PROXY'] = proxy
        os.environ['HTTPS_PROXY'] = proxy

    if data_source == 'twitter_standard_api':
        data = twapi_search(query,
                            twapi_max,
                            sleep_time=twapi_sleep_time,
                            lang=lang)
    elif data_source == 'twitterscraper':
        data = twscrape_search(query,
                               lang=lang,
                               poolsize=twscrape_poolsize,
                               begindate=twscrape_begindate)
    elif data_source == 'github_api':
        data = github_issue_org_fetch(ghapi_org, ghapi_since)
    elif data_source == 'stackoverflow_api':
        data = soapi_search(query, begindate=soapi_begindate)
    else:
        cl.error('Data source %r is not implemented' % data_source)
        sys.exit(-1)

    if remove_duplicates:
        data = iterator_aggregate_list(data)
        data_no_duplicate_text = remove_duplicate_text(data)
        cl.info('Exporting data without duplicate text')
        export_csv(data_no_duplicate_text, data_source_file(save_filename))

        save_filename_full = name_with_title_suffix(save_filename, '-full')
        cl.info('Exporting full data')
        export_csv(data, data_source_file(save_filename_full))
    else:
        export_csv(data, data_source_file(save_filename))
    def wrapper(*args, **kwargs):
        p = cl.progress(f"Running test case '{func.__name__}'...",
                        cl.PROGRESS_SPIN,
                        erase=True)

        try:
            func(*args, **kwargs)
        except:
            p.stop()
            cl.error(f"Test case '{func.__name__}' failed.")
            raise
        else:
            p.stop()
            cl.success(f"Test case '{func.__name__}' passed.")
示例#12
0
def main():
    cl.section('Welcome to Python Challenges')
    cl.info(f'Python version: {PYTHON_VERSION}')
    level_status = [False] * NUM_LEVELS
    while True:
        show_level_stats(level_status)

        cl.info(f'Enter a level number (1-{NUM_LEVELS}) to solve a level, '
                'or enter 0 to view source code')
        level_number = get_level_number()

        if level_number == 0:
            print(SOURCE, end='')
            continue

        if level_status[level_number - 1]:
            cl.success('You already solved this level')
            continue

        level_func = globals()[f'level_{level_number}']
        answer = get_input(f'Your answer for level {level_number}: ')

        timer = threading.Timer(CHALLENGE_TIMEOUT, die, args=('Timeout!', ))
        timer.start()

        try:
            global_check(answer)
            answer = ast.literal_eval(answer.strip())
        except Exception:  # pylint: disable=broad-except
            timer.cancel()
            cl.error('Wrong answer')
            if DEBUG_MODE:
                traceback.print_exc(file=sys.stdout)
            continue

        try:
            level_func(answer)
        except Exception:  # pylint: disable=broad-except
            timer.cancel()
            cl.error('Wrong answer')
            if DEBUG_MODE:
                traceback.print_exc(file=sys.stdout)
            continue

        timer.cancel()
        cl.success('Correct answer')
        level_status[level_number - 1] = True
示例#13
0
import sys

import colorlabels as cl

from service.auth import register
from util import validate_password, validate_username

if __name__ == '__main__':
    if len(sys.argv) < 3:
        cl.warning('Usage: %s username password' % sys.argv[0])
        sys.exit(-1)

    username = sys.argv[1]
    password = sys.argv[2]

    r = validate_username(username)
    if not r:
        cl.error(str(r))
        sys.exit(-1)

    r = validate_password(password)
    if not r:
        cl.error(str(r))
        sys.exit(-1)

    if register(username, password):
        cl.success('Successfully registered user %r.' % username)
    else:
        cl.error('User %r already exists!' % username)
示例#14
0
    {
        'name': 'Bandit',
        'command': ['bandit', '-c', 'bandit.yml', '-r', '.'],
    },
    {
        'name': 'Vermin',
        'command': ['vermin', '.'],
    },
]  # type: List[Linter]


def run_linter(linter: Linter) -> bool:
    linter_name = linter['name']
    cl.progress('Running linter {}'.format(linter_name))
    result = subprocess.call(linter['command'])  # nosec
    if result == 0:
        cl.success('Linter {} success'.format(linter_name))
        return True
    cl.error('Linter {} failed'.format(linter_name))
    return False


# Avoid short-circuiting to show all linter output at the same time.
all_results = [run_linter(linter) for linter in linters]  # type: List[bool]

if all(all_results):
    cl.success('All linters success')
else:
    cl.error('Some linters failed, check output for more information')
    sys.exit(1)