jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def handler(request, _operation, _current_url):
    """Handler for a file:// URL.

    Args:
        request: QNetworkRequest to answer to.
        _operation: The HTTP operation being done.
        _current_url: The page we're on currently.

    Return:
        A QNetworkReply for directories, None for files.
    """
    path = request.url().toLocalFile()
    try:
        if os.path.isdir(path):
            data = dirbrowser_html(path)
            return networkreply.FixedDataNetworkReply(
                request, data, 'text/html')
        return None
    except UnicodeEncodeError:
        return None
//  [python]
def handler(request):
    """Handler for a file:// URL.

    Args:
        request: QNetworkRequest to answer to.

    Return:
        A QNetworkReply for directories, None for files.
    """
    path = request.url().toLocalFile()
    try:
        if os.path.isdir(path):
            data = dirbrowser_html(path)
            return networkreply.FixedDataNetworkReply(
                request, data, 'text/html')
        return None
    except UnicodeEncodeError:
        return None
//  [python]
def load_event(self, args, filename, from_misp, stix_version):
    self.outputname = '{}.json'.format(filename)
    try:
        event_distribution = args[0]
        if not isinstance(event_distribution, int):
            event_distribution = int(event_distribution) if event_distribution.isdigit() else 5
    except IndexError:
        event_distribution = 5
    try:
        attribute_distribution = args[1]
        if attribute_distribution == 'event':
            attribute_distribution = event_distribution
        elif not isinstance(attribute_distribution, int):
            attribute_distribution = int(attribute_distribution) if attribute_distribution.isdigit() else event_distribution
    except IndexError:
        attribute_distribution = event_distribution
    self.misp_event.distribution = event_distribution
    self.__attribute_distribution = attribute_distribution
    self.from_misp = from_misp
    self.load_mapping()
//  [python]
def load_event(self, args, filename, from_misp, stix_version):
    self.outputname = '{}.json'.format(filename)
    if len(args) > 0 and args[0]:
        self.add_original_file(filename, args[0], stix_version)
    try:
        event_distribution = args[1]
        if not isinstance(event_distribution, int):
            event_distribution = int(event_distribution) if event_distribution.isdigit() else 5
    except IndexError:
        event_distribution = 5
    try:
        attribute_distribution = args[2]
        if attribute_distribution == 'event':
            attribute_distribution = event_distribution
        elif not isinstance(attribute_distribution, int):
            attribute_distribution = int(attribute_distribution) if attribute_distribution.isdigit() else event_distribution
    except IndexError:
        attribute_distribution = event_distribution
    self.misp_event.distribution = event_distribution
    self.__attribute_distribution = attribute_distribution
    self.from_misp = from_misp
    self.load_mapping()
//  [python]
def __init__(self):
    keys = [key for key in list(request.form.keys()) + list(request.files.keys())]
    field_infos = [FieldInfo.factory(key) for key in keys if key != "csrf_token"]
    # important to sort them so they will be in expected order on command line
    self.field_infos = list(sorted(field_infos))
//  [python]
def __init__(self):
    field_infos = [
        FieldInfo.factory(key)
        for key in list(request.form.keys()) + list(request.files.keys())
    ]
    # important to sort them so they will be in expected order on command line
    self.field_infos = list(sorted(field_infos))
//  [python]
def test_credits_view_json(self):
    response = self.get_credits("json")
    self.assertEqual(response.status_code, 200)
    self.assertJSONEqual(
        response.content.decode(),
        [{"Czech": [["weblate@example.org", "Weblate <b>Test</b>", 1]]}],
    )
//  [python]
def test_credits_view_json(self):
    response = self.get_credits("json")
    self.assertEqual(response.status_code, 200)
    self.assertJSONEqual(
        response.content.decode(),
        [{"Czech": [["weblate@example.org", "Weblate Test", 1]]}],
    )
//  [python]
def test_credits_one(self, expected_count=1):
    self.add_change()
    data = generate_credits(
        None,
        timezone.now() - timedelta(days=1),
        timezone.now() + timedelta(days=1),
        translation__component=self.component,
    )
    self.assertEqual(
        data,
        [
            {
                "Czech": [
                    ("weblate@example.org", "Weblate <b>Test</b>", expected_count)
                ]
            }
        ],
    )
//  [python]
def test_credits_one(self, expected_count=1):
    self.add_change()
    data = generate_credits(
        None,
        timezone.now() - timedelta(days=1),
        timezone.now() + timedelta(days=1),
        translation__component=self.component,
    )
    self.assertEqual(
        data, [{"Czech": [("weblate@example.org", "Weblate Test", expected_count)]}]
    )
//  [python]
def test_credits_view_html(self):
    response = self.get_credits("html")
    self.assertEqual(response.status_code, 200)
    self.assertHTMLEqual(
        response.content.decode(),
        "<table>\n"
        "<tr>\n<th>Czech</th>\n"
        '<td><ul><li><a href="mailto:weblate@example.org">'
        "Weblate &lt;b&gt;Test&lt;/b&gt;</a> (1)</li></ul></td>\n</tr>\n"
        "</table>",
    )
//  [python]
def test_credits_view_html(self):
    response = self.get_credits("html")
    self.assertEqual(response.status_code, 200)
    self.assertHTMLEqual(
        response.content.decode(),
        "<table>\n"
        "<tr>\n<th>Czech</th>\n"
        '<td><ul><li><a href="mailto:weblate@example.org">'
        "Weblate Test</a> (1)</li></ul></td>\n</tr>\n"
        "</table>",
    )
