示例#1
0
文件: render.py 项目: zhiiker/FarBox
def render_template_for_farbox_bucket_by_gevent(**kwargs):
    #appctx = _app_ctx_stack.top
    reqctx = _request_ctx_stack.top
    result_html = get_result_by_gevent_with_timeout_block(
        curry(render_template_for_farbox_bucket_with_context, reqctx=reqctx, **kwargs),
        timeout = 3,
        fallback_function = curry(render_html_content_for_timeout_page, reqctx=reqctx),
        auto_kill = True,
        raise_error = True,
    )
    return result_html
示例#2
0
def auto_update_bucket_and_get_files_info(bucket, return_data=True):
    current_id = get_bucket_last_record_id(bucket)
    old_id = get_bucket_last_record_id_computed(bucket)
    if current_id == old_id:
        if return_data:
            return get_bucket_files_configs(bucket)
        else:
            return
    if return_data:
        files_info = get_result_by_gevent_with_timeout_block(
            curry(update_bucket_files_info, bucket, current_id),
            timeout=5, fallback_function=curry(get_bucket_files_configs, bucket))
        return files_info
    else:
        # 不返回结果的话,就直接异步处理
        spawn(update_bucket_files_info, bucket, current_id)
示例#3
0
文件: sync.py 项目: flottant/FarBox
def sync_folder_simply(
    node,
    root,
    private_key,
    should_encrypt_file=False,
    app_name_for_sync=None,
    auto_clean_bucket=True,
    exclude_rpath_func=None,
):
    app_name_for_sync = app_name_for_sync or 'farbox_bucket'
    should_sync_file_func = should_sync_file_for_sync_folder_simply
    if exclude_rpath_func:
        should_sync_file_func = curry(should_sync_file_for_sync_folder_simply,
                                      extra_func=exclude_rpath_func)
    worker = FarBoxBucketSyncWorker(
        server_node=node,
        root=root,
        private_key=private_key,
        should_encrypt_file=should_encrypt_file,
        app_name_for_sync=app_name_for_sync,
        should_sync_file_func=should_sync_file_func,
        auto_clean_bucket=auto_clean_bucket,
    )
    changed = worker.sync()
    return changed
示例#4
0
def replace_vars_in_scss(raw_content, new_vars, compile_scss=False):
    if not raw_content:  # return nothing
        return ''
    new_content = scss_key_value_compiler.sub(
        curry(replace_var_in_scss_func, new_vars=new_vars), raw_content)
    if compile_scss:  # 编译为普通的css内容
        new_content = compile_css_with_timeout(new_content)
        raw_version = get_md5(raw_content)
        new_content = '/*%s*/\n%s' % (
            raw_version, new_content
        )  # compile 之后,保留原始内容的version,这样后续,可以进行对比,是否需要更新
    return new_content
示例#5
0
def cache_result(*args, **kwargs):
    prefix = 'cached_by_func_'

    # @cache_result这样直接用
    if len(args) == 1 and hasattr(args[0], '__call__'):
        func = args[0]
        cache_name = prefix + func.func_name
        return cache_wrapper(func, cache_name)

    # @cache_result('key_value')
    elif len(args) == 1 and isinstance(args[0], string_types):
        cache_name = prefix + args[0]
        return curry(cache_wrapper, cache_name=cache_name)
def re_get_html_content_for_wiki_links(post_doc,
                                       html_content=None,
                                       tag_url_prefix=None,
                                       url_root=None,
                                       url_prefix=None,
                                       hit_url_path=False):
    if not isinstance(post_doc, dict):
        return ""
    if html_content is None:
        html_content = post_doc.get("content") or ""
    new_html_content = re.sub(
        '<a href="(/__wiki_(?:link|tag)/.*?)"',
        curry(re_get_html_content_for_wiki_links_replacer,
              post_doc=post_doc,
              tag_url_prefix=tag_url_prefix,
              url_root=url_root,
              url_prefix=url_prefix,
              hit_url_path=hit_url_path), html_content)
    return new_html_content
示例#7
0
def compute_content_with_referred_docs(post_doc,
                                       html_content=None,
                                       show_date=True,
                                       url_prefix=None,
                                       url_root=None,
                                       hit_url_path=False):
    path = get_path_from_record(post_doc)
    if not path:
        return ""
    content = html_content or post_doc.get("content") or ""
    new_content = re.sub(
        r"""(<[^<>]+>)?<a [^<>]*?href=['"](.*?)['"][^<>]*?>.*?</a>(</\w+>|<\w+ */>)?""",
        curry(
            re_sub_for_referred_docs,
            show_date=show_date,
            post_doc=post_doc,
            url_prefix=url_prefix,
            url_root=url_root,
            hit_url_path=hit_url_path,
        ), content)
    return new_content
示例#8
0
def get_attr_func(obj, attr, functions):
    # 在一个指定的functions 的 dict 中,试图取得obj.attr 时,返回对应的 function(如果有的话)
    first_arg = None  # 被调用的时候隐含的参数,比如posts.sort_by_date, 其中实际调用的是posts.sort 这个属性
    original_func = None
    if attr in functions:
        original_func = functions[attr]
    elif '_by_' in attr:  # posts.sort_by_date('-')
        name, first_arg = attr.split('_by_', 1)
        if name in functions:
            original_func = functions[name]
    if original_func:
        wrapped_func = curry(original_func, obj)
        # 不能被 wrap 的函数
        if hasattr(original_func, 'arg_spec'):
            # 比如返回一个 dict(如 humech),就不能是一个 function 了,或者指定了就是一个属性
            args_length = len(original_func.arg_spec.args)
            if args_length == 1 and not original_func.arg_spec.varargs and not original_func.arg_spec.keywords:
                # 原函数仅仅接收一个参数
                return wrapped_func()
            elif not args_length:
                # 不接受任何参数的, curry 都是不行的, 其实就是相当于一个属性(的函数运行)
                return original_func()
        return wrapped_func