jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def qute_settings(url):
    """Handler for qute://settings. View/change qute configuration."""
    if url.path() == '/set':
        return _qute_settings_set(url)

    src = jinja.render('settings.html', title='settings',
                       configdata=configdata,
                       confget=config.instance.get_str)
    return 'text/html', src
//  [python]
def remote_static():
    """Stream *large* static files with special requirements."""
    file_path = request.args.get("file") or "."

    # Check that user is logged in or that file extension is valid
    if controllers.check_session_tracks(file_path) is False:
        return abort(403)

    range_header = request.headers.get("Range", None)
    if not range_header and (file_path.endswith(".bam") or file_path.endswith(".cram")):
        return abort(500)

    new_resp = send_file_partial(file_path)
    return new_resp
//  [python]
def remote_static():
    """Stream *large* static files with special requirements."""
    file_path = request.args.get("file") or "."

    # Check that user is logged in or that file extension is valid
    if current_user.is_authenticated is False or file_path not in session.get("igv_tracks", []):
        LOG.warning(f"{file_path} not in {session.get('igv_tracks', [])}")
        return abort(403)

    range_header = request.headers.get("Range", None)
    if not range_header and (file_path.endswith(".bam") or file_path.endswith(".cram")):
        return abort(500)

    new_resp = send_file_partial(file_path)
    return new_resp
//  [python]
def new_type_to_old_type(typ: new.BasePrimitive) -> old.NodeType:
    if isinstance(typ, new.BoolDefinition):
        return old.BaseType("bool")
    if isinstance(typ, new.AddressDefinition):
        return old.BaseType("address")
    if isinstance(typ, new.InterfaceDefinition):
        return old.InterfaceType(typ._id)
    if isinstance(typ, new.BytesMDefinition):
        m = typ._length  # type: ignore
        return old.BaseType(f"bytes{m}")
    if isinstance(typ, new.BytesArrayDefinition):
        return old.ByteArrayType(typ.length)
    if isinstance(typ, new.StringDefinition):
        return old.StringType(typ.length)
    if isinstance(typ, new.DecimalDefinition):
        return old.BaseType("decimal")
    if isinstance(typ, new.SignedIntegerAbstractType):
        bits = typ._bits  # type: ignore
        return old.BaseType("int" + str(bits))
    if isinstance(typ, new.UnsignedIntegerAbstractType):
        bits = typ._bits  # type: ignore
        return old.BaseType("uint" + str(bits))
    if isinstance(typ, new.ArrayDefinition):
        return old.SArrayType(new_type_to_old_type(typ.value_type), typ.length)
    if isinstance(typ, new.DynamicArrayDefinition):
        return old.DArrayType(new_type_to_old_type(typ.value_type), typ.length)
    if isinstance(typ, new.TupleDefinition):
        return old.TupleType([new_type_to_old_type(t) for t in typ.value_type])
    if isinstance(typ, new.StructDefinition):
        return old.StructType(
            {n: new_type_to_old_type(t) for (n, t) in typ.members.items()}, typ._id
        )
    raise InvalidType(f"unknown type {typ}")
//  [python]
def new_type_to_old_type(typ: new.BasePrimitive) -> old.NodeType:
    if isinstance(typ, new.BoolDefinition):
        return old.BaseType("bool")
    if isinstance(typ, new.AddressDefinition):
        return old.BaseType("address")
    if isinstance(typ, new.InterfaceDefinition):
        return old.InterfaceType(typ._id)
    if isinstance(typ, new.BytesMDefinition):
        m = typ._length  # type: ignore
        return old.BaseType(f"bytes{m}")
    if isinstance(typ, new.BytesArrayDefinition):
        return old.ByteArrayType(typ.length)
    if isinstance(typ, new.StringDefinition):
        return old.StringType(typ.length)
    if isinstance(typ, new.DecimalDefinition):
        return old.BaseType("decimal")
    if isinstance(typ, new.SignedIntegerAbstractType):
        bits = typ._bits  # type: ignore
        return old.BaseType("int" + str(bits))
    if isinstance(typ, new.UnsignedIntegerAbstractType):
        bits = typ._bits  # type: ignore
        return old.BaseType("uint" + str(bits))
    if isinstance(typ, new.ArrayDefinition):
        return old.SArrayType(new_type_to_old_type(typ.value_type), typ.length)
    if isinstance(typ, new.DynamicArrayDefinition):
        return old.DArrayType(new_type_to_old_type(typ.value_type), typ.length)
    if isinstance(typ, new.TupleDefinition):
        return old.TupleType(typ.value_type)
    if isinstance(typ, new.StructDefinition):
        return old.StructType(
            {n: new_type_to_old_type(t) for (n, t) in typ.members.items()}, typ._id
        )
    raise InvalidType(f"unknown type {typ}")
//  [python]
def command(self):
    args = list(self.args)
    if len(args) > 0:
        check = args[0]
    else:
        check = self.data.get('check', '')
    check = 'True' in check
    
    all = self._gnupg().list_secret_keys()
    if check:
        res = {fprint : all[fprint] for fprint in all
            if not (all[fprint]['revoked'] or all[fprint]['disabled'])}
    else:
        res = all
    return self._success("Searched for secret keys", res)
//  [python]
def command(self):
    res = self._gnupg().list_secret_keys()
    return self._success("Searched for secret keys", res)
//  [python]
def get_file(recipe):
    if not recipe.link:
        recipe.link = Dropbox.get_share_link(recipe)
        recipe.save()

    url = recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.')
    if validators.url(url, public=True):
        response = requests.get(url)

        return io.BytesIO(response.content)
//  [python]
def get_file(recipe):
    if not recipe.link:
        recipe.link = Dropbox.get_share_link(recipe)
        recipe.save()

    response = requests.get(recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.'))

    return io.BytesIO(response.content)
//  [python]
def uploadimage():
    # print(request.json)
    if not request.json or "image" not in request.json:
        print("No data sent or no image provided. Aborting with 400.")
        abort(400)

    im_b64 = request.json["image"]
    img_bytes = base64.b64decode(im_b64.encode("utf-8"))
    img_bytes, valid = allowed_file(img_bytes)
    if not valid:
        return escape({"entry": "False"})
    img = Image.open(io.BytesIO(img_bytes))

    file_ending = img.format
    print(f"File has filetype {file_ending}.")

    if file_ending == "JPEG":
        file_ending = ".jpg"
    else:
        file_ending = ".png"

    one_hundred_million = 100000000
    lots_of_nine = 999999999

    file_name = None

    f = open("all_files", "r")
    all_files = ast.literal_eval(f.read())
    f.close()

    attempt = 0

    while file_name is None or file_name in all_files:
        if attempt <= 1000:
            file_name = random.randint(one_hundred_million, lots_of_nine)

            file_name = base64.b64encode(str(file_name).encode("utf-8")).decode("utf-8")

            print(f"Trying new file name: {file_name}")
        else:
            attempt = 0
            one_hundred_million += 100000
            lots_of_nine += 1000000

            while one_hundred_million >= lots_of_nine:
                one_hundred_million -= 10000

            one_hundred_million -= 10000

    print(f"Successful file name: {file_name}")

    title = request.json["title"]
    if title[:9] == "[PAUSED] ":
        title = title[9::]

    singer = request.json["singer"]
    album = request.json["album"]

    file_db_entry = [
        {"title": title, "singer": singer, "album": album},
        file_name,
        file_ending,
    ]
    print(f"New db entry: {file_db_entry}")

    all_files.append(file_db_entry)

    # caching
    # we want a limit of X amount of files as defined by the config

    # 1. see how long the list is
    # 2. if it is over get_config()'s cache limit, delete value [0]
    # 3. delete it on disk.

    cache, x, y = get_config()
    del x
    del y

    length = len(all_files)
    while (
        length > cache
    ):  # if it is not over the limit, it will skip. if it is, it does this.
        # if we have gone over our cache limit, let's delete the first entry.
        filename = all_files[0][1] + all_files[0][2]
        remove(werkzeug.utils.secure_filename(filename))
        del all_files[0]
        length = len(all_files)

    f = open("all_files", "w")
    f.write(str(all_files))
    f.close()

    file_name = file_name + file_ending

    img.save(file_name)

    print(f"Saved {file_name} from {file_db_entry}.")
    print(f"Returning {file_db_entry}.")
    return escape(str({"entry": file_db_entry}))
//  [python]
def uploadimage():
    # print(request.json)
    if not request.json or "image" not in request.json:
        print("No data sent or no image provided. Aborting with 400.")
        abort(400)

    im_b64 = request.json["image"]
    img_bytes = base64.b64decode(im_b64.encode("utf-8"))
    img_bytes, valid = allowed_file(img_bytes)
    if not valid:
        return escape({"entry": "False"})
    img = Image.open(io.BytesIO(img_bytes))

    file_ending = img.format
    print(f"File has filetype {file_ending}.")

    if file_ending == "JPEG":
        file_ending = ".jpg"
    else:
        file_ending = ".png"

    one_hundred_million = 100000000
    lots_of_nine = 999999999

    file_name = None

    f = open("all_files", "r")
    all_files = ast.literal_eval(f.read())
    f.close()

    attempt = 0

    while file_name is None or file_name in all_files:
        if attempt <= 1000:
            file_name = random.randint(one_hundred_million, lots_of_nine)

            file_name = base64.b64encode(str(file_name).encode("utf-8")).decode("utf-8")

            print(f"Trying new file name: {file_name}")
        else:
            attempt = 0
            one_hundred_million += 100000
            lots_of_nine += 1000000

            while one_hundred_million >= lots_of_nine:
                one_hundred_million -= 10000

            one_hundred_million -= 10000

    print(f"Successful file name: {file_name}")

    title = request.json["title"]
    if title[:9] == "[PAUSED] ":
        title = title[9::]

    singer = request.json["singer"]
    album = request.json["album"]

    file_db_entry = [
        {"title": title, "singer": singer, "album": album},
        file_name,
        file_ending,
    ]
    print(f"New db entry: {file_db_entry}")

    all_files.append(file_db_entry)

    # caching
    # we want a limit of X amount of files as defined by the config

    # 1. see how long the list is
    # 2. if it is over get_config()'s cache limit, delete value [0]
    # 3. delete it on disk.

    cache, x, y = get_config()
    del x
    del y

    length = len(all_files)
    while (
        length > cache
    ):  # if it is not over the limit, it will skip. if it is, it does this.
        # if we have gone over our cache limit, let's delete the first entry.
        filename = all_files[0][1] + all_files[0][2]
        remove(filename)
        del all_files[0]
        length = len(all_files)

    f = open("all_files", "w")
    f.write(str(all_files))
    f.close()

    file_name = file_name + file_ending

    img.save(file_name)

    print(f"Saved {file_name} from {file_db_entry}.")
    print(f"Returning {file_db_entry}.")
    return escape(str({"entry": file_db_entry}))
//  [python]
def upload_file(request, freeze_upload_folder):
    path = Path(tempfile.mkdtemp()) / freeze_upload_folder / f"{request.node.name}.txt"
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w") as f:
        f.write(request.node.name)
    return str(path.absolute())
//  [python]
def upload_file(request):
    path = tempfile.mkdtemp()
    file_name = os.path.join(path, "%s.txt" % request.node.name)
    with open(file_name, "w") as f:
        f.write(request.node.name)
    return file_name
//  [python]
def yet_another_upload_file(request, freeze_upload_folder):
    path = (
        Path(tempfile.mkdtemp())
        / freeze_upload_folder
        / f"yet_another_{request.node.name}.txt"
    )
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w") as f:
        f.write(request.node.name)
    return str(path.absolute())
//  [python]
def yet_another_upload_file(request):
    path = tempfile.mkdtemp()
    file_name = os.path.join(path, "yet_another_%s.txt" % request.node.name)
    with open(file_name, "w") as f:
        f.write(request.node.name)
    return file_name
//  [python]
def render_POST(self, request):
    """
    Register with the Identity Server
    """
    send_cors(request)

    args = get_args(request, ('matrix_server_name', 'access_token'))

    hostname = args['matrix_server_name'].lower()

    if not is_valid_hostname(hostname):
        request.setResponseCode(400)
        return {
            'errcode': 'M_INVALID_PARAM',
            'error': 'matrix_server_name must be a valid hostname'
        }

    result = yield self.client.get_json(
        "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s"
        % (
            hostname,
            urllib.parse.quote(args['access_token']),
        ),
        1024 * 5,
    )
    if 'sub' not in result:
        raise Exception("Invalid response from homeserver")

    user_id = result['sub']
    tok = yield issueToken(self.sydent, user_id)

    # XXX: `token` is correct for the spec, but we released with `access_token`
    # for a substantial amount of time. Serve both to make spec-compliant clients
    # happy.
    defer.returnValue({
        "access_token": tok,
        "token": tok,
    })
//  [python]
def render_POST(self, request):
    """
    Register with the Identity Server
    """
    send_cors(request)

    args = get_args(request, ('matrix_server_name', 'access_token'))

    result = yield self.client.get_json(
        "matrix://%s/_matrix/federation/v1/openid/userinfo?access_token=%s" % (
            args['matrix_server_name'], urllib.parse.quote(args['access_token']),
        ),
        1024 * 5,
    )
    if 'sub' not in result:
        raise Exception("Invalid response from homeserver")

    user_id = result['sub']
    tok = yield issueToken(self.sydent, user_id)

    # XXX: `token` is correct for the spec, but we released with `access_token`
    # for a substantial amount of time. Serve both to make spec-compliant clients
    # happy.
    defer.returnValue({
        "access_token": tok,
        "token": tok,
    })
//  [python]
def another_upload_file(request, freeze_upload_folder):
    path = (
        Path(tempfile.mkdtemp())
        / freeze_upload_folder
        / f"another_{request.node.name}.txt"
    )
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w") as f:
        f.write(request.node.name)
    return str(path.absolute())
//  [python]
def another_upload_file(request):
    path = tempfile.mkdtemp()
    file_name = os.path.join(path, "another_%s.txt" % request.node.name)
    with open(file_name, "w") as f:
        f.write(request.node.name)
    return file_name
//  [python]
def __init__(self, *expressions, ordering=(), **extra):
    if not isinstance(ordering, (list, tuple)):
        ordering = [ordering]
    ordering = ordering or []
    # Transform minus sign prefixed strings into an OrderBy() expression.
    ordering = (
        (OrderBy(F(o[1:]), descending=True) if isinstance(o, str) and o[0] == '-' else o)
        for o in ordering
    )
    super().__init__(*expressions, **extra)
    self.ordering = self._parse_expressions(*ordering)
//  [python]
def __init__(self, expression, ordering=(), **extra):
    if not isinstance(ordering, (list, tuple)):
        ordering = [ordering]
    ordering = ordering or []
    # Transform minus sign prefixed strings into an OrderBy() expression.
    ordering = (
        (OrderBy(F(o[1:]), descending=True) if isinstance(o, str) and o[0] == '-' else o)
        for o in ordering
    )
    super().__init__(expression, **extra)
    self.ordering = self._parse_expressions(*ordering)
//  [python]
def preprocess_input_exprs_arg_string(input_exprs_str, safe=True):
  """Parses input arg into dictionary that maps input key to python expression.

  Parses input string in the format of 'input_key=<python expression>' into a
  dictionary that maps each input_key to its python expression.

  Args:
    input_exprs_str: A string that specifies python expression for input keys.
      Each input is separated by semicolon. For each input key:
        'input_key=<python expression>'
    safe: Whether to evaluate the python expression as literals or allow
      arbitrary calls (e.g. numpy usage).

  Returns:
    A dictionary that maps input keys to their values.

  Raises:
    RuntimeError: An error when the given input string is in a bad format.
  """
  input_dict = {}

  for input_raw in filter(bool, input_exprs_str.split(';')):
    if '=' not in input_exprs_str:
      raise RuntimeError('--input_exprs "%s" format is incorrect. Please follow'
                         '"<input_key>=<python expression>"' % input_exprs_str)
    input_key, expr = input_raw.split('=', 1)
    if safe:
      try:
        input_dict[input_key] = ast.literal_eval(expr)
      except:
        raise RuntimeError(
            f'Expression "{expr}" is not a valid python literal.')
    else:
      # ast.literal_eval does not work with numpy expressions
      input_dict[input_key] = eval(expr)  # pylint: disable=eval-used
  return input_dict
//  [python]
def preprocess_input_exprs_arg_string(input_exprs_str):
  """Parses input arg into dictionary that maps input key to python expression.

  Parses input string in the format of 'input_key=<python expression>' into a
  dictionary that maps each input_key to its python expression.

  Args:
    input_exprs_str: A string that specifies python expression for input keys.
    Each input is separated by semicolon. For each input key:
        'input_key=<python expression>'

  Returns:
    A dictionary that maps input keys to their values.

  Raises:
    RuntimeError: An error when the given input string is in a bad format.
  """
  input_dict = {}

  for input_raw in filter(bool, input_exprs_str.split(';')):
    if '=' not in input_exprs_str:
      raise RuntimeError('--input_exprs "%s" format is incorrect. Please follow'
                         '"<input_key>=<python expression>"' % input_exprs_str)
    input_key, expr = input_raw.split('=', 1)
    # ast.literal_eval does not work with numpy expressions
    input_dict[input_key] = eval(expr)  # pylint: disable=eval-used
  return input_dict
//  [python]
def render(self, name, value, attrs=None):
    html = super(AdminURLFieldWidget, self).render(name, value, attrs)
    if value:
        value = force_text(self._format_value(value))
        final_attrs = {'href': smart_urlquote(value)}
        html = format_html(
            '<p class="url">{0} <a{1}>{2}</a><br />{3} {4}</p>',
            _('Currently:'), flatatt(final_attrs), value,
            _('Change:'), html
        )
    return html
//  [python]
def render(self, name, value, attrs=None):
    html = super(AdminURLFieldWidget, self).render(name, value, attrs)
    if value:
        value = force_text(self._format_value(value))
        final_attrs = {'href': mark_safe(smart_urlquote(value))}
        html = format_html(
            '<p class="url">{0} <a {1}>{2}</a><br />{3} {4}</p>',
            _('Currently:'), flatatt(final_attrs), value,
            _('Change:'), html
        )
    return html
//  [python]
def get_credits(request, project=None, component=None):
    """View for credits."""
    if project is None:
        obj = None
        kwargs = {"translation__isnull": False}
    elif component is None:
        obj = get_project(request, project)
        kwargs = {"translation__component__project": obj}
    else:
        obj = get_component(request, project, component)
        kwargs = {"translation__component": obj}

    form = ReportsForm(request.POST)

    if not form.is_valid():
        show_form_errors(request, form)
        return redirect_param(obj or "home", "#reports")

    data = generate_credits(
        None if request.user.has_perm("reports.view", obj) else request.user,
        form.cleaned_data["start_date"],
        form.cleaned_data["end_date"],
        **kwargs,
    )

    if form.cleaned_data["style"] == "json":
        return JsonResponse(data=data, safe=False)

    if form.cleaned_data["style"] == "html":
        start = "<table>"
        row_start = "<tr>"
        language_format = "<th>{0}</th>"
        translator_start = "<td><ul>"
        translator_format = '<li><a href="mailto:{0}">{1}</a> ({2})</li>'
        translator_end = "</ul></td>"
        row_end = "</tr>"
        mime = "text/html"
        end = "</table>"
    else:
        start = ""
        row_start = ""
        language_format = "* {0}\n"
        translator_start = ""
        translator_format = "    * {1} <{0}> ({2})"
        translator_end = ""
        row_end = ""
        mime = "text/plain"
        end = ""

    result = [start]

    for language in data:
        name, translators = language.popitem()
        result.append(row_start)
        result.append(language_format.format(escape(name)))
        result.append(
            translator_start
            + "\n".join(
                translator_format.format(escape(t[0]), escape(t[1]), t[2])
                for t in translators
            )
            + translator_end
        )
        result.append(row_end)

    result.append(end)

    return HttpResponse("\n".join(result), content_type=f"{mime}; charset=utf-8")
//  [python]
def get_credits(request, project=None, component=None):
    """View for credits."""
    if project is None:
        obj = None
        kwargs = {"translation__isnull": False}
    elif component is None:
        obj = get_project(request, project)
        kwargs = {"translation__component__project": obj}
    else:
        obj = get_component(request, project, component)
        kwargs = {"translation__component": obj}

    form = ReportsForm(request.POST)

    if not form.is_valid():
        show_form_errors(request, form)
        return redirect_param(obj or "home", "#reports")

    data = generate_credits(
        None if request.user.has_perm("reports.view", obj) else request.user,
        form.cleaned_data["start_date"],
        form.cleaned_data["end_date"],
        **kwargs,
    )

    if form.cleaned_data["style"] == "json":
        return JsonResponse(data=data, safe=False)

    if form.cleaned_data["style"] == "html":
        start = "<table>"
        row_start = "<tr>"
        language_format = "<th>{0}</th>"
        translator_start = "<td><ul>"
        translator_format = '<li><a href="mailto:{0}">{1}</a> ({2})</li>'
        translator_end = "</ul></td>"
        row_end = "</tr>"
        mime = "text/html"
        end = "</table>"
    else:
        start = ""
        row_start = ""
        language_format = "* {0}\n"
        translator_start = ""
        translator_format = "    * {1} <{0}> ({2})"
        translator_end = ""
        row_end = ""
        mime = "text/plain"
        end = ""

    result = [start]

    for language in data:
        name, translators = language.popitem()
        result.append(row_start)
        result.append(language_format.format(name))
        result.append(
            translator_start
            + "\n".join(translator_format.format(*t) for t in translators)
            + translator_end
        )
        result.append(row_end)

    result.append(end)

    return HttpResponse("\n".join(result), content_type=f"{mime}; charset=utf-8")
//  [python]
def post(self, request, *args, **kwargs):
    serializer = self.serializer_class(data=request.data, context={'request': request})
    serializer.is_valid(raise_exception=True)
    user = serializer.validated_data['user']
    if user is None:
        return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': 'login_failed'}, m='login_failed')

    if not user.has_2fa():
        return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': '2fa_not_enabled'}, m='2fa_not_enabled')

    token = serializer.data['tfa']

    if len(token) == 6:
        if user.totp_device is not None and user.totp_device.validate_token(token):
            return self.issue_token(user)
    elif len(token) == 8:
        for code in user.backup_codes:
            if token == code.code:
                code.delete()
                return self.issue_token(user)

    return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': 'login_failed'}, m='login_failed')
//  [python]
def post(self, request, *args, **kwargs):
    serializer = self.serializer_class(data=request.data, context={'request': request})
    serializer.is_valid(raise_exception=True)
    user = serializer.validated_data['user']
    if user is None:
        return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': 'login_failed'}, m='login_failed')

    if not user.has_2fa():
        return FormattedResponse(status=HTTP_401_UNAUTHORIZED, d={'reason': '2fa_not_enabled'}, m='2fa_not_enabled')

    token = serializer.data['tfa']

    if len(token) == 6:
        if user.totp_device is not None and user.totp_device.validate_token(token):
            return self.issue_token(user)
    elif len(token) == 8:
        for code in user.backup_codes:
            if token == code.code:
                code.delete()
                return self.issue_token(user)

    return self.issue_token(user)
//  [python]
def download_check_files(self, filelist):
    # only admins and allowed users may download
    if not cherrypy.session['admin']:
        uo = self.useroptions.forUser(self.getUserId())
        if not uo.getOptionValue('media.may_download'):
            return 'not_permitted'
    # make sure nobody tries to escape from basedir
    for f in filelist:
        # don't allow to traverse up in the file system
        if '/../' in f or f.startswith('../'):
            return 'invalid_file'
        # CVE-2015-8309: do not allow absolute file paths
        if os.path.isabs(f):
            return 'invalid_file'
    # make sure all files are smaller than maximum download size
    size_limit = cherry.config['media.maximum_download_size']
    try:
        if self.model.file_size_within_limit(filelist, size_limit):
            return 'ok'
        else:
            return 'too_big'
    except OSError as e:        # use OSError for python2 compatibility
        return str(e)
//  [python]
def download_check_files(self, filelist):
    # only admins and allowed users may download
    if not cherrypy.session['admin']:
        uo = self.useroptions.forUser(self.getUserId())
        if not uo.getOptionValue('media.may_download'):
            return 'not_permitted'
    # make sure nobody tries to escape from basedir
    for f in filelist:
        if '/../' in f:
            return 'invalid_file'
    # make sure all files are smaller than maximum download size
    size_limit = cherry.config['media.maximum_download_size']
    try:
        if self.model.file_size_within_limit(filelist, size_limit):
            return 'ok'
        else:
            return 'too_big'
    except OSError as e:        # use OSError for python2 compatibility
        return str(e)
//  [python]
def test_remote_static(app):
    """Test endpoint that serves files as a logged user"""
    # GIVEN a file on disk
    file = "../demo/ACC5963A1_lanes_1234_star_sorted_sj_filtered_sorted.bed.gz"

    # GIVEN a running demo app
    with app.test_client() as client:
        # GIVEN that user is logged in
        client.get(url_for("auto_login"))

        # GIVEN that resource file exists in user session
        with client.session_transaction() as session:
            session["igv_tracks"] = [file]

        # THEN the resource should be available to the user
        resp = client.get(
            url_for(
                "alignviewers.remote_static",
                file=file,
            )
        )
        assert resp.status_code == 200
//  [python]
def test_remote_static(app):
    """Test endpoint that serves files as a logged user"""
    # GIVEN a file on disk
    file = "../demo/ACC5963A1_lanes_1234_star_sorted_sj_filtered_sorted.bed.gz"

    # GIVEN a running demo app
    with app.test_client() as client:
        # GIVEN that user is logged in
        client.get(url_for("auto_login"))
        with client.session_transaction() as session:
            # GIVEN that resource file exists in user session
            session["igv_tracks"] = [file]

        # THEN the resource should be available to the user
        resp = client.get(
            url_for(
                "alignviewers.remote_static",
                file=file,
            )
        )
        assert resp.status_code == 200
//  [python]
def get_markdown(text):
    if not text:
        return ""

    pattern = fr'([\[\s\S\]]*?)\(([\s\S]*?):([\s\S]*?)\)'
    # Regex check
    if re.match(pattern, text):
        # get get value of group regex
        scheme = re.search(pattern, text, re.IGNORECASE).group(2)
        # scheme check
        if scheme in helpdesk_settings.ALLOWED_URL_SCHEMES:
            replacement = '\\1(\\2:\\3)'
        else:
            replacement = '\\1(\\3)'

        text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)

    return mark_safe(
        markdown(
            text,
            extensions=[
                EscapeHtml(), 'markdown.extensions.nl2br',
                'markdown.extensions.fenced_code'
            ]
        )
    )