//  [python]
def command_dispatch(request):
    """
    Xtheme command dispatch view.

    :param request: A request
    :type request: django.http.HttpRequest
    :return: A response
    :rtype: django.http.HttpResponse
    """
    command = request.POST.get("command")
    if command:
        response = handle_command(request, command)
        if response:
            return response
    raise Problem("Error! Unknown command: `%r`" % escape(command))
//  [python]
def command_dispatch(request):
    """
    Xtheme command dispatch view.

    :param request: A request
    :type request: django.http.HttpRequest
    :return: A response
    :rtype: django.http.HttpResponse
    """
    command = request.POST.get("command")
    if command:
        response = handle_command(request, command)
        if response:
            return response
    raise Problem("Error! Unknown command: `%r`" % command)
//  [python]
def contains(field: Term, value: str) -> Criterion:
    return Like(field, field.wrap_constant(f"%{escape_like(value)}%"))
//  [python]
def contains(field: Term, value: str) -> Criterion:
    return field.like(f"%{value}%")
//  [python]
def add_security_headers(resp):
    resp.headers['Content-Security-Policy'] = "default-src 'self' 'unsafe-inline' 'unsafe-eval'; img-src 'self' data:"
    if request.endpoint == "editbook.edit_book":
        resp.headers['Content-Security-Policy'] += " *"
    resp.headers['X-Content-Type-Options'] = 'nosniff'
    resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
    resp.headers['X-XSS-Protection'] = '1; mode=block'
    resp.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
    return resp
//  [python]
def add_security_headers(resp):
    resp.headers['Content-Security-Policy'] = "default-src 'self' 'unsafe-inline' 'unsafe-eval';"
    if request.endpoint == "editbook.edit_book":
        resp.headers['Content-Security-Policy'] += "img-src * data:"
    resp.headers['X-Content-Type-Options'] = 'nosniff'
    resp.headers['X-Frame-Options'] = 'SAMEORIGIN'
    resp.headers['X-XSS-Protection'] = '1; mode=block'
    resp.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
    # log.debug(request.full_path)
    return resp
//  [python]
def insensitive_contains(field: Term, value: str) -> Criterion:
    return Like(Upper(field), field.wrap_constant(Upper(f"%{escape_like(value)}%")))
//  [python]
def insensitive_contains(field: Term, value: str) -> Criterion:
    return Upper(field).like(Upper(f"%{value}%"))
//  [python]
def insensitive_starts_with(field: Term, value: str) -> Criterion:
    return Like(Upper(field), field.wrap_constant(Upper(f"{escape_like(value)}%")))
//  [python]
def insensitive_starts_with(field: Term, value: str) -> Criterion:
    return Upper(field).like(Upper(f"{value}%"))
//  [python]
def load(self, stream):
    '''read vault steam and return python object'''
    return yaml.safe_load(self.vault.decrypt(stream))
//  [python]
def load(self, stream):
    '''read vault steam and return python object'''
    return yaml.load(self.vault.decrypt(stream))
//  [python]
def insensitive_exact(field: Term, value: str) -> Criterion:
    return Upper(field).eq(Upper(str(value)))
//  [python]
def insensitive_exact(field: Term, value: str) -> Criterion:
    return Upper(field).eq(Upper(f"{value}"))
//  [python]
def starts_with(field: Term, value: str) -> Criterion:
    return Like(field, field.wrap_constant(f"{escape_like(value)}%"))
//  [python]
def starts_with(field: Term, value: str) -> Criterion:
    return field.like(f"{value}%")
//  [python]
def insensitive_ends_with(field: Term, value: str) -> Criterion:
    return Like(Upper(field), field.wrap_constant(Upper(f"%{escape_like(value)}")))
//  [python]
def insensitive_ends_with(field: Term, value: str) -> Criterion:
    return Upper(field).like(Upper(f"%{value}"))
//  [python]
def __init__(self, bot: bot.Red):
    super().__init__()
    self.bot = bot
    self.config = Config.get_conf(self, identifier=2_113_674_295, force_registration=True)
    self.config.register_global(custom={}, tenorkey=None)
    self.config.register_guild(custom={})
    self.try_after = None
//  [python]
def __init__(self, bot):
    super().__init__()
    self.bot = bot
    self.config = Config.get_conf(self, identifier=2_113_674_295, force_registration=True)
    self.config.register_global(custom={}, tenorkey=None)
    self.config.register_guild(custom={})
    self.try_after = None
//  [python]
async def actset(self, ctx: commands.Context):
    """
    Configure various settings for the act cog.
    """
//  [python]
async def actset(self, ctx):
    """
    Configure various settings for the act cog.
    """
//  [python]
async def act(self, ctx: commands.Context, *, target: Union[discord.Member, str] = None):
    """
    Acts on the specified user.
    """
    if not target or isinstance(target, str):
        return  # no help text

    try:
        if not ctx.guild:
            raise KeyError()
        message = await self.config.guild(ctx.guild).get_raw("custom", ctx.invoked_with)
    except KeyError:
        try:
            message = await self.config.get_raw("custom", ctx.invoked_with)
        except KeyError:
            message = NotImplemented

    if message is None:  # ignored command
        return
    elif message is NotImplemented:  # default
        # humanize action text
        action = inflection.humanize(ctx.invoked_with).split()
        iverb = -1

        for cycle in range(2):
            if iverb > -1:
                break
            for i, act in enumerate(action):
                act = act.lower()
                if (
                    act in NOLY_ADV
                    or act in CONJ
                    or (act.endswith("ly") and act not in LY_VERBS)
                    or (not cycle and act in SOFT_VERBS)
                ):
                    continue
                action[i] = inflection.pluralize(action[i])
                iverb = max(iverb, i)

        if iverb < 0:
            return
        action.insert(iverb + 1, target.mention)
        message = italics(" ".join(action))
    else:
        assert isinstance(message, str)
        message = fmt_re.sub(functools.partial(self.repl, target), message)

    # add reaction gif
    if self.try_after and ctx.message.created_at < self.try_after:
        return await ctx.send(message)
    if not await ctx.embed_requested():
        return await ctx.send(message)
    key = (await ctx.bot.get_shared_api_tokens("tenor")).get("api_key")
    if not key:
        return await ctx.send(message)
    async with aiohttp.request(
        "GET",
        "https://api.tenor.com/v1/search",
        params={
            "q": ctx.invoked_with,
            "key": key,
            "anon_id": str(ctx.author.id ^ ctx.me.id),
            "media_filter": "minimal",
            "contentfilter": "off" if getattr(ctx.channel, "nsfw", False) else "low",
            "ar_range": "wide",
            "limit": "8",
            "locale": get_locale(),
        },
    ) as response:
        json: dict
        if response.status == 429:
            self.try_after = ctx.message.created_at + 30
            json = {}
        elif response.status >= 400:
            json = {}
        else:
            json = await response.json()
    if not json.get("results"):
        return await ctx.send(message)
    message = f"{message}\n\n{random.choice(json['results'])['itemurl']}"
    await ctx.send(
        message,
        allowed_mentions=discord.AllowedMentions(
            users=False if target in ctx.message.mentions else [target]
        ),
    )
//  [python]
async def act(self, ctx, *, target: Union[discord.Member, str] = None):
    """
    Acts on the specified user.
    """
    if not target or isinstance(target, str):
        return  # no help text

    try:
        if not ctx.guild:
            raise KeyError()
        message = await self.config.guild(ctx.guild).get_raw("custom", ctx.invoked_with)
    except KeyError:
        try:
            message = await self.config.get_raw("custom", ctx.invoked_with)
        except KeyError:
            message = NotImplemented

    if message is None:  # ignored command
        return
    elif message is NotImplemented:  # default
        # humanize action text
        action = inflection.humanize(ctx.invoked_with).split()
        iverb = -1

        for cycle in range(2):
            if iverb > -1:
                break
            for i, act in enumerate(action):
                act = act.lower()
                if (
                    act in NOLY_ADV
                    or act in CONJ
                    or (act.endswith("ly") and act not in LY_VERBS)
                    or (not cycle and act in SOFT_VERBS)
                ):
                    continue
                action[i] = inflection.pluralize(action[i])
                iverb = max(iverb, i)

        if iverb < 0:
            return
        action.insert(iverb + 1, target.mention)
        message = italics(" ".join(action))
    else:
        message = message.format(target, user=target)

    # add reaction gif
    if self.try_after and ctx.message.created_at < self.try_after:
        return await ctx.send(message)
    if not await ctx.embed_requested():
        return await ctx.send(message)
    key = (await ctx.bot.get_shared_api_tokens("tenor")).get("api_key")
    if not key:
        return await ctx.send(message)
    async with aiohttp.request(
        "GET",
        "https://api.tenor.com/v1/search",
        params={
            "q": ctx.invoked_with,
            "key": key,
            "anon_id": str(ctx.author.id ^ ctx.me.id),
            "media_filter": "minimal",
            "contentfilter": "off" if getattr(ctx.channel, "nsfw", False) else "low",
            "ar_range": "wide",
            "limit": "8",
            "locale": get_locale(),
        },
    ) as response:
        json: dict
        if response.status == 429:
            self.try_after = ctx.message.created_at + 30
            json = {}
        elif response.status >= 400:
            json = {}
        else:
            json = await response.json()
    if not json.get("results"):
        return await ctx.send(message)
    message = f"{message}\n\n{random.choice(json['results'])['itemurl']}"
    await ctx.send(
        message,
        allowed_mentions=discord.AllowedMentions(
            users=False if target in ctx.message.mentions else [target]
        ),
    )
//  [python]
async def ignore_global(self, ctx: commands.Context, command: str.lower):
    """
    Globally ignore or unignore the specified action.

    The bot will no longer respond to these actions.
    """
    try:
        await self.config.get_raw("custom", command)
    except KeyError:
        await self.config.set_raw("custom", command, value=None)
    else:
        await self.config.clear_raw("custom", command)
    await ctx.tick()
//  [python]
async def ignore_global(self, ctx, command: str.lower):
    """
    Globally ignore or unignore the specified action.

    The bot will no longer respond to these actions.
    """
    try:
        await self.config.get_raw("custom", command)
    except KeyError:
        await self.config.set_raw("custom", command, value=None)
    else:
        await self.config.clear_raw("custom", command)
    await ctx.tick()
//  [python]
async def ignore(self, ctx: commands.Context, command: str.lower):
    """
    Ignore or unignore the specified action.

    The bot will no longer respond to these actions.
    """
    try:
        custom = await self.config.guild(ctx.guild).get_raw("custom", command)
    except KeyError:
        custom = NotImplemented
    if custom is None:
        await self.config.guild(ctx.guild).clear_raw("custom", command)
        await ctx.send("I will no longer ignore the {command} action".format(command=command))
    else:
        await self.config.guild(ctx.guild).set_raw("custom", command, value=None)
        await ctx.send("I will now ignore the {command} action".format(command=command))
//  [python]
async def ignore(self, ctx, command: str.lower):
    """
    Ignore or unignore the specified action.

    The bot will no longer respond to these actions.
    """
    try:
        custom = await self.config.guild(ctx.guild).get_raw("custom", command)
    except KeyError:
        custom = NotImplemented
    if custom is None:
        await self.config.guild(ctx.guild).clear_raw("custom", command)
        await ctx.send("I will no longer ignore the {command} action".format(command=command))
    else:
        await self.config.guild(ctx.guild).set_raw("custom", command, value=None)
        await ctx.send("I will now ignore the {command} action".format(command=command))
//  [python]
async def tenorkey(self, ctx: commands.Context):
    """
    Sets a Tenor GIF API key to enable reaction gifs with act commands.

    You can obtain a key from here: https://tenor.com/developer/dashboard
    """
    instructions = [
        "Go to the Tenor developer dashboard: https://tenor.com/developer/dashboard",
        "Log in or sign up if you haven't already.",
        "Click `+ Create new app` and fill out the form.",
        "Copy the key from the app you just created.",
        "Give the key to Red with this command:\n"
        f"`{ctx.prefix}set api tenor api_key your_api_key`\n"
        "Replace `your_api_key` with the key you just got.\n"
        "Everything else should be the same.",
    ]
    instructions = [f"**{i}.** {v}" for i, v in enumerate(instructions, 1)]
    await ctx.maybe_send_embed("\n".join(instructions))
//  [python]
async def tenorkey(self, ctx):
    """
    Sets a Tenor GIF API key to enable reaction gifs with act commands.

    You can obtain a key from here: https://tenor.com/developer/dashboard
    """
    instructions = [
        "Go to the Tenor developer dashboard: https://tenor.com/developer/dashboard",
        "Log in or sign up if you haven't already.",
        "Click `+ Create new app` and fill out the form.",
        "Copy the key from the app you just created.",
        "Give the key to Red with this command:\n"
        f"`{ctx.prefix}set api tenor api_key your_api_key`\n"
        "Replace `your_api_key` with the key you just got.\n"
        "Everything else should be the same.",
    ]
    instructions = [f"**{i}.** {v}" for i, v in enumerate(instructions, 1)]
    await ctx.maybe_send_embed("\n".join(instructions))
//  [python]
async def customize(self, ctx: commands.Context, command: str.lower, *, response: str = None):
    """
    Customize the response to an action.

    You can use {0} or {user} to dynamically replace with the specified target of the action.
    Formats like {0.name} or {0.mention} can also be used.
    """
    if not response:
        await self.config.guild(ctx.guild).clear_raw("custom", command)
        await ctx.tick()
    else:
        await self.config.guild(ctx.guild).set_raw("custom", command, value=response)
        await ctx.send(
            fmt_re.sub(functools.partial(self.repl, ctx.author), response),
            allowed_mentions=discord.AllowedMentions(users=False),
        )
//  [python]
async def customize(self, ctx, command: str.lower, *, response: str = None):
    """
    Customize the response to an action.

    You can use {0} or {user} to dynamically replace with the specified target of the action.
    Formats like {0.name} or {0.mention} can also be used.
    """
    if not response:
        await self.config.guild(ctx.guild).clear_raw("custom", command)
    else:
        await self.config.guild(ctx.guild).set_raw("custom", command, value=response)
    await ctx.tick()
//  [python]
def test_more_custom_templates(self):
    """
    Test custom templates and metadata
    Template is relative to collection-specific dir
    Add custom metadata and test its presence in custom search page
    """
    custom_search = os.path.join(self.root_dir, COLLECTIONS, 'test',
                                  'templates', 'search.html')

    # add metadata
    main(['metadata', 'test', '--set', 'some=value'])

    with open(custom_search, 'w+b') as fh:
        fh.write(b'overriden search page: ')
        fh.write(b'{{ metadata | tojson }}\n')

    # force clear of jinja env cache to reload
    self.app.rewriterapp.jinja_env.jinja_env.cache = {}

    resp = self.testapp.get('/test/')
    resp.charset = 'utf-8'
    assert resp.status_int == 200
    assert resp.content_type == 'text/html'
    assert 'overriden search page: ' in resp.text
    #assert '"some":"value"' in resp.text, resp.text
    assert '{&#34;some&#34;:&#34;value&#34;}' in resp.text, resp.text
//  [python]
def test_more_custom_templates(self):
    """
    Test custom templates and metadata
    Template is relative to collection-specific dir
    Add custom metadata and test its presence in custom search page
    """
    custom_search = os.path.join(self.root_dir, COLLECTIONS, 'test',
                                  'templates', 'search.html')

    # add metadata
    main(['metadata', 'test', '--set', 'some=value'])

    with open(custom_search, 'w+b') as fh:
        fh.write(b'overriden search page: ')
        fh.write(b'{{ metadata | tojson }}\n')

    # force clear of jinja env cache to reload
    self.app.rewriterapp.jinja_env.jinja_env.cache = {}

    resp = self.testapp.get('/test/')
    resp.charset = 'utf-8'
    assert resp.status_int == 200
    assert resp.content_type == 'text/html'
    assert 'overriden search page: ' in resp.text
    print(resp.text)
    assert '"some":"value"' in resp.text, resp.text
//  [python]
def valid_id(opts, id_):
    '''
    Returns if the passed id is valid
    '''
    try:
        if any(x in id_ for x in ('/', '\\', '\0')):
            return False
        return bool(clean_path(opts['pki_dir'], id_))
    except (AttributeError, KeyError, TypeError):
        return False
//  [python]
def valid_id(opts, id_):
    '''
    Returns if the passed id is valid
    '''
    try:
        return bool(clean_path(opts['pki_dir'], id_)) and clean_id(id_)
    except (AttributeError, KeyError, TypeError) as e:
        return False
