jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def task_remove(request, task_id):
    """
    remove task by task_id
    :param request:
    :return:
    """
    if request.method == 'POST':
        # delete job from DjangoJob
        task = Task.objects.get(id=task_id)
        clients = clients_of_task(task)
        for client in clients:
            job_id = get_job_id(client, task)
            DjangoJob.objects.filter(name=job_id).delete()
        # delete task
        Task.objects.filter(id=task_id).delete()
        return JsonResponse({'result': '1'})
//  [python]
def task_remove(request, task_id):
    """
    remove task by task_id
    :param request:
    :return:
    """
    if request.method == 'POST':
        try:
            # delete job from DjangoJob
            task = Task.objects.get(id=task_id)
            clients = clients_of_task(task)
            for client in clients:
                job_id = get_job_id(client, task)
                DjangoJob.objects.filter(name=job_id).delete()
            # delete task
            Task.objects.filter(id=task_id).delete()
            return JsonResponse({'result': '1'})
        except:
            return JsonResponse({'result': '0'})
//  [python]
def func_begin(self, name):
    ctype = get_c_type(name)
    self.emit("PyObject*", 0)
    self.emit("ast2obj_%s(void* _o)" % (name), 0)
    self.emit("{", 0)
    self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
    self.emit("PyObject *result = NULL, *value = NULL;", 1)
    self.emit('if (!o) {', 1)
    self.emit("Py_RETURN_NONE;", 2)
    self.emit("}", 1)
    self.emit('', 0)
//  [python]
def func_begin(self, name):
    ctype = get_c_type(name)
    self.emit("PyObject*", 0)
    self.emit("ast2obj_%s(void* _o)" % (name), 0)
    self.emit("{", 0)
    self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
    self.emit("PyObject *result = NULL, *value = NULL;", 1)
    self.emit('if (!o) {', 1)
    self.emit("Py_INCREF(Py_None);", 2)
    self.emit('return Py_None;', 2)
    self.emit("}", 1)
    self.emit('', 0)
//  [python]
def visitField(self, field, name, sum=None, prod=None, depth=0):
    ctype = get_c_type(field.type)
    self.emit("if (lookup_attr_id(obj, &PyId_%s, &tmp) < 0) {" % field.name, depth)
    self.emit("return 1;", depth+1)
    self.emit("}", depth)
    if not field.opt:
        self.emit("if (tmp == NULL) {", depth)
        message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
        format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
        self.emit(format % message, depth+1, reflow=False)
        self.emit("return 1;", depth+1)
    else:
        self.emit("if (tmp == NULL || tmp == Py_None) {", depth)
        self.emit("Py_CLEAR(tmp);", depth+1)
        if self.isNumeric(field):
            self.emit("%s = 0;" % field.name, depth+1)
        elif not self.isSimpleType(field):
            self.emit("%s = NULL;" % field.name, depth+1)
        else:
            raise TypeError("could not determine the default value for %s" % field.name)
    self.emit("}", depth)
    self.emit("else {", depth)

    self.emit("int res;", depth+1)
    if field.seq:
        self.emit("Py_ssize_t len;", depth+1)
        self.emit("Py_ssize_t i;", depth+1)
        self.emit("if (!PyList_Check(tmp)) {", depth+1)
        self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
                  "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
                  (name, field.name),
                  depth+2, reflow=False)
        self.emit("goto failed;", depth+2)
        self.emit("}", depth+1)
        self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
        if self.isSimpleType(field):
            self.emit("%s = _Ta3_asdl_int_seq_new(len, arena);" % field.name, depth+1)
        else:
            self.emit("%s = _Ta3_asdl_seq_new(len, arena);" % field.name, depth+1)
        self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
        self.emit("for (i = 0; i < len; i++) {", depth+1)
        self.emit("%s val;" % ctype, depth+2)
        self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
                  field.type, depth+2, reflow=False)
        self.emit("if (res != 0) goto failed;", depth+2)
        self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
        self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
                  "changed size during iteration\");" %
                  (name, field.name),
                  depth+3, reflow=False)
        self.emit("goto failed;", depth+3)
        self.emit("}", depth+2)
        self.emit("asdl_seq_SET(%s, i, val);" % field.name, depth+2)
        self.emit("}", depth+1)
    else:
        self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
                  (field.type, field.name), depth+1)
        self.emit("if (res != 0) goto failed;", depth+1)

    self.emit("Py_CLEAR(tmp);", depth+1)
    self.emit("}", depth)
