Skip to content

Commit 4d8a570

Browse files
committed
Initial commit.
0 parents  commit 4d8a570

File tree

11 files changed

+554
-0
lines changed

11 files changed

+554
-0
lines changed

.github/workflows/tests.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Run tests
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-python@v5
12+
with: {python-version: 3.13}
13+
- run: pip install git+https://github.com/Kodiologist/hy.git@fecc8068a371140f66ec9f67e40cacb8d8e63a01
14+
- run: pip install hyrule --no-deps
15+
- run: pip install pytest
16+
- run: pytest

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__/
2+
/.pytest_cache
3+
/dist
4+
/build
5+
/py2hy.egg-info

README.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
py2hy
2+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3+
4+
py2hy is a library and command-line interface to translate Python code to `Hy <http://hylang.org>`__ code. As with Hy's built-in ``hy2py``, all style information is discarded, including most comments. The result is messy, in part since there is no Hy autoformatter (yet?), but it works, and it makes a good starting point for a hand translation. You can also use py2hy when still learning Hy, to help figure out how to do something in Hy given an example in Python.
5+
6+
py2hy is currently unreleased, since it depends on bugfixes in Hy 1.1.0, which is also unreleased.
7+
8+
Usage
9+
============================================================
10+
11+
To use the command-line interface, see ``python3 -m py2hy --help``. The programmtic interface comprises two functions, of which see the docstrings:
12+
13+
- ``py2hy.ast_to_models``
14+
- ``py2hy.ast_to_text``
15+
16+
The test suite uses pytest.
17+
18+
Unimplemented nodes
19+
============================================================
20+
21+
The following features of Python's ``ast`` are not yet implemented, and are unlikely to get implemented unless I find myself wanting them for some reason. I'll accept patches for them, though.
22+
23+
- ``type_comment`` for these node types: ``For``, ``AsyncFor``, ``With``, ``AsyncWith``
24+
- Type aliases: ``TypeAlias``, ``TypeIgnore``, ``TypeVar``, ``ParamSpec``, ``TypeVarTuple``
25+
- ``TryStar``
26+
- Pattern-matching: ``Match``, ``MatchValue``, ``MatchSingleton``, ``MatchSequence``, ``MatchMapping``, ``MatchClass``, ``MatchStar``, ``MatchAs``, ``MatchOr``
27+
28+
License
29+
============================================================
30+
31+
This program is copyright 2025 Kodi B. Arfer.
32+
33+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
34+
35+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the `GNU General Public License`_ for more details.
36+
37+
.. _`GNU General Public License`: http://www.gnu.org/licenses/

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import hy, pytest
2+
3+
def pytest_collect_file(file_path, parent):
4+
if file_path.name.startswith('test_') and file_path.suffix == '.hy':
5+
return pytest.Module.from_parent(parent, path = file_path)

py2hy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import hy
2+
from py2hy.translate import ast_to_models, ast_to_text

py2hy/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import sys, py2hy.cli
2+
py2hy.cli.main(*sys.argv)

py2hy/cli.hy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(import
2+
sys
3+
pathlib [Path]
4+
ast
5+
py2hy
6+
hyrule [parse-args])
7+
8+
(defn main [_ #* args]
9+
10+
(setv p (parse-args
11+
:prog "py2hy"
12+
:args args
13+
:description "Translate Python code to Hy code."
14+
:spec [
15+
["FILE" :nargs "?" :type Path
16+
:help "Python source file (default: use standard input)"]]))
17+
18+
(print :end "" (py2hy.ast-to-text
19+
(if p.FILE
20+
(ast.parse (.read-text p.FILE) p.FILE)
21+
(ast.parse (.read sys.stdin) "<stdin>")))))

0 commit comments

Comments
 (0)