Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OMI translate in Python #41

Closed
nesnoj opened this issue Sep 24, 2021 · 2 comments
Closed

Use OMI translate in Python #41

nesnoj opened this issue Sep 24, 2021 · 2 comments
Labels
type: bug 🐛 Something isn't working

Comments

@nesnoj
Copy link

nesnoj commented Sep 24, 2021

I expected OMI to work in Python using translate(), I tried

from omi.cli import translate
translate('oep-v1.4', 'oep-v1.4', 'out.json', 'in.json')

but it outputs

Usage: oep-v1.4 [OPTIONS] FILE_PATH
Try 'oep-v1.4 --help' for help.

Error: Got unexpected extra arguments (e p - v 1 . 4)

and exits Python.
What's wrong here?

@nesnoj nesnoj added the type: bug 🐛 Something isn't working label Sep 24, 2021
@nesnoj nesnoj changed the title Use OMI from Python Use OMI translate in Python Sep 24, 2021
@MGlauer
Copy link
Contributor

MGlauer commented Oct 5, 2021

This looks like the wrong function call. Everything in the cli.py is intended to be called via the command line interface (hence the name). They look like callable python functions, but the @-wrappers actually change how the function call works. What you want is what is inside the function, i.e.:

from omi.dialects import get_dialect

file_path = "in.json"
o = "out.json" # or None for printing
f = "oep-v1.4"
t = "oep-v1.4"

with open(file_path, "r") as infile:
        from_dialect = get_dialect(f)()
        obj = from_dialect.parse(infile.read())
        to_dialect = get_dialect(t)()
        s = to_dialect.compile_and_render(obj)
        if o:
            with open(o, "w") as outfile:
                outfile.write(s)
        else:
            print(s)

Instead of loading the dialects dynamically (get_dialect(f)(), etc.), you can also use the respective dialect directly:
from_dialect = to_dialect = OEP_V_1_4_Dialect

@nesnoj
Copy link
Author

nesnoj commented Oct 25, 2021

Thanks @MGlauer, works like a charm 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants