Skip to content

Commit e731a56

Browse files
committed
Initial commit
0 parents  commit e731a56

19 files changed

+1422
-0
lines changed

Diff for: .editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[Makefile]
12+
indent_style = tab

Diff for: .github/workflows/tests.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
python-version: ["3.8", "3.9", "3.10"]
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python ${{ matrix.python-version }}
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: ${{ matrix.python-version }}
17+
cache: pip
18+
cache-dependency-path: setup.py
19+
- name: Install dependencies
20+
run: |
21+
python -m pip install --upgrade pip
22+
make deps
23+
- name: Test
24+
run: |
25+
make test
26+
- name: Lint
27+
run: |
28+
make lint
29+
- name: Format
30+
run: |
31+
make format-check

Diff for: .gitignore

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,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+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/

Diff for: .pylintrc

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[MESSAGES CONTROL]
2+
3+
disable=
4+
invalid-name,
5+
missing-class-docstring,
6+
missing-function-docstring,
7+
missing-module-docstring,
8+
too-few-public-methods,
9+
too-many-instance-attributes,
10+
too-many-public-methods
11+
12+
[FORMAT]
13+
14+
max-args=10
15+
max-line-length=130

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Christopher Giroir
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.

Diff for: Makefile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.DEFAULT_TARGET: deps
2+
3+
.PHONY: deps format format-check run lint test build publish clean
4+
5+
deps:
6+
pip install --progress-bar off -e .[dev]
7+
8+
format:
9+
python -m black .
10+
11+
format-check:
12+
python -m black --check .
13+
14+
run:
15+
python -m mypyprox.server
16+
17+
lint:
18+
python -m pylint mypyprox/ tests/
19+
20+
test:
21+
coverage run --source=mypyprox -m pytest
22+
coverage report
23+
coverage html
24+
25+
build: clean
26+
python setup.py sdist bdist_wheel
27+
28+
publish: build
29+
twine upload dist/*
30+
31+
clean:
32+
rm -rf build dist
33+
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete

Diff for: README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# MyPyProx
2+
3+
[![Tests](https://github.com/kelsin/mypyprox/actions/workflows/tests.yml/badge.svg)](https://github.com/kelsin/mypyprox/actions/workflows/tests.yml)
4+
5+
Python implementation of the mysql server wire protocol in order to create a SQL
6+
proxy. This allows you to create a server that mysql clients can connect to in
7+
order to run queries.
8+
9+
## Python Support
10+
11+
Tests run in 3.8, 3.9, and 3.10. The package should work in older versions but
12+
tests won't run due to the usage of the AsyncIO test helpers in 3.8.
13+
14+
## Todo
15+
16+
- *Minor* Proper connection IDs
17+
- Proper types support. Right now all data is returned as a string type, we need
18+
to properly inspect the DataFrame and return proper column information.
19+
- Add support for (at least) the `mysql_native_password` authentication method
20+
with another callback.
21+
- *Eventually* Compression support
22+
- *Eventually* SSL support
23+
24+
The last two items shouldn't block us pushing a pypi package.
25+
26+
## Usage
27+
28+
Until we finish more of the Todo items above we won't be publishing on
29+
pypi. Until then download the repo and install locally into your projects:
30+
31+
``` shell
32+
# Download
33+
git checkout https://github.com/kelsin/mypyprox.git
34+
35+
# To use in your project:
36+
cd <your project>
37+
pip install -e <path_to_mypyprox>
38+
39+
# Or to develop
40+
cd mypyprox
41+
make deps
42+
```
43+
44+
This library is meant to be used as the basis for a proxy SQL service. A minimal
45+
use case might look like this:
46+
47+
``` python
48+
import asyncio
49+
import pandas
50+
51+
from mypyprox.server import MysqlServer
52+
53+
# Handlers take in a string query and should return a
54+
# pandas.DataFrame with the expected data. In order to
55+
# make the mysql cli client work you should handle the
56+
# @@version_comment query like below.
57+
def query_handler(query):
58+
if query.lower() == "select @@version_comment limit 1":
59+
return pandas.DataFrame(
60+
data={"@@version_comment": ["MyPyProx Python Proxy - MIT"]}
61+
)
62+
63+
return pandas.DataFrame(data={"col1": ["foo", "bar"], "col2": [1.0, 2.0]})
64+
65+
# Run the server
66+
if __name__ == "__main__":
67+
server = MysqlServer(handler=query_handler)
68+
asyncio.run(server.start())
69+
```
70+
71+
You can pass a `socket` keyword argument to `MysqlServer` to listen on a unix
72+
socket. You can pass `host` or `port` to change the defaults of `127.0.0.1` and
73+
`3306`. You can pass any keyword arguments you want to pass onto the `asyncio`
74+
server creation methods in the `MysqlServer.start` method.
75+
76+
The `mypyprox/server.py` file is runnable with our default handler (returns
77+
static data for every query) by using `make run` in the repository.
78+
79+
## Development
80+
81+
You can install dependencies with `make deps`. You can format your code with
82+
`make format`. You can lint with `make lint`. You can run tests with `make
83+
test`. This will build a coverage report in `./htmlcov/index.html`. You can
84+
build a pip package with `make build`.

Diff for: mypyprox/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Implementation of the mysql server wire protocol"""

Diff for: mypyprox/constants.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from mypyprox import types
2+
3+
DEFAULT_SERVER_CAPABILITIES = (
4+
types.Capabilities.CLIENT_PROTOCOL_41 | types.Capabilities.CLIENT_DEPRECATE_EOF
5+
)

0 commit comments

Comments
 (0)