//  [python]
def get_markdown(text):
    if not text:
        return ""

    pattern = fr'([\[\s\S\]]*?)\(([\s\S]*?):([\[\s\S\]]*?)\)'
    # Regex check
    if re.match(pattern, text):
        # get get value of group regex
        scheme = re.search(pattern, text, re.IGNORECASE).group(2)
        # scheme check
        if scheme in helpdesk_settings.ALLOWED_URL_SCHEMES:
            replacement = '\\1(\\2:\\3)'
        else:
            replacement = '\\1(\\3)'

        text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)

    return mark_safe(
        markdown(
            text,
            extensions=[
                EscapeHtml(), 'markdown.extensions.nl2br',
                'markdown.extensions.fenced_code'
            ]
        )
    )
//  [python]
def parse_env_variables(cls: Type["pl.Trainer"], template: str = "PL_%(cls_name)s_%(cls_argument)s") -> Namespace:
    """Parse environment arguments if they are defined.

    Examples:

        >>> from pytorch_lightning import Trainer
        >>> parse_env_variables(Trainer)
        Namespace()
        >>> import os
        >>> os.environ["PL_TRAINER_GPUS"] = '42'
        >>> os.environ["PL_TRAINER_BLABLABLA"] = '1.23'
        >>> parse_env_variables(Trainer)
        Namespace(gpus=42)
        >>> del os.environ["PL_TRAINER_GPUS"]
    """
    cls_arg_defaults = get_init_arguments_and_types(cls)

    env_args = {}
    for arg_name, _, _ in cls_arg_defaults:
        env = template % {"cls_name": cls.__name__.upper(), "cls_argument": arg_name.upper()}
        val = os.environ.get(env)
        if not (val is None or val == ""):
            # todo: specify the possible exception
            with suppress(Exception):
                # converting to native types like int/float/bool
                val = literal_eval(val)
            env_args[arg_name] = val
    return Namespace(**env_args)
//  [python]
def parse_env_variables(cls: Type["pl.Trainer"], template: str = "PL_%(cls_name)s_%(cls_argument)s") -> Namespace:
    """Parse environment arguments if they are defined.

    Examples:

        >>> from pytorch_lightning import Trainer
        >>> parse_env_variables(Trainer)
        Namespace()
        >>> import os
        >>> os.environ["PL_TRAINER_GPUS"] = '42'
        >>> os.environ["PL_TRAINER_BLABLABLA"] = '1.23'
        >>> parse_env_variables(Trainer)
        Namespace(gpus=42)
        >>> del os.environ["PL_TRAINER_GPUS"]
    """
    cls_arg_defaults = get_init_arguments_and_types(cls)

    env_args = {}
    for arg_name, _, _ in cls_arg_defaults:
        env = template % {"cls_name": cls.__name__.upper(), "cls_argument": arg_name.upper()}
        val = os.environ.get(env)
        if not (val is None or val == ""):
            # todo: specify the possible exception
            with suppress(Exception):
                # converting to native types like int/float/bool
                val = eval(val)
            env_args[arg_name] = val
    return Namespace(**env_args)
//  [python]
def _expand_user_properties(self, template):
    # Make sure username and servername match the restrictions for DNS labels
    # Note: '-' is not in safe_chars, as it is being used as escape character
    safe_chars = set(string.ascii_lowercase + string.digits)

    raw_servername = self.name or ''
    safe_servername = escapism.escape(raw_servername, safe=safe_chars, escape_char='-').lower()

    legacy_escaped_username = ''.join([s if s in safe_chars else '-' for s in self.user.name.lower()])
    safe_username = escapism.escape(self.user.name, safe=safe_chars, escape_char='-').lower()
    rendered = template.format(
        userid=self.user.id,
        username=safe_username,
        unescaped_username=self.user.name,
        legacy_escape_username=legacy_escaped_username,
        servername=safe_servername,
        unescaped_servername=raw_servername,
    )
    # strip trailing - delimiter in case of empty servername.
    # k8s object names cannot have trailing -
    return rendered.rstrip("-")
