jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def recipe_from_source(request):
    """
    function to retrieve a recipe from a given url or source string
    :param request: standard request with additional post parameters
            - url: url to use for importing recipe
            - data: if no url is given recipe is imported from provided source data
            - (optional) bookmarklet: id of bookmarklet import to use, overrides URL and data attributes
    :return: JsonResponse containing the parsed json, original html,json and images
    """
    if request.method == 'GET':
        return HttpResponse(status=405)
    request_payload = json.loads(request.body.decode('utf-8'))
    url = request_payload.get('url', None)
    data = request_payload.get('data', None)
    bookmarklet = request_payload.get('bookmarklet', None)

    if bookmarklet := BookmarkletImport.objects.filter(pk=bookmarklet).first():
        url = bookmarklet.url
        data = bookmarklet.html
        bookmarklet.delete()

    # headers to use for request to external sites
    external_request_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7"}

    if not url and not data:
        return JsonResponse({
            'error': True,
            'msg': _('Nothing to do.')
        }, status=400)

    # in manual mode request complete page to return it later
    if url:
        try:
            if validators.url(url, public=True):
                data = requests.get(url, headers=external_request_headers).content
            else:
                return JsonResponse({
                    'error': True,
                    'msg': _('Invalid Url')
                }, status=400)
        except requests.exceptions.ConnectionError:
            return JsonResponse({
                'error': True,
                'msg': _('Connection Refused.')
            }, status=400)
        except requests.exceptions.MissingSchema:
            return JsonResponse({
                'error': True,
                'msg': _('Bad URL Schema.')
            }, status=400)

    recipe_json, recipe_tree, recipe_html, recipe_images = get_recipe_from_source(data, url, request)
    if len(recipe_tree) == 0 and len(recipe_json) == 0:
        return JsonResponse({
            'error': True,
            'msg': _('No usable data could be found.')
        }, status=400)
    else:
        return JsonResponse({
            'recipe_json': recipe_json,
            'recipe_tree': recipe_tree,
            'recipe_html': recipe_html,
            'recipe_images': list(dict.fromkeys(recipe_images)),
        })
//  [python]
def recipe_from_source(request):
    """
    function to retrieve a recipe from a given url or source string
    :param request: standard request with additional post parameters
            - url: url to use for importing recipe
            - data: if no url is given recipe is imported from provided source data
            - (optional) bookmarklet: id of bookmarklet import to use, overrides URL and data attributes
    :return: JsonResponse containing the parsed json, original html,json and images
    """
    if request.method == 'GET':
        return HttpResponse(status=405)
    request_payload = json.loads(request.body.decode('utf-8'))
    url = request_payload.get('url', None)
    data = request_payload.get('data', None)
    bookmarklet = request_payload.get('bookmarklet', None)

    if bookmarklet := BookmarkletImport.objects.filter(pk=bookmarklet).first():
        url = bookmarklet.url
        data = bookmarklet.html
        bookmarklet.delete()

    # headers to use for request to external sites
    external_request_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7"}

    if not url and not data:
        return JsonResponse({
            'error': True,
            'msg': _('Nothing to do.')
        }, status=400)

    # in manual mode request complete page to return it later
    if url:
        try:
            data = requests.get(url, headers=external_request_headers).content
        except requests.exceptions.ConnectionError:
            return JsonResponse({
                'error': True,
                'msg': _('Connection Refused.')
            }, status=400)
        except requests.exceptions.MissingSchema:
            return JsonResponse({
                'error': True,
                'msg': _('Bad URL Schema.')
            }, status=400)
    recipe_json, recipe_tree, recipe_html, recipe_images = get_recipe_from_source(data, url, request)
    if len(recipe_tree) == 0 and len(recipe_json) == 0:
        return JsonResponse({
            'error': True,
            'msg': _('No usable data could be found.')
        }, status=400)
    else:
        return JsonResponse({
            'recipe_json': recipe_json,
            'recipe_tree': recipe_tree,
            'recipe_html': recipe_html,
            'recipe_images': list(dict.fromkeys(recipe_images)),
        })
//  [python]
def unarchive(byte_array: bytes, directory: Text) -> Text:
    """Tries to unpack a byte array interpreting it as an archive.

    Tries to use tar first to unpack, if that fails, zip will be used."""

    try:
        tar = TarSafe.open(fileobj=IOReader(byte_array))
        tar.extractall(directory)
        tar.close()
        return directory
    except tarfile.TarError:
        zip_ref = zipfile.ZipFile(IOReader(byte_array))
        zip_ref.extractall(directory)
        zip_ref.close()
        return directory
//  [python]
def unarchive(byte_array: bytes, directory: Text) -> Text:
    """Tries to unpack a byte array interpreting it as an archive.

    Tries to use tar first to unpack, if that fails, zip will be used."""

    try:
        tar = tarfile.open(fileobj=IOReader(byte_array))
        tar.extractall(directory)
        tar.close()
        return directory
    except tarfile.TarError:
        zip_ref = zipfile.ZipFile(IOReader(byte_array))
        zip_ref.extractall(directory)
        zip_ref.close()
        return directory
