morphql 0.1.35__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.
- morphql-0.1.35/LICENSE +21 -0
- morphql-0.1.35/PKG-INFO +113 -0
- morphql-0.1.35/README.md +98 -0
- morphql-0.1.35/pyproject.toml +26 -0
- morphql-0.1.35/setup.cfg +4 -0
- morphql-0.1.35/src/morphql/__init__.py +3 -0
- morphql-0.1.35/src/morphql/morphql.py +361 -0
- morphql-0.1.35/src/morphql.egg-info/PKG-INFO +113 -0
- morphql-0.1.35/src/morphql.egg-info/SOURCES.txt +11 -0
- morphql-0.1.35/src/morphql.egg-info/dependency_links.txt +1 -0
- morphql-0.1.35/src/morphql.egg-info/top_level.txt +1 -0
- morphql-0.1.35/tests/test_cli.py +307 -0
- morphql-0.1.35/tests/test_server.py +150 -0
morphql-0.1.35/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Hyperwindmill
|
|
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.
|
morphql-0.1.35/PKG-INFO
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: morphql
|
|
3
|
+
Version: 0.1.35
|
|
4
|
+
Summary: Python client for MorphQL — transform data with declarative queries.
|
|
5
|
+
Author: Hyperwindmill
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Hyperwindmill/morphql
|
|
8
|
+
Project-URL: Repository, https://github.com/Hyperwindmill/morphql
|
|
9
|
+
Project-URL: Issues, https://github.com/Hyperwindmill/morphql/issues
|
|
10
|
+
Keywords: morphql,transform,query,json,xml,edi,dsl
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# morphql · Python client
|
|
17
|
+
|
|
18
|
+
Python client for [MorphQL](https://github.com/Hyperwindmill/morphql) — a declarative DSL for structural data transformation.
|
|
19
|
+
|
|
20
|
+
Delegates execution to the MorphQL **CLI** or a running **MorphQL server**. Zero external dependencies. Python >= 3.8.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install morphql
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from morphql import MorphQL
|
|
32
|
+
|
|
33
|
+
# One-shot static call (CLI provider)
|
|
34
|
+
result = MorphQL.execute(
|
|
35
|
+
"from json to json transform set name = source.firstName",
|
|
36
|
+
data={"firstName": "Alice"},
|
|
37
|
+
)
|
|
38
|
+
print(result) # {"name":"Alice"}
|
|
39
|
+
|
|
40
|
+
# From a .morphql file
|
|
41
|
+
result = MorphQL.execute_file("transform.morphql", data={"x": 1})
|
|
42
|
+
|
|
43
|
+
# Instance with preset defaults (server provider)
|
|
44
|
+
morph = MorphQL(provider="server", server_url="http://localhost:3000")
|
|
45
|
+
result = morph.run("from json to json transform set x = source.x", data={"x": 42})
|
|
46
|
+
result = morph.run_file("transform.morphql", data={"x": 42})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Providers
|
|
50
|
+
|
|
51
|
+
| Provider | How it works |
|
|
52
|
+
|------------|---------------------------------------------------|
|
|
53
|
+
| `"cli"` | Runs `morphql` CLI via `subprocess` (default) |
|
|
54
|
+
| `"server"` | `POST {server_url}/v1/execute` via `urllib` |
|
|
55
|
+
|
|
56
|
+
## Options
|
|
57
|
+
|
|
58
|
+
| Option | Default | Env var |
|
|
59
|
+
|--------------|-----------------------------|-----------------------|
|
|
60
|
+
| `provider` | `"cli"` | `MORPHQL_PROVIDER` |
|
|
61
|
+
| `runtime` | `"node"` | `MORPHQL_RUNTIME` |
|
|
62
|
+
| `cli_path` | `"morphql"` | `MORPHQL_CLI_PATH` |
|
|
63
|
+
| `node_path` | `"node"` | `MORPHQL_NODE_PATH` |
|
|
64
|
+
| `qjs_path` | auto-resolved | `MORPHQL_QJS_PATH` |
|
|
65
|
+
| `cache_dir` | `$TMPDIR/morphql` | `MORPHQL_CACHE_DIR` |
|
|
66
|
+
| `server_url` | `"http://localhost:3000"` | `MORPHQL_SERVER_URL` |
|
|
67
|
+
| `api_key` | `None` | `MORPHQL_API_KEY` |
|
|
68
|
+
| `timeout` | `30` (seconds) | `MORPHQL_TIMEOUT` |
|
|
69
|
+
|
|
70
|
+
Priority: call kwarg > instance default > env var > hardcoded default.
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### Static
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
MorphQL.execute(query, data=None, **options) -> str
|
|
78
|
+
MorphQL.execute_file(query_file, data=None, **options) -> str
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Instance
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
morph = MorphQL(**defaults)
|
|
85
|
+
morph.run(query, data=None, **options) -> str
|
|
86
|
+
morph.run_file(query_file, data=None, **options) -> str
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`data` accepts `str` (JSON), `dict`, `list`, or `None`.
|
|
90
|
+
|
|
91
|
+
## Exceptions
|
|
92
|
+
|
|
93
|
+
| Situation | Exception |
|
|
94
|
+
|----------------------------|---------------------|
|
|
95
|
+
| `query` missing or empty | `ValueError` |
|
|
96
|
+
| Query file not found | `FileNotFoundError` |
|
|
97
|
+
| CLI exits with error | `RuntimeError` |
|
|
98
|
+
| Server HTTP error | `RuntimeError` |
|
|
99
|
+
| Server unreachable | `RuntimeError` |
|
|
100
|
+
|
|
101
|
+
## Running tests
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
cd packages/python
|
|
105
|
+
PYTHONPATH=src python -m unittest discover tests/
|
|
106
|
+
# or with pytest:
|
|
107
|
+
pip install pytest
|
|
108
|
+
pytest
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT
|
morphql-0.1.35/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# morphql · Python client
|
|
2
|
+
|
|
3
|
+
Python client for [MorphQL](https://github.com/Hyperwindmill/morphql) — a declarative DSL for structural data transformation.
|
|
4
|
+
|
|
5
|
+
Delegates execution to the MorphQL **CLI** or a running **MorphQL server**. Zero external dependencies. Python >= 3.8.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install morphql
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from morphql import MorphQL
|
|
17
|
+
|
|
18
|
+
# One-shot static call (CLI provider)
|
|
19
|
+
result = MorphQL.execute(
|
|
20
|
+
"from json to json transform set name = source.firstName",
|
|
21
|
+
data={"firstName": "Alice"},
|
|
22
|
+
)
|
|
23
|
+
print(result) # {"name":"Alice"}
|
|
24
|
+
|
|
25
|
+
# From a .morphql file
|
|
26
|
+
result = MorphQL.execute_file("transform.morphql", data={"x": 1})
|
|
27
|
+
|
|
28
|
+
# Instance with preset defaults (server provider)
|
|
29
|
+
morph = MorphQL(provider="server", server_url="http://localhost:3000")
|
|
30
|
+
result = morph.run("from json to json transform set x = source.x", data={"x": 42})
|
|
31
|
+
result = morph.run_file("transform.morphql", data={"x": 42})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Providers
|
|
35
|
+
|
|
36
|
+
| Provider | How it works |
|
|
37
|
+
|------------|---------------------------------------------------|
|
|
38
|
+
| `"cli"` | Runs `morphql` CLI via `subprocess` (default) |
|
|
39
|
+
| `"server"` | `POST {server_url}/v1/execute` via `urllib` |
|
|
40
|
+
|
|
41
|
+
## Options
|
|
42
|
+
|
|
43
|
+
| Option | Default | Env var |
|
|
44
|
+
|--------------|-----------------------------|-----------------------|
|
|
45
|
+
| `provider` | `"cli"` | `MORPHQL_PROVIDER` |
|
|
46
|
+
| `runtime` | `"node"` | `MORPHQL_RUNTIME` |
|
|
47
|
+
| `cli_path` | `"morphql"` | `MORPHQL_CLI_PATH` |
|
|
48
|
+
| `node_path` | `"node"` | `MORPHQL_NODE_PATH` |
|
|
49
|
+
| `qjs_path` | auto-resolved | `MORPHQL_QJS_PATH` |
|
|
50
|
+
| `cache_dir` | `$TMPDIR/morphql` | `MORPHQL_CACHE_DIR` |
|
|
51
|
+
| `server_url` | `"http://localhost:3000"` | `MORPHQL_SERVER_URL` |
|
|
52
|
+
| `api_key` | `None` | `MORPHQL_API_KEY` |
|
|
53
|
+
| `timeout` | `30` (seconds) | `MORPHQL_TIMEOUT` |
|
|
54
|
+
|
|
55
|
+
Priority: call kwarg > instance default > env var > hardcoded default.
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
### Static
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
MorphQL.execute(query, data=None, **options) -> str
|
|
63
|
+
MorphQL.execute_file(query_file, data=None, **options) -> str
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Instance
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
morph = MorphQL(**defaults)
|
|
70
|
+
morph.run(query, data=None, **options) -> str
|
|
71
|
+
morph.run_file(query_file, data=None, **options) -> str
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`data` accepts `str` (JSON), `dict`, `list`, or `None`.
|
|
75
|
+
|
|
76
|
+
## Exceptions
|
|
77
|
+
|
|
78
|
+
| Situation | Exception |
|
|
79
|
+
|----------------------------|---------------------|
|
|
80
|
+
| `query` missing or empty | `ValueError` |
|
|
81
|
+
| Query file not found | `FileNotFoundError` |
|
|
82
|
+
| CLI exits with error | `RuntimeError` |
|
|
83
|
+
| Server HTTP error | `RuntimeError` |
|
|
84
|
+
| Server unreachable | `RuntimeError` |
|
|
85
|
+
|
|
86
|
+
## Running tests
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
cd packages/python
|
|
90
|
+
PYTHONPATH=src python -m unittest discover tests/
|
|
91
|
+
# or with pytest:
|
|
92
|
+
pip install pytest
|
|
93
|
+
pytest
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "morphql"
|
|
7
|
+
version = "0.1.35"
|
|
8
|
+
description = "Python client for MorphQL — transform data with declarative queries."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
dependencies = []
|
|
13
|
+
keywords = ["morphql", "transform", "query", "json", "xml", "edi", "dsl"]
|
|
14
|
+
authors = [{ name = "Hyperwindmill" }]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/Hyperwindmill/morphql"
|
|
18
|
+
Repository = "https://github.com/Hyperwindmill/morphql"
|
|
19
|
+
Issues = "https://github.com/Hyperwindmill/morphql/issues"
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.packages.find]
|
|
22
|
+
where = ["src"]
|
|
23
|
+
|
|
24
|
+
[tool.pytest.ini_options]
|
|
25
|
+
testpaths = ["tests"]
|
|
26
|
+
pythonpath = ["src"]
|
morphql-0.1.35/setup.cfg
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MorphQL Python Wrapper
|
|
3
|
+
|
|
4
|
+
A minimalist Python client for MorphQL — transform data with declarative queries.
|
|
5
|
+
Delegates execution to the MorphQL CLI or server via pluggable providers.
|
|
6
|
+
|
|
7
|
+
https://github.com/Hyperwindmill/morphql
|
|
8
|
+
License: MIT
|
|
9
|
+
Requires Python >= 3.8. No external dependencies.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import platform
|
|
15
|
+
import subprocess
|
|
16
|
+
import tempfile
|
|
17
|
+
import urllib.error
|
|
18
|
+
import urllib.request
|
|
19
|
+
from typing import Any, Dict, Optional, Union
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class MorphQL:
|
|
23
|
+
PROVIDER_CLI = "cli"
|
|
24
|
+
PROVIDER_SERVER = "server"
|
|
25
|
+
|
|
26
|
+
RUNTIME_NODE = "node"
|
|
27
|
+
RUNTIME_QJS = "qjs"
|
|
28
|
+
|
|
29
|
+
_OPTION_DEFAULTS: Dict[str, Any] = {
|
|
30
|
+
"provider": PROVIDER_CLI,
|
|
31
|
+
"runtime": RUNTIME_NODE,
|
|
32
|
+
"cli_path": "morphql",
|
|
33
|
+
"node_path": "node",
|
|
34
|
+
"qjs_path": None,
|
|
35
|
+
"cache_dir": None,
|
|
36
|
+
"server_url": "http://localhost:3000",
|
|
37
|
+
"api_key": None,
|
|
38
|
+
"timeout": 30,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_ENV_MAP: Dict[str, str] = {
|
|
42
|
+
"provider": "MORPHQL_PROVIDER",
|
|
43
|
+
"runtime": "MORPHQL_RUNTIME",
|
|
44
|
+
"cli_path": "MORPHQL_CLI_PATH",
|
|
45
|
+
"node_path": "MORPHQL_NODE_PATH",
|
|
46
|
+
"qjs_path": "MORPHQL_QJS_PATH",
|
|
47
|
+
"cache_dir": "MORPHQL_CACHE_DIR",
|
|
48
|
+
"server_url": "MORPHQL_SERVER_URL",
|
|
49
|
+
"api_key": "MORPHQL_API_KEY",
|
|
50
|
+
"timeout": "MORPHQL_TIMEOUT",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
def __init__(self, **defaults: Any) -> None:
|
|
54
|
+
self._defaults = defaults
|
|
55
|
+
|
|
56
|
+
# ------------------------------------------------------------------
|
|
57
|
+
# Public static API
|
|
58
|
+
# ------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def execute(
|
|
62
|
+
cls,
|
|
63
|
+
query: str,
|
|
64
|
+
data: Union[str, dict, list, None] = None,
|
|
65
|
+
**options: Any,
|
|
66
|
+
) -> str:
|
|
67
|
+
if not query:
|
|
68
|
+
raise ValueError("MorphQL: 'query' is required")
|
|
69
|
+
config = cls._resolve_config(options)
|
|
70
|
+
return cls._dispatch(query, data, config)
|
|
71
|
+
|
|
72
|
+
@classmethod
|
|
73
|
+
def execute_file(
|
|
74
|
+
cls,
|
|
75
|
+
query_file: str,
|
|
76
|
+
data: Union[str, dict, list, None] = None,
|
|
77
|
+
**options: Any,
|
|
78
|
+
) -> str:
|
|
79
|
+
cls._validate_query_file(query_file)
|
|
80
|
+
config = cls._resolve_config(options)
|
|
81
|
+
return cls._dispatch(None, data, config, query_file=query_file)
|
|
82
|
+
|
|
83
|
+
# ------------------------------------------------------------------
|
|
84
|
+
# Public instance API
|
|
85
|
+
# ------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
def run(
|
|
88
|
+
self,
|
|
89
|
+
query: str,
|
|
90
|
+
data: Union[str, dict, list, None] = None,
|
|
91
|
+
**options: Any,
|
|
92
|
+
) -> str:
|
|
93
|
+
if not query:
|
|
94
|
+
raise ValueError("MorphQL: 'query' is required")
|
|
95
|
+
config = self._resolve_config(options, self._defaults)
|
|
96
|
+
return self._dispatch(query, data, config)
|
|
97
|
+
|
|
98
|
+
def run_file(
|
|
99
|
+
self,
|
|
100
|
+
query_file: str,
|
|
101
|
+
data: Union[str, dict, list, None] = None,
|
|
102
|
+
**options: Any,
|
|
103
|
+
) -> str:
|
|
104
|
+
self._validate_query_file(query_file)
|
|
105
|
+
config = self._resolve_config(options, self._defaults)
|
|
106
|
+
return self._dispatch(None, data, config, query_file=query_file)
|
|
107
|
+
|
|
108
|
+
# ------------------------------------------------------------------
|
|
109
|
+
# Dispatch
|
|
110
|
+
# ------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
@classmethod
|
|
113
|
+
def _dispatch(
|
|
114
|
+
cls,
|
|
115
|
+
query: Optional[str],
|
|
116
|
+
data: Any,
|
|
117
|
+
config: Dict[str, Any],
|
|
118
|
+
query_file: Optional[str] = None,
|
|
119
|
+
) -> str:
|
|
120
|
+
if config["provider"] == cls.PROVIDER_SERVER:
|
|
121
|
+
return cls._execute_via_server(query, data, config, query_file)
|
|
122
|
+
return cls._execute_via_cli(query, data, config, query_file)
|
|
123
|
+
|
|
124
|
+
# ------------------------------------------------------------------
|
|
125
|
+
# CLI Provider
|
|
126
|
+
# ------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
@classmethod
|
|
129
|
+
def _execute_via_cli(
|
|
130
|
+
cls,
|
|
131
|
+
query: Optional[str],
|
|
132
|
+
data: Any,
|
|
133
|
+
config: Dict[str, Any],
|
|
134
|
+
query_file: Optional[str] = None,
|
|
135
|
+
) -> str:
|
|
136
|
+
data_str = cls._normalize_data(data)
|
|
137
|
+
cache_dir = cls._resolve_cache_dir(config)
|
|
138
|
+
cmd = cls._resolve_cli_command(config)
|
|
139
|
+
|
|
140
|
+
if query_file is not None:
|
|
141
|
+
args = cmd + ["-Q", query_file, "-i", data_str, "--cache-dir", cache_dir]
|
|
142
|
+
else:
|
|
143
|
+
args = cmd + ["-q", query, "-i", data_str, "--cache-dir", cache_dir]
|
|
144
|
+
|
|
145
|
+
env = {**os.environ, "NODE_NO_WARNINGS": "1"}
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
result = subprocess.run(
|
|
149
|
+
args,
|
|
150
|
+
stdout=subprocess.PIPE,
|
|
151
|
+
stderr=subprocess.PIPE,
|
|
152
|
+
env=env,
|
|
153
|
+
timeout=int(config["timeout"]),
|
|
154
|
+
)
|
|
155
|
+
except FileNotFoundError:
|
|
156
|
+
raise RuntimeError(
|
|
157
|
+
f"MorphQL: CLI executable not found — {args[0]}"
|
|
158
|
+
)
|
|
159
|
+
except subprocess.TimeoutExpired:
|
|
160
|
+
raise RuntimeError("MorphQL: CLI process timed out")
|
|
161
|
+
|
|
162
|
+
if result.returncode != 0:
|
|
163
|
+
stderr = result.stderr.decode(errors="replace").strip()
|
|
164
|
+
stdout = result.stdout.decode(errors="replace").strip()
|
|
165
|
+
msg = stderr if stderr else stdout
|
|
166
|
+
raise RuntimeError(
|
|
167
|
+
f"MorphQL CLI error (exit code {result.returncode}): {msg}"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
return result.stdout.decode(errors="replace").strip()
|
|
171
|
+
|
|
172
|
+
@classmethod
|
|
173
|
+
def _resolve_cli_command(cls, config: Dict[str, Any]) -> list:
|
|
174
|
+
if config["runtime"] == cls.RUNTIME_QJS:
|
|
175
|
+
return cls._resolve_qjs_command(config)
|
|
176
|
+
|
|
177
|
+
# 1. User-specified custom path
|
|
178
|
+
if config["cli_path"] != "morphql":
|
|
179
|
+
return [config["cli_path"]]
|
|
180
|
+
|
|
181
|
+
# 2. Bundled morphql.js (next to this package)
|
|
182
|
+
bundled = os.path.join(os.path.dirname(__file__), "..", "bin", "morphql.js")
|
|
183
|
+
bundled = os.path.normpath(bundled)
|
|
184
|
+
if os.path.isfile(bundled):
|
|
185
|
+
return [config["node_path"], bundled]
|
|
186
|
+
|
|
187
|
+
# 3. System-installed morphql binary
|
|
188
|
+
return [config["cli_path"]]
|
|
189
|
+
|
|
190
|
+
@classmethod
|
|
191
|
+
def _resolve_qjs_command(cls, config: Dict[str, Any]) -> list:
|
|
192
|
+
qjs_bin = config.get("qjs_path") or cls._maybe_install_qjs(config)
|
|
193
|
+
|
|
194
|
+
# Locate the JS bundle
|
|
195
|
+
bundle = os.path.normpath(
|
|
196
|
+
os.path.join(os.path.dirname(__file__), "..", "..", "..", "cli", "dist", "qjs", "qjs.js")
|
|
197
|
+
)
|
|
198
|
+
if not os.path.isfile(bundle):
|
|
199
|
+
bundle = os.path.normpath(
|
|
200
|
+
os.path.join(os.path.dirname(__file__), "..", "bin", "qjs.js")
|
|
201
|
+
)
|
|
202
|
+
if not os.path.isfile(bundle):
|
|
203
|
+
raise RuntimeError(
|
|
204
|
+
"MorphQL: QuickJS bundle not found. Run build:qjs or place it in bin/qjs.js"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return [qjs_bin, "--std", "-m", bundle]
|
|
208
|
+
|
|
209
|
+
@classmethod
|
|
210
|
+
def _maybe_install_qjs(cls, config: Dict[str, Any]) -> str:
|
|
211
|
+
system = platform.system().lower()
|
|
212
|
+
machine = platform.machine().lower()
|
|
213
|
+
|
|
214
|
+
if "windows" in system:
|
|
215
|
+
suffix = "-windows-x86_64.exe"
|
|
216
|
+
elif "darwin" in system:
|
|
217
|
+
suffix = "-darwin"
|
|
218
|
+
else:
|
|
219
|
+
suffix = "-linux-x86_64"
|
|
220
|
+
|
|
221
|
+
local_name = "qjs" + suffix
|
|
222
|
+
|
|
223
|
+
# 1. Bundled bin/
|
|
224
|
+
bundled = os.path.normpath(
|
|
225
|
+
os.path.join(os.path.dirname(__file__), "..", "bin", local_name)
|
|
226
|
+
)
|
|
227
|
+
if os.path.isfile(bundled):
|
|
228
|
+
return os.path.realpath(bundled)
|
|
229
|
+
|
|
230
|
+
# 2. Cache dir
|
|
231
|
+
cache_dir = cls._resolve_cache_dir(config)
|
|
232
|
+
cached = os.path.join(cache_dir, local_name)
|
|
233
|
+
if os.path.isfile(cached):
|
|
234
|
+
return os.path.realpath(cached)
|
|
235
|
+
|
|
236
|
+
# 3. Download
|
|
237
|
+
os.makedirs(cache_dir, exist_ok=True)
|
|
238
|
+
version = "v0.11.0"
|
|
239
|
+
url = f"https://github.com/quickjs-ng/quickjs/releases/download/{version}/{local_name}"
|
|
240
|
+
try:
|
|
241
|
+
cls._download_file(url, cached)
|
|
242
|
+
os.chmod(cached, 0o755)
|
|
243
|
+
return os.path.realpath(cached)
|
|
244
|
+
except Exception:
|
|
245
|
+
return "qjs"
|
|
246
|
+
|
|
247
|
+
@classmethod
|
|
248
|
+
def _download_file(cls, url: str, target: str) -> None:
|
|
249
|
+
try:
|
|
250
|
+
with urllib.request.urlopen(url, timeout=60) as response:
|
|
251
|
+
content = response.read()
|
|
252
|
+
except urllib.error.URLError as e:
|
|
253
|
+
raise RuntimeError(f"MorphQL: Failed to download {url}: {e}")
|
|
254
|
+
|
|
255
|
+
if len(content) < 1000:
|
|
256
|
+
raise RuntimeError(f"MorphQL: Downloaded file too small from {url}")
|
|
257
|
+
|
|
258
|
+
with open(target, "wb") as f:
|
|
259
|
+
f.write(content)
|
|
260
|
+
|
|
261
|
+
@classmethod
|
|
262
|
+
def _resolve_cache_dir(cls, config: Dict[str, Any]) -> str:
|
|
263
|
+
if config.get("cache_dir"):
|
|
264
|
+
return config["cache_dir"]
|
|
265
|
+
return os.path.join(tempfile.gettempdir(), "morphql")
|
|
266
|
+
|
|
267
|
+
# ------------------------------------------------------------------
|
|
268
|
+
# Server Provider
|
|
269
|
+
# ------------------------------------------------------------------
|
|
270
|
+
|
|
271
|
+
@classmethod
|
|
272
|
+
def _execute_via_server(
|
|
273
|
+
cls,
|
|
274
|
+
query: Optional[str],
|
|
275
|
+
data: Any,
|
|
276
|
+
config: Dict[str, Any],
|
|
277
|
+
query_file: Optional[str] = None,
|
|
278
|
+
) -> str:
|
|
279
|
+
if query_file is not None:
|
|
280
|
+
with open(query_file, "r", encoding="utf-8") as f:
|
|
281
|
+
query = f.read().strip()
|
|
282
|
+
|
|
283
|
+
# Server expects decoded data, not raw JSON string
|
|
284
|
+
if isinstance(data, str):
|
|
285
|
+
try:
|
|
286
|
+
data = json.loads(data)
|
|
287
|
+
except (json.JSONDecodeError, ValueError):
|
|
288
|
+
pass
|
|
289
|
+
|
|
290
|
+
payload = json.dumps({"query": query, "data": data}).encode("utf-8")
|
|
291
|
+
url = config["server_url"].rstrip("/") + "/v1/execute"
|
|
292
|
+
|
|
293
|
+
headers = {"Content-Type": "application/json"}
|
|
294
|
+
if config.get("api_key"):
|
|
295
|
+
headers["X-API-KEY"] = config["api_key"]
|
|
296
|
+
|
|
297
|
+
req = urllib.request.Request(url, data=payload, headers=headers, method="POST")
|
|
298
|
+
|
|
299
|
+
try:
|
|
300
|
+
with urllib.request.urlopen(req, timeout=int(config["timeout"])) as response:
|
|
301
|
+
body = response.read().decode("utf-8")
|
|
302
|
+
except urllib.error.HTTPError as e:
|
|
303
|
+
body = e.read().decode("utf-8", errors="replace")
|
|
304
|
+
raise RuntimeError(f"MorphQL server returned HTTP {e.code}: {body}")
|
|
305
|
+
except urllib.error.URLError as e:
|
|
306
|
+
raise RuntimeError(f"MorphQL server unreachable: {url} — {e.reason}")
|
|
307
|
+
|
|
308
|
+
try:
|
|
309
|
+
response_data = json.loads(body)
|
|
310
|
+
except json.JSONDecodeError:
|
|
311
|
+
raise RuntimeError(f"MorphQL server returned invalid JSON: {body}")
|
|
312
|
+
|
|
313
|
+
if not response_data.get("success"):
|
|
314
|
+
msg = response_data.get("message", body)
|
|
315
|
+
raise RuntimeError(f"MorphQL server error: {msg}")
|
|
316
|
+
|
|
317
|
+
return response_data["result"]
|
|
318
|
+
|
|
319
|
+
# ------------------------------------------------------------------
|
|
320
|
+
# Config Resolution
|
|
321
|
+
# ------------------------------------------------------------------
|
|
322
|
+
|
|
323
|
+
@classmethod
|
|
324
|
+
def _resolve_config(
|
|
325
|
+
cls,
|
|
326
|
+
options: Dict[str, Any],
|
|
327
|
+
defaults: Optional[Dict[str, Any]] = None,
|
|
328
|
+
) -> Dict[str, Any]:
|
|
329
|
+
config: Dict[str, Any] = {}
|
|
330
|
+
for key, hardcoded in cls._OPTION_DEFAULTS.items():
|
|
331
|
+
if key in options:
|
|
332
|
+
config[key] = options[key]
|
|
333
|
+
elif defaults and key in defaults:
|
|
334
|
+
config[key] = defaults[key]
|
|
335
|
+
else:
|
|
336
|
+
env_key = cls._ENV_MAP.get(key)
|
|
337
|
+
env_val = os.environ.get(env_key, "") if env_key else ""
|
|
338
|
+
if env_val:
|
|
339
|
+
config[key] = env_val
|
|
340
|
+
else:
|
|
341
|
+
config[key] = hardcoded
|
|
342
|
+
return config
|
|
343
|
+
|
|
344
|
+
# ------------------------------------------------------------------
|
|
345
|
+
# Helpers
|
|
346
|
+
# ------------------------------------------------------------------
|
|
347
|
+
|
|
348
|
+
@staticmethod
|
|
349
|
+
def _normalize_data(data: Any) -> str:
|
|
350
|
+
if data is None:
|
|
351
|
+
return "{}"
|
|
352
|
+
if isinstance(data, (dict, list)):
|
|
353
|
+
return json.dumps(data)
|
|
354
|
+
return str(data)
|
|
355
|
+
|
|
356
|
+
@staticmethod
|
|
357
|
+
def _validate_query_file(path: str) -> None:
|
|
358
|
+
if not os.path.exists(path):
|
|
359
|
+
raise FileNotFoundError(f"MorphQL: query file not found: {path}")
|
|
360
|
+
if not os.access(path, os.R_OK):
|
|
361
|
+
raise PermissionError(f"MorphQL: query file not readable: {path}")
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: morphql
|
|
3
|
+
Version: 0.1.35
|
|
4
|
+
Summary: Python client for MorphQL — transform data with declarative queries.
|
|
5
|
+
Author: Hyperwindmill
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Hyperwindmill/morphql
|
|
8
|
+
Project-URL: Repository, https://github.com/Hyperwindmill/morphql
|
|
9
|
+
Project-URL: Issues, https://github.com/Hyperwindmill/morphql/issues
|
|
10
|
+
Keywords: morphql,transform,query,json,xml,edi,dsl
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# morphql · Python client
|
|
17
|
+
|
|
18
|
+
Python client for [MorphQL](https://github.com/Hyperwindmill/morphql) — a declarative DSL for structural data transformation.
|
|
19
|
+
|
|
20
|
+
Delegates execution to the MorphQL **CLI** or a running **MorphQL server**. Zero external dependencies. Python >= 3.8.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install morphql
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
from morphql import MorphQL
|
|
32
|
+
|
|
33
|
+
# One-shot static call (CLI provider)
|
|
34
|
+
result = MorphQL.execute(
|
|
35
|
+
"from json to json transform set name = source.firstName",
|
|
36
|
+
data={"firstName": "Alice"},
|
|
37
|
+
)
|
|
38
|
+
print(result) # {"name":"Alice"}
|
|
39
|
+
|
|
40
|
+
# From a .morphql file
|
|
41
|
+
result = MorphQL.execute_file("transform.morphql", data={"x": 1})
|
|
42
|
+
|
|
43
|
+
# Instance with preset defaults (server provider)
|
|
44
|
+
morph = MorphQL(provider="server", server_url="http://localhost:3000")
|
|
45
|
+
result = morph.run("from json to json transform set x = source.x", data={"x": 42})
|
|
46
|
+
result = morph.run_file("transform.morphql", data={"x": 42})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Providers
|
|
50
|
+
|
|
51
|
+
| Provider | How it works |
|
|
52
|
+
|------------|---------------------------------------------------|
|
|
53
|
+
| `"cli"` | Runs `morphql` CLI via `subprocess` (default) |
|
|
54
|
+
| `"server"` | `POST {server_url}/v1/execute` via `urllib` |
|
|
55
|
+
|
|
56
|
+
## Options
|
|
57
|
+
|
|
58
|
+
| Option | Default | Env var |
|
|
59
|
+
|--------------|-----------------------------|-----------------------|
|
|
60
|
+
| `provider` | `"cli"` | `MORPHQL_PROVIDER` |
|
|
61
|
+
| `runtime` | `"node"` | `MORPHQL_RUNTIME` |
|
|
62
|
+
| `cli_path` | `"morphql"` | `MORPHQL_CLI_PATH` |
|
|
63
|
+
| `node_path` | `"node"` | `MORPHQL_NODE_PATH` |
|
|
64
|
+
| `qjs_path` | auto-resolved | `MORPHQL_QJS_PATH` |
|
|
65
|
+
| `cache_dir` | `$TMPDIR/morphql` | `MORPHQL_CACHE_DIR` |
|
|
66
|
+
| `server_url` | `"http://localhost:3000"` | `MORPHQL_SERVER_URL` |
|
|
67
|
+
| `api_key` | `None` | `MORPHQL_API_KEY` |
|
|
68
|
+
| `timeout` | `30` (seconds) | `MORPHQL_TIMEOUT` |
|
|
69
|
+
|
|
70
|
+
Priority: call kwarg > instance default > env var > hardcoded default.
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### Static
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
MorphQL.execute(query, data=None, **options) -> str
|
|
78
|
+
MorphQL.execute_file(query_file, data=None, **options) -> str
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Instance
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
morph = MorphQL(**defaults)
|
|
85
|
+
morph.run(query, data=None, **options) -> str
|
|
86
|
+
morph.run_file(query_file, data=None, **options) -> str
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`data` accepts `str` (JSON), `dict`, `list`, or `None`.
|
|
90
|
+
|
|
91
|
+
## Exceptions
|
|
92
|
+
|
|
93
|
+
| Situation | Exception |
|
|
94
|
+
|----------------------------|---------------------|
|
|
95
|
+
| `query` missing or empty | `ValueError` |
|
|
96
|
+
| Query file not found | `FileNotFoundError` |
|
|
97
|
+
| CLI exits with error | `RuntimeError` |
|
|
98
|
+
| Server HTTP error | `RuntimeError` |
|
|
99
|
+
| Server unreachable | `RuntimeError` |
|
|
100
|
+
|
|
101
|
+
## Running tests
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
cd packages/python
|
|
105
|
+
PYTHONPATH=src python -m unittest discover tests/
|
|
106
|
+
# or with pytest:
|
|
107
|
+
pip install pytest
|
|
108
|
+
pytest
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/morphql/__init__.py
|
|
5
|
+
src/morphql/morphql.py
|
|
6
|
+
src/morphql.egg-info/PKG-INFO
|
|
7
|
+
src/morphql.egg-info/SOURCES.txt
|
|
8
|
+
src/morphql.egg-info/dependency_links.txt
|
|
9
|
+
src/morphql.egg-info/top_level.txt
|
|
10
|
+
tests/test_cli.py
|
|
11
|
+
tests/test_server.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
morphql
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import subprocess
|
|
3
|
+
import unittest
|
|
4
|
+
from unittest.mock import MagicMock, patch
|
|
5
|
+
|
|
6
|
+
from morphql import MorphQL
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _make_result(stdout: str = "", returncode: int = 0, stderr: str = "") -> MagicMock:
|
|
10
|
+
m = MagicMock()
|
|
11
|
+
m.stdout = stdout.encode()
|
|
12
|
+
m.stderr = stderr.encode()
|
|
13
|
+
m.returncode = returncode
|
|
14
|
+
return m
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TestCliExecute(unittest.TestCase):
|
|
18
|
+
@patch("subprocess.run")
|
|
19
|
+
def test_execute_passes_query_flag(self, mock_run):
|
|
20
|
+
mock_run.return_value = _make_result('{"ok": true}')
|
|
21
|
+
result = MorphQL.execute("from json to json transform set x = x", '{"x":1}')
|
|
22
|
+
args = mock_run.call_args[0][0]
|
|
23
|
+
self.assertIn("-q", args)
|
|
24
|
+
self.assertEqual(result, '{"ok": true}')
|
|
25
|
+
|
|
26
|
+
@patch("subprocess.run")
|
|
27
|
+
def test_execute_passes_data_flag(self, mock_run):
|
|
28
|
+
mock_run.return_value = _make_result("result")
|
|
29
|
+
MorphQL.execute("from json to json transform set x = x", {"key": "val"})
|
|
30
|
+
args = mock_run.call_args[0][0]
|
|
31
|
+
self.assertIn("-i", args)
|
|
32
|
+
idx = args.index("-i")
|
|
33
|
+
self.assertEqual(json.loads(args[idx + 1]), {"key": "val"})
|
|
34
|
+
|
|
35
|
+
@patch("subprocess.run")
|
|
36
|
+
def test_execute_none_data_sends_empty_object(self, mock_run):
|
|
37
|
+
mock_run.return_value = _make_result("{}")
|
|
38
|
+
MorphQL.execute("from json to json transform set x = x")
|
|
39
|
+
args = mock_run.call_args[0][0]
|
|
40
|
+
idx = args.index("-i")
|
|
41
|
+
self.assertEqual(args[idx + 1], "{}")
|
|
42
|
+
|
|
43
|
+
@patch("subprocess.run")
|
|
44
|
+
def test_execute_raises_on_nonzero_exit(self, mock_run):
|
|
45
|
+
mock_run.return_value = _make_result("", returncode=1, stderr="syntax error")
|
|
46
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
47
|
+
MorphQL.execute("bad query", "{}")
|
|
48
|
+
self.assertIn("syntax error", str(ctx.exception))
|
|
49
|
+
|
|
50
|
+
@patch("subprocess.run")
|
|
51
|
+
def test_execute_file_passes_Q_flag(self, mock_run):
|
|
52
|
+
mock_run.return_value = _make_result("ok")
|
|
53
|
+
import tempfile, os
|
|
54
|
+
with tempfile.NamedTemporaryFile(suffix=".morphql", delete=False, mode="w") as f:
|
|
55
|
+
f.write("from json to json transform set x = x")
|
|
56
|
+
path = f.name
|
|
57
|
+
try:
|
|
58
|
+
MorphQL.execute_file(path, "{}")
|
|
59
|
+
args = mock_run.call_args[0][0]
|
|
60
|
+
self.assertIn("-Q", args)
|
|
61
|
+
self.assertIn(path, args)
|
|
62
|
+
finally:
|
|
63
|
+
os.unlink(path)
|
|
64
|
+
|
|
65
|
+
def test_execute_file_raises_if_not_found(self):
|
|
66
|
+
with self.assertRaises(FileNotFoundError):
|
|
67
|
+
MorphQL.execute_file("/nonexistent/query.morphql")
|
|
68
|
+
|
|
69
|
+
def test_execute_raises_if_query_empty(self):
|
|
70
|
+
with self.assertRaises(ValueError):
|
|
71
|
+
MorphQL.execute("")
|
|
72
|
+
|
|
73
|
+
@patch("subprocess.run")
|
|
74
|
+
def test_instance_run_uses_preset_defaults(self, mock_run):
|
|
75
|
+
mock_run.return_value = _make_result("result")
|
|
76
|
+
morph = MorphQL(provider="cli")
|
|
77
|
+
morph.run("from json to json transform set x = x", "{}")
|
|
78
|
+
self.assertTrue(mock_run.called)
|
|
79
|
+
|
|
80
|
+
@patch("subprocess.run")
|
|
81
|
+
def test_node_no_warnings_env_set(self, mock_run):
|
|
82
|
+
mock_run.return_value = _make_result("ok")
|
|
83
|
+
MorphQL.execute("from json to json transform set x = x", "{}")
|
|
84
|
+
env = mock_run.call_args[1]["env"]
|
|
85
|
+
self.assertEqual(env.get("NODE_NO_WARNINGS"), "1")
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TestCliRunFile(unittest.TestCase):
|
|
89
|
+
@patch("subprocess.run")
|
|
90
|
+
def test_run_file_passes_Q_flag(self, mock_run):
|
|
91
|
+
mock_run.return_value = _make_result("ok")
|
|
92
|
+
import tempfile, os
|
|
93
|
+
with tempfile.NamedTemporaryFile(suffix=".morphql", delete=False, mode="w") as f:
|
|
94
|
+
f.write("from json to json transform set x = x")
|
|
95
|
+
path = f.name
|
|
96
|
+
try:
|
|
97
|
+
morph = MorphQL(provider="cli")
|
|
98
|
+
morph.run_file(path, "{}")
|
|
99
|
+
args = mock_run.call_args[0][0]
|
|
100
|
+
self.assertIn("-Q", args)
|
|
101
|
+
self.assertIn(path, args)
|
|
102
|
+
finally:
|
|
103
|
+
os.unlink(path)
|
|
104
|
+
|
|
105
|
+
def test_run_file_raises_if_not_found(self):
|
|
106
|
+
morph = MorphQL(provider="cli")
|
|
107
|
+
with self.assertRaises(FileNotFoundError):
|
|
108
|
+
morph.run_file("/nonexistent/query.morphql")
|
|
109
|
+
|
|
110
|
+
def test_run_raises_if_query_empty(self):
|
|
111
|
+
morph = MorphQL(provider="cli")
|
|
112
|
+
with self.assertRaises(ValueError):
|
|
113
|
+
morph.run("")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class TestCliErrorPaths(unittest.TestCase):
|
|
117
|
+
@patch("subprocess.run")
|
|
118
|
+
def test_timeout_raises_runtime_error(self, mock_run):
|
|
119
|
+
mock_run.side_effect = subprocess.TimeoutExpired(cmd=["morphql"], timeout=30)
|
|
120
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
121
|
+
MorphQL.execute("from json to json transform set x = x", "{}")
|
|
122
|
+
self.assertIn("timed out", str(ctx.exception))
|
|
123
|
+
|
|
124
|
+
@patch("subprocess.run")
|
|
125
|
+
def test_cli_not_found_raises_runtime_error(self, mock_run):
|
|
126
|
+
mock_run.side_effect = FileNotFoundError()
|
|
127
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
128
|
+
MorphQL.execute(
|
|
129
|
+
"from json to json transform set x = x",
|
|
130
|
+
"{}",
|
|
131
|
+
cli_path="/no/such/binary",
|
|
132
|
+
)
|
|
133
|
+
self.assertIn("not found", str(ctx.exception))
|
|
134
|
+
|
|
135
|
+
@patch("subprocess.run")
|
|
136
|
+
def test_stderr_preferred_over_stdout_on_error(self, mock_run):
|
|
137
|
+
mock_run.return_value = _make_result("stdout msg", returncode=1, stderr="stderr msg")
|
|
138
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
139
|
+
MorphQL.execute("bad query", "{}")
|
|
140
|
+
self.assertIn("stderr msg", str(ctx.exception))
|
|
141
|
+
|
|
142
|
+
@patch("subprocess.run")
|
|
143
|
+
def test_stdout_used_when_stderr_empty(self, mock_run):
|
|
144
|
+
mock_run.return_value = _make_result("stdout only", returncode=1, stderr="")
|
|
145
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
146
|
+
MorphQL.execute("bad query", "{}")
|
|
147
|
+
self.assertIn("stdout only", str(ctx.exception))
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
class TestQjsRuntime(unittest.TestCase):
|
|
151
|
+
"""Tests for the QuickJS runtime path."""
|
|
152
|
+
|
|
153
|
+
@patch("subprocess.run")
|
|
154
|
+
@patch("os.path.isfile")
|
|
155
|
+
def test_qjs_command_uses_qjs_bin_and_bundle(self, mock_isfile, mock_run):
|
|
156
|
+
"""When runtime=qjs and qjs_path is set, command must be [qjs_bin, --std, -m, bundle]."""
|
|
157
|
+
mock_run.return_value = _make_result("ok")
|
|
158
|
+
# Make isfile return True for the bundle path so it doesn't raise
|
|
159
|
+
mock_isfile.return_value = True
|
|
160
|
+
|
|
161
|
+
MorphQL.execute(
|
|
162
|
+
"from json to json transform set x = x",
|
|
163
|
+
"{}",
|
|
164
|
+
runtime="qjs",
|
|
165
|
+
qjs_path="/usr/bin/qjs",
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
args = mock_run.call_args[0][0]
|
|
169
|
+
self.assertEqual(args[0], "/usr/bin/qjs")
|
|
170
|
+
self.assertIn("--std", args)
|
|
171
|
+
self.assertIn("-m", args)
|
|
172
|
+
|
|
173
|
+
@patch("subprocess.run")
|
|
174
|
+
@patch("os.path.isfile")
|
|
175
|
+
def test_qjs_command_raises_if_bundle_not_found(self, mock_isfile, mock_run):
|
|
176
|
+
"""RuntimeError raised when neither monorepo nor distributed bundle exists."""
|
|
177
|
+
mock_isfile.return_value = False
|
|
178
|
+
|
|
179
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
180
|
+
MorphQL.execute(
|
|
181
|
+
"from json to json transform set x = x",
|
|
182
|
+
"{}",
|
|
183
|
+
runtime="qjs",
|
|
184
|
+
qjs_path="/usr/bin/qjs",
|
|
185
|
+
)
|
|
186
|
+
self.assertIn("bundle not found", str(ctx.exception))
|
|
187
|
+
|
|
188
|
+
@patch("subprocess.run")
|
|
189
|
+
@patch("os.path.isfile")
|
|
190
|
+
@patch("platform.system", return_value="Linux")
|
|
191
|
+
@patch("platform.machine", return_value="x86_64")
|
|
192
|
+
def test_qjs_resolves_linux_binary_suffix(self, mock_machine, mock_system, mock_isfile, mock_run):
|
|
193
|
+
"""On Linux, the bundled binary name must include -linux-x86_64."""
|
|
194
|
+
# First isfile call (bundled qjs bin) → True so it returns that path
|
|
195
|
+
# Second isfile call onwards (bundle .js) → True
|
|
196
|
+
def isfile_side_effect(path):
|
|
197
|
+
return True
|
|
198
|
+
mock_isfile.side_effect = isfile_side_effect
|
|
199
|
+
mock_run.return_value = _make_result("ok")
|
|
200
|
+
|
|
201
|
+
MorphQL.execute(
|
|
202
|
+
"from json to json transform set x = x",
|
|
203
|
+
"{}",
|
|
204
|
+
runtime="qjs",
|
|
205
|
+
# no qjs_path → must auto-resolve
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
args = mock_run.call_args[0][0]
|
|
209
|
+
# The resolved qjs binary path should contain the linux suffix
|
|
210
|
+
self.assertTrue(
|
|
211
|
+
any("linux-x86_64" in str(a) for a in args),
|
|
212
|
+
f"Expected linux-x86_64 in args: {args}",
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
@patch("morphql.morphql.MorphQL._download_file")
|
|
216
|
+
@patch("os.chmod")
|
|
217
|
+
@patch("os.makedirs")
|
|
218
|
+
@patch("os.path.isfile", return_value=False)
|
|
219
|
+
@patch("platform.system", return_value="Linux")
|
|
220
|
+
@patch("platform.machine", return_value="x86_64")
|
|
221
|
+
def test_qjs_falls_back_to_system_qjs_on_download_failure(
|
|
222
|
+
self, mock_machine, mock_system, mock_isfile, mock_makedirs, mock_chmod, mock_download
|
|
223
|
+
):
|
|
224
|
+
"""If download raises, _maybe_install_qjs must fall back to 'qjs'."""
|
|
225
|
+
mock_download.side_effect = RuntimeError("network error")
|
|
226
|
+
|
|
227
|
+
result = MorphQL._maybe_install_qjs({"cache_dir": None})
|
|
228
|
+
self.assertEqual(result, "qjs")
|
|
229
|
+
|
|
230
|
+
@patch("morphql.morphql.MorphQL._download_file")
|
|
231
|
+
@patch("os.chmod")
|
|
232
|
+
@patch("os.makedirs")
|
|
233
|
+
@patch("os.path.isfile", return_value=False)
|
|
234
|
+
@patch("platform.system", return_value="Linux")
|
|
235
|
+
@patch("platform.machine", return_value="x86_64")
|
|
236
|
+
def test_qjs_download_called_when_not_cached(
|
|
237
|
+
self, mock_machine, mock_system, mock_isfile, mock_makedirs, mock_chmod, mock_download
|
|
238
|
+
):
|
|
239
|
+
"""_download_file must be called with a GitHub URL when no local binary exists."""
|
|
240
|
+
MorphQL._maybe_install_qjs({"cache_dir": "/tmp/morphql-test"})
|
|
241
|
+
self.assertTrue(mock_download.called)
|
|
242
|
+
url = mock_download.call_args[0][0]
|
|
243
|
+
self.assertIn("quickjs-ng", url)
|
|
244
|
+
self.assertIn("linux-x86_64", url)
|
|
245
|
+
|
|
246
|
+
@patch("urllib.request.urlopen")
|
|
247
|
+
def test_download_file_raises_on_small_response(self, mock_urlopen):
|
|
248
|
+
"""_download_file must raise RuntimeError if downloaded content is < 1000 bytes."""
|
|
249
|
+
m = MagicMock()
|
|
250
|
+
m.read.return_value = b"tiny"
|
|
251
|
+
m.__enter__ = lambda s: s
|
|
252
|
+
m.__exit__ = MagicMock(return_value=False)
|
|
253
|
+
mock_urlopen.return_value = m
|
|
254
|
+
|
|
255
|
+
import tempfile, os
|
|
256
|
+
with tempfile.NamedTemporaryFile(delete=False) as f:
|
|
257
|
+
target = f.name
|
|
258
|
+
try:
|
|
259
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
260
|
+
MorphQL._download_file("https://example.com/qjs", target)
|
|
261
|
+
self.assertIn("too small", str(ctx.exception))
|
|
262
|
+
finally:
|
|
263
|
+
os.unlink(target)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
class TestConfigResolution(unittest.TestCase):
|
|
267
|
+
@patch("subprocess.run")
|
|
268
|
+
def test_env_var_used_as_fallback(self, mock_run):
|
|
269
|
+
mock_run.return_value = _make_result("ok")
|
|
270
|
+
import os
|
|
271
|
+
saved = os.environ.pop("MORPHQL_CLI_PATH", None)
|
|
272
|
+
os.environ["MORPHQL_CLI_PATH"] = "my-morphql-bin"
|
|
273
|
+
try:
|
|
274
|
+
MorphQL.execute("from json to json transform set x = x", "{}")
|
|
275
|
+
args = mock_run.call_args[0][0]
|
|
276
|
+
self.assertIn("my-morphql-bin", args)
|
|
277
|
+
finally:
|
|
278
|
+
del os.environ["MORPHQL_CLI_PATH"]
|
|
279
|
+
if saved is not None:
|
|
280
|
+
os.environ["MORPHQL_CLI_PATH"] = saved
|
|
281
|
+
|
|
282
|
+
@patch("subprocess.run")
|
|
283
|
+
def test_call_kwarg_overrides_instance_default(self, mock_run):
|
|
284
|
+
"""provider='server' kwarg must override instance default provider='cli'."""
|
|
285
|
+
from unittest.mock import patch as mpatch
|
|
286
|
+
|
|
287
|
+
mock_run.return_value = _make_result("should_not_be_called")
|
|
288
|
+
response_mock = MagicMock()
|
|
289
|
+
response_mock.read.return_value = json.dumps(
|
|
290
|
+
{"success": True, "result": "server_result"}
|
|
291
|
+
).encode()
|
|
292
|
+
response_mock.__enter__ = lambda s: s
|
|
293
|
+
response_mock.__exit__ = MagicMock(return_value=False)
|
|
294
|
+
|
|
295
|
+
morph = MorphQL(provider="cli")
|
|
296
|
+
with mpatch("urllib.request.urlopen", return_value=response_mock):
|
|
297
|
+
result = morph.run(
|
|
298
|
+
"from json to json transform set x = x",
|
|
299
|
+
"{}",
|
|
300
|
+
provider="server",
|
|
301
|
+
)
|
|
302
|
+
self.assertEqual(result, "server_result")
|
|
303
|
+
mock_run.assert_not_called()
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
if __name__ == "__main__":
|
|
307
|
+
unittest.main()
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import unittest
|
|
3
|
+
from io import BytesIO
|
|
4
|
+
from unittest.mock import MagicMock, patch
|
|
5
|
+
|
|
6
|
+
from morphql import MorphQL
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _make_response(body: dict, status: int = 200) -> MagicMock:
|
|
10
|
+
raw = json.dumps(body).encode()
|
|
11
|
+
m = MagicMock()
|
|
12
|
+
m.read.return_value = raw
|
|
13
|
+
m.status = status
|
|
14
|
+
m.__enter__ = lambda s: s
|
|
15
|
+
m.__exit__ = MagicMock(return_value=False)
|
|
16
|
+
return m
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TestServerExecute(unittest.TestCase):
|
|
20
|
+
@patch("urllib.request.urlopen")
|
|
21
|
+
def test_execute_returns_result(self, mock_urlopen):
|
|
22
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "hello"})
|
|
23
|
+
result = MorphQL.execute(
|
|
24
|
+
"from json to json transform set x = x",
|
|
25
|
+
'{"x": 1}',
|
|
26
|
+
provider="server",
|
|
27
|
+
)
|
|
28
|
+
self.assertEqual(result, "hello")
|
|
29
|
+
|
|
30
|
+
@patch("urllib.request.urlopen")
|
|
31
|
+
def test_payload_contains_query_and_data(self, mock_urlopen):
|
|
32
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "ok"})
|
|
33
|
+
MorphQL.execute(
|
|
34
|
+
"from json to json transform set x = x",
|
|
35
|
+
{"a": 1},
|
|
36
|
+
provider="server",
|
|
37
|
+
)
|
|
38
|
+
req = mock_urlopen.call_args[0][0]
|
|
39
|
+
payload = json.loads(req.data)
|
|
40
|
+
self.assertEqual(payload["query"], "from json to json transform set x = x")
|
|
41
|
+
self.assertEqual(payload["data"], {"a": 1})
|
|
42
|
+
|
|
43
|
+
@patch("urllib.request.urlopen")
|
|
44
|
+
def test_json_string_data_is_decoded(self, mock_urlopen):
|
|
45
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "ok"})
|
|
46
|
+
MorphQL.execute(
|
|
47
|
+
"from json to json transform set x = x",
|
|
48
|
+
'{"key": "value"}',
|
|
49
|
+
provider="server",
|
|
50
|
+
)
|
|
51
|
+
req = mock_urlopen.call_args[0][0]
|
|
52
|
+
payload = json.loads(req.data)
|
|
53
|
+
self.assertEqual(payload["data"], {"key": "value"})
|
|
54
|
+
|
|
55
|
+
@patch("urllib.request.urlopen")
|
|
56
|
+
def test_api_key_header_sent(self, mock_urlopen):
|
|
57
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "ok"})
|
|
58
|
+
MorphQL.execute(
|
|
59
|
+
"from json to json transform set x = x",
|
|
60
|
+
"{}",
|
|
61
|
+
provider="server",
|
|
62
|
+
api_key="secret123",
|
|
63
|
+
)
|
|
64
|
+
req = mock_urlopen.call_args[0][0]
|
|
65
|
+
self.assertEqual(req.get_header("X-api-key"), "secret123")
|
|
66
|
+
|
|
67
|
+
@patch("urllib.request.urlopen")
|
|
68
|
+
def test_raises_on_success_false(self, mock_urlopen):
|
|
69
|
+
mock_urlopen.return_value = _make_response(
|
|
70
|
+
{"success": False, "message": "bad query"}
|
|
71
|
+
)
|
|
72
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
73
|
+
MorphQL.execute("bad", "{}", provider="server")
|
|
74
|
+
self.assertIn("bad query", str(ctx.exception))
|
|
75
|
+
|
|
76
|
+
@patch("urllib.request.urlopen")
|
|
77
|
+
def test_uses_correct_endpoint(self, mock_urlopen):
|
|
78
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "ok"})
|
|
79
|
+
MorphQL.execute(
|
|
80
|
+
"from json to json transform set x = x",
|
|
81
|
+
"{}",
|
|
82
|
+
provider="server",
|
|
83
|
+
server_url="http://myhost:4000",
|
|
84
|
+
)
|
|
85
|
+
req = mock_urlopen.call_args[0][0]
|
|
86
|
+
self.assertEqual(req.full_url, "http://myhost:4000/v1/execute")
|
|
87
|
+
|
|
88
|
+
@patch("urllib.request.urlopen")
|
|
89
|
+
def test_instance_run_server(self, mock_urlopen):
|
|
90
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "done"})
|
|
91
|
+
morph = MorphQL(provider="server", server_url="http://localhost:3000")
|
|
92
|
+
result = morph.run("from json to json transform set x = x", "{}")
|
|
93
|
+
self.assertEqual(result, "done")
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class TestServerErrorPaths(unittest.TestCase):
|
|
97
|
+
@patch("urllib.request.urlopen")
|
|
98
|
+
def test_http_error_raises_runtime_error(self, mock_urlopen):
|
|
99
|
+
import urllib.error
|
|
100
|
+
err = urllib.error.HTTPError(
|
|
101
|
+
url="http://localhost:3000/v1/execute",
|
|
102
|
+
code=500,
|
|
103
|
+
msg="Internal Server Error",
|
|
104
|
+
hdrs=None,
|
|
105
|
+
fp=BytesIO(b'{"message": "server crashed"}'),
|
|
106
|
+
)
|
|
107
|
+
mock_urlopen.side_effect = err
|
|
108
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
109
|
+
MorphQL.execute("from json to json transform set x = x", "{}", provider="server")
|
|
110
|
+
self.assertIn("500", str(ctx.exception))
|
|
111
|
+
|
|
112
|
+
@patch("urllib.request.urlopen")
|
|
113
|
+
def test_url_error_raises_runtime_error(self, mock_urlopen):
|
|
114
|
+
import urllib.error
|
|
115
|
+
mock_urlopen.side_effect = urllib.error.URLError(reason="Connection refused")
|
|
116
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
117
|
+
MorphQL.execute("from json to json transform set x = x", "{}", provider="server")
|
|
118
|
+
self.assertIn("unreachable", str(ctx.exception))
|
|
119
|
+
|
|
120
|
+
@patch("urllib.request.urlopen")
|
|
121
|
+
def test_invalid_json_response_raises_runtime_error(self, mock_urlopen):
|
|
122
|
+
m = MagicMock()
|
|
123
|
+
m.read.return_value = b"not json at all"
|
|
124
|
+
m.__enter__ = lambda s: s
|
|
125
|
+
m.__exit__ = MagicMock(return_value=False)
|
|
126
|
+
mock_urlopen.return_value = m
|
|
127
|
+
with self.assertRaises(RuntimeError) as ctx:
|
|
128
|
+
MorphQL.execute("from json to json transform set x = x", "{}", provider="server")
|
|
129
|
+
self.assertIn("invalid JSON", str(ctx.exception))
|
|
130
|
+
|
|
131
|
+
@patch("urllib.request.urlopen")
|
|
132
|
+
def test_run_file_via_server(self, mock_urlopen):
|
|
133
|
+
mock_urlopen.return_value = _make_response({"success": True, "result": "file_result"})
|
|
134
|
+
import tempfile, os
|
|
135
|
+
with tempfile.NamedTemporaryFile(suffix=".morphql", delete=False, mode="w") as f:
|
|
136
|
+
f.write("from json to json transform set x = x")
|
|
137
|
+
path = f.name
|
|
138
|
+
try:
|
|
139
|
+
morph = MorphQL(provider="server")
|
|
140
|
+
result = morph.run_file(path, "{}")
|
|
141
|
+
self.assertEqual(result, "file_result")
|
|
142
|
+
req = mock_urlopen.call_args[0][0]
|
|
143
|
+
payload = json.loads(req.data)
|
|
144
|
+
self.assertIn("from json", payload["query"])
|
|
145
|
+
finally:
|
|
146
|
+
os.unlink(path)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
if __name__ == "__main__":
|
|
150
|
+
unittest.main()
|