//  [python]
def visitField(self, field, name, sum=None, prod=None, depth=0):
    ctype = get_c_type(field.type)
    if field.opt:
        check = "exists_not_none(obj, &PyId_%s)" % (field.name,)
    else:
        check = "_PyObject_HasAttrId(obj, &PyId_%s)" % (field.name,)
    self.emit("if (%s) {" % (check,), depth, reflow=False)
    self.emit("int res;", depth+1)
    if field.seq:
        self.emit("Py_ssize_t len;", depth+1)
        self.emit("Py_ssize_t i;", depth+1)
    self.emit("tmp = _PyObject_GetAttrId(obj, &PyId_%s);" % field.name, depth+1)
    self.emit("if (tmp == NULL) goto failed;", depth+1)
    if field.seq:
        self.emit("if (!PyList_Check(tmp)) {", depth+1)
        self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
                  "be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
                  (name, field.name),
                  depth+2, reflow=False)
        self.emit("goto failed;", depth+2)
        self.emit("}", depth+1)
        self.emit("len = PyList_GET_SIZE(tmp);", depth+1)
        if self.isSimpleType(field):
            self.emit("%s = _Ta3_asdl_int_seq_new(len, arena);" % field.name, depth+1)
        else:
            self.emit("%s = _Ta3_asdl_seq_new(len, arena);" % field.name, depth+1)
        self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
        self.emit("for (i = 0; i < len; i++) {", depth+1)
        self.emit("%s value;" % ctype, depth+2)
        self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &value, arena);" %
                  field.type, depth+2, reflow=False)
        self.emit("if (res != 0) goto failed;", depth+2)
        self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
        self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
                  "changed size during iteration\");" %
                  (name, field.name),
                  depth+3, reflow=False)
        self.emit("goto failed;", depth+3)
        self.emit("}", depth+2)
        self.emit("asdl_seq_SET(%s, i, value);" % field.name, depth+2)
        self.emit("}", depth+1)
    else:
        self.emit("res = obj2ast_%s(tmp, &%s, arena);" %
                  (field.type, field.name), depth+1)
        self.emit("if (res != 0) goto failed;", depth+1)

    self.emit("Py_CLEAR(tmp);", depth+1)
    self.emit("} else {", depth)
    if not field.opt:
        message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
        format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
        self.emit(format % message, depth+1, reflow=False)
        self.emit("return 1;", depth+1)
    else:
        if self.isNumeric(field):
            self.emit("%s = 0;" % field.name, depth+1)
        elif not self.isSimpleType(field):
            self.emit("%s = NULL;" % field.name, depth+1)
        else:
            raise TypeError("could not determine the default value for %s" % field.name)
    self.emit("}", depth)
//  [python]
def validate_and_sanitize_search_inputs(fn, instance, args, kwargs):
	kwargs.update(dict(zip(fn.__code__.co_varnames, args)))
	sanitize_searchfield(kwargs['searchfield'])
	kwargs['start'] = cint(kwargs['start'])
	kwargs['page_len'] = cint(kwargs['page_len'])

	if kwargs['doctype'] and not frappe.db.exists('DocType', kwargs['doctype']):
		return []

	return fn(**kwargs)
//  [python]
def job_list(request, client_id, project_name):
    """
    get job list of project from one client
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :return: list of jobs
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        scrapyd = get_scrapyd(client)
        result = scrapyd.list_jobs(project_name)
        jobs = []
        statuses = ['pending', 'running', 'finished']
        for status in statuses:
            for job in result.get(status):
                job['status'] = status
                jobs.append(job)
        return JsonResponse(jobs)
//  [python]
def job_list(request, client_id, project_name):
    """
    get job list of project from one client
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :return: list of jobs
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        scrapyd = get_scrapyd(client)
        try:
            result = scrapyd.list_jobs(project_name)
            jobs = []
            statuses = ['pending', 'running', 'finished']
            for status in statuses:
                for job in result.get(status):
                    job['status'] = status
                    jobs.append(job)
            return JsonResponse(jobs)
        except ConnectionError:
            return JsonResponse({'message': 'Connect Error'}, status=500)
//  [python]
def emit(self, s, depth, reflow=True):
    # XXX reflow long lines?
    if reflow:
        lines = reflow_lines(s, depth)
    else:
        lines = [s]
    for line in lines:
        if line:
            line = (" " * TABSIZE * depth) + line
        self.file.write(line + "\n")
//  [python]
def emit(self, s, depth, reflow=True):
    # XXX reflow long lines?
    if reflow:
        lines = reflow_lines(s, depth)
    else:
        lines = [s]
    for line in lines:
        line = (" " * TABSIZE * depth) + line + "\n"
        self.file.write(line)
//  [python]
def _register_function_args(context: Context, sig: FunctionSignature) -> List[IRnode]:
    ret = []

    # the type of the calldata
    base_args_t = TupleType([arg.typ for arg in sig.base_args])

    # tuple with the abi_encoded args
    if sig.is_init_func:
        base_args_ofst = IRnode(0, location=DATA, typ=base_args_t, encoding=Encoding.ABI)
    else:
        base_args_ofst = IRnode(4, location=CALLDATA, typ=base_args_t, encoding=Encoding.ABI)

    for i, arg in enumerate(sig.base_args):

        arg_ir = get_element_ptr(base_args_ofst, i)

        if needs_clamp(arg.typ, Encoding.ABI):
            # allocate a memory slot for it and copy
            p = context.new_variable(arg.name, arg.typ, is_mutable=False)
            dst = IRnode(p, typ=arg.typ, location=MEMORY)

            copy_arg = make_setter(dst, arg_ir)
            copy_arg.source_pos = getpos(arg.ast_source)
            ret.append(copy_arg)
        else:
            assert abi_encoding_matches_vyper(arg.typ)
            # leave it in place
            context.vars[arg.name] = VariableRecord(
                name=arg.name,
                pos=arg_ir,
                typ=arg.typ,
                mutable=False,
                location=arg_ir.location,
                encoding=Encoding.ABI,
            )

    return ret
//  [python]
def _register_function_args(context: Context, sig: FunctionSignature) -> List[IRnode]:
    ret = []

    # the type of the calldata
    base_args_t = TupleType([arg.typ for arg in sig.base_args])

    # tuple with the abi_encoded args
    if sig.is_init_func:
        base_args_ofst = IRnode(0, location=DATA, typ=base_args_t, encoding=Encoding.ABI)
    else:
        base_args_ofst = IRnode(4, location=CALLDATA, typ=base_args_t, encoding=Encoding.ABI)

    for i, arg in enumerate(sig.base_args):

        arg_ir = get_element_ptr(base_args_ofst, i)

        if _should_decode(arg.typ):
            # allocate a memory slot for it and copy
            p = context.new_variable(arg.name, arg.typ, is_mutable=False)
            dst = IRnode(p, typ=arg.typ, location=MEMORY)

            copy_arg = make_setter(dst, arg_ir)
            copy_arg.source_pos = getpos(arg.ast_source)
            ret.append(copy_arg)
        else:
            # leave it in place
            context.vars[arg.name] = VariableRecord(
                name=arg.name,
                pos=arg_ir,
                typ=arg.typ,
                mutable=False,
                location=arg_ir.location,
                encoding=Encoding.ABI,
            )

    return ret
//  [python]
def help_page(page_slug: str) -> str:
    """Fava's included documentation."""
    if page_slug not in HELP_PAGES:
        abort(404)
    html = markdown2.markdown_path(
        (resource_path("help") / (page_slug + ".md")),
        extras=["fenced-code-blocks", "tables", "header-ids"],
    )
    return render_template(
        "_layout.html",
        active_page="help",
        page_slug=page_slug,
        help_html=Markup(
            render_template_string(
                html,
                beancount_version=beancount_version,
                fava_version=fava_version,
            )
        ),
        HELP_PAGES=HELP_PAGES,
    )
//  [python]
def help_page(page_slug: str) -> str:
    """Fava's included documentation."""
    if page_slug not in HELP_PAGES:
        abort(404)
    html = markdown2.markdown_path(
        (resource_path("help") / (page_slug + ".md")),
        extras=["fenced-code-blocks", "tables", "header-ids"],
    )
    return render_template(
        "_layout.html",
        active_page="help",
        page_slug=page_slug,
        help_html=render_template_string(
            html,
            beancount_version=beancount_version,
            fava_version=fava_version,
        ),
        HELP_PAGES=HELP_PAGES,
    )
//  [python]
def main(srcfile, dump_module=False):
    argv0 = sys.argv[0]
    components = argv0.split(os.sep)
    argv0 = os.sep.join(components[-2:])
    auto_gen_msg = common_msg % argv0
    mod = asdl.parse(srcfile)
    if dump_module:
        print('Parsed Module:')
        print(mod)
    if not asdl.check(mod):
        sys.exit(1)
    if H_FILE:
        with open(H_FILE, "w") as f:
            f.write(auto_gen_msg)
            f.write('#include "asdl.h"\n\n')
            c = ChainOfVisitors(TypeDefVisitor(f),
                                StructVisitor(f),
                                PrototypeVisitor(f),
                                )
            c.visit(mod)
            f.write("PyObject* Ta3AST_mod2obj(mod_ty t);\n")
            f.write("mod_ty Ta3AST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
            f.write("int Ta3AST_Check(PyObject* obj);\n")

    if C_FILE:
        with open(C_FILE, "w") as f:
            f.write(auto_gen_msg)
            f.write('#include <stddef.h>\n')
            f.write('\n')
            f.write('#include "Python.h"\n')
            f.write('#include "%s-ast.h"\n' % mod.name)
            f.write('\n')
            f.write("static PyTypeObject AST_type;\n")
            v = ChainOfVisitors(
                PyTypesDeclareVisitor(f),
                PyTypesVisitor(f),
                Obj2ModPrototypeVisitor(f),
                FunctionVisitor(f),
                ObjVisitor(f),
                Obj2ModVisitor(f),
                ASTModuleVisitor(f),
                PartingShots(f),
                )
            v.visit(mod)
//  [python]
def main(srcfile, dump_module=False):
    argv0 = sys.argv[0]
    components = argv0.split(os.sep)
    argv0 = os.sep.join(components[-2:])
    auto_gen_msg = common_msg % argv0
    mod = asdl.parse(srcfile)
    if dump_module:
        print('Parsed Module:')
        print(mod)
    if not asdl.check(mod):
        sys.exit(1)
    if INC_DIR:
        p = "%s/%s-ast.h" % (INC_DIR, mod.name)
        f = open(p, "w")
        f.write(auto_gen_msg)
        f.write('#include "asdl.h"\n\n')
        c = ChainOfVisitors(TypeDefVisitor(f),
                            StructVisitor(f),
                            PrototypeVisitor(f),
                            )
        c.visit(mod)
        f.write("PyObject* Ta3AST_mod2obj(mod_ty t);\n")
        f.write("mod_ty Ta3AST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
        f.write("int Ta3AST_Check(PyObject* obj);\n")
        f.close()

    if SRC_DIR:
        p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c")
        f = open(p, "w")
        f.write(auto_gen_msg)
        f.write('#include <stddef.h>\n')
        f.write('\n')
        f.write('#include "Python.h"\n')
        f.write('#include "%s-ast.h"\n' % mod.name)
        f.write('\n')
        f.write("static PyTypeObject AST_type;\n")
        v = ChainOfVisitors(
            PyTypesDeclareVisitor(f),
            PyTypesVisitor(f),
            Obj2ModPrototypeVisitor(f),
            FunctionVisitor(f),
            ObjVisitor(f),
            Obj2ModVisitor(f),
            ASTModuleVisitor(f),
            PartingShots(f),
            )
        v.visit(mod)
        f.close()
//  [python]
def test_big_arrays(self):
    L = (1 << 31) + 100000
    a = np.empty(L, dtype=np.uint8)
    with tempdir() as tmpdir:
        tmp = open(os.path.join(tmpdir, "file.npz"), "w")
        np.savez(tmp, a=a)
        del a
        npfile = np.load(tmp)
        a = npfile['a']
        npfile.close()
//  [python]
def test_big_arrays(self):
    L = (1 << 31) + 100000
    tmp = mktemp(suffix='.npz')
    a = np.empty(L, dtype=np.uint8)
    np.savez(tmp, a=a)
    del a
    npfile = np.load(tmp)
    a = npfile['a']
    npfile.close()
    os.remove(tmp)
//  [python]
def load_metadata_preview(request, c_type, c_id, conn=None, share_id=None, **kwargs):
    """
    This is the image 'Preview' tab for the right-hand panel.
    """
    context = {}

    # the index of a field within a well
    index = getIntOrDefault(request, "index", 0)

    manager = BaseContainer(conn, **{str(c_type): long(c_id)})
    if share_id:
        context["share"] = BaseShare(conn, share_id)
    if c_type == "well":
        manager.image = manager.well.getImage(index)

    allRdefs = manager.image.getAllRenderingDefs()
    rdefs = {}
    rdefId = manager.image.getRenderingDefId()
    # remove duplicates per user
    for r in allRdefs:
        ownerId = r["owner"]["id"]
        r["current"] = r["id"] == rdefId
        # if duplicate rdefs for user, pick one with highest ID
        if ownerId not in rdefs or rdefs[ownerId]["id"] < r["id"]:
            rdefs[ownerId] = r
    rdefs = rdefs.values()
    # format into rdef strings,
    # E.g. {c: '1|3118:35825$FF0000,2|2086:18975$FFFF00', m: 'c'}
    rdefQueries = []
    for r in rdefs:
        chs = []
        for i, c in enumerate(r["c"]):
            act = "-"
            if c["active"]:
                act = ""
            color = c["lut"] if "lut" in c else c["color"]
            reverse = "r" if c["inverted"] else "-r"
            chs.append(
                "%s%s|%s:%s%s$%s" % (act, i + 1, c["start"], c["end"], reverse, color)
            )
        rdefQueries.append(
            {
                "id": r["id"],
                "owner": escape(r["owner"]),  # May be used unsafe later
                "c": ",".join(chs),
                "m": r["model"] == "greyscale" and "g" or "c",
            }
        )
    max_w, max_h = conn.getMaxPlaneSize()
    size_x = manager.image.getSizeX()
    size_y = manager.image.getSizeY()

    context["tiledImage"] = (size_x * size_y) > (max_w * max_h)
    context["manager"] = manager
    context["rdefsJson"] = json.dumps(rdefQueries)
    context["rdefs"] = rdefs
    context["template"] = "webclient/annotations/metadata_preview.html"
    return context
//  [python]
def load_metadata_preview(request, c_type, c_id, conn=None, share_id=None, **kwargs):
    """
    This is the image 'Preview' tab for the right-hand panel.
    """
    context = {}

    # the index of a field within a well
    index = getIntOrDefault(request, "index", 0)

    manager = BaseContainer(conn, **{str(c_type): long(c_id)})
    if share_id:
        context["share"] = BaseShare(conn, share_id)
    if c_type == "well":
        manager.image = manager.well.getImage(index)

    allRdefs = manager.image.getAllRenderingDefs()
    rdefs = {}
    rdefId = manager.image.getRenderingDefId()
    # remove duplicates per user
    for r in allRdefs:
        ownerId = r["owner"]["id"]
        r["current"] = r["id"] == rdefId
        # if duplicate rdefs for user, pick one with highest ID
        if ownerId not in rdefs or rdefs[ownerId]["id"] < r["id"]:
            rdefs[ownerId] = r
    rdefs = rdefs.values()
    # format into rdef strings,
    # E.g. {c: '1|3118:35825$FF0000,2|2086:18975$FFFF00', m: 'c'}
    rdefQueries = []
    for r in rdefs:
        chs = []
        for i, c in enumerate(r["c"]):
            act = "-"
            if c["active"]:
                act = ""
            color = c["lut"] if "lut" in c else c["color"]
            reverse = "r" if c["inverted"] else "-r"
            chs.append(
                "%s%s|%s:%s%s$%s" % (act, i + 1, c["start"], c["end"], reverse, color)
            )
        rdefQueries.append(
            {
                "id": r["id"],
                "owner": r["owner"],
                "c": ",".join(chs),
                "m": r["model"] == "greyscale" and "g" or "c",
            }
        )
    max_w, max_h = conn.getMaxPlaneSize()
    size_x = manager.image.getSizeX()
    size_y = manager.image.getSizeY()

    context["tiledImage"] = (size_x * size_y) > (max_w * max_h)
    context["manager"] = manager
    context["rdefsJson"] = json.dumps(rdefQueries)
    context["rdefs"] = rdefs
    context["template"] = "webclient/annotations/metadata_preview.html"
    return context
//  [python]
def get_file(path):
    try:
        data_file, metadata = get_info(path)
    except (OSError, werkzeug.exceptions.NotFound):
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    response = flask.make_response(flask.send_file(
        str(data_file),
    ))
    generate_headers(
        response.headers,
        metadata["headers"],
    )
    return response
//  [python]
def get_file(path):
    try:
        data_file, metadata = get_info(
            path,
            pathlib.Path(app.config["DATA_ROOT"])
        )
    except (OSError, ValueError):
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    response = flask.make_response(flask.send_file(
        str(data_file),
    ))
    generate_headers(
        response.headers,
        metadata["headers"],
    )
    return response
//  [python]
def head_file(path):
    try:
        data_file, metadata = get_info(path)

        stat = data_file.stat()
    except (OSError, werkzeug.exceptions.NotFound):
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    response = flask.Response()
    response.headers["Content-Length"] = str(stat.st_size)
    generate_headers(
        response.headers,
        metadata["headers"],
    )
    return response
//  [python]
def head_file(path):
    try:
        data_file, metadata = get_info(
            path,
            pathlib.Path(app.config["DATA_ROOT"])
        )

        stat = data_file.stat()
    except (OSError, ValueError):
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    response = flask.Response()
    response.headers["Content-Length"] = str(stat.st_size)
    generate_headers(
        response.headers,
        metadata["headers"],
    )
    return response
//  [python]
def put_file(path):
    try:
        data_file, metadata_file = get_paths(app.config["DATA_ROOT"], path)
    except werkzeug.exceptions.NotFound:
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    verification_key = flask.request.args.get("v", "")
    length = int(flask.request.headers.get("Content-Length", 0))
    hmac_input = "{} {}".format(path, length).encode("utf-8")
    key = app.config["SECRET_KEY"]
    mac = hmac.new(key, hmac_input, hashlib.sha256)
    digest = mac.hexdigest()

    if not hmac.compare_digest(digest, verification_key):
        return flask.Response(
            "Invalid verification key",
            403,
            mimetype="text/plain",
        )

    content_type = flask.request.headers.get(
        "Content-Type",
        "application/octet-stream",
    )

    data_file.parent.mkdir(parents=True, exist_ok=True, mode=0o770)

    try:
        with write_file(data_file) as fout:
            stream_file(flask.request.stream, fout, length)

            with metadata_file.open("x") as f:
                json.dump(
                    {
                        "headers": {"Content-Type": content_type},
                    },
                    f,
                )
    except EOFError:
        return flask.Response(
            "Bad Request",
            400,
            mimetype="text/plain",
        )
    except OSError as exc:
        if exc.errno == errno.EEXIST:
            return flask.Response(
                "Conflict",
                409,
                mimetype="text/plain",
            )
        raise

    return flask.Response(
        "Created",
        201,
        mimetype="text/plain",
    )
//  [python]
def put_file(path):
    try:
        dest_path = sanitized_join(
            path,
            pathlib.Path(app.config["DATA_ROOT"]),
        )
    except ValueError:
        return flask.Response(
            "Not Found",
            404,
            mimetype="text/plain",
        )

    verification_key = flask.request.args.get("v", "")
    length = int(flask.request.headers.get("Content-Length", 0))
    hmac_input = "{} {}".format(path, length).encode("utf-8")
    key = app.config["SECRET_KEY"]
    mac = hmac.new(key, hmac_input, hashlib.sha256)
    digest = mac.hexdigest()

    if not hmac.compare_digest(digest, verification_key):
        return flask.Response(
            "Invalid verification key",
            403,
            mimetype="text/plain",
        )

    content_type = flask.request.headers.get(
        "Content-Type",
        "application/octet-stream",
    )

    dest_path.parent.mkdir(parents=True, exist_ok=True, mode=0o770)
    data_file, metadata_file = get_paths(dest_path)

    try:
        with write_file(data_file) as fout:
            stream_file(flask.request.stream, fout, length)

            with metadata_file.open("x") as f:
                json.dump(
                    {
                        "headers": {"Content-Type": content_type},
                    },
                    f,
                )
    except EOFError:
        return flask.Response(
            "Bad Request",
            400,
            mimetype="text/plain",
        )
    except OSError as exc:
        if exc.errno == errno.EEXIST:
            return flask.Response(
                "Conflict",
                409,
                mimetype="text/plain",
            )
        raise

    return flask.Response(
        "Created",
        201,
        mimetype="text/plain",
    )
//  [python]
def get_paths(root: str, sub_path: str) \
        -> typing.Tuple[pathlib.Path, pathlib.Path]:
    base_path = flask.safe_join(root, sub_path)
    data_file = pathlib.Path(base_path + ".data")
    metadata_file = pathlib.Path(base_path + ".meta")

    return data_file, metadata_file
//  [python]
def get_paths(base_path: pathlib.Path):
    data_file = pathlib.Path(str(base_path) + ".data")
    metadata_file = pathlib.Path(str(base_path) + ".meta")

    return data_file, metadata_file
//  [python]
def setup_paginator(context):
    """
    custom paginator tag
    Inspired from http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/
    """
    if (context["is_paginated"]):
        " Initialize variables "
        in_leading_range = in_trailing_range = False
        pages_outside_leading_range = pages_outside_trailing_range = range(0)

        if (context["pages"] <= LEADING_PAGE_RANGE_DISPLAYED):
            in_leading_range = in_trailing_range = True
            page_numbers = [n for n in range(1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
        elif (context["current_page_number"] <= LEADING_PAGE_RANGE):
            in_leading_range = True
            page_numbers = [n for n in range(1, LEADING_PAGE_RANGE_DISPLAYED + 1) if n > 0 and n <= context["pages"]]
            pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
        elif (context["current_page_number"] > context["pages"] - TRAILING_PAGE_RANGE):
            in_trailing_range = True
            page_numbers = [n for n in range(context["pages"] - TRAILING_PAGE_RANGE_DISPLAYED + 1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
            pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]
        else:
            page_numbers = [n for n in range(context["current_page_number"] - ADJACENT_PAGES, context["current_page_number"] + ADJACENT_PAGES + 1) if n > 0 and n <= context["pages"]]
            pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
            pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]

        page_object = context['page_object']
        #patch for change in django 1.5
        if page_object.has_previous():
            previous_page_number = page_object.previous_page_number()
        else:
            previous_page_number = None

        if page_object.has_next():
            next_page_number = page_object.next_page_number()
        else:
            next_page_number = None

        return {
            "base_url": escape(context["base_url"]),
            "is_paginated": context["is_paginated"],
            "previous": previous_page_number,
            "has_previous": page_object.has_previous(),
            "next": next_page_number,
            "has_next": page_object.has_next(),
            "page": context["current_page_number"],
            "pages": context["pages"],
            "page_numbers": page_numbers,
            "in_leading_range" : in_leading_range,
            "in_trailing_range" : in_trailing_range,
            "pages_outside_leading_range": pages_outside_leading_range,
            "pages_outside_trailing_range": pages_outside_trailing_range,
        }
//  [python]
def setup_paginator(context):
    """
    custom paginator tag
    Inspired from http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/
    """
    if (context["is_paginated"]):
        " Initialize variables "
        in_leading_range = in_trailing_range = False
        pages_outside_leading_range = pages_outside_trailing_range = range(0)

        if (context["pages"] <= LEADING_PAGE_RANGE_DISPLAYED):
            in_leading_range = in_trailing_range = True
            page_numbers = [n for n in range(1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
        elif (context["current_page_number"] <= LEADING_PAGE_RANGE):
            in_leading_range = True
            page_numbers = [n for n in range(1, LEADING_PAGE_RANGE_DISPLAYED + 1) if n > 0 and n <= context["pages"]]
            pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
        elif (context["current_page_number"] > context["pages"] - TRAILING_PAGE_RANGE):
            in_trailing_range = True
            page_numbers = [n for n in range(context["pages"] - TRAILING_PAGE_RANGE_DISPLAYED + 1, context["pages"] + 1) if n > 0 and n <= context["pages"]]
            pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]
        else:
            page_numbers = [n for n in range(context["current_page_number"] - ADJACENT_PAGES, context["current_page_number"] + ADJACENT_PAGES + 1) if n > 0 and n <= context["pages"]]
            pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)]
            pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)]

        page_object = context['page_object']
        #patch for change in django 1.5
        if page_object.has_previous():
            previous_page_number = page_object.previous_page_number()
        else:
            previous_page_number = None

        if page_object.has_next():
            next_page_number = page_object.next_page_number()
        else:
            next_page_number = None

        return {
            "base_url": context["base_url"],
            "is_paginated": context["is_paginated"],
            "previous": previous_page_number,
            "has_previous": page_object.has_previous(),
            "next": next_page_number,
            "has_next": page_object.has_next(),
            "page": context["current_page_number"],
            "pages": context["pages"],
            "page_numbers": page_numbers,
            "in_leading_range" : in_leading_range,
            "in_trailing_range" : in_trailing_range,
            "pages_outside_leading_range": pages_outside_leading_range,
            "pages_outside_trailing_range": pages_outside_trailing_range,
        }
//  [python]
def _decompress(compressed_path: Text, target_path: Text) -> None:

    with TarSafe.open(compressed_path, "r:gz") as tar:
        tar.extractall(target_path)  # target dir will be created if it not exists
//  [python]
def _decompress(compressed_path: Text, target_path: Text) -> None:

    with tarfile.open(compressed_path, "r:gz") as tar:
        tar.extractall(target_path)  # target dir will be created if it not exists
//  [python]
def main():
    app = create_app()

    init_errorhandler()

    app.register_blueprint(web)
    app.register_blueprint(opds)
    app.register_blueprint(jinjia)
    app.register_blueprint(about)
    app.register_blueprint(shelf)
    app.register_blueprint(admi)
    app.register_blueprint(remotelogin)
    app.register_blueprint(meta)
    app.register_blueprint(gdrive)
    app.register_blueprint(EditBook)
    if kobo_available:
        app.register_blueprint(kobo)
        app.register_blueprint(kobo_auth)
    if oauth_available:
        app.register_blueprint(oauth)
    success = web_server.start()
    sys.exit(0 if success else 1)
//  [python]
def main():
    app = create_app()

    init_errorhandler()

    app.register_blueprint(web)
    app.register_blueprint(opds)
    app.register_blueprint(jinjia)
    app.register_blueprint(about)
    app.register_blueprint(shelf)
    app.register_blueprint(admi)
    app.register_blueprint(remotelogin)
    app.register_blueprint(meta)
    app.register_blueprint(gdrive)
    app.register_blueprint(editbook)
    if kobo_available:
        app.register_blueprint(kobo)
        app.register_blueprint(kobo_auth)
    if oauth_available:
        app.register_blueprint(oauth)
    success = web_server.start()
    sys.exit(0 if success else 1)
//  [python]
def _download_file(bucket, filename, local_dir):
    key = bucket.get_key(filename)
    local_filename = os.path.join(local_dir, os.path.basename(filename))
    key.get_contents_to_filename(local_filename)
    return local_filename
//  [python]
def _download_file(bucket, filename, local_dir):
    key = bucket.get_key(filename)
    local_filename = os.path.join(local_dir, filename)
    key.get_contents_to_filename(local_filename)
    return local_filename
//  [python]
def parse(source, filename='<unknown>', mode='exec', *, type_comments=False):
    """
    Parse the source into an AST node.
    Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
    Pass type_comments=True to get back type comments where the syntax allows.
    """
    flags = PyCF_ONLY_AST
    if type_comments:
        flags |= PyCF_TYPE_COMMENTS
    return compile(source, filename, mode, flags)
//  [python]
def parse(source, filename='<unknown>', mode='exec'):
    """
    Parse the source into an AST node.
    Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
    """
    return compile(source, filename, mode, PyCF_ONLY_AST)
//  [python]
def test_patch_bot_role(self) -> None:
    self.login("desdemona")

    email = "default-bot@zulip.com"
    user_profile = self.get_bot_user(email)

    do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=user_profile)

    req = dict(role=UserProfile.ROLE_GUEST)

    result = self.client_patch(f"/json/bots/{self.get_bot_user(email).id}", req)
    self.assert_json_success(result)

    user_profile = self.get_bot_user(email)
    self.assertEqual(user_profile.role, UserProfile.ROLE_GUEST)

    # Test for not allowing a non-owner user to make assign a bot an owner role
    desdemona = self.example_user("desdemona")
    do_change_user_role(desdemona, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None)

    req = dict(role=UserProfile.ROLE_REALM_OWNER)

    result = self.client_patch(f"/json/users/{user_profile.id}", req)
    self.assert_json_error(result, "Must be an organization owner")

    result = self.client_patch(f"/json/bots/{user_profile.id}", req)
    self.assert_json_error(result, "Must be an organization owner")

    # Test for not allowing a non-administrator user to assign a bot an administrator role
    shiva = self.example_user("shiva")
    self.assertEqual(shiva.role, UserProfile.ROLE_MODERATOR)
    self.login_user(shiva)
    do_change_bot_owner(user_profile, shiva, acting_user=None)

    req = dict(role=UserProfile.ROLE_REALM_ADMINISTRATOR)

    result = self.client_patch(f"/json/users/{user_profile.id}", req)
    self.assert_json_error(result, "Must be an organization administrator")

    result = self.client_patch(f"/json/bots/{user_profile.id}", req)
    self.assert_json_error(result, "Must be an organization administrator")