//  [python]
def test_replay_banner_metadata(self, fmod):
    """ Test adding metadata in replay banner (both framed and non-frame)
    """
    resp = self.get('/test/20140103030321{0}/http://example.com/?example=1', fmod)
    assert '<div>Custom Banner Here!</div>' in resp.text
    #assert '"some":"value"' in resp.text
    assert '{&#34;some&#34;:&#34;value&#34;}' in resp.text, resp.text
//  [python]
def test_replay_banner_metadata(self, fmod):
    """ Test adding metadata in replay banner (both framed and non-frame)
    """
    resp = self.get('/test/20140103030321{0}/http://example.com/?example=1', fmod)
    assert '<div>Custom Banner Here!</div>' in resp.text
    assert '"some":"value"' in resp.text
//  [python]
async def initialize(self, bot: bot.Red):
    # temporary backwards compatibility
    key = await self.config.tenorkey()
    if not key:
        return
    await bot.set_shared_api_tokens("tenor", api_key=key)
    await self.config.tenorkey.clear()
//  [python]
async def initialize(self, bot):
    # temporary backwards compatibility
    key = await self.config.tenorkey()
    if not key:
        return
    await bot.set_shared_api_tokens("tenor", api_key=key)
    await self.config.tenorkey.clear()
//  [python]
def process_statistics(self, metadata, _):
    args = [metadata.hostname, '-p', metadata.profile, '-g',
            ':'.join([g for g in metadata.groups])]
    self.debug_log("running triggers")
    for notifier in os.listdir(self.data):
        self.debug_log("running %s" % notifier)
        if ((notifier[-1] == '~') or
            (notifier[:2] == '.#') or
            (notifier[-4:] == '.swp') or
            (notifier in ['SCCS', '.svn', '4913'])):
            continue
        npath = os.path.join(self.data, notifier)
        self.async_run([npath] + args)
//  [python]
def process_statistics(self, metadata, _):
    args = [metadata.hostname, '-p', metadata.profile, '-g',
            ':'.join([g for g in metadata.groups])]
    for notifier in os.listdir(self.data):
        if ((notifier[-1] == '~') or
            (notifier[:2] == '.#') or
            (notifier[-4:] == '.swp') or
            (notifier in ['SCCS', '.svn', '4913'])):
            continue
        npath = self.data + '/' + notifier
        self.logger.debug("Running %s %s" % (npath, " ".join(args)))
        async_run(npath, args)
//  [python]
def async_run(self, args):
    pid = os.fork()
    if pid:
        os.waitpid(pid, 0)
    else:
        dpid = os.fork()
        if not dpid:
            self.debug_log("Running %s" % " ".join(pipes.quote(a)
                                                   for a in args))
            proc = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
            (out, err) = proc.communicate()
            rv = proc.wait()
            if rv != 0:
                self.logger.error("Trigger: Error running %s (%s): %s" %
                                  (args[0], rv, err))
            elif err:
                self.debug_log("Trigger: Error: %s" % err)
        os._exit(0)
//  [python]
def async_run(prog, args):
    pid = os.fork()
    if pid:
        os.waitpid(pid, 0)
    else:
        dpid = os.fork()
        if not dpid:
            os.system(" ".join([prog] + args))
        os._exit(0)
//  [python]
def get_recipe_from_file(self, file):
    recipe_xml = file

    recipe = Recipe.objects.create(
        name=recipe_xml.find('title').text.strip(),
        created_by=self.request.user, internal=True, space=self.request.space)

    if recipe_xml.find('preptime') is not None and recipe_xml.find('preptime').text is not None:
        recipe.working_time = parse_time(recipe_xml.find('preptime').text.strip())

    if recipe_xml.find('cooktime') is not None and recipe_xml.find('cooktime').text is not None:
        recipe.waiting_time = parse_time(recipe_xml.find('cooktime').text.strip())

    if recipe_xml.find('quantity') is not None and recipe_xml.find('quantity').text is not None:
        recipe.servings = parse_servings(recipe_xml.find('quantity').text.strip())
        recipe.servings_text = parse_servings_text(recipe_xml.find('quantity').text.strip())

    if recipe_xml.find('url') is not None and recipe_xml.find('url').text is not None:
        recipe.source_url = recipe_xml.find('url').text.strip()

    if recipe_xml.find('description') is not None:  # description is a list of <li>'s with text
        if len(recipe_xml.find('description')) > 0:
            recipe.description = recipe_xml.find('description')[0].text[:512]

    for step in recipe_xml.find('recipetext').getchildren():
        step = Step.objects.create(
            instruction=step.text.strip(), space=self.request.space,
        )
        recipe.steps.add(step)

    ingredient_parser = IngredientParser(self.request, True)

    for ingredient in recipe_xml.find('ingredient').getchildren():
        if ingredient.text.strip() != '':
            amount, unit, food, note = ingredient_parser.parse(ingredient.text.strip())
            f = ingredient_parser.get_food(food)
            u = ingredient_parser.get_unit(unit)
            recipe.steps.first().ingredients.add(Ingredient.objects.create(
                food=f, unit=u, amount=amount, note=note, original_text=ingredient.text.strip(), space=self.request.space,
            ))

    if recipe_xml.find('imageurl') is not None:
        try:
            url = recipe_xml.find('imageurl').text.strip()
            if validators.url(url, public=True):
                response = requests.get(url)
            self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    recipe.save()

    return recipe
