pykernel-cli 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Your Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.4
2
+ Name: pykernel-cli
3
+ Version: 1.0.0
4
+ Summary: A hackable Python REPL with first-class command support
5
+ Author-email: zKaiden <odrekzinho@gmail.com>
6
+ License: MIT
7
+ Keywords: repl,shell,interactive,python
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Environment :: Console
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Topic :: Software Development :: Interpreters
16
+ Requires-Python: >=3.11
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Provides-Extra: dev
20
+ Requires-Dist: build; extra == "dev"
21
+ Requires-Dist: twine; extra == "dev"
22
+ Dynamic: license-file
23
+
24
+ # PyKernel
25
+
26
+ A hackable Python REPL with first-class command support.
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ pip install pykernel
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ```bash
37
+ pykernel
38
+ # or
39
+ python -m pykernel
40
+ ```
41
+
42
+ Type `/help` to see all commands.
43
+
44
+ ## Commands
45
+
46
+ | Command | Description |
47
+ |---|---|
48
+ | `/help` | List all commands |
49
+ | `/vars` | List variables in the namespace |
50
+ | `/run <file>` | Execute a Python file |
51
+ | `/sh <cmd>` | Run a shell command |
52
+ | `/snip` | Manage code snippets |
53
+ | `/timeit <expr>` | Time an expression |
54
+
55
+ ## Plugins
56
+
57
+ Drop a `.py` file with a `register(registry)` function into `~/.pykernel/plugins/`:
58
+
59
+ ```python
60
+ def register(reg):
61
+ @reg.command("hello", help="Say hello")
62
+ def cmd_hello(ctx, *args):
63
+ ctx.print("Hello!")
64
+ ```
65
+
66
+ ## Config
67
+
68
+ Edit `~/.pykernel/config.toml`:
69
+
70
+ ```toml
71
+ [kernel]
72
+ prompt = ">>> "
73
+ theme = "dark" # dark | light | none
74
+ ```
@@ -0,0 +1,51 @@
1
+ # PyKernel
2
+
3
+ A hackable Python REPL with first-class command support.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pip install pykernel
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ pykernel
15
+ # or
16
+ python -m pykernel
17
+ ```
18
+
19
+ Type `/help` to see all commands.
20
+
21
+ ## Commands
22
+
23
+ | Command | Description |
24
+ |---|---|
25
+ | `/help` | List all commands |
26
+ | `/vars` | List variables in the namespace |
27
+ | `/run <file>` | Execute a Python file |
28
+ | `/sh <cmd>` | Run a shell command |
29
+ | `/snip` | Manage code snippets |
30
+ | `/timeit <expr>` | Time an expression |
31
+
32
+ ## Plugins
33
+
34
+ Drop a `.py` file with a `register(registry)` function into `~/.pykernel/plugins/`:
35
+
36
+ ```python
37
+ def register(reg):
38
+ @reg.command("hello", help="Say hello")
39
+ def cmd_hello(ctx, *args):
40
+ ctx.print("Hello!")
41
+ ```
42
+
43
+ ## Config
44
+
45
+ Edit `~/.pykernel/config.toml`:
46
+
47
+ ```toml
48
+ [kernel]
49
+ prompt = ">>> "
50
+ theme = "dark" # dark | light | none
51
+ ```
File without changes
@@ -0,0 +1,2 @@
1
+ from pykernel.kernel import main
2
+ main()
File without changes
@@ -0,0 +1,144 @@
1
+ """
2
+ Built-in commands — always available.
3
+
4
+ /help [command] — list all commands or describe one
5
+ /exit | /quit — exit the kernel
6
+ /clear — clear the screen
7
+ /history [n] — show last n inputs (default 20)
8
+ /run <file.py> — execute a Python file inside the kernel
9
+ /reset — clear the execution namespace
10
+ /alias <new> <target> — create an alias for an existing command
11
+ /commands — list all commands (alias for /help)
12
+ """
13
+
14
+ import os, pathlib, traceback
15
+ from pykernel.core.registry import CommandRegistry
16
+
17
+
18
+ def register(reg: CommandRegistry):
19
+
20
+ # ── /help ─────────────────────────────────────────────────────────────────
21
+
22
+ @reg.command("help", aliases=["?", "commands"],
23
+ help="List commands or show help for a specific command",
24
+ usage="/help [command]",
25
+ category="Core")
26
+ def cmd_help(ctx, *args):
27
+ p = ctx.paint
28
+ if args:
29
+ name = args[0].lstrip("/")
30
+ cmd = ctx.registry.resolve(name)
31
+ if cmd is None:
32
+ ctx.print(p.error(f"No command: /{name}"))
33
+ return
34
+ ctx.print(p.header(f" /{cmd.name}"))
35
+ if cmd.aliases:
36
+ ctx.print(p.dim(f" Aliases : " + ", ".join(f"/{a}" for a in cmd.aliases)))
37
+ ctx.print(p.dim(f" Usage : {cmd.usage}"))
38
+ ctx.print(f" {cmd.help}")
39
+ return
40
+
41
+ by_cat = ctx.registry.by_category()
42
+ ctx.print()
43
+ for cat, cmds in sorted(by_cat.items()):
44
+ ctx.print(p.header(f" {cat}"))
45
+ rows = [(f"/{c.name}", ", ".join(f"/{a}" for a in c.aliases), c.help)
46
+ for c in cmds]
47
+ ctx.print(p.table(rows, headers=["Command", "Aliases", "Description"]))
48
+ ctx.print()
49
+ ctx.print(p.dim(" Tip: type /help <command> for detailed usage."))
50
+ ctx.print()
51
+
52
+ # ── /exit ─────────────────────────────────────────────────────────────────
53
+
54
+ @reg.command("exit", aliases=["quit", "q"],
55
+ help="Exit the kernel",
56
+ usage="/exit",
57
+ category="Core")
58
+ def cmd_exit(ctx, *args):
59
+ ctx.print(ctx.paint.dim(" Goodbye."))
60
+ raise SystemExit(0)
61
+
62
+ # ── /clear ────────────────────────────────────────────────────────────────
63
+
64
+ @reg.command("clear", aliases=["cls"],
65
+ help="Clear the terminal screen",
66
+ usage="/clear",
67
+ category="Core")
68
+ def cmd_clear(ctx, *args):
69
+ os.system("clear" if os.name != "nt" else "cls")
70
+
71
+ # ── /history ──────────────────────────────────────────────────────────────
72
+
73
+ @reg.command("history", aliases=["hist"],
74
+ help="Show recent input history",
75
+ usage="/history [n]",
76
+ category="Core")
77
+ def cmd_history(ctx, *args):
78
+ p = ctx.paint
79
+ n = int(args[0]) if args and args[0].isdigit() else 20
80
+ items = ctx.history[-(n):]
81
+ if not items:
82
+ ctx.print(p.dim(" (no history yet)"))
83
+ return
84
+ ctx.print()
85
+ for i, entry in enumerate(items, start=max(1, len(ctx.history) - n + 1)):
86
+ prefix = p.dim(f" {i:>4} │ ")
87
+ # truncate long entries
88
+ snippet = entry.replace("\n", "⏎ ")[:80]
89
+ ctx.print(prefix + snippet)
90
+ ctx.print()
91
+
92
+ # ── /run ──────────────────────────────────────────────────────────────────
93
+
94
+ @reg.command("run",
95
+ help="Execute a Python file in the kernel namespace",
96
+ usage="/run <path/to/file.py>",
97
+ category="Core")
98
+ def cmd_run(ctx, *args):
99
+ p = ctx.paint
100
+ if not args:
101
+ ctx.print(p.error("Usage: /run <path/to/file.py>"))
102
+ return
103
+ fpath = pathlib.Path(args[0]).expanduser()
104
+ if not fpath.exists():
105
+ ctx.print(p.error(f"File not found: {fpath}"))
106
+ return
107
+ try:
108
+ source = fpath.read_text()
109
+ exec(compile(source, str(fpath), "exec"), ctx.env)
110
+ ctx.print(p.success(f" ✓ Ran {fpath.name}"))
111
+ except Exception:
112
+ ctx.print(p.error(traceback.format_exc()))
113
+
114
+ # ── /reset ────────────────────────────────────────────────────────────────
115
+
116
+ @reg.command("reset",
117
+ help="Clear all variables from the kernel namespace",
118
+ usage="/reset",
119
+ category="Core")
120
+ def cmd_reset(ctx, *args):
121
+ kept = {"__name__", "__builtins__"}
122
+ for k in list(ctx.env.keys()):
123
+ if k not in kept:
124
+ del ctx.env[k]
125
+ ctx.print(ctx.paint.success(" ✓ Namespace cleared."))
126
+
127
+ # ── /alias ────────────────────────────────────────────────────────────────
128
+
129
+ @reg.command("alias",
130
+ help="Create an alias for an existing command (/alias hi greet)",
131
+ usage="/alias <new_name> <existing_command>",
132
+ category="Core")
133
+ def cmd_alias(ctx, *args):
134
+ p = ctx.paint
135
+ if len(args) < 2:
136
+ ctx.print(p.error("Usage: /alias <new_name> <existing_command>"))
137
+ return
138
+ new_name, target = args[0].lstrip("/"), args[1].lstrip("/")
139
+ cmd = ctx.registry.resolve(target)
140
+ if cmd is None:
141
+ ctx.print(p.error(f"No command: /{target}"))
142
+ return
143
+ ctx.registry._alias[new_name] = cmd.name
144
+ ctx.print(p.success(f" ✓ /{new_name} → /{cmd.name}"))
@@ -0,0 +1,164 @@
1
+ """
2
+ Inspector commands — explore the live kernel namespace.
3
+
4
+ /vars — list all variables in the namespace
5
+ /who <name> — print type + repr of a variable
6
+ /doc <name> — show docstring
7
+ /source <name> — show source code (functions / classes)
8
+ /del <name> — delete a variable
9
+ /timeit <expr> — time an expression
10
+ """
11
+
12
+ import inspect, timeit as _timeit, textwrap
13
+ from pykernel.core.registry import CommandRegistry
14
+
15
+
16
+ def register(reg: CommandRegistry):
17
+
18
+ # ── /vars ─────────────────────────────────────────────────────────────────
19
+
20
+ @reg.command("vars", aliases=["ls", "env"],
21
+ help="List all variables in the kernel namespace",
22
+ usage="/vars",
23
+ category="Inspector")
24
+ def cmd_vars(ctx, *args):
25
+ p = ctx.paint
26
+ ns = ctx.namespace_vars()
27
+ if not ns:
28
+ ctx.print(p.dim(" (namespace is empty)"))
29
+ return
30
+ rows = []
31
+ for name, val in sorted(ns.items()):
32
+ typ = type(val).__name__
33
+ snippet = repr(val)[:60].replace("\n", " ")
34
+ rows.append((name, typ, snippet))
35
+ ctx.print()
36
+ ctx.print(p.table(rows, headers=["Name", "Type", "Value"]))
37
+ ctx.print()
38
+
39
+ # ── /who ──────────────────────────────────────────────────────────────────
40
+
41
+ @reg.command("who",
42
+ help="Show detailed info about a variable",
43
+ usage="/who <name>",
44
+ category="Inspector")
45
+ def cmd_who(ctx, *args):
46
+ p = ctx.paint
47
+ if not args:
48
+ ctx.print(p.error("Usage: /who <name>"))
49
+ return
50
+ name = args[0]
51
+ if not ctx.has_var(name):
52
+ ctx.print(p.error(f"'{name}' is not defined in the namespace"))
53
+ return
54
+ val = ctx.get_var(name)
55
+ typ = type(val)
56
+ ctx.print()
57
+ ctx.print(p.header(f" {name}"))
58
+ ctx.print(f" type : {p.code(typ.__module__ + '.' + typ.__name__)}")
59
+ ctx.print(f" repr : {repr(val)[:120]}")
60
+ if hasattr(val, "__len__"):
61
+ try: ctx.print(f" len : {len(val)}")
62
+ except: pass
63
+ if hasattr(val, "shape"):
64
+ ctx.print(f" shape : {val.shape}")
65
+ if hasattr(val, "dtype"):
66
+ ctx.print(f" dtype : {val.dtype}")
67
+ ctx.print(f" id : {id(val)}")
68
+ ctx.print()
69
+
70
+ # ── /doc ──────────────────────────────────────────────────────────────────
71
+
72
+ @reg.command("doc",
73
+ help="Show the docstring of a name",
74
+ usage="/doc <name>",
75
+ category="Inspector")
76
+ def cmd_doc(ctx, *args):
77
+ p = ctx.paint
78
+ if not args:
79
+ ctx.print(p.error("Usage: /doc <name>"))
80
+ return
81
+ name = args[0]
82
+ # try namespace first, then builtins
83
+ val = ctx.get_var(name, None) or __builtins__.__dict__.get(name) if isinstance(__builtins__, dict) else getattr(__builtins__, name, None)
84
+ if val is None:
85
+ try:
86
+ import builtins
87
+ val = getattr(builtins, name, None)
88
+ except: pass
89
+ if val is None:
90
+ ctx.print(p.error(f"'{name}' not found"))
91
+ return
92
+ doc = inspect.getdoc(val) or "(no docstring)"
93
+ ctx.print()
94
+ ctx.print(p.header(f" {name}"))
95
+ ctx.print(textwrap.indent(doc, " "))
96
+ ctx.print()
97
+
98
+ # ── /source ───────────────────────────────────────────────────────────────
99
+
100
+ @reg.command("source", aliases=["src"],
101
+ help="Show source code of a function or class",
102
+ usage="/source <name>",
103
+ category="Inspector")
104
+ def cmd_source(ctx, *args):
105
+ p = ctx.paint
106
+ if not args:
107
+ ctx.print(p.error("Usage: /source <name>"))
108
+ return
109
+ name = args[0]
110
+ val = ctx.get_var(name)
111
+ if val is None:
112
+ ctx.print(p.error(f"'{name}' not in namespace"))
113
+ return
114
+ try:
115
+ src = inspect.getsource(val)
116
+ ctx.print()
117
+ ctx.print(p.code(src))
118
+ ctx.print()
119
+ except (TypeError, OSError) as e:
120
+ ctx.print(p.error(str(e)))
121
+
122
+ # ── /del ──────────────────────────────────────────────────────────────────
123
+
124
+ @reg.command("del", aliases=["delete", "rm"],
125
+ help="Delete a variable from the namespace",
126
+ usage="/del <name>",
127
+ category="Inspector")
128
+ def cmd_del(ctx, *args):
129
+ p = ctx.paint
130
+ if not args:
131
+ ctx.print(p.error("Usage: /del <name>"))
132
+ return
133
+ for name in args:
134
+ if ctx.delete_var(name):
135
+ ctx.print(p.success(f" ✓ Deleted '{name}'"))
136
+ else:
137
+ ctx.print(p.warning(f" ⚠ '{name}' not in namespace"))
138
+
139
+ # ── /timeit ───────────────────────────────────────────────────────────────
140
+
141
+ @reg.command("timeit",
142
+ help="Time an expression (/timeit sum(range(1000000)))",
143
+ usage="/timeit <expression> [repeat=N]",
144
+ category="Inspector")
145
+ def cmd_timeit(ctx, *args):
146
+ p = ctx.paint
147
+ if not args:
148
+ ctx.print(p.error("Usage: /timeit <expression>"))
149
+ return
150
+ # allow trailing repeat=N
151
+ repeat = 3
152
+ expr_parts = list(args)
153
+ if expr_parts and expr_parts[-1].startswith("repeat="):
154
+ try: repeat = int(expr_parts.pop()[7:])
155
+ except: pass
156
+ expr = " ".join(expr_parts)
157
+ try:
158
+ times = _timeit.repeat(expr, globals=ctx.env, repeat=repeat, number=1000)
159
+ best = min(times) * 1_000_000 / 1000 # µs per loop
160
+ ctx.print()
161
+ ctx.print(p.info(f" {best:.3f} µs ± per loop (best of {repeat}×1000)"))
162
+ ctx.print()
163
+ except Exception as e:
164
+ ctx.print(p.error(str(e)))
@@ -0,0 +1,101 @@
1
+ """
2
+ Shell commands — run system commands, navigate dirs, etc.
3
+
4
+ /sh <cmd> — run a shell command (output captured into _)
5
+ /cd <dir> — change working directory
6
+ /pwd — print working directory
7
+ /ls [dir] — list directory contents
8
+ """
9
+
10
+ import os, subprocess, shlex, pathlib
11
+ from pykernel.core.registry import CommandRegistry
12
+
13
+
14
+ def register(reg: CommandRegistry):
15
+
16
+ # ── /sh ───────────────────────────────────────────────────────────────────
17
+
18
+ @reg.command("sh", aliases=["!"],
19
+ help="Run a shell command; output stored in `_out`",
20
+ usage="/sh <shell command>",
21
+ category="Shell")
22
+ def cmd_sh(ctx, *args):
23
+ p = ctx.paint
24
+ if not args:
25
+ ctx.print(p.error("Usage: /sh <command>"))
26
+ return
27
+ cmd = " ".join(args)
28
+ try:
29
+ result = subprocess.run(
30
+ cmd, shell=True, text=True,
31
+ capture_output=True
32
+ )
33
+ out = result.stdout
34
+ err = result.stderr
35
+ if out:
36
+ ctx.print(out, end="")
37
+ if err:
38
+ ctx.print(p.warning(err), end="")
39
+ if result.returncode != 0:
40
+ ctx.print(p.dim(f" exit code: {result.returncode}"))
41
+ # inject result into namespace
42
+ ctx.set_var("_out", out)
43
+ ctx.set_var("_err", err)
44
+ ctx.set_var("_retcode", result.returncode)
45
+ except Exception as e:
46
+ ctx.print(p.error(str(e)))
47
+
48
+ # ── /cd ───────────────────────────────────────────────────────────────────
49
+
50
+ @reg.command("cd",
51
+ help="Change the current working directory",
52
+ usage="/cd <path>",
53
+ category="Shell")
54
+ def cmd_cd(ctx, *args):
55
+ p = ctx.paint
56
+ dest = pathlib.Path(args[0]).expanduser() if args else pathlib.Path.home()
57
+ try:
58
+ os.chdir(dest)
59
+ ctx.print(p.info(f" → {os.getcwd()}"))
60
+ except FileNotFoundError:
61
+ ctx.print(p.error(f"Directory not found: {dest}"))
62
+ except PermissionError:
63
+ ctx.print(p.error(f"Permission denied: {dest}"))
64
+
65
+ # ── /pwd ──────────────────────────────────────────────────────────────────
66
+
67
+ @reg.command("pwd",
68
+ help="Print the current working directory",
69
+ usage="/pwd",
70
+ category="Shell")
71
+ def cmd_pwd(ctx, *args):
72
+ ctx.print(ctx.paint.info(f" {os.getcwd()}"))
73
+
74
+ # ── /ls ───────────────────────────────────────────────────────────────────
75
+
76
+ @reg.command("ls",
77
+ help="List directory contents",
78
+ usage="/ls [path]",
79
+ category="Shell")
80
+ def cmd_ls(ctx, *args):
81
+ p = ctx.paint
82
+ path = pathlib.Path(args[0]).expanduser() if args else pathlib.Path(".")
83
+ try:
84
+ entries = sorted(path.iterdir(), key=lambda e: (not e.is_dir(), e.name.lower()))
85
+ except (FileNotFoundError, PermissionError) as e:
86
+ ctx.print(p.error(str(e)))
87
+ return
88
+
89
+ ctx.print()
90
+ for entry in entries:
91
+ if entry.is_dir():
92
+ ctx.print(p.accent(f" 📁 {entry.name}/"))
93
+ else:
94
+ size = entry.stat().st_size
95
+ size_s = (
96
+ f"{size / 1_048_576:.1f} MB" if size > 1_048_576
97
+ else f"{size / 1_024:.1f} KB" if size > 1_024
98
+ else f"{size} B"
99
+ )
100
+ ctx.print(f" 📄 {entry.name:<40} {p.dim(size_s)}")
101
+ ctx.print()