//  [python]
def test_patch_bot_role(self) -> None:
    self.login("desdemona")

    email = "default-bot@zulip.com"
    user_profile = self.get_bot_user(email)

    do_change_user_role(user_profile, UserProfile.ROLE_MEMBER, acting_user=user_profile)

    req = dict(role=UserProfile.ROLE_GUEST)

    result = self.client_patch(f"/json/bots/{self.get_bot_user(email).id}", req)
    self.assert_json_success(result)

    user_profile = self.get_bot_user(email)
    self.assertEqual(user_profile.role, UserProfile.ROLE_GUEST)

    # Test for not allowing a non-owner user to make assign a bot an owner role
    desdemona = self.example_user("desdemona")
    do_change_user_role(desdemona, UserProfile.ROLE_REALM_ADMINISTRATOR, acting_user=None)

    req = dict(role=UserProfile.ROLE_REALM_OWNER)

    result = self.client_patch(f"/json/bots/{self.get_bot_user(email).id}", req)
    self.assert_json_error(result, "Must be an organization owner")
//  [python]
def test_get_imports(self, mocker):
    from openapi_python_client.parser.properties import DateTimeProperty

    name = mocker.MagicMock()
    mocker.patch("openapi_python_client.utils.snake_case")
    p = DateTimeProperty(name=name, required=True, default=None)
    assert p.get_imports(prefix="") == {
        "import datetime",
        "from typing import cast",
    }

    p.required = False
    assert p.get_imports(prefix="") == {
        "from typing import Optional",
        "import datetime",
        "from typing import cast",
    }
//  [python]
def test_get_imports(self, mocker):
    from openapi_python_client.parser.properties import DateTimeProperty

    name = mocker.MagicMock()
    mocker.patch("openapi_python_client.utils.snake_case")
    p = DateTimeProperty(name=name, required=True, default=None)
    assert p.get_imports(prefix="") == {
        "from datetime import datetime",
        "from typing import cast",
    }

    p.required = False
    assert p.get_imports(prefix="") == {
        "from typing import Optional",
        "from datetime import datetime",
        "from typing import cast",
    }
//  [python]
def test_property_from_data_array(self, mocker):
    name = mocker.MagicMock()
    required = mocker.MagicMock()
    data = oai.Schema(type="array", items={"type": "number", "default": "0.0"},)
    ListProperty = mocker.patch(f"{MODULE_NAME}.ListProperty")
    FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")
    mocker.patch("openapi_python_client.utils.remove_string_escapes", return_value=name)

    from openapi_python_client.parser.properties import property_from_data

    p = property_from_data(name=name, required=required, data=data)

    FloatProperty.assert_called_once_with(name=name, required=True, default="0.0")
    ListProperty.assert_called_once_with(
        name=name, required=required, default=None, inner_property=FloatProperty.return_value
    )
    assert p == ListProperty.return_value