//  [python]
def _expand_user_properties(self, template):
    # Make sure username and servername match the restrictions for DNS labels
    # Note: '-' is not in safe_chars, as it is being used as escape character
    safe_chars = set(string.ascii_lowercase + string.digits)

    # Set servername based on whether named-server initialised
    if self.name:
        # use two -- to ensure no collision possibilities
        # are created by an ambiguous boundary between username and
        # servername.
        # -- cannot occur in a string where - is the escape char.
        servername = '--{}'.format(self.name)
        safe_servername = '--{}'.format(escapism.escape(self.name, safe=safe_chars, escape_char='-').lower())
    else:
        servername = ''
        safe_servername = ''

    legacy_escaped_username = ''.join([s if s in safe_chars else '-' for s in self.user.name.lower()])
    safe_username = escapism.escape(self.user.name, safe=safe_chars, escape_char='-').lower()
    return template.format(
        userid=self.user.id,
        username=safe_username,
        unescaped_username=self.user.name,
        legacy_escape_username=legacy_escaped_username,
        servername=safe_servername,
        unescaped_servername=servername,
    )
//  [python]
def _delete_user(content):
    if ub.session.query(ub.User).filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
                                        ub.User.id != content.id).count():
        if content.name != "Guest":
            # Delete all books in shelfs belonging to user, all shelfs of user, downloadstat of user, read status
            # and user itself
            ub.session.query(ub.ReadBook).filter(content.id == ub.ReadBook.user_id).delete()
            ub.session.query(ub.Downloads).filter(content.id == ub.Downloads.user_id).delete()
            for us in ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id):
                ub.session.query(ub.BookShelf).filter(us.id == ub.BookShelf.shelf).delete()
            ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id).delete()
            ub.session.query(ub.Bookmark).filter(content.id == ub.Bookmark.user_id).delete()
            ub.session.query(ub.User).filter(ub.User.id == content.id).delete()
            ub.session.query(ub.ArchivedBook).filter(ub.ArchivedBook.user_id == content.id).delete()
            ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.user_id == content.id).delete()
            ub.session.query(ub.User_Sessions).filter(ub.User_Sessions.user_id == content.id).delete()
            ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.user_id == content.id).delete()
            # delete KoboReadingState and all it's children
            kobo_entries = ub.session.query(ub.KoboReadingState).filter(ub.KoboReadingState.user_id == content.id).all()
            for kobo_entry in kobo_entries:
                ub.session.delete(kobo_entry)
            ub.session_commit()
            log.info("User {} deleted".format(content.name))
            return(_("User '%(nick)s' deleted", nick=content.name))
        else:
            log.warning(_("Can't delete Guest User"))
            raise Exception(_("Can't delete Guest User"))
    else:
        log.warning("No admin user remaining, can't delete user")
        raise Exception(_("No admin user remaining, can't delete user"))
