Skip to content

Commit c2c60ac

Browse files
committed
fix: Reimplemented IO and added examples
0 parents  commit c2c60ac

16 files changed

Lines changed: 2930 additions & 0 deletions

.gitignore

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
# Created by https://www.gitignore.io/api/vim,python
3+
4+
### Python ###
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
*.egg-info/
28+
.installed.cfg
29+
*.egg
30+
MANIFEST
31+
32+
# PyInstaller
33+
# Usually these files are written by a python script from a template
34+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
35+
*.manifest
36+
*.spec
37+
38+
# Installer logs
39+
pip-log.txt
40+
pip-delete-this-directory.txt
41+
42+
# Unit test / coverage reports
43+
htmlcov/
44+
.tox/
45+
.coverage
46+
.coverage.*
47+
.cache
48+
nosetests.xml
49+
coverage.xml
50+
*.cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
63+
# Flask stuff:
64+
instance/
65+
.webassets-cache
66+
67+
# Scrapy stuff:
68+
.scrapy
69+
70+
# Sphinx documentation
71+
docs/_build/
72+
73+
# PyBuilder
74+
target/
75+
76+
# Jupyter Notebook
77+
.ipynb_checkpoints
78+
79+
# pyenv
80+
.python-version
81+
82+
# celery beat schedule file
83+
celerybeat-schedule
84+
85+
# SageMath parsed files
86+
*.sage.py
87+
88+
# Environments
89+
.env
90+
.venv
91+
env/
92+
venv/
93+
ENV/
94+
env.bak/
95+
venv.bak/
96+
97+
# Spyder project settings
98+
.spyderproject
99+
.spyproject
100+
101+
# Rope project settings
102+
.ropeproject
103+
104+
# mkdocs documentation
105+
/site
106+
107+
# mypy
108+
.mypy_cache/
109+
110+
### Python Patch ###
111+
.venv/
112+
113+
### Vim ###
114+
# Swap
115+
[._]*.s[a-v][a-z]
116+
[._]*.sw[a-p]
117+
[._]s[a-rt-v][a-z]
118+
[._]ss[a-gi-z]
119+
[._]sw[a-p]
120+
121+
# Session
122+
Session.vim
123+
124+
# Temporary
125+
.netrwhist
126+
*~
127+
# Auto-generated tag files
128+
tags
129+
# Persistent undo
130+
[._]*.un~
131+
132+
133+
# End of https://www.gitignore.io/api/vim,python
134+
135+
136+
# test stuff
137+
/cov.xml
138+
/go/
139+
/tests/langservers/
140+
141+
.vscode

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Tomlin7
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.

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# DAP Client: Debug Adapter Protocol Client for Python
2+
3+
DAP Client is a generic client-side implementation of the [Debug Adapter Protocol (DAP)](https://microsoft.github.io/debug-adapter-protocol/) for Python. It provides a clean, strongly-typed API for interacting with debug adapters but is decoupled from the transport layer (IO), allowing integration into any framework (synchronous, asynchronous, GUI, etc.).
4+
5+
For a comprehensive example of how to build a debugger UI with this client, see [sandbox.py](sandbox/sandbox.py) implementation.
6+
7+
## Key Features
8+
9+
- **Protocol-First**: Implements the DAP state machine and message parsing without forcing a specific IO model.
10+
- **Strongly Typed**: Uses Pydantic models for request arguments and event bodies.
11+
- **Easy Integration**: Can be used with `subprocess`, `socket`, `asyncio`, or any other transport mechanism.
12+
13+
## Installation
14+
15+
```bash
16+
pip install dap-python
17+
```
18+
19+
If you are developing/contributing to the code, use `pip install -e .` instead. I also support using `poetry`.
20+
21+
## Quick Start
22+
23+
The `DAPClient` requires an IO handler to send and receive bytes from the debug adapter. The library acts as a translation layer:
24+
- You feed it raw bytes from the adapter -> It yields high-level `Event` objects.
25+
- You call methods like `client.launch()` -> It buffers raw bytes for you to send to the adapter.
26+
27+
### Example: minimal synchronous runner
28+
29+
This example assumes you have an IO handler similar to `sandbox/dap_io.py`.
30+
31+
```python
32+
import time
33+
from dap.client import DAPClient
34+
from dap.events import InitializedEvent
35+
36+
# Import your IO handler (see sandbox/dap_io.py for a reference implementation)
37+
from sandbox.dap_io import IO
38+
39+
# 1. Start the debug adapter (e.g., debugpy)
40+
adapter_cmd = "python -m debugpy.adapter"
41+
io = IO(adapter_cmd)
42+
io.start()
43+
44+
# 2. Initialize the DAP Client
45+
client = DAPClient(
46+
clientID="my-debugger",
47+
clientName="My Custom Debugger",
48+
locale="en-US",
49+
pathFormat="path"
50+
)
51+
52+
# 3. Send the Initialize Request
53+
# client.send() returns the raw bytes that need to be sent to the adapter
54+
io.write(client.send())
55+
56+
# 4. Event Loop
57+
try:
58+
while True:
59+
# Read raw data from the adapter's stdout
60+
data = io.read()
61+
if data:
62+
# Feed data to the client to parse events
63+
events = client.recv(data)
64+
65+
for event in events:
66+
print(f"Received: {event}")
67+
68+
if isinstance(event, InitializedEvent):
69+
print("Adapter Initialized!")
70+
# Once initialized, we can configure and launch
71+
client.launch(program="script.py", console="internalConsole")
72+
client.configuration_done()
73+
74+
# Flush requests generated by the above calls
75+
io.write(client.send())
76+
77+
time.sleep(0.01)
78+
except KeyboardInterrupt:
79+
io.stop()
80+
```
81+
82+
## Architecture
83+
84+
The library is designed around the `DAPClient` class in `dap.client`.
85+
86+
### Data Flow
87+
88+
```
89+
[ Debug Adapter ] <== bytes ==> [ IO Handler ] <== bytes ==> [ DAPClient ] <== Objects ==> [ Your Application ]
90+
```
91+
92+
1. **Send**: You call `client.step_in(threadId=1)`. The client updates its internal state and queues the JSON-RPC message. You call `client.send()` to get the bytes and write them to your transport.
93+
2. **Receive**: You read bytes from your transport and call `client.recv(bytes)`. The client parses the buffer and yields `Event`, `Response`, or `Request` objects.
94+
95+
## API Reference
96+
97+
### `DAPClient`
98+
99+
The main entry point. Common methods include:
100+
- `launch(...)` / `attach(...)`
101+
- `set_breakpoints(...)` / `configuration_done()`
102+
- `continue_execution(...)` / `step_in(...)` / `next(...)`
103+
- `stack_trace(...)` / `scopes(...)` / `variables(...)`
104+
- `disconnect()`
105+
106+
See `dap/client.py` for the full list of supported methods and arguments.

dap/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Client library for managing Debug Adapter Protocol (DAP) requests & responses."""
2+
3+
from dap.client import DAPClient, ClientState
4+
from dap.protocol import ProtocolMessage, Request, Response, Event, CancelRequest, CancelResponse
5+
from dap.events import *
6+
from dap.requests import *
7+
from dap.types import *
8+
9+
10+
__version__ = "1.0.0"

0 commit comments

Comments
 (0)