//  [python]
def test_property_from_data_array(self, mocker):
    name = mocker.MagicMock()
    required = mocker.MagicMock()
    data = oai.Schema(type="array", items={"type": "number", "default": "0.0"},)
    ListProperty = mocker.patch(f"{MODULE_NAME}.ListProperty")
    FloatProperty = mocker.patch(f"{MODULE_NAME}.FloatProperty")

    from openapi_python_client.parser.properties import property_from_data

    p = property_from_data(name=name, required=required, data=data)

    FloatProperty.assert_called_once_with(name=f"{name}_item", required=True, default="0.0")
    ListProperty.assert_called_once_with(
        name=name, required=required, default=None, inner_property=FloatProperty.return_value
    )
    assert p == ListProperty.return_value
//  [python]
def to_yaml(self, **kwargs):
  """Returns a yaml string containing the network configuration.

  Note: Since TF 2.6, this method is no longer supported and will raise a
  RuntimeError.

  To load a network from a yaml save file, use
  `keras.models.model_from_yaml(yaml_string, custom_objects={})`.

  `custom_objects` should be a dictionary mapping
  the names of custom losses / layers / etc to the corresponding
  functions / classes.

  Args:
      **kwargs: Additional keyword arguments
          to be passed to `yaml.dump()`.

  Returns:
      A YAML string.

  Raises:
      RuntimeError: announces that the method poses a security risk
  """
  raise RuntimeError(
      'Method `model.to_yaml()` has been removed due to security risk of '
      'arbitrary code execution. Please use `model.to_json()` instead.'
  )