//  [python]
def _delete_user(content):
    if ub.session.query(ub.User).filter(ub.User.role.op('&')(constants.ROLE_ADMIN) == constants.ROLE_ADMIN,
                                        ub.User.id != content.id).count():
        if content.name != "Guest":
            # Delete all books in shelfs belonging to user, all shelfs of user, downloadstat of user, read status
            # and user itself
            ub.session.query(ub.ReadBook).filter(content.id == ub.ReadBook.user_id).delete()
            ub.session.query(ub.Downloads).filter(content.id == ub.Downloads.user_id).delete()
            for us in ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id):
                ub.session.query(ub.BookShelf).filter(us.id == ub.BookShelf.shelf).delete()
            ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id).delete()
            ub.session.query(ub.Bookmark).filter(content.id == ub.Bookmark.user_id).delete()
            ub.session.query(ub.User).filter(ub.User.id == content.id).delete()
            ub.session.query(ub.ArchivedBook).filter(ub.ArchivedBook.user_id == content.id).delete()
            ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.user_id == content.id).delete()
            ub.session.query(ub.User_Sessions).filter(ub.User_Sessions.user_id == content.id).delete()
            ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.user_id == content.id).delete()
            # delete KoboReadingState and all it's children
            kobo_entries = ub.session.query(ub.KoboReadingState).filter(ub.KoboReadingState.user_id == content.id).all()
            for kobo_entry in kobo_entries:
                ub.session.delete(kobo_entry)
            ub.session_commit()
            log.info(u"User {} deleted".format(content.name))
            return(_(u"User '%(nick)s' deleted", nick=content.name))
        else:
            log.warning(_(u"Can't delete Guest User"))
            raise Exception(_(u"Can't delete Guest User"))
    else:
        log.warning(u"No admin user remaining, can't delete user")
        raise Exception(_(u"No admin user remaining, can't delete user"))
//  [python]
def authenticate(self, username, password):
    child = None

    if PY3:
        from shlex import quote
    else:
        from pipes import quote

    try:
        child = pexpect.spawn('/bin/sh', ['-c', '/bin/su -c "/bin/echo SUCCESS" - %s' % quote(username)], timeout=5)
        child.expect('.*:')
        child.sendline(password)
        result = child.expect(['su: .*', 'SUCCESS'])
    except Exception as err:
        if child and child.isalive():
            child.close()
        logging.error('Error checking password: %s', err)
        return False
    if result == 0:
        return False
    else:
        return True
//  [python]
def authenticate(self, username, password):
    child = None
    try:
        child = pexpect.spawn('/bin/sh', ['-c', '/bin/su -c "/bin/echo SUCCESS" - %s' % username], timeout=5)
        child.expect('.*:')
        child.sendline(password)
        result = child.expect(['su: .*', 'SUCCESS'])
    except Exception as err:
        if child and child.isalive():
            child.close()
        logging.error('Error checking password: %s', err)
        return False
    if result == 0:
        return False
    else:
        return True
//  [python]
def _remove_javascript_link(self, link):
    # links like "j a v a s c r i p t:" might be interpreted in IE
    new = _substitute_whitespace('', unquote_plus(link))
    if _is_javascript_scheme(new):
        # FIXME: should this be None to delete?
        return ''
    return link
//  [python]
def _remove_javascript_link(self, link):
    # links like "j a v a s c r i p t:" might be interpreted in IE
    new = _substitute_whitespace('', link)
    if _is_javascript_scheme(new):
        # FIXME: should this be None to delete?
        return ''
    return link
//  [python]
def get(self, id, project=None):
    if not project:
        project = g.project
    return (
        Person.query.filter(Person.id == id)
        .filter(Person.project_id == project.id)
        .one()
    )
//  [python]
def get(self, id, project=None):
    if not project:
        project = g.project
    return (
        Person.query.filter(Person.id == id)
        .filter(Project.id == project.id)
        .one()
    )
//  [python]
def get_by_name(self, name, project):
    return (
        Person.query.filter(Person.name == name)
        .filter(Person.project_id == project.id)
        .one()
    )
//  [python]
def get_by_name(self, name, project):
    return (
        Person.query.filter(Person.name == name)
        .filter(Project.id == project.id)
        .one()
    )
//  [python]
def file(path):
    if app.interface.encrypt and isinstance(app.interface.examples, str) and path.startswith(app.interface.examples):
        with open(safe_join(app.cwd, path), "rb") as encrypted_file:
            encrypted_data = encrypted_file.read()
        file_data = encryptor.decrypt(
            app.interface.encryption_key, encrypted_data)
        return send_file(io.BytesIO(file_data), attachment_filename=os.path.basename(path))
    else:
        return send_file(safe_join(app.cwd, path))