//  [python]
def _lookup(self, name, *args, **kwargs):
    instance = self._lookup_loader.get(name.lower(), loader=self._loader, templar=self)

    if instance is not None:
        wantlist = kwargs.pop('wantlist', False)
        allow_unsafe = kwargs.pop('allow_unsafe', C.DEFAULT_ALLOW_UNSAFE_LOOKUPS)

        from ansible.utils.listify import listify_lookup_plugin_terms
        loop_terms = listify_lookup_plugin_terms(terms=args, templar=self, loader=self._loader, fail_on_undefined=True, convert_bare=False)
        # safely catch run failures per #5059
        try:
            ran = instance.run(loop_terms, variables=self._available_variables, **kwargs)
        except (AnsibleUndefinedVariable, UndefinedError) as e:
            raise AnsibleUndefinedVariable(e)
        except Exception as e:
            if self._fail_on_lookup_errors:
                raise AnsibleError("An unhandled exception occurred while running the lookup plugin '%s'. Error was a %s, "
                                   "original message: %s" % (name, type(e), e))
            ran = None

        if ran and not allow_unsafe:
            from ansible.vars.unsafe_proxy import UnsafeProxy, wrap_var
            if wantlist:
                ran = wrap_var(ran)
            else:
                try:
                    ran = UnsafeProxy(",".join(ran))
                except TypeError:
                    if isinstance(ran, list) and len(ran) == 1:
                        ran = wrap_var(ran[0])
                    else:
                        ran = wrap_var(ran)

            if self.cur_context:
                self.cur_context.unsafe = True
        return ran
    else:
        raise AnsibleError("lookup plugin (%s) not found" % name)
//  [python]
def _lookup(self, name, *args, **kwargs):
    instance = self._lookup_loader.get(name.lower(), loader=self._loader, templar=self)

    if instance is not None:
        wantlist = kwargs.pop('wantlist', False)

        from ansible.utils.listify import listify_lookup_plugin_terms
        loop_terms = listify_lookup_plugin_terms(terms=args, templar=self, loader=self._loader, fail_on_undefined=True, convert_bare=False)
        # safely catch run failures per #5059
        try:
            ran = instance.run(loop_terms, variables=self._available_variables, **kwargs)
        except (AnsibleUndefinedVariable, UndefinedError) as e:
            raise AnsibleUndefinedVariable(e)
        except Exception as e:
            if self._fail_on_lookup_errors:
                raise AnsibleError("An unhandled exception occurred while running the lookup plugin '%s'. Error was a %s, "
                                   "original message: %s" % (name, type(e), e))
            ran = None

        if ran:
            if wantlist:
                ran = wrap_var(ran)
            else:
                try:
                    ran = UnsafeProxy(",".join(ran))
                except TypeError:
                    if isinstance(ran, list) and len(ran) == 1:
                        ran = wrap_var(ran[0])
                    else:
                        ran = wrap_var(ran)

        return ran
    else:
        raise AnsibleError("lookup plugin (%s) not found" % name)
//  [python]
def whitelist(fn):
	"""Decorator: Whitelist method to be called remotely via REST API."""
	frappe.whitelist()(fn)
	return fn
//  [python]
def whitelist(f):
	"""Decorator: Whitelist method to be called remotely via REST API."""
	f.whitelisted = True
	return f
//  [python]
def event_from_pdu_json(pdu_json, outlier=False):
    """Construct a FrozenEvent from an event json received over federation

    Args:
        pdu_json (object): pdu as received over federation
        outlier (bool): True to mark this event as an outlier

    Returns:
        FrozenEvent

    Raises:
        SynapseError: if the pdu is missing required fields or is otherwise
            not a valid matrix event
    """
    # we could probably enforce a bunch of other fields here (room_id, sender,
    # origin, etc etc)
    assert_params_in_request(pdu_json, ('event_id', 'type', 'depth'))

    depth = pdu_json['depth']
    if not isinstance(depth, six.integer_types):
        raise SynapseError(400, "Depth %r not an intger" % (depth, ),
                           Codes.BAD_JSON)

    if depth < 0:
        raise SynapseError(400, "Depth too small", Codes.BAD_JSON)
    elif depth > MAX_DEPTH:
        raise SynapseError(400, "Depth too large", Codes.BAD_JSON)

    event = FrozenEvent(
        pdu_json
    )

    event.internal_metadata.outlier = outlier

    return event
//  [python]
def event_from_pdu_json(pdu_json, outlier=False):
    """Construct a FrozenEvent from an event json received over federation

    Args:
        pdu_json (object): pdu as received over federation
        outlier (bool): True to mark this event as an outlier

    Returns:
        FrozenEvent

    Raises:
        SynapseError: if the pdu is missing required fields
    """
    # we could probably enforce a bunch of other fields here (room_id, sender,
    # origin, etc etc)
    assert_params_in_request(pdu_json, ('event_id', 'type'))
    event = FrozenEvent(
        pdu_json
    )

    event.internal_metadata.outlier = outlier

    return event
//  [python]
def is_whitelisted(self, method_name):
	method = getattr(self, method_name, None)
	if not fn:
		raise NotFound("Method {0} not found".format(method_name))

	is_whitelisted(getattr(method, '__func__', method))
//  [python]
def is_whitelisted(self, method):
	fn = getattr(self, method, None)
	if not fn:
		raise NotFound("Method {0} not found".format(method))
	elif not getattr(fn, "whitelisted", False):
		raise Forbidden("Method {0} not whitelisted".format(method))