//  [python]
def to_yaml(self, **kwargs):
  """Returns a yaml string containing the network configuration.

  To load a network from a yaml save file, use
  `keras.models.model_from_yaml(yaml_string, custom_objects={})`.

  `custom_objects` should be a dictionary mapping
  the names of custom losses / layers / etc to the corresponding
  functions / classes.

  Args:
      **kwargs: Additional keyword arguments
          to be passed to `yaml.dump()`.

  Returns:
      A YAML string.

  Raises:
      ImportError: if yaml module is not found.
  """
  if yaml is None:
    raise ImportError(
        'Requires yaml module installed (`pip install pyyaml`).')
  return yaml.dump(self._updated_config(), **kwargs)
//  [python]
def pascal_case(value: str) -> str:
    return fix_keywords(stringcase.pascalcase(sanitize(value)))
//  [python]
def pascal_case(value: str) -> str:
    return stringcase.pascalcase(_sanitize(value))
//  [python]
def group_title(value: str) -> str:
    value = re.sub(r"([A-Z]{2,})([A-Z][a-z]|[ \-_]|$)", lambda m: m.group(1).title() + m.group(2), value.strip())
    value = re.sub(r"(^|[ _-])([A-Z])", lambda m: m.group(1) + m.group(2).lower(), value)
    return value