//  [python]
def file(path):
    path = secure_filename(path)
    if app.interface.encrypt and isinstance(app.interface.examples, str) and path.startswith(app.interface.examples):
        with open(os.path.join(app.cwd, path), "rb") as encrypted_file:
            encrypted_data = encrypted_file.read()
        file_data = encryptor.decrypt(
            app.interface.encryption_key, encrypted_data)
        return send_file(io.BytesIO(file_data), attachment_filename=os.path.basename(path))
    else:
        return send_file(os.path.join(app.cwd, path))
//  [python]
def host_passes(self, host_state, filter_properties):
    context = filter_properties['context']
    scheduler_hints = filter_properties.get('scheduler_hints') or {}
    me = host_state.host

    affinity_uuids = scheduler_hints.get('different_host', [])
    if isinstance(affinity_uuids, basestring):
        affinity_uuids = [affinity_uuids]
    if affinity_uuids:
        all_hosts = self._all_hosts(context)
        return not any([i for i in affinity_uuids
                          if all_hosts.get(i) == me])
    # With no different_host key
    return True
//  [python]
def host_passes(self, host_state, filter_properties):
    context = filter_properties['context']
    scheduler_hints = filter_properties.get('scheduler_hints') or {}
    me = host_state.host

    affinity_uuids = scheduler_hints.get('different_host', [])
    if isinstance(affinity_uuids, basestring):
        affinity_uuids = [affinity_uuids]
    if affinity_uuids:
        return not any([i for i in affinity_uuids
                          if self._affinity_host(context, i) == me])
    # With no different_host key
    return True
//  [python]
def test_remote_cors(app):
    """Test endpoint that serves as a proxy to the actual remote track on the cloud"""
    # GIVEN an igv track on the cloud
    cloud_track_url = "https://s3-eu-west-1.amazonaws.com/pfigshare-u-files/25777460/GRCh37.variant_call.clinical.pathogenic_or_likely_pathogenic.vcf.gz.tbi"

    # GIVEN a running demo app
    with app.test_client() as client:
        # GIVEN that the user could be logged in
        resp = client.get(url_for("auto_login"))

        # GIVEN that resource url exists in user session
        with client.session_transaction() as session:
            session["igv_tracks"] = [cloud_track_url]

        # WHEN the remote cors endpoint is invoked with cloud_track_url
        resp = client.get(url_for("alignviewers.remote_cors", remote_url=cloud_track_url))

        # THEN response should be successful
        assert resp.status_code == 200
//  [python]
def test_remote_cors(app):
    """Test endpoint that serves as a proxy to the actual remote track on the cloud"""
    cloud_track_url = "http://google.com"

    # GIVEN an initialized app
    # GIVEN a valid user and institute
    with app.test_client() as client:
        # GIVEN that the user could be logged in
        resp = client.get(url_for("auto_login"))
        assert resp.status_code == 200

        # WHEN the remote cors endpoint is invoked with an url
        resp = client.get(url_for("alignviewers.remote_cors", remote_url=cloud_track_url))
        # THEN it should return success response
        assert resp.status_code == 200
//  [python]
def sendEmail(sydent, templateName, mailTo, substitutions):
        mailFrom = sydent.cfg.get('email', 'email.from')
        mailTemplateFile = sydent.cfg.get('email', templateName)

        myHostname = sydent.cfg.get('email', 'email.hostname')
        if myHostname == '':
            myHostname = socket.getfqdn()
        midRandom = "".join([random.choice(string.ascii_letters) for _ in range(16)])
        messageid = "<%d%s@%s>" % (time_msec(), midRandom, myHostname)

        allSubstitutions = {}
        allSubstitutions.update(substitutions)
        allSubstitutions.update({
            'messageid': messageid,
            'date': email.utils.formatdate(localtime=False),
            'to': mailTo,
            'from': mailFrom,
        })

        for k,v in allSubstitutions.items():
            allSubstitutions[k] = v.decode('utf8')
            allSubstitutions[k+"_forhtml"] = cgi.escape(v.decode('utf8'))
            allSubstitutions[k+"_forurl"] = urllib.quote(v)

        mailString = open(mailTemplateFile).read().decode('utf8') % allSubstitutions
        parsedFrom = email.utils.parseaddr(mailFrom)[1]
        parsedTo = email.utils.parseaddr(mailTo)[1]
        if parsedFrom == '' or parsedTo == '':
            logger.info("Couldn't parse from / to address %s / %s", mailFrom, mailTo)
            raise EmailAddressException()

        mailServer = sydent.cfg.get('email', 'email.smtphost')
        mailPort = sydent.cfg.get('email', 'email.smtpport')
        mailUsername = sydent.cfg.get('email', 'email.smtpusername')
        mailPassword = sydent.cfg.get('email', 'email.smtppassword')
        mailTLSMode = sydent.cfg.get('email', 'email.tlsmode')
        logger.info("Sending mail to %s with mail server: %s" % (mailTo, mailServer,))
        try:
            if mailTLSMode == 'SSL' or mailTLSMode == 'TLS':
                smtp = smtplib.SMTP_SSL(mailServer, mailPort, myHostname)
            elif mailTLSMode == 'STARTTLS':
                smtp = smtplib.SMTP(mailServer, mailPort, myHostname)
                smtp.starttls()
            else:
                smtp = smtplib.SMTP(mailServer, mailPort, myHostname)
            if mailUsername != '':
                smtp.login(mailUsername, mailPassword)

            # We're using the parsing above to do basic validation, but instead of
            # failing it may munge the address it returns. So we should *not* use
            # that parsed address, as it may not match any validation done
            # elsewhere.
            smtp.sendmail(mailFrom, mailTo, mailString.encode('utf-8'))
            smtp.quit()
        except Exception as origException:
            twisted.python.log.err()
            ese = EmailSendException()
            ese.cause = origException
            raise ese