//  [python]
def _notify(self, assoc, attempt):
    """
    Sends data about a new association (and, if necessary, the associated invites)
    to the associated MXID's homeserver.

    :param assoc: The association to send down to the homeserver.
    :type assoc: dict[str, any]
    :param attempt: The number of previous attempts to send this association.
    :type attempt: int
    """
    mxid = assoc["mxid"]
    mxid_parts = mxid.split(":", 1)

    if len(mxid_parts) != 2:
        logger.error(
            "Can't notify on bind for unparseable mxid %s. Not retrying.",
            assoc["mxid"],
        )
        return

    matrix_server = mxid_parts[1]

    if not is_valid_matrix_server_name(matrix_server):
        logger.error(
            "MXID server part '%s' not a valid Matrix server name. Not retrying.",
            matrix_server,
        )
        return

    post_url = "matrix://%s/_matrix/federation/v1/3pid/onbind" % (
        matrix_server,
    )

    logger.info("Making bind callback to: %s", post_url)

    # Make a POST to the chosen Synapse server
    http_client = FederationHttpClient(self.sydent)
    try:
        response = yield http_client.post_json_get_nothing(post_url, assoc, {})
    except Exception as e:
        self._notifyErrback(assoc, attempt, e)
        return

    # If the request failed, try again with exponential backoff
    if response.code != 200:
        self._notifyErrback(
            assoc, attempt, "Non-OK error code received (%d)" % response.code
        )
    else:
        logger.info("Successfully notified on bind for %s" % (mxid,))

        # Skip the deletion step if instructed so by the config.
        if not self.sydent.delete_tokens_on_bind:
            return

        # Only remove sent tokens when they've been successfully sent.
        try:
            joinTokenStore = JoinTokenStore(self.sydent)
            joinTokenStore.deleteTokens(assoc["medium"], assoc["address"])
            logger.info(
                "Successfully deleted invite for %s from the store",
                assoc["address"],
            )
        except Exception:
            logger.exception(
                "Couldn't remove invite for %s from the store",
                assoc["address"],
            )
//  [python]
def _notify(self, assoc, attempt):
    """
    Sends data about a new association (and, if necessary, the associated invites)
    to the associated MXID's homeserver.

    :param assoc: The association to send down to the homeserver.
    :type assoc: dict[str, any]
    :param attempt: The number of previous attempts to send this association.
    :type attempt: int
    """
    mxid = assoc["mxid"]
    mxid_parts = mxid.split(":", 1)

    if len(mxid_parts) != 2:
        logger.error(
            "Can't notify on bind for unparseable mxid %s. Not retrying.",
            assoc["mxid"],
        )
        return

    matrix_server = mxid_parts[1]

    if not is_valid_hostname(matrix_server):
        logger.error(
            "MXID server part '%s' not a valid hostname. Not retrying.",
            matrix_server,
        )
        return

    post_url = "matrix://%s/_matrix/federation/v1/3pid/onbind" % (
        matrix_server,
    )

    logger.info("Making bind callback to: %s", post_url)

    # Make a POST to the chosen Synapse server
    http_client = FederationHttpClient(self.sydent)
    try:
        response = yield http_client.post_json_get_nothing(post_url, assoc, {})
    except Exception as e:
        self._notifyErrback(assoc, attempt, e)
        return

    # If the request failed, try again with exponential backoff
    if response.code != 200:
        self._notifyErrback(
            assoc, attempt, "Non-OK error code received (%d)" % response.code
        )
    else:
        logger.info("Successfully notified on bind for %s" % (mxid,))

        # Skip the deletion step if instructed so by the config.
        if not self.sydent.delete_tokens_on_bind:
            return

        # Only remove sent tokens when they've been successfully sent.
        try:
            joinTokenStore = JoinTokenStore(self.sydent)
            joinTokenStore.deleteTokens(assoc["medium"], assoc["address"])
            logger.info(
                "Successfully deleted invite for %s from the store",
                assoc["address"],
            )
        except Exception as e:
            logger.exception(
                "Couldn't remove invite for %s from the store",
                assoc["address"],
            )
