mcad2py 0.1.0__py3-none-any.whl
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.
- mcad2py/__init__.py +16 -0
- mcad2py/cli.py +75 -0
- mcad2py/convert.py +41 -0
- mcad2py/emit/__init__.py +0 -0
- mcad2py/emit/codegen.py +927 -0
- mcad2py/emit/notebook_backend.py +174 -0
- mcad2py/emit/py_backend.py +93 -0
- mcad2py/ir.py +666 -0
- mcad2py/loader.py +130 -0
- mcad2py/mapping.py +204 -0
- mcad2py/parser/__init__.py +0 -0
- mcad2py/parser/expressions.py +436 -0
- mcad2py/parser/namespaces.py +31 -0
- mcad2py/parser/regions.py +829 -0
- mcad2py/runtime.py +1479 -0
- mcad2py/shapes.py +341 -0
- mcad2py/text.py +44 -0
- mcad2py-0.1.0.dist-info/METADATA +153 -0
- mcad2py-0.1.0.dist-info/RECORD +23 -0
- mcad2py-0.1.0.dist-info/WHEEL +5 -0
- mcad2py-0.1.0.dist-info/entry_points.txt +2 -0
- mcad2py-0.1.0.dist-info/licenses/LICENSE +21 -0
- mcad2py-0.1.0.dist-info/top_level.txt +1 -0
mcad2py/__init__.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Convert PTC Mathcad Prime (.mcdx) worksheets to Python / Jupyter notebooks."""
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version as _version
|
|
4
|
+
|
|
5
|
+
from .convert import convert_file, convert_worksheet
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
# Read the installed distribution's metadata rather than keeping a second
|
|
9
|
+
# copy of the number here: pyproject.toml stays the single source of truth,
|
|
10
|
+
# so the two can't drift (and a release that stamps the version only has to
|
|
11
|
+
# touch one file).
|
|
12
|
+
__version__ = _version("mcad2py")
|
|
13
|
+
except PackageNotFoundError: # imported from a source tree, not installed
|
|
14
|
+
__version__ = "0.0.0.dev0"
|
|
15
|
+
|
|
16
|
+
__all__ = ["convert_file", "convert_worksheet", "__version__"]
|
mcad2py/cli.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Command-line interface: ``mcad2py convert file.mcdx -o out.ipynb``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from .convert import convert_file
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def main(argv: list[str] | None = None) -> int:
|
|
13
|
+
parser = argparse.ArgumentParser(
|
|
14
|
+
prog="mcad2py",
|
|
15
|
+
description="Convert PTC Mathcad Prime (.mcdx) worksheets to Python / Jupyter.",
|
|
16
|
+
)
|
|
17
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
18
|
+
|
|
19
|
+
conv = sub.add_parser("convert", help="convert a .mcdx file")
|
|
20
|
+
conv.add_argument("input", type=Path, help="path to the .mcdx file")
|
|
21
|
+
conv.add_argument(
|
|
22
|
+
"-o",
|
|
23
|
+
"--output",
|
|
24
|
+
type=Path,
|
|
25
|
+
help="output file; '-' for stdout. Default: input with .ipynb/.py suffix.",
|
|
26
|
+
)
|
|
27
|
+
conv.add_argument(
|
|
28
|
+
"-f",
|
|
29
|
+
"--format",
|
|
30
|
+
choices=["notebook", "py"],
|
|
31
|
+
help="output format (default: inferred from -o suffix, else notebook).",
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
args = parser.parse_args(argv)
|
|
35
|
+
if args.command == "convert":
|
|
36
|
+
return _run_convert(args)
|
|
37
|
+
parser.error("unknown command")
|
|
38
|
+
return 2
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _run_convert(args: argparse.Namespace) -> int:
|
|
42
|
+
fmt = args.format or _infer_format(args.output)
|
|
43
|
+
try:
|
|
44
|
+
result = convert_file(args.input, fmt=fmt)
|
|
45
|
+
except (OSError, ValueError) as exc:
|
|
46
|
+
# The loader raises these with user-facing messages (missing file, not a
|
|
47
|
+
# zip, no worksheet.xml), so print the message rather than a traceback.
|
|
48
|
+
# OSError (which FileNotFoundError subclasses) also covers the read
|
|
49
|
+
# failures underneath -- a permission problem, a dead network drive --
|
|
50
|
+
# which are equally not the user's bug to debug.
|
|
51
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
52
|
+
return 1
|
|
53
|
+
|
|
54
|
+
if args.output is None:
|
|
55
|
+
suffix = ".ipynb" if fmt == "notebook" else ".py"
|
|
56
|
+
out_path = args.input.with_suffix(suffix)
|
|
57
|
+
elif str(args.output) == "-":
|
|
58
|
+
sys.stdout.write(result)
|
|
59
|
+
return 0
|
|
60
|
+
else:
|
|
61
|
+
out_path = args.output
|
|
62
|
+
|
|
63
|
+
out_path.write_text(result, encoding="utf-8")
|
|
64
|
+
print(f"wrote {out_path}")
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _infer_format(output: Path | None) -> str:
|
|
69
|
+
if output is not None and output.suffix == ".py":
|
|
70
|
+
return "py"
|
|
71
|
+
return "notebook"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
if __name__ == "__main__":
|
|
75
|
+
raise SystemExit(main())
|
mcad2py/convert.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""High-level conversion: ``.mcdx`` -> IR -> notebook / python."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from . import ir
|
|
8
|
+
from .emit.notebook_backend import to_ipynb_string
|
|
9
|
+
from .emit.py_backend import to_python
|
|
10
|
+
from .loader import McdxPackage, load_mcdx
|
|
11
|
+
from .parser.regions import parse_worksheet
|
|
12
|
+
from .text import extract_text
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def convert_worksheet(pkg: McdxPackage) -> ir.Worksheet:
|
|
16
|
+
"""Parse a loaded package into an IR worksheet (with text resolved)."""
|
|
17
|
+
|
|
18
|
+
def resolve_text(idref: str) -> str:
|
|
19
|
+
data = pkg.text_package(idref)
|
|
20
|
+
return extract_text(data) if data else ""
|
|
21
|
+
|
|
22
|
+
return parse_worksheet(
|
|
23
|
+
pkg.worksheet_xml,
|
|
24
|
+
text_resolver=resolve_text,
|
|
25
|
+
image_resolver=pkg.image,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def convert_file(
|
|
30
|
+
path: str | Path,
|
|
31
|
+
*,
|
|
32
|
+
fmt: str = "notebook",
|
|
33
|
+
) -> str:
|
|
34
|
+
"""Convert a ``.mcdx`` file to source. ``fmt`` is ``"notebook"`` or ``"py"``."""
|
|
35
|
+
pkg = load_mcdx(path)
|
|
36
|
+
ws = convert_worksheet(pkg)
|
|
37
|
+
if fmt == "py":
|
|
38
|
+
return to_python(ws)
|
|
39
|
+
if fmt == "notebook":
|
|
40
|
+
return to_ipynb_string(ws)
|
|
41
|
+
raise ValueError(f"unknown format: {fmt!r} (expected 'notebook' or 'py')")
|
mcad2py/emit/__init__.py
ADDED
|
File without changes
|