Skip to content

Commit

Permalink
Merge branch 'matlab2numpy' into mh_python
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoqing committed Apr 21, 2022
2 parents fc50db2 + 03d84b1 commit 082db51
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 40 additions & 3 deletions mh_python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from collections import OrderedDict
from io import StringIO
from pathlib import Path
from itertools import chain
from tempfile import NamedTemporaryFile
import keyword

from miss_hit_core import command_line, pathutil, work_package, cfg_tree
Expand Down Expand Up @@ -291,7 +293,12 @@ def script_file_visitor(self, node: Script_File, n_parent, relation):
def sequence_of_statements_visitor(
self, node: Sequence_Of_Statements, n_parent, relation
):
self[node] = "\n".join([self.pop(l) for l in node.l_statements])
self[node] = "\n".join([self.pop(l) for l in node.l_statements]) if node.l_statements else "pass"

def function_pointer_visitor(
self, node: Function_Pointer, n_parent, relation
):
self[node] = self.pop(node.n_name)

def function_definition_visitor(
self, node: Function_Definition, n_parent, relation
Expand Down Expand Up @@ -623,7 +630,7 @@ def parse_args(argv=None):
return command_line.parse_args(clp)


def process_one_file(path: Path, options=None, mh=None):
def process_one_file(path: [Path, str], options=None, mh=None):
if options is None:
options = parse_args()

Expand All @@ -639,7 +646,37 @@ def process_one_file(path: Path, options=None, mh=None):
wp = work_package.create(False, path, options.input_encoding, mh, options, {})
backend = MH_Python(options)
wp.register_file()
return backend.process_result(backend.process_wp(wp))
backend.process_result(backend.process_wp(wp))

for msg in chain.from_iterable(wp.mh.messages.get(wp.filename, {}).values()):
if msg.kind == "error" and "expected IDENTIFIER, reached EOF instead" == msg.message:
raise EOFError(msg.message)
elif msg.kind.endswith("error"):
mh.emit_message(msg)
raise SyntaxError(msg.message)


def process_one_block(src: str, inline=True):
with NamedTemporaryFile('w', suffix='.m') as f:
f.write(src)
f.flush()

target_path = Path(f.name).with_suffix('.py')

try:
options = parse_args([
"mh_python",
"--single",
"--python-alongside",
f.name])
if inline is True:
options.inline_mode = inline

process_one_file(f.name, options)
return target_path.read_text()
finally:
if target_path.exists():
target_path.unlink()


def main_handler():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="mh_python",
version="0.0.7",
version="0.0.8",
author="Chaoqing Wang",
author_email="[email protected]",
description="Matlab to Python/Numpy translator",
Expand Down

0 comments on commit 082db51

Please sign in to comment.