codex-python 0.1.0__py3-none-any.whl

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.
codex/__init__.py ADDED
@@ -0,0 +1,30 @@
1
+ """codex
2
+
3
+ Python interface for the Codex CLI.
4
+
5
+ Usage:
6
+ from codex import run_exec
7
+ output = run_exec("explain this codebase to me")
8
+ """
9
+
10
+ from .api import (
11
+ CodexClient,
12
+ CodexError,
13
+ CodexNotFoundError,
14
+ CodexProcessError,
15
+ find_binary,
16
+ run_exec,
17
+ )
18
+
19
+ __all__ = [
20
+ "__version__",
21
+ "CodexError",
22
+ "CodexNotFoundError",
23
+ "CodexProcessError",
24
+ "CodexClient",
25
+ "find_binary",
26
+ "run_exec",
27
+ ]
28
+
29
+ # Managed by Hatch via pyproject.toml [tool.hatch.version]
30
+ __version__ = "0.1.0"
codex/api.py ADDED
@@ -0,0 +1,165 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import shutil
5
+ import subprocess
6
+ from collections.abc import Iterable, Mapping, Sequence
7
+ from dataclasses import dataclass
8
+
9
+
10
+ class CodexError(Exception):
11
+ """Base exception for codex-python."""
12
+
13
+
14
+ class CodexNotFoundError(CodexError):
15
+ """Raised when the 'codex' binary cannot be found or executed."""
16
+
17
+ def __init__(self, executable: str = "codex") -> None:
18
+ super().__init__(
19
+ f"Codex CLI not found: '{executable}'.\n"
20
+ "Install from https://github.com/openai/codex or ensure it is on PATH."
21
+ )
22
+ self.executable = executable
23
+
24
+
25
+ @dataclass(slots=True)
26
+ class CodexProcessError(CodexError):
27
+ """Raised when the codex process exits with a non‑zero status."""
28
+
29
+ returncode: int
30
+ cmd: Sequence[str]
31
+ stdout: str
32
+ stderr: str
33
+
34
+ def __str__(self) -> str: # pragma: no cover - repr is sufficient
35
+ return (
36
+ f"Codex process failed with exit code {self.returncode}.\n"
37
+ f"Command: {' '.join(self.cmd)}\n"
38
+ f"stderr:\n{self.stderr.strip()}"
39
+ )
40
+
41
+
42
+ def find_binary(executable: str = "codex") -> str:
43
+ """Return the absolute path to the Codex CLI binary or raise if not found."""
44
+ path = shutil.which(executable)
45
+ if not path:
46
+ raise CodexNotFoundError(executable)
47
+ return path
48
+
49
+
50
+ def run_exec(
51
+ prompt: str,
52
+ *,
53
+ model: str | None = None,
54
+ full_auto: bool = False,
55
+ cd: str | None = None,
56
+ timeout: float | None = None,
57
+ env: Mapping[str, str] | None = None,
58
+ executable: str = "codex",
59
+ extra_args: Iterable[str] | None = None,
60
+ ) -> str:
61
+ """
62
+ Run `codex exec` with the given prompt and return stdout as text.
63
+
64
+ - Raises CodexNotFoundError if the binary is unavailable.
65
+ - Raises CodexProcessError on non‑zero exit with captured stdout/stderr.
66
+ """
67
+ bin_path = find_binary(executable)
68
+
69
+ cmd: list[str] = [bin_path]
70
+
71
+ if cd:
72
+ cmd.extend(["--cd", cd])
73
+ if model:
74
+ cmd.extend(["-m", model])
75
+ if full_auto:
76
+ cmd.append("--full-auto")
77
+ if extra_args:
78
+ cmd.extend(list(extra_args))
79
+
80
+ cmd.extend(["exec", prompt])
81
+
82
+ completed = subprocess.run(
83
+ cmd,
84
+ capture_output=True,
85
+ text=True,
86
+ timeout=timeout,
87
+ env={**os.environ, **(dict(env) if env else {})},
88
+ check=False,
89
+ )
90
+
91
+ stdout = completed.stdout or ""
92
+ stderr = completed.stderr or ""
93
+ if completed.returncode != 0:
94
+ raise CodexProcessError(
95
+ returncode=completed.returncode,
96
+ cmd=tuple(cmd),
97
+ stdout=stdout,
98
+ stderr=stderr,
99
+ )
100
+ return stdout
101
+
102
+
103
+ @dataclass(slots=True)
104
+ class CodexClient:
105
+ """Lightweight, synchronous client for the Codex CLI.
106
+
107
+ Provides defaults for repeated invocations and convenience helpers.
108
+ """
109
+
110
+ executable: str = "codex"
111
+ model: str | None = None
112
+ full_auto: bool = False
113
+ cd: str | None = None
114
+ env: Mapping[str, str] | None = None
115
+ extra_args: Sequence[str] | None = None
116
+
117
+ def ensure_available(self) -> str:
118
+ """Return the resolved binary path or raise CodexNotFoundError."""
119
+ return find_binary(self.executable)
120
+
121
+ def run(
122
+ self,
123
+ prompt: str,
124
+ *,
125
+ model: str | None = None,
126
+ full_auto: bool | None = None,
127
+ cd: str | None = None,
128
+ timeout: float | None = None,
129
+ env: Mapping[str, str] | None = None,
130
+ extra_args: Iterable[str] | None = None,
131
+ ) -> str:
132
+ """Execute `codex exec` and return stdout.
133
+
134
+ Explicit arguments override the client's defaults.
135
+ """
136
+ eff_model = model if model is not None else self.model
137
+ eff_full_auto = full_auto if full_auto is not None else self.full_auto
138
+ eff_cd = cd if cd is not None else self.cd
139
+
140
+ # Merge environment overlays; run_exec will merge with os.environ
141
+ merged_env: Mapping[str, str] | None
142
+ if self.env and env:
143
+ tmp = dict(self.env)
144
+ tmp.update(env)
145
+ merged_env = tmp
146
+ else:
147
+ merged_env = env or self.env
148
+
149
+ # Compose extra args
150
+ eff_extra: list[str] = []
151
+ if self.extra_args:
152
+ eff_extra.extend(self.extra_args)
153
+ if extra_args:
154
+ eff_extra.extend(list(extra_args))
155
+
156
+ return run_exec(
157
+ prompt,
158
+ model=eff_model,
159
+ full_auto=eff_full_auto,
160
+ cd=eff_cd,
161
+ timeout=timeout,
162
+ env=merged_env,
163
+ executable=self.executable,
164
+ extra_args=eff_extra,
165
+ )
codex/py.typed ADDED
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,143 @@
1
+ Metadata-Version: 2.4
2
+ Name: codex-python
3
+ Version: 0.1.0
4
+ Summary: A minimal Python library scaffold for codex-python
5
+ Project-URL: Homepage, https://github.com/gersmann/codex-python
6
+ Project-URL: Repository, https://github.com/gersmann/codex-python
7
+ Project-URL: Issues, https://github.com/gersmann/codex-python/issues
8
+ License: MIT License
9
+
10
+ Copyright (c) 2025 gersmann
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+
30
+ License-File: LICENSE
31
+ Keywords: codex,library,scaffold
32
+ Classifier: License :: OSI Approved :: MIT License
33
+ Classifier: Operating System :: OS Independent
34
+ Classifier: Programming Language :: Python :: 3
35
+ Classifier: Programming Language :: Python :: 3 :: Only
36
+ Classifier: Programming Language :: Python :: 3.13
37
+ Classifier: Typing :: Typed
38
+ Requires-Python: >=3.13
39
+ Description-Content-Type: text/markdown
40
+
41
+ # codex-python
42
+
43
+ A minimal Python library scaffold using `uv` with Python 3.13+.
44
+
45
+ ## Quickstart
46
+
47
+ - Requires Python 3.13+.
48
+ - Package import name: `codex`.
49
+ - Distribution name (PyPI): `codex-python`.
50
+
51
+ ### Repo
52
+
53
+ - Git: `git@github.com:gersmann/codex-python.git`
54
+ - URL: https://github.com/gersmann/codex-python
55
+
56
+ ## Usage
57
+
58
+ Basic non-interactive execution via Codex CLI:
59
+
60
+ ```
61
+ from codex import run_exec
62
+
63
+ out = run_exec("explain this repo")
64
+ print(out)
65
+ ```
66
+
67
+ Options:
68
+
69
+ - Choose model: `run_exec("...", model="gpt-4.1")`
70
+ - Full auto: `run_exec("scaffold a cli", full_auto=True)`
71
+ - Run in another dir: `run_exec("...", cd="/path/to/project")`
72
+
73
+ Using a client with defaults:
74
+
75
+ ```
76
+ from codex import CodexClient
77
+
78
+ client = CodexClient(model="gpt-4.1", full_auto=True)
79
+ print(client.run("explain this repo"))
80
+ ```
81
+
82
+ ### Install uv
83
+
84
+ - macOS (Homebrew): `brew install uv`
85
+ - Or via install script:
86
+ - Unix/macOS: `curl -LsSf https://astral.sh/uv/install.sh | sh`
87
+ - Windows (PowerShell): `iwr https://astral.sh/uv/install.ps1 -UseBasicParsing | iex`
88
+
89
+ See: https://docs.astral.sh/uv/
90
+
91
+ ### Create a virtual env (optional)
92
+
93
+ ```
94
+ uv python install 3.13
95
+ uv venv --python 3.13
96
+ . .venv/bin/activate # or .venv\Scripts\activate on Windows
97
+ ```
98
+
99
+ ### Build
100
+
101
+ ```
102
+ uv build
103
+ ```
104
+
105
+ Artifacts appear in `dist/` (`.whl` and `.tar.gz`).
106
+
107
+ ### Publish to PyPI
108
+
109
+ - Manual:
110
+
111
+ ```
112
+ export PYPI_API_TOKEN="pypi-XXXX" # create at https://pypi.org/manage/account/token/
113
+ uv publish --token "$PYPI_API_TOKEN"
114
+ ```
115
+
116
+ - GitHub Actions: add a repository secret `PYPI_API_TOKEN` and push a tag like `v0.1.0`.
117
+ The workflow at `.github/workflows/publish.yml` builds and publishes with `uv` on `v*` tags.
118
+
119
+ ### Dev tooling
120
+
121
+ - Lint: `make lint` (ruff + mypy)
122
+ - Tests: `make test` (pytest)
123
+ - Format: `make fmt` (ruff formatter)
124
+ - Pre-commit: `uvx pre-commit install && uvx pre-commit run --all-files`
125
+
126
+ ## Project Layout
127
+
128
+ ```
129
+ .
130
+ ├── codex/ # package root (import name: codex)
131
+ │ └── __init__.py # version lives here
132
+ ├── pyproject.toml # PEP 621 metadata, hatchling build backend
133
+ ├── README.md
134
+ └── .gitignore
135
+ ```
136
+
137
+ ## Versioning
138
+
139
+ Version is managed via `codex/__init__.py` and exposed as `__version__`. The build uses Hatch’s version source.
140
+
141
+ ## Python Compatibility
142
+
143
+ - Requires Python `>=3.13`.
@@ -0,0 +1,7 @@
1
+ codex/__init__.py,sha256=E7HRcKV0xN5jhgmzYsAkQ2lm1KngmQgiH1T1LbQQ1mU,514
2
+ codex/api.py,sha256=aP1OcnMxSF-vJyxwVIllbydmrFiUESsuePvCyYkBXxo,4614
3
+ codex/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
4
+ codex_python-0.1.0.dist-info/METADATA,sha256=sVDPpdgXr-zFL0ege7Spc6ctWuPVE4-Sjnwb17Sls98,4246
5
+ codex_python-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ codex_python-0.1.0.dist-info/licenses/LICENSE,sha256=ZhGahTKhsCbPWNmZ7ugZ14LVewMo4Gh1OeIOlQabyrI,1066
7
+ codex_python-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 gersmann
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.
22
+