//  [python]
def group_title(value: str) -> str:
    value = re.sub(r"([A-Z]{2,})([A-Z][a-z]|[ -_]|$)", lambda m: m.group(1).title() + m.group(2), value.strip())
    value = re.sub(r"(^|[ _-])([A-Z])", lambda m: m.group(1) + m.group(2).lower(), value)
    return value
//  [python]
def snake_case(value: str) -> str:
    return fix_keywords(stringcase.snakecase(group_title(sanitize(value))))
//  [python]
def snake_case(value: str) -> str:
    return stringcase.snakecase(group_title(_sanitize(value)))
//  [python]
def traverse(cls, base, request, path_items):
    """See ``zope.app.pagetemplate.engine``."""

    path_items = list(path_items)
    path_items.reverse()

    while path_items:
        name = path_items.pop()
        if ITraversable.providedBy(base):
            base = getattr(base, cls.traverseMethod)(name)
        else:
            base = traversePathElement(base, name, path_items,
                                       request=request)

    return base
//  [python]
def kebab_case(value: str) -> str:
    return fix_keywords(stringcase.spinalcase(group_title(sanitize(value))))
//  [python]
def kebab_case(value: str) -> str:
    return stringcase.spinalcase(group_title(_sanitize(value)))
//  [python]
def _is_javascript_scheme(s):
    is_image_url = False
    for image_type in _find_image_dataurls(s):
        is_image_url = True
        if _is_unsafe_image_type(image_type):
            return True
    if is_image_url:
        return False
    return bool(_is_possibly_malicious_scheme(s))
//  [python]
def _is_javascript_scheme(s):
    if _is_image_dataurl(s):
        return None
    return _is_possibly_malicious_scheme(s)
//  [python]
def shutdown():
    task = request.get_json().get('parameter', -1)
    showtext = {}
    if task in (0, 1):  # valid commandos received
        # close all database connections
        calibre_db.dispose()
        ub.dispose()

        if task == 0:
            showtext['text'] = _(u'Server restarted, please reload page')
        else:
            showtext['text'] = _(u'Performing shutdown of server, please close window')
        # stop gevent/tornado server
        web_server.stop(task == 0)
        return json.dumps(showtext)

    if task == 2:
        log.warning("reconnecting to calibre database")
        calibre_db.reconnect_db(config, ub.app_DB_path)
        showtext['text'] = _(u'Reconnect successful')
        return json.dumps(showtext)

    showtext['text'] = _(u'Unknown command')
    return json.dumps(showtext), 400
//  [python]
def shutdown():
    task = int(request.args.get("parameter").strip())
    showtext = {}
    if task in (0, 1):  # valid commandos received
        # close all database connections
        calibre_db.dispose()
        ub.dispose()

        if task == 0:
            showtext['text'] = _(u'Server restarted, please reload page')
        else:
            showtext['text'] = _(u'Performing shutdown of server, please close window')
        # stop gevent/tornado server
        web_server.stop(task == 0)
        return json.dumps(showtext)

    if task == 2:
        log.warning("reconnecting to calibre database")
        calibre_db.reconnect_db(config, ub.app_DB_path)
        showtext['text'] = _(u'Reconnect successful')
        return json.dumps(showtext)

    showtext['text'] = _(u'Unknown command')
    return json.dumps(showtext), 400
//  [python]
def get_current_phase(self, requested_phase_identifier):
    found = False
    for phase in self.phases:
        if phase.is_valid():
            phase.process()
        if found or not requested_phase_identifier or requested_phase_identifier == phase.identifier:
            found = True  # We're at or past the requested phase
            if not phase.should_skip():
                return phase
        if not phase.should_skip() and not phase.is_valid():  # A past phase is not valid, that's the current one
            return phase
    raise Http404("Error! Phase with identifier `%s` not found." % escape(requested_phase_identifier))
//  [python]
def get_current_phase(self, requested_phase_identifier):
    found = False
    for phase in self.phases:
        if phase.is_valid():
            phase.process()
        if found or not requested_phase_identifier or requested_phase_identifier == phase.identifier:
            found = True  # We're at or past the requested phase
            if not phase.should_skip():
                return phase
        if not phase.should_skip() and not phase.is_valid():  # A past phase is not valid, that's the current one
            return phase
    raise Http404("Error! Phase with identifier `%s` not found." % requested_phase_identifier)  # pragma: no cover
//  [python]
def test_evaluate_dict_key_as_underscore(self):
    # Traversing to the name `_` will raise a DeprecationWarning
    # because it will go away in Zope 6.
    ec = self._makeContext()
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        self.assertEqual(ec.evaluate('d/_'), 'under')