//  [python]
def get_recipe_from_file(self, file):
    recipe_xml = file

    recipe = Recipe.objects.create(
        name=recipe_xml.find('title').text.strip(),
        created_by=self.request.user, internal=True, space=self.request.space)

    if recipe_xml.find('preptime') is not None and recipe_xml.find('preptime').text is not None:
        recipe.working_time = parse_time(recipe_xml.find('preptime').text.strip())

    if recipe_xml.find('cooktime') is not None and recipe_xml.find('cooktime').text is not None:
        recipe.waiting_time = parse_time(recipe_xml.find('cooktime').text.strip())

    if recipe_xml.find('quantity') is not None and recipe_xml.find('quantity').text is not None:
        recipe.servings = parse_servings(recipe_xml.find('quantity').text.strip())
        recipe.servings_text = parse_servings_text(recipe_xml.find('quantity').text.strip())

    if recipe_xml.find('url') is not None and recipe_xml.find('url').text is not None:
        recipe.source_url = recipe_xml.find('url').text.strip()

    if recipe_xml.find('description') is not None:  # description is a list of <li>'s with text
        if len(recipe_xml.find('description')) > 0:
            recipe.description = recipe_xml.find('description')[0].text[:512]

    for step in recipe_xml.find('recipetext').getchildren():
        step = Step.objects.create(
            instruction=step.text.strip(), space=self.request.space,
        )
        recipe.steps.add(step)

    ingredient_parser = IngredientParser(self.request, True)

    for ingredient in recipe_xml.find('ingredient').getchildren():
        if ingredient.text.strip() != '':
            amount, unit, food, note = ingredient_parser.parse(ingredient.text.strip())
            f = ingredient_parser.get_food(food)
            u = ingredient_parser.get_unit(unit)
            recipe.steps.first().ingredients.add(Ingredient.objects.create(
                food=f, unit=u, amount=amount, note=note, original_text=ingredient.text.strip(), space=self.request.space,
            ))

    if recipe_xml.find('imageurl') is not None:
        try:
            response = requests.get(recipe_xml.find('imageurl').text.strip())
            self.import_recipe_image(recipe, BytesIO(response.content))
        except Exception as e:
            print('failed to import image ', str(e))

    recipe.save()

    return recipe
//  [python]
def test_get_response_requests_exception(self, mock_get):
    mock_response = mock.Mock()
    mock_response.status_code = 500
    mock_response.text = "Server Error"
    exception_message = "Some requests exception"
    requests_exception = requests_or_advocate.RequestException(exception_message)
    mock_response.raise_for_status.side_effect = requests_exception
    mock_get.return_value = mock_response

    url = "https://example.com/"
    query_runner = BaseHTTPQueryRunner({})
    response, error = query_runner.get_response(url)
    mock_get.assert_called_once_with("get", url, auth=None)
    self.assertIsNotNone(error)
    self.assertEqual(exception_message, error)
//  [python]
def test_get_response_requests_exception(self, mock_get):
    mock_response = mock.Mock()
    mock_response.status_code = 500
    mock_response.text = "Server Error"
    exception_message = "Some requests exception"
    requests_exception = requests.RequestException(exception_message)
    mock_response.raise_for_status.side_effect = requests_exception
    mock_get.return_value = mock_response

    url = "https://example.com/"
    query_runner = BaseHTTPQueryRunner({})
    response, error = query_runner.get_response(url)
    mock_get.assert_called_once_with("get", url, auth=None)
    self.assertIsNotNone(error)
    self.assertEqual(exception_message, error)
//  [python]
def login():
    from flask_login import current_user

    default_redirect_url = request.script_root + url_for("index")
    redirect_url = request.args.get("redirect", default_redirect_url)

    parsed = urlparse(redirect_url)  # check if redirect url is valid
    if parsed.scheme != "" or parsed.netloc != "":
        _logger.warning(
            f"Got an invalid redirect URL with the login attempt, misconfiguration or attack attempt: {redirect_url}"
        )
        redirect_url = default_redirect_url

    permissions = sorted(
        filter(
            lambda x: x is not None and isinstance(x, OctoPrintPermission),
            map(
                lambda x: getattr(Permissions, x.strip()),
                request.args.get("permissions", "").split(","),
            ),
        ),
        key=lambda x: x.get_name(),
    )
    if not permissions:
        permissions = [Permissions.STATUS, Permissions.SETTINGS_READ]

    user_id = request.args.get("user_id", "")

    if (not user_id or current_user.get_id() == user_id) and has_permissions(
        *permissions
    ):
        return redirect(redirect_url)

    render_kwargs = {
        "theming": [],
        "redirect_url": redirect_url,
        "permission_names": map(lambda x: x.get_name(), permissions),
        "user_id": user_id,
        "logged_in": not current_user.is_anonymous,
    }

    try:
        additional_assets = _add_additional_assets("octoprint.theming.login")

        # backwards compatibility to forcelogin & loginui plugins which were replaced by this built-in dialog
        additional_assets += _add_additional_assets("octoprint.plugin.forcelogin.theming")
        additional_assets += _add_additional_assets("octoprint.plugin.loginui.theming")

        render_kwargs.update({"theming": additional_assets})
    except Exception:
        _logger.exception("Error processing theming CSS, ignoring")

    return render_template("login.jinja2", **render_kwargs)
//  [python]
def login():
    from flask_login import current_user

    redirect_url = request.args.get("redirect", request.script_root + url_for("index"))
    permissions = sorted(
        filter(
            lambda x: x is not None and isinstance(x, OctoPrintPermission),
            map(
                lambda x: getattr(Permissions, x.strip()),
                request.args.get("permissions", "").split(","),
            ),
        ),
        key=lambda x: x.get_name(),
    )
    if not permissions:
        permissions = [Permissions.STATUS, Permissions.SETTINGS_READ]

    user_id = request.args.get("user_id", "")

    if (not user_id or current_user.get_id() == user_id) and has_permissions(
        *permissions
    ):
        return redirect(redirect_url)

    render_kwargs = {
        "theming": [],
        "redirect_url": redirect_url,
        "permission_names": map(lambda x: x.get_name(), permissions),
        "user_id": user_id,
        "logged_in": not current_user.is_anonymous,
    }

    try:
        additional_assets = _add_additional_assets("octoprint.theming.login")

        # backwards compatibility to forcelogin & loginui plugins which were replaced by this built-in dialog
        additional_assets += _add_additional_assets("octoprint.plugin.forcelogin.theming")
        additional_assets += _add_additional_assets("octoprint.plugin.loginui.theming")

        render_kwargs.update({"theming": additional_assets})
    except Exception:
        _logger.exception("Error processing theming CSS, ignoring")

    return render_template("login.jinja2", **render_kwargs)
//  [python]
def get_object_src_http(dataset, rel_path):
    path = _get_obj_absolute_path(dataset, rel_path)
    response = send_file(path,
                         cache_timeout=datetime.timedelta(
                             days=365).total_seconds(),
                         add_etags=True,
                         conditional=True)
    return response
//  [python]
def get_object_src_http(dataset, rel_path):
    path = _get_obj_abosolute_path(dataset, rel_path)
    response = send_file(path,
                         cache_timeout=datetime.timedelta(
                             days=365).total_seconds(),
                         add_etags=True,
                         conditional=True)
    return response
//  [python]
def constructObject(data):
    try:
        m = import_module("messagetypes." + data[""])
        classBase = getattr(m, data[""].title())
    except (NameError, ImportError):
        logger.error("Don't know how to handle message type: \"%s\"", data[""], exc_info=True)
        return None
    try:
        returnObj = classBase()
        returnObj.decode(data)
    except KeyError as e:
        logger.error("Missing mandatory key %s", e)
        return None
    except:
        logger.error("classBase fail", exc_info=True)
        return None
    else:
        return returnObj
//  [python]
def constructObject(data):
    try:
        classBase = eval(data[""] + "." + data[""].title())
    except NameError:
        logger.error("Don't know how to handle message type: \"%s\"", data[""])
        return None
    try:
        returnObj = classBase()
        returnObj.decode(data)
    except KeyError as e:
        logger.error("Missing mandatory key %s", e)
        return None
    except:
        logger.error("classBase fail", exc_info=True)
        return None
    else:
        return returnObj
//  [python]
def _get_object_element(dataset, seq_no, rel_path, download_link):
    """If rel_path and download_link are not None, we are called from scope.
    Otherwise we are called from ID and need to run SQL query to fetch these attrs."""

    if rel_path is None:
        query = "SELECT rel_path, download_link FROM " + \
        dataset + \
        " WHERE sequence_no = %s"

        cnx = mysql.connector.connect(user=DB_USER,
                                    password=DB_PASSWORD,
                                    host=DB_HOST,
                                    database=DB_DBNAME,
                                    port=DB_PORT)
        cursor = cnx.cursor()
        cursor.execute(query, (seq_no,))

        row = cursor.fetchone()

        if not row:
            return None

        rel_path, download_link = row[0], row[1]

    if LOCAL_OBJ_URI:
        src_uri = 'file://' + _get_obj_absolute_path(dataset, rel_path)
    else:
        src_uri = url_for('.get_object_src_http', dataset=dataset, rel_path=rel_path)

    return '<object id={} src={} hyperfind.external-link={} />' \
        .format(
        quoteattr(url_for('.get_object_id', dataset=dataset, seq_no=seq_no)),
        quoteattr(src_uri),
        quoteattr(download_link))
//  [python]
def _get_object_element(dataset, seq_no, rel_path, download_link):
    """If rel_path and download_link are not None, we are called from scope.
    Otherwise we are called from ID and need to run SQL query to fetch these attrs."""

    if rel_path is None:
        query = "SELECT rel_path, download_link FROM " + \
        dataset + \
        " WHERE sequence_no = %s"

        cnx = mysql.connector.connect(user=DB_USER,
                                    password=DB_PASSWORD,
                                    host=DB_HOST,
                                    database=DB_DBNAME,
                                    port=DB_PORT)
        cursor = cnx.cursor()
        cursor.execute(query, (seq_no,))

        row = cursor.fetchone()

        if not row:
            return None

        rel_path, download_link = row[0], row[1]

    if LOCAL_OBJ_URI:
        src_uri = 'file://' + os.path.join(DATAROOT, dataset, rel_path)
    else:
        src_uri = url_for('.get_object_src_http', dataset=dataset, rel_path=rel_path)

    return '<object id={} src={} hyperfind.external-link={} />' \
        .format(
        quoteattr(url_for('.get_object_id', dataset=dataset, seq_no=seq_no)),
        quoteattr(src_uri),
        quoteattr(download_link))
//  [python]
def test_get_response_httperror_exception(self, mock_get):
    mock_response = mock.Mock()
    mock_response.status_code = 500
    mock_response.text = "Server Error"
    http_error = requests_or_advocate.HTTPError()
    mock_response.raise_for_status.side_effect = http_error
    mock_get.return_value = mock_response

    url = "https://example.com/"
    query_runner = BaseHTTPQueryRunner({})
    response, error = query_runner.get_response(url)
    mock_get.assert_called_once_with("get", url, auth=None)
    self.assertIsNotNone(error)
    self.assertIn("Failed to execute query", error)
//  [python]
def test_get_response_httperror_exception(self, mock_get):
    mock_response = mock.Mock()
    mock_response.status_code = 500
    mock_response.text = "Server Error"
    http_error = requests.HTTPError()
    mock_response.raise_for_status.side_effect = http_error
    mock_get.return_value = mock_response

    url = "https://example.com/"
    query_runner = BaseHTTPQueryRunner({})
    response, error = query_runner.get_response(url)
    mock_get.assert_called_once_with("get", url, auth=None)
    self.assertIsNotNone(error)
    self.assertIn("Failed to execute query", error)
