update.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:alfred-10000ft-scripts 作者: jceelen 项目源码 文件源码
def main(wf):
    ####################################################################
    # Get and Parse arguments
    ####################################################################

    # Build argument parser to parse script args and collect their values
    parser = argparse.ArgumentParser()

    # Check if the a force argument is parced and set the max_age
    parser.add_argument('--update', dest='update_method',
                        nargs='?', default='normal')

    args = parser.parse_args(wf.args)

    wf.logger.info('update_method = ' + args.update_method)

    ####################################################################
    # Run argument-specific actions
    ####################################################################

    if args.update_method == 'force':
        max_age = 1
    else:
        max_age = 600

    ####################################################################
    # Get data the data from 10.000ft
    ####################################################################

    try:
        # Get API key from Keychain
        api_key = wf.get_password('10k_api_key')

        # Retrieve projects from cache if available and no more than 600
        # seconds old

        def wrapper():
            """`cached_data` can only take a bare callable (no args),
            so we need to wrap callables needing arguments in a function
            that needs none.
            """
            return get_projects(api_key)

        # Get the new data
        projects = wf.cached_data('projects', wrapper, max_age=max_age)
        # Record our progress in the log file
        wf.logger.info('{} projects cached, max_age {} second(s)'.format(
            len(projects), max_age))

    except PasswordNotFound:  # API key has not yet been set
        # Nothing we can do about this, so just log it
        wf.logger.error('No API key saved')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号