//  [python]
def test_process_request__no_file(self, freeze_upload_folder, rf, caplog):
    request = rf.post(
        "/",
        data={
            "file": "custom/location/tmp/s3file/does_not_exist.txt",
            "s3file": "file",
            "file-s3f-signature": "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc",
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    assert not request.FILES.getlist("file")
    assert (
        "File not found: custom/location/tmp/s3file/does_not_exist.txt"
        in caplog.text
    )
//  [python]
def test_process_request__no_file(self, rf, caplog):
    request = rf.post("/", data={"file": "does_not_exist.txt", "s3file": "file"})
    S3FileMiddleware(lambda x: None)(request)
    assert not request.FILES.getlist("file")
    assert "File not found: does_not_exist.txt" in caplog.text
//  [python]
def publish(self, id_, identity, uow=None):
    """Publish a draft.

    Idea:
        - Get the draft from the data layer (draft is not passed in)
        - Validate it more strictly than when it was originally saved
          (drafts can be incomplete but only complete drafts can be turned
          into records)
        - Create or update associated (published) record with data
    """
    # Get the draft
    draft = self.draft_cls.pid.resolve(id_, registered_only=False)
    self.require_permission(identity, "publish", record=draft)

    # Validate the draft strictly - since a draft can be saved with errors
    # we do a strict validation here to make sure only valid drafts can be
    # published.
    self._validate_draft(identity, draft)

    # Create the record from the draft
    latest_id = draft.versions.latest_id
    record = self.record_cls.publish(draft)

    # Run components
    self.run_components(
        'publish', identity, draft=draft, record=record, uow=uow)

    # Commit and index
    uow.register(RecordCommitOp(record, indexer=self.indexer))
    uow.register(RecordDeleteOp(draft, force=False, indexer=self.indexer))

    if latest_id:
        self._reindex_latest(latest_id, uow=uow)

    return self.result_item(
        self, identity, record, links_tpl=self.links_item_tpl)
//  [python]
def publish(self, id_, identity, uow=None):
    """Publish a draft.

    Idea:
        - Get the draft from the data layer (draft is not passed in)
        - Validate it more strictly than when it was originally saved
          (drafts can be incomplete but only complete drafts can be turned
          into records)
        - Create or update associated (published) record with data
    """
    self.require_permission(identity, "publish")

    # Get the draft
    draft = self.draft_cls.pid.resolve(id_, registered_only=False)

    # Validate the draft strictly - since a draft can be saved with errors
    # we do a strict validation here to make sure only valid drafts can be
    # published.
    self._validate_draft(identity, draft)

    # Create the record from the draft
    latest_id = draft.versions.latest_id
    record = self.record_cls.publish(draft)

    # Run components
    self.run_components(
        'publish', identity, draft=draft, record=record, uow=uow)

    # Commit and index
    uow.register(RecordCommitOp(record, indexer=self.indexer))
    uow.register(RecordDeleteOp(draft, force=False, indexer=self.indexer))

    if latest_id:
        self._reindex_latest(latest_id, uow=uow)

    return self.result_item(
        self, identity, record, links_tpl=self.links_item_tpl)
//  [python]
def test_get_files_from_storage(self, freeze_upload_folder):
    content = b"test_get_files_from_storage"
    name = storage.save(
        "tmp/s3file/test_get_files_from_storage", ContentFile(content)
    )
    files = S3FileMiddleware.get_files_from_storage(
        [os.path.join(storage.aws_location, name)],
        "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc",
    )
    file = next(files)
    assert file.read() == content
//  [python]
def test_get_files_from_storage(self):
    content = b"test_get_files_from_storage"
    name = storage.save(
        "tmp/s3file/test_get_files_from_storage", ContentFile(content)
    )
    files = S3FileMiddleware.get_files_from_storage(
        [os.path.join(storage.aws_location, name)]
    )
    file = next(files)
    assert file.read() == content
//  [python]
def test_process_request__no_location(self, freeze_upload_folder, rf, settings):
    settings.AWS_LOCATION = ""
    uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
    request = rf.post("/", data={"file": uploaded_file})
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"uploaded"

    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    request = rf.post(
        "/",
        data={
            "file": f"tmp/s3file/s3_file.txt",
            "s3file": "file",
            "file-s3f-signature": "scjzm3N8njBQIVSGEhOchtM0TkGyb2U6OXGLVlRUZhY",
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"s3file"
//  [python]
def test_process_request__no_location(self, rf, settings):
    settings.AWS_LOCATION = ""
    uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
    request = rf.post("/", data={"file": uploaded_file})
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"uploaded"

    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    request = rf.post(
        "/", data={"file": "tmp/s3file/s3_file.txt", "s3file": "file"}
    )
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"s3file"
//  [python]
def test_qute_settings_persistence(short_tmpdir, request, quteproc_new):
    """Make sure settings from qute://settings are persistent."""
    args = _base_args(request.config) + ['--basedir', str(short_tmpdir)]
    quteproc_new.start(args)
    quteproc_new.open_path('qute://settings/')
    quteproc_new.send_cmd(':jseval --world main '
                          'cset("search.ignore_case", "always")')

    assert quteproc_new.get_setting('search.ignore_case') == 'always'

    quteproc_new.send_cmd(':quit')
    quteproc_new.wait_for_quit()

    quteproc_new.start(args)
    assert quteproc_new.get_setting('search.ignore_case') == 'always'
//  [python]
def test_qute_settings_persistence(short_tmpdir, request, quteproc_new):
    """Make sure settings from qute://settings are persistent."""
    args = _base_args(request.config) + ['--basedir', str(short_tmpdir)]
    quteproc_new.start(args)
    quteproc_new.open_path(
        'qute://settings/set?option=search.ignore_case&value=always')
    assert quteproc_new.get_setting('search.ignore_case') == 'always'

    quteproc_new.send_cmd(':quit')
    quteproc_new.wait_for_quit()

    quteproc_new.start(args)
    assert quteproc_new.get_setting('search.ignore_case') == 'always'
//  [python]
def render(self, request):
	action = "download"
	if "action" in request.args:
		action = request.args["action"][0]

	if "file" in request.args:
		filename = lenient_force_utf_8(request.args["file"][0])
		filename = sanitise_filename_slashes(os.path.realpath(filename))

		if not os.path.exists(filename):
			return "File '%s' not found" % (filename)

		if action == "stream":
			name = "stream"
			if "name" in request.args:
				name = request.args["name"][0]

			port = config.OpenWebif.port.value
			proto = 'http'
			if request.isSecure():
				port = config.OpenWebif.https_port.value
				proto = 'https'
			ourhost = request.getHeader('host')
			m = re.match('.+\:(\d+)$', ourhost)
			if m is not None:
				port = m.group(1)

			response = "#EXTM3U\n#EXTVLCOPT--http-reconnect=true\n#EXTINF:-1,%s\n%s://%s:%s/file?action=download&file=%s" % (name, proto, request.getRequestHostname(), port, quote(filename))
			request.setHeader("Content-Disposition", 'attachment;filename="%s.m3u"' % name)
			request.setHeader("Content-Type", "application/x-mpegurl")
			return response
		elif action == "delete":
			request.setResponseCode(http.OK)
			return "TODO: DELETE FILE: %s" % (filename)
		elif action == "download":
			request.setHeader("Content-Disposition", "attachment;filename=\"%s\"" % (filename.split('/')[-1]))
			rfile = static.File(filename, defaultType = "application/octet-stream")
			return rfile.render(request)
		else: 
			return "wrong action parameter"

	if "dir" in request.args:
		path = request.args["dir"][0]
		pattern = '*'
		data = []
		if "pattern" in request.args:
			pattern = request.args["pattern"][0]
		directories = []
		files = []
		if fileExists(path):
			try:
				files = glob.glob(path+'/'+pattern)
			except:
				files = []
			files.sort()
			tmpfiles = files[:]
			for x in tmpfiles:
				if os.path.isdir(x):
					directories.append(x + '/')
					files.remove(x)
			data.append({"result": True,"dirs": directories,"files": files})
		else:
			data.append({"result": False,"message": "path %s not exits" % (path)})
		request.setHeader("content-type", "application/json; charset=utf-8")
		return json.dumps(data, indent=2)
//  [python]
def render(self, request):
	action = "download"
	if "action" in request.args:
		action = request.args["action"][0]

	if "file" in request.args:
		filename = request.args["file"][0].decode('utf-8', 'ignore').encode('utf-8')
		filename = re.sub("^/+", "/", os.path.realpath(filename))

		if not os.path.exists(filename):
			return "File '%s' not found" % (filename)

		if action == "stream":
			name = "stream"
			if "name" in request.args:
				name = request.args["name"][0]

			port = config.OpenWebif.port.value
			proto = 'http'
			if request.isSecure():
				port = config.OpenWebif.https_port.value
				proto = 'https'
			ourhost = request.getHeader('host')
			m = re.match('.+\:(\d+)$', ourhost)
			if m is not None:
				port = m.group(1)

			response = "#EXTM3U\n#EXTVLCOPT--http-reconnect=true\n#EXTINF:-1,%s\n%s://%s:%s/file?action=download&file=%s" % (name, proto, request.getRequestHostname(), port, quote(filename))
			request.setHeader("Content-Disposition", 'attachment;filename="%s.m3u"' % name)
			request.setHeader("Content-Type", "application/x-mpegurl")
			return response
		elif action == "delete":
			request.setResponseCode(http.OK)
			return "TODO: DELETE FILE: %s" % (filename)
		elif action == "download":
			request.setHeader("Content-Disposition", "attachment;filename=\"%s\"" % (filename.split('/')[-1]))
			rfile = static.File(filename, defaultType = "application/octet-stream")
			return rfile.render(request)
		else: 
			return "wrong action parameter"

	if "dir" in request.args:
		path = request.args["dir"][0]
		pattern = '*'
		data = []
		if "pattern" in request.args:
			pattern = request.args["pattern"][0]
		directories = []
		files = []
		if fileExists(path):
			try:
				files = glob.glob(path+'/'+pattern)
			except:
				files = []
			files.sort()
			tmpfiles = files[:]
			for x in tmpfiles:
				if os.path.isdir(x):
					directories.append(x + '/')
					files.remove(x)
			data.append({"result": True,"dirs": directories,"files": files})
		else:
			data.append({"result": False,"message": "path %s not exits" % (path)})
		request.setHeader("content-type", "application/json; charset=utf-8")
		return json.dumps(data, indent=2)
//  [python]
def _create(self):
    url = recurly.base_uri() + self.collection_path
    return self.post(url)
//  [python]
def _create(self):
    url = urljoin(recurly.base_uri(), self.collection_path)
    return self.post(url)
//  [python]
def all(cls, **kwargs):
    """Return a `Page` of instances of this `Resource` class from
    its general collection endpoint.

    Only `Resource` classes with specified `collection_path`
    endpoints can be requested with this method. Any provided
    keyword arguments are passed to the API endpoint as query
    parameters.

    """
    url = recurly.base_uri() + cls.collection_path
    if kwargs:
        url = '%s?%s' % (url, urlencode(kwargs))
    return Page.page_for_url(url)
//  [python]
def all(cls, **kwargs):
    """Return a `Page` of instances of this `Resource` class from
    its general collection endpoint.

    Only `Resource` classes with specified `collection_path`
    endpoints can be requested with this method. Any provided
    keyword arguments are passed to the API endpoint as query
    parameters.

    """
    url = urljoin(recurly.base_uri(), cls.collection_path)
    if kwargs:
        url = '%s?%s' % (url, urlencode(kwargs))
    return Page.page_for_url(url)
//  [python]
def extra_view_dispatch(request, view):
    """
    Dispatch to an Xtheme extra view.

    :param request: A request.
    :type request: django.http.HttpRequest
    :param view: View name.
    :type view: str
    :return: A response of some kind.
    :rtype: django.http.HttpResponse
    """
    theme = getattr(request, "theme", None) or get_current_theme(request.shop)
    view_func = get_view_by_name(theme, view)
    if not view_func:
        msg = "Error! %s/%s: Not found." % (getattr(theme, "identifier", None), escape(view))
        return HttpResponseNotFound(msg)
    return view_func(request)
//  [python]
def extra_view_dispatch(request, view):
    """
    Dispatch to an Xtheme extra view.

    :param request: A request.
    :type request: django.http.HttpRequest
    :param view: View name.
    :type view: str
    :return: A response of some kind.
    :rtype: django.http.HttpResponse
    """
    theme = getattr(request, "theme", None) or get_current_theme(request.shop)
    view_func = get_view_by_name(theme, view)
    if not view_func:
        msg = "Error! %s/%s: Not found." % (getattr(theme, "identifier", None), view)
        return HttpResponseNotFound(msg)
    return view_func(request)
//  [python]
def testInvalidSparseTensor(self):
  with test_util.force_cpu():
    shape = [2, 2]
    val = [0]
    dense = constant_op.constant(np.zeros(shape, dtype=np.int32))

    for bad_idx in [
        [[-1, 0]],  # -1 is invalid.
        [[1, 3]],  # ...so is 3.
    ]:
      sparse = sparse_tensor.SparseTensorValue(bad_idx, val, shape)
      with self.assertRaisesRegex(
          (ValueError, errors_impl.InvalidArgumentError), "invalid index"):
        s = sparse_ops.sparse_add(sparse, dense)
        self.evaluate(s)
//  [python]
def testInvalidSparseTensor(self):
  with test_util.force_cpu():
    shape = [2, 2]
    val = [0]
    dense = constant_op.constant(np.zeros(shape, dtype=np.int32))

    for bad_idx in [
        [[-1, 0]],  # -1 is invalid.
        [[1, 3]],  # ...so is 3.
    ]:
      sparse = sparse_tensor.SparseTensorValue(bad_idx, val, shape)
      s = sparse_ops.sparse_add(sparse, dense)

      with self.assertRaisesRegex(errors_impl.InvalidArgumentError,
                                  "invalid index"):
        self.evaluate(s)
//  [python]
def count(cls, **kwargs):
    """Return a count of server side resources given
    filtering arguments in kwargs.
    """
    url = recurly.base_uri() + cls.collection_path
    if kwargs:
        url = '%s?%s' % (url, urlencode(kwargs))
    return Page.count_for_url(url)
//  [python]
def count(cls, **kwargs):
    """Return a count of server side resources given
    filtering arguments in kwargs.
    """
    url = urljoin(recurly.base_uri(), cls.collection_path)
    if kwargs:
        url = '%s?%s' % (url, urlencode(kwargs))
    return Page.count_for_url(url)
//  [python]
def test_process_request(self, freeze_upload_folder, rf):
    uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
    request = rf.post("/", data={"file": uploaded_file})
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"uploaded"

    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    request = rf.post(
        "/",
        data={
            "file": "custom/location/tmp/s3file/s3_file.txt",
            "s3file": "file",
            "file-s3f-signature": "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc",
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"s3file"
//  [python]
def test_process_request(self, rf):
    uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
    request = rf.post("/", data={"file": uploaded_file})
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"uploaded"

    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    request = rf.post(
        "/",
        data={
            "file": "custom/location/tmp/s3file/s3_file.txt",
            "s3file": "file",
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    assert request.FILES.getlist("file")
    assert request.FILES.get("file").read() == b"s3file"
//  [python]
def get(cls, uuid):
    """Return a `Resource` instance of this class identified by
    the given code or UUID.

    Only `Resource` classes with specified `member_path` attributes
    can be directly requested with this method.

    """
    uuid = quote(str(uuid))
    url = recurly.base_uri() + (cls.member_path % (uuid,))
    resp, elem = cls.element_for_url(url)
    return cls.from_element(elem)
//  [python]
def get(cls, uuid):
    """Return a `Resource` instance of this class identified by
    the given code or UUID.

    Only `Resource` classes with specified `member_path` attributes
    can be directly requested with this method.

    """
    url = urljoin(recurly.base_uri(), cls.member_path % (uuid,))
    resp, elem = cls.element_for_url(url)
    return cls.from_element(elem)
//  [python]
def mysql_contains(field: Term, value: str) -> Criterion:
    return Like(
        functions.Cast(field, SqlTypes.CHAR), StrWrapper(f"%{escape_like(value)}%"), escape=""
    )
//  [python]
def mysql_contains(field: Field, value: str) -> Criterion:
    return functions.Cast(field, SqlTypes.CHAR).like(f"%{value}%")
//  [python]
def mysql_insensitive_contains(field: Term, value: str) -> Criterion:
    return Like(
        functions.Upper(functions.Cast(field, SqlTypes.CHAR)),
        functions.Upper(StrWrapper(f"%{escape_like(value)}%")),
        escape="",
    )
//  [python]
def mysql_insensitive_contains(field: Field, value: str) -> Criterion:
    return functions.Upper(functions.Cast(field, SqlTypes.CHAR)).like(functions.Upper(f"%{value}%"))
//  [python]
def mysql_insensitive_starts_with(field: Term, value: str) -> Criterion:
    return Like(
        functions.Upper(functions.Cast(field, SqlTypes.CHAR)),
        functions.Upper(StrWrapper(f"{escape_like(value)}%")),
        escape="",
    )
//  [python]
def mysql_insensitive_starts_with(field: Field, value: str) -> Criterion:
    return functions.Upper(functions.Cast(field, SqlTypes.CHAR)).like(functions.Upper(f"{value}%"))
//  [python]
def mysql_insensitive_exact(field: Term, value: str) -> Criterion:
    return functions.Upper(functions.Cast(field, SqlTypes.CHAR)).eq(functions.Upper(str(value)))
//  [python]
def mysql_insensitive_exact(field: Field, value: str) -> Criterion:
    return functions.Upper(functions.Cast(field, SqlTypes.CHAR)).eq(functions.Upper(f"{value}"))
//  [python]
def __init__(self, *, openapi: GeneratorData) -> None:
    self.openapi: GeneratorData = openapi
    self.env: Environment = Environment(loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True)

    self.project_name: str = self.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
    self.project_dir: Path = Path.cwd() / self.project_name

    self.package_name: str = self.package_name_override or self.project_name.replace("-", "_")
    self.package_dir: Path = self.project_dir / self.package_name
    self.package_description: str = utils.remove_string_escapes(
        f"A client library for accessing {self.openapi.title}"
    )
    self.version: str = openapi.version

    self.env.filters.update(self.TEMPLATE_FILTERS)
//  [python]
def __init__(self, *, openapi: GeneratorData) -> None:
    self.openapi: GeneratorData = openapi
    self.env: Environment = Environment(loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True)

    self.project_name: str = self.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
    self.project_dir: Path = Path.cwd() / self.project_name

    self.package_name: str = self.package_name_override or self.project_name.replace("-", "_")
    self.package_dir: Path = self.project_dir / self.package_name
    self.package_description: str = f"A client library for accessing {self.openapi.title}"
    self.version: str = openapi.version

    self.env.filters.update(self.TEMPLATE_FILTERS)
//  [python]
def mysql_starts_with(field: Term, value: str) -> Criterion:
    return Like(
        functions.Cast(field, SqlTypes.CHAR), StrWrapper(f"{escape_like(value)}%"), escape=""
    )
//  [python]
def mysql_starts_with(field: Field, value: str) -> Criterion:
    return functions.Cast(field, SqlTypes.CHAR).like(f"{value}%")
//  [python]
def test_get_delete_dataobj(test_app, client: FlaskClient, note_fixture):
    response = client.post("/dataobj/delete/1")
    assert response.status_code == 302
//  [python]
def test_get_delete_dataobj(test_app, client: FlaskClient, note_fixture):
    response = client.get("/dataobj/delete/1")
    assert response.status_code == 302
//  [python]
def mysql_insensitive_ends_with(field: Term, value: str) -> Criterion:
    return Like(
        functions.Upper(functions.Cast(field, SqlTypes.CHAR)),
        functions.Upper(StrWrapper(f"%{escape_like(value)}")),
        escape="",
    )
//  [python]
def mysql_insensitive_ends_with(field: Field, value: str) -> Criterion:
    return functions.Upper(functions.Cast(field, SqlTypes.CHAR)).like(functions.Upper(f"%{value}"))
//  [python]
def mysql_ends_with(field: Term, value: str) -> Criterion:
    return Like(
        functions.Cast(field, SqlTypes.CHAR), StrWrapper(f"%{escape_like(value)}"), escape=""
    )
//  [python]
def mysql_ends_with(field: Field, value: str) -> Criterion:
    return functions.Cast(field, SqlTypes.CHAR).like(f"%{value}")
//  [python]
def test_process_request__multiple_files(self, freeze_upload_folder, rf):
    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    storage.save("tmp/s3file/s3_other_file.txt", ContentFile(b"other s3file"))
    request = rf.post(
        "/",
        data={
            "file": [
                "custom/location/tmp/s3file/s3_file.txt",
                "custom/location/tmp/s3file/s3_other_file.txt",
            ],
            "file-s3f-signature": "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc",
            "other_file-s3f-signature": "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc",
            "s3file": ["file", "other_file"],
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    files = request.FILES.getlist("file")
    assert files[0].read() == b"s3file"
    assert files[1].read() == b"other s3file"
//  [python]
def test_process_request__multiple_files(self, rf):
    storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
    storage.save("tmp/s3file/s3_other_file.txt", ContentFile(b"other s3file"))
    request = rf.post(
        "/",
        data={
            "file": [
                "custom/location/tmp/s3file/s3_file.txt",
                "custom/location/tmp/s3file/s3_other_file.txt",
            ],
            "s3file": ["file", "other_file"],
        },
    )
    S3FileMiddleware(lambda x: None)(request)
    files = request.FILES.getlist("file")
    assert files[0].read() == b"s3file"
    assert files[1].read() == b"other s3file"
//  [python]
def load_module(name):
    if os.path.exists(name) and os.path.splitext(name)[1] == '.py':
        mod = imp.new_module(os.path.splitext(os.path.basename(name))[0])
        with open(name) as f:
            exec(f.read(), mod.__dict__, mod.__dict__)
        return mod

    return importlib.import_module('dbusmock.templates.' + name)
//  [python]
def load_module(name):
    if os.path.exists(name) and os.path.splitext(name)[1] == '.py':
        sys.path.insert(0, os.path.dirname(os.path.abspath(name)))
        try:
            m = os.path.splitext(os.path.basename(name))[0]
            module = importlib.import_module(m)
        finally:
            sys.path.pop(0)

        return module

    return importlib.import_module('dbusmock.templates.' + name)
//  [python]
def test_get_delete_dataobj_not_found(test_app, client: FlaskClient):
    response = client.post("/dataobj/delete/1")
    assert response.status_code == 302
//  [python]
def test_get_delete_dataobj_not_found(test_app, client: FlaskClient):
    response = client.get("/dataobj/delete/1")
    assert response.status_code == 302
//  [python]
def retrieveBoilerplateFile(doc, name, group=None, status=None, error=True, allowLocal=True):
    # Looks in three or four locations, in order:
    # the folder the spec source is in, the group's boilerplate folder, the megagroup's boilerplate folder, and the generic boilerplate folder.
    # In each location, it first looks for the file specialized on status, and then for the generic file.
    # Filenames must be of the format NAME.include or NAME-STATUS.include
    if group is None and doc.md.group is not None:
        group = doc.md.group.lower()
    if status is None:
        if doc.md.status is not None:
            status = doc.md.status
        elif doc.md.rawStatus is not None:
            status = doc.md.rawStatus
    megaGroup, status = splitStatus(status)

    searchLocally = allowLocal and doc.md.localBoilerplate[name]

    def boilerplatePath(*segs):
        return scriptPath("boilerplate", *segs)

    statusFile = f"{name}-{status}.include"
    genericFile = f"{name}.include"
    sources = []
    if searchLocally:
        sources.append(doc.inputSource.relative(statusFile))  # Can be None.
        sources.append(doc.inputSource.relative(genericFile))
    else:
        for f in (statusFile, genericFile):
            if doc.inputSource.cheaplyExists(f):
                warn(
                    (
                        "Found {0} next to the specification without a matching\n"
                        + "Local Boilerplate: {1} yes\n"
                        + "in the metadata. This include won't be found when building via a URL."
                    ).format(f, name)
                )
                # We should remove this after giving specs time to react to the warning:
                sources.append(doc.inputSource.relative(f))
    if group:
        sources.append(InputSource(boilerplatePath(group, statusFile), chroot=False))
        sources.append(InputSource(boilerplatePath(group, genericFile), chroot=False))
    if megaGroup:
        sources.append(InputSource(boilerplatePath(megaGroup, statusFile), chroot=False))
        sources.append(InputSource(boilerplatePath(megaGroup, genericFile), chroot=False))
    sources.append(InputSource(boilerplatePath(statusFile), chroot=False))
    sources.append(InputSource(boilerplatePath(genericFile), chroot=False))

    # Watch all the possible sources, not just the one that got used, because if
    # an earlier one appears, we want to rebuild.
    doc.recordDependencies(*sources)

    for source in sources:
        if source is not None:
            try:
                return source.read().content
            except OSError:
                # That input doesn't exist.
                pass
    else:
        if error:
            die(
                "Couldn't find an appropriate include file for the {0} inclusion, given group='{1}' and status='{2}'.",
                name,
                group,
                status,
            )
        return ""
//  [python]
def retrieveBoilerplateFile(doc, name, group=None, status=None, error=True):
    # Looks in three or four locations, in order:
    # the folder the spec source is in, the group's boilerplate folder, the megagroup's boilerplate folder, and the generic boilerplate folder.
    # In each location, it first looks for the file specialized on status, and then for the generic file.
    # Filenames must be of the format NAME.include or NAME-STATUS.include
    if group is None and doc.md.group is not None:
        group = doc.md.group.lower()
    if status is None:
        if doc.md.status is not None:
            status = doc.md.status
        elif doc.md.rawStatus is not None:
            status = doc.md.rawStatus
    megaGroup, status = splitStatus(status)

    searchLocally = doc.md.localBoilerplate[name]

    def boilerplatePath(*segs):
        return scriptPath("boilerplate", *segs)

    statusFile = f"{name}-{status}.include"
    genericFile = f"{name}.include"
    sources = []
    if searchLocally:
        sources.append(doc.inputSource.relative(statusFile))  # Can be None.
        sources.append(doc.inputSource.relative(genericFile))
    else:
        for f in (statusFile, genericFile):
            if doc.inputSource.cheaplyExists(f):
                warn(
                    (
                        "Found {0} next to the specification without a matching\n"
                        + "Local Boilerplate: {1} yes\n"
                        + "in the metadata. This include won't be found when building via a URL."
                    ).format(f, name)
                )
                # We should remove this after giving specs time to react to the warning:
                sources.append(doc.inputSource.relative(f))
    if group:
        sources.append(InputSource(boilerplatePath(group, statusFile)))
        sources.append(InputSource(boilerplatePath(group, genericFile)))
    if megaGroup:
        sources.append(InputSource(boilerplatePath(megaGroup, statusFile)))
        sources.append(InputSource(boilerplatePath(megaGroup, genericFile)))
    sources.append(InputSource(boilerplatePath(statusFile)))
    sources.append(InputSource(boilerplatePath(genericFile)))

    # Watch all the possible sources, not just the one that got used, because if
    # an earlier one appears, we want to rebuild.
    doc.recordDependencies(*sources)

    for source in sources:
        if source is not None:
            try:
                return source.read().content
            except OSError:
                # That input doesn't exist.
                pass
    else:
        if error:
            die(
                "Couldn't find an appropriate include file for the {0} inclusion, given group='{1}' and status='{2}'.",
                name,
                group,
                status,
            )
        return ""
//  [python]
def get_field_options(self, field):
    options = {}
    options['label'] = field.label
    if getattr(settings, 'WAGTAILFORMS_HELP_TEXT_ALLOW_HTML', False):
        options['help_text'] = field.help_text
    else:
        options['help_text'] = conditional_escape(field.help_text)
    options['required'] = field.required
    options['initial'] = field.default_value
    return options
//  [python]
def get_field_options(self, field):
    options = {}
    options['label'] = field.label
    options['help_text'] = field.help_text
    options['required'] = field.required
    options['initial'] = field.default_value
    return options
//  [python]
def receivePing():
    vrequest = request.form['id']
    db.sentences_victim('report_online', [vrequest], 2)
    return json.dumps({'status' : 'OK', 'vId' : vrequest});
//  [python]
def receivePing():
    vrequest = request.form['id']
    db.sentences_victim('report_online', [vrequest])
    return json.dumps({'status' : 'OK', 'vId' : vrequest});