//  [python]
def test_get_conditions(self, freeze_upload_folder):
    conditions = ClearableFileInput().get_conditions(None)
    assert all(
        condition in conditions
        for condition in [
            {"bucket": "test-bucket"},
            {"success_action_status": "201"},
            ["starts-with", "$key", "custom/location/tmp/s3file"],
            ["starts-with", "$Content-Type", ""],
        ]
    ), conditions
//  [python]
def test_get_conditions(self, freeze):
    conditions = ClearableFileInput().get_conditions(None)
    assert all(
        condition in conditions
        for condition in [
            {"bucket": "test-bucket"},
            {"success_action_status": "201"},
            ["starts-with", "$key", "custom/location/tmp"],
            ["starts-with", "$Content-Type", ""],
        ]
    ), conditions
//  [python]
def test_build_attr(self, freeze_upload_folder):
    assert set(ClearableFileInput().build_attrs({}).keys()) == {
        "class",
        "data-url",
        "data-fields-x-amz-algorithm",
        "data-fields-x-amz-date",
        "data-fields-x-amz-signature",
        "data-fields-x-amz-credential",
        "data-fields-policy",
        "data-fields-key",
        "data-s3f-signature",
    }
    assert (
        ClearableFileInput().build_attrs({})["data-s3f-signature"]
        == "tFV9nGZlq9WX1I5Sotit18z1f4C_3lPnj33_zo4LZRc"
    )
    assert ClearableFileInput().build_attrs({})["class"] == "s3file"
    assert (
        ClearableFileInput().build_attrs({"class": "my-class"})["class"]
        == "my-class s3file"
    )
//  [python]
def test_build_attr(self):
    assert set(ClearableFileInput().build_attrs({}).keys()) == {
        "class",
        "data-url",
        "data-fields-x-amz-algorithm",
        "data-fields-x-amz-date",
        "data-fields-x-amz-signature",
        "data-fields-x-amz-credential",
        "data-fields-policy",
        "data-fields-key",
    }
    assert ClearableFileInput().build_attrs({})["class"] == "s3file"
    assert (
        ClearableFileInput().build_attrs({"class": "my-class"})["class"]
        == "my-class s3file"
    )
//  [python]
def test_value_from_datadict(self, freeze_upload_folder, client, upload_file):
    with open(upload_file) as f:
        uploaded_file = storage.save(freeze_upload_folder / "test.jpg", f)
    response = client.post(
        reverse("upload"),
        {
            "file": f"custom/location/{uploaded_file}",
            "file-s3f-signature": "m94qBxBsnMIuIICiY133kX18KkllSPMVbhGAdAwNn1A",
            "s3file": "file",
        },
    )

    assert response.status_code == 201
//  [python]
def test_value_from_datadict(self, client, upload_file):
    print(storage.location)
    with open(upload_file) as f:
        uploaded_file = storage.save("test.jpg", f)
    response = client.post(
        reverse("upload"),
        {
            "file": json.dumps([uploaded_file]),
            "s3file": '["file"]',
        },
    )

    assert response.status_code == 201
//  [python]
def from_dict(d: Dict[str, Any]) -> AModel:
    an_enum_value = AnEnum(d["an_enum_value"])

    some_dict = d["some_dict"]

    def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
        a_camel_date_time: Union[datetime.datetime, datetime.date]
        try:
            a_camel_date_time = datetime.datetime.fromisoformat(d["aCamelDateTime"])

            return a_camel_date_time
        except:
            pass
        a_camel_date_time = datetime.date.fromisoformat(d["aCamelDateTime"])

        return a_camel_date_time

    a_camel_date_time = _parse_a_camel_date_time(d["aCamelDateTime"])

    a_date = datetime.date.fromisoformat(d["a_date"])

    nested_list_of_enums = []
    for nested_list_of_enums_item_data in d.get("nested_list_of_enums") or []:
        nested_list_of_enums_item = []
        for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
            nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)

            nested_list_of_enums_item.append(nested_list_of_enums_item_item)

        nested_list_of_enums.append(nested_list_of_enums_item)

    return AModel(
        an_enum_value=an_enum_value,
        some_dict=some_dict,
        a_camel_date_time=a_camel_date_time,
        a_date=a_date,
        nested_list_of_enums=nested_list_of_enums,
    )
//  [python]
def from_dict(d: Dict[str, Any]) -> AModel:
    an_enum_value = AnEnum(d["an_enum_value"])

    def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime, date]:
        a_camel_date_time: Union[datetime, date]
        try:
            a_camel_date_time = datetime.fromisoformat(d["aCamelDateTime"])

            return a_camel_date_time
        except:
            pass
        a_camel_date_time = date.fromisoformat(d["aCamelDateTime"])

        return a_camel_date_time

    a_camel_date_time = _parse_a_camel_date_time(d["aCamelDateTime"])

    a_date = date.fromisoformat(d["a_date"])

    nested_list_of_enums = []
    for nested_list_of_enums_item_data in d.get("nested_list_of_enums") or []:
        nested_list_of_enums_item = []
        for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
            nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)

            nested_list_of_enums_item.append(nested_list_of_enums_item_item)

        nested_list_of_enums.append(nested_list_of_enums_item)

    some_dict = d.get("some_dict")

    return AModel(
        an_enum_value=an_enum_value,
        a_camel_date_time=a_camel_date_time,
        a_date=a_date,
        nested_list_of_enums=nested_list_of_enums,
        some_dict=some_dict,
    )