Skip to content

Commit

Permalink
Move transpiler settings to respective packages
Browse files Browse the repository at this point in the history
Finish making main() arg parsing dynamic.
  • Loading branch information
jayvdb authored and MiguelMarcelino committed Dec 26, 2023
1 parent c5cc536 commit efdfc61
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 425 deletions.
457 changes: 35 additions & 422 deletions py2many/cli.py

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions py2many/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
import pathlib

from unittest.mock import Mock

from .language import LanguageSettings

from .python_transformer import PythonTranspiler, RestoreMainRewriter

from .rewriters import InferredAnnAssignRewriter

from pycpp import settings as cpp_settings
from pydart import settings as dart_settings
from pygo import settings as go_settings
from pyjl import settings as julia_settings
from pykt import settings as kotlin_settings
from pynim import settings as nim_settings
from pyrs import settings as rust_settings
from pysmt import settings as smt_settings
from pyv import settings as vlang_settings


PY2MANY_DIR = pathlib.Path(__file__).parent
ROOT_DIR = PY2MANY_DIR.parent
FAKE_ARGS = Mock(indent=4)


def python_settings(args, env=os.environ):
return LanguageSettings(
PythonTranspiler(),
".py",
"Python",
formatter=["black"],
rewriters=[RestoreMainRewriter()],
post_rewriters=[InferredAnnAssignRewriter()],
)


ALL_SETTINGS = {
"python": python_settings,
"cpp": cpp_settings,
"rust": rust_settings,
"julia": julia_settings,
"kotlin": kotlin_settings,
"nim": nim_settings,
"dart": dart_settings,
"go": go_settings,
"vlang": vlang_settings,
"smt": smt_settings,
}


def _get_all_settings(args, env=os.environ):
return dict((key, func(args, env=env)) for key, func in ALL_SETTINGS.items())
64 changes: 64 additions & 0 deletions pycpp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
import os.path
import sys

from distutils import spawn

from py2many.language import LanguageSettings

from .transpiler import CppListComparisonRewriter, CppTranspiler

USER_HOME = os.path.expanduser("~/")


def _conan_include_dirs():
CONAN_CATCH2 = "catch2/3.0.1/_/_/package/a25d6c83542b56b72fdaa05a85db5d46f5f0f71c"
CONAN_CPPITERTOOLS = (
"cppitertools/2.1/_/_/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9"
)
return [
"-I",
f"{USER_HOME}/.conan/data/{CONAN_CATCH2}/include",
"-I",
f"{USER_HOME}/.conan/data/{CONAN_CPPITERTOOLS}/include",
]


def settings(args, env=os.environ):
clang_format_style = env.get("CLANG_FORMAT_STYLE")
cxx = env.get("CXX")
default_cxx = ["clang++", "g++-11", "g++"]
if cxx:
if not spawn.find_executable(cxx):
print(f"Warning: CXX({cxx}) not found")
cxx = None
if not cxx:
for exe in default_cxx:
if spawn.find_executable(exe):
cxx = exe
break
else:
cxx = default_cxx[0]
cxx_flags = env.get("CXXFLAGS")
if cxx_flags:
cxx_flags = cxx_flags.split()
else:
cxx_flags = ["-std=c++17", "-Wall", "-Werror"]
cxx_flags = _conan_include_dirs() + cxx_flags
if cxx.startswith("clang++") and not sys.platform == "win32":
cxx_flags += ["-stdlib=libc++"]

if clang_format_style:
clang_format_cmd = ["clang-format", f"-style={clang_format_style}", "-i"]
else:
clang_format_cmd = ["clang-format", "-i"]

return LanguageSettings(
CppTranspiler(args.extension, args.no_prologue),
".cpp",
"C++",
clang_format_cmd,
None,
[CppListComparisonRewriter()],
linter=[cxx, *cxx_flags],
)
15 changes: 15 additions & 0 deletions pydart/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os

from py2many.language import LanguageSettings

from .transpiler import DartIntegerDivRewriter, DartTranspiler


def settings(args, env=os.environ):
return LanguageSettings(
DartTranspiler(),
".dart",
"Dart",
["dart", "format"],
post_rewriters=[DartIntegerDivRewriter()],
)
44 changes: 44 additions & 0 deletions pygo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os

from pathlib import Path

from py2many.language import LanguageSettings

import py2many

from .inference import infer_go_types
from .transpiler import (
GoIfExpRewriter,
GoMethodCallRewriter,
GoNoneCompareRewriter,
GoPropagateTypeAnnotation,
GoTranspiler,
GoVisibilityRewriter,
)

PY2MANY_DIR = Path(py2many.__file__).parent
ROOT_DIR = PY2MANY_DIR.parent


def settings(args, env=os.environ):
config_filename = "revive.toml"
CWD = Path.cwd()
if os.path.exists(CWD / config_filename):
revive_config = CWD / config_filename
elif os.path.exists(ROOT_DIR / config_filename):
revive_config = ROOT_DIR / config_filename
else:
revive_config = None
return LanguageSettings(
GoTranspiler(),
".go",
"Go",
["gofmt", "-w"],
None,
[GoNoneCompareRewriter(), GoVisibilityRewriter(), GoIfExpRewriter()],
[infer_go_types],
[GoMethodCallRewriter(), GoPropagateTypeAnnotation()],
linter=(
["revive", "--config", str(revive_config)] if revive_config else ["revive"]
),
)
45 changes: 45 additions & 0 deletions pyjl/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os

from distutils import spawn
from functools import lru_cache
from pathlib import Path
from subprocess import run

from py2many.language import LanguageSettings

from .inference import infer_julia_types
from .rewriters import JuliaIndexingRewriter
from .transpiler import JuliaMethodCallRewriter, JuliaTranspiler


@lru_cache()
def _julia_formatter_path():
proc = run(
["julia", "-e", "import JuliaFormatter;print(pathof(JuliaFormatter))"],
capture_output=True,
)
if not proc.returncode and proc.stdout:
return str(Path(proc.stdout.decode("utf8")).parent.parent / "bin" / "format.jl")


def settings(args, env=os.environ):
format_jl = spawn.find_executable("format.jl")
if not format_jl:
julia = spawn.find_executable("julia")
if julia:
format_jl = _julia_formatter_path()

if format_jl:
format_jl = ["julia", "-O0", "--compile=min", "--startup=no", format_jl, "-v"]
else:
format_jl = ["format.jl", "-v"]
return LanguageSettings(
JuliaTranspiler(),
".jl",
"Julia",
format_jl,
None,
rewriters=[],
transformers=[infer_julia_types],
post_rewriters=[JuliaIndexingRewriter(), JuliaMethodCallRewriter()],
)
19 changes: 19 additions & 0 deletions pykt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

from py2many.language import LanguageSettings

from .inference import infer_kotlin_types
from .transpiler import KotlinBitOpRewriter, KotlinPrintRewriter, KotlinTranspiler


def settings(args, env=os.environ):
return LanguageSettings(
KotlinTranspiler(),
".kt",
"Kotlin",
["ktlint", "-F"],
rewriters=[KotlinBitOpRewriter()],
transformers=[infer_kotlin_types],
post_rewriters=[KotlinPrintRewriter()],
linter=["ktlint"],
)
23 changes: 23 additions & 0 deletions pynim/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

from py2many.language import LanguageSettings

from .inference import infer_nim_types
from .transpiler import NimNoneCompareRewriter, NimTranspiler


def settings(args, env=os.environ):
nim_args = {}
nimpretty_args = []
if args.indent is not None:
nim_args["indent"] = args.indent
nimpretty_args.append(f"--indent:{args.indent}")
return LanguageSettings(
NimTranspiler(**nim_args),
".nim",
"Nim",
["nimpretty", *nimpretty_args],
None,
[NimNoneCompareRewriter()],
[infer_nim_types],
)
26 changes: 26 additions & 0 deletions pyrs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os

from functools import partial

from py2many.language import LanguageSettings

from .inference import infer_rust_types
from .transpiler import (
RustLoopIndexRewriter,
RustNoneCompareRewriter,
RustStringJoinRewriter,
RustTranspiler,
)


def settings(args, env=os.environ):
return LanguageSettings(
RustTranspiler(args.extension, args.no_prologue),
".rs",
"Rust",
["rustfmt", "--edition=2018"],
None,
[RustNoneCompareRewriter()],
[partial(infer_rust_types, extension=args.extension)],
[RustLoopIndexRewriter(), RustStringJoinRewriter()],
)
19 changes: 19 additions & 0 deletions pysmt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

from py2many.language import LanguageSettings

from .inference import infer_smt_types
from .transpiler import SmtTranspiler


def settings(args, env=os.environ):
cljstyle_args = ["fix"]
return LanguageSettings(
SmtTranspiler(),
".smt",
"SMT",
["cljstyle", *cljstyle_args],
None,
[],
[infer_smt_types],
)
28 changes: 28 additions & 0 deletions pyv/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os

from py2many.language import LanguageSettings

from .inference import infer_v_types
from .transpiler import (
VComprehensionRewriter,
VDictRewriter,
VNoneCompareRewriter,
VTranspiler,
)


def settings(args, env=os.environ):
v_args = {}
vfmt_args = ["fmt", "-w"]
if args.indent is not None:
v_args["indent"] = args.indent
vfmt_args.append(f"--indent:{args.indent}")
return LanguageSettings(
VTranspiler(**v_args),
".v",
"V",
["v", *vfmt_args],
None,
[VNoneCompareRewriter(), VDictRewriter(), VComprehensionRewriter()],
[infer_v_types],
)
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
from unittest_expander import foreach, expand

from py2many.cli import (
_conan_include_dirs,
_create_cmd,
_get_all_settings,
_get_output_path,
_relative_to_cwd,
main,
)
from pycpp import _conan_include_dirs

import py2many.cli

Expand Down
4 changes: 2 additions & 2 deletions tests/test_transpile_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def test_go_recursive(self):
"annotation_transformer.py",
"ast_helpers.py",
"astx.py",
"context.py",
"cli.py",
"context.py",
"declaration_extractor.py",
"exceptions.py",
"language.py",
Expand Down Expand Up @@ -263,5 +263,5 @@ def test_vlang_recursive(self):
_suppress_exceptions=(AstNotImplementedError, AstUnrecognisedBinOp),
),
format_error_count=7,
failure_count=12,
failure_count=13,
)

0 comments on commit efdfc61

Please sign in to comment.