• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thomwiggers / onebot / 20856128817

09 Jan 2026 03:05PM UTC coverage: 68.859% (-0.2%) from 69.064%
20856128817

Pull #213

github

thomwiggers
style: fix formatting in python plugin and sandbox server
Pull Request #213: feat: refactor python-sandbox to standalone server

2 of 20 new or added lines in 1 file covered. (10.0%)

827 of 1201 relevant lines covered (68.86%)

3.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.25
/onebot/plugins/python.py
1
"""
2
=====================================================
3
:mod:`onebot.plugins.python` Run python commands in docker
4
=====================================================
5

6
Allow to run Python commands
7

8
Config:
9
=======
10

11
.. code-block: ini
12

13
    [bot]
14
    includes=
15
      onebot.plugins.python
16
"""
17

18
from typing import Self
5✔
19
import irc3
5✔
20
from irc3.plugins.command import command
5✔
21
import requests
5✔
22
import os
5✔
23

24

25
@irc3.plugin
5✔
26
class PythonPlugin:
5✔
27
    """Execute commands after having connected"""
28

29
    def __init__(self, bot):
5✔
30
        self.bot = bot
×
31
        self.log = bot.log.getChild(__name__)
×
NEW
32
        self.sandbox_url = os.environ.get("PYTHON_SANDBOX_URL", "http://localhost:8080")
×
33

34
    @command(use_shlex=False)
5✔
35
    def py(self, _mask, _target, args):
5✔
36
        """Execute a command in a Python 3 interpreter
37

38
        %%py <command>...
39
        """
40
        cmd = " ".join(args["<command>"])
×
41
        self.log.debug("Command: '%s'", cmd)
×
NEW
42
        try:
×
NEW
43
            response = requests.post(
×
44
                self.sandbox_url,
45
                json={"code": cmd},
46
                timeout=10,
47
            )
NEW
48
            response.raise_for_status()
×
NEW
49
            data = response.json()
×
NEW
50
            stdout = data.get("stdout", "")
×
NEW
51
            stderr = data.get("stderr", "")
×
52

NEW
53
            if stdout:
×
NEW
54
                for line in stdout.split("\n")[:2]:
×
NEW
55
                    yield line[:200]
×
NEW
56
            if stderr:
×
NEW
57
                for line in stderr.split("\n")[:2]:
×
NEW
58
                    yield f"Error: {line[:200]}"
×
NEW
59
            if not stdout and not stderr:
×
NEW
60
                yield "No output."
×
61

NEW
62
        except requests.exceptions.RequestException as e:
×
NEW
63
            self.log.error("Failed to connect to sandbox: %s", e)
×
NEW
64
            yield "Error: Could not connect to Python sandbox."
×
65

66
    @classmethod
67
    def reload(cls, old: Self) -> Self:  # pragma: no cover
68
        return cls(old.bot)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc