Skip to content

Commit

Permalink
Merge pull request #455 from lf-lang/python-paths
Browse files Browse the repository at this point in the history
New Python functions `lf.package_directory()` and `lf.source_directory()`
  • Loading branch information
lhstrh authored Jul 4, 2024
2 parents 38294a3 + 450d11e commit 8fe0436
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
16 changes: 16 additions & 0 deletions python/include/pythontarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ PyObject* py_schedule_copy(PyObject* self, PyObject* args);
*/
PyObject* py_request_stop(PyObject* self, PyObject* args);

/**
* @brief Return the source directory path (where the main .lf file is) as a string.
* @param self The lf object.
* @param args Empty.
* @return PyObject* A Python string.
*/
PyObject* py_source_directory(PyObject* self, PyObject* args);

/**
* @brief Return the root project directory path as a string.
* @param self The lf object.
* @param args Empty.
* @return PyObject* A Python string.
*/
PyObject* py_package_directory(PyObject* self, PyObject* args);

//////////////////////////////////////////////////////////////
///////////// Main function callable from Python code
PyObject* py_main(PyObject* self, PyObject* args);
Expand Down
35 changes: 29 additions & 6 deletions python/lib/pythontarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ PyObject* py_request_stop(PyObject* self, PyObject* args) {
return Py_None;
}

PyObject* py_source_directory(PyObject* self, PyObject* args) {
#ifndef LF_SOURCE_DIRECTORY
// This should not occur.
PyErr_SetString(PyExc_RuntimeError, "LF_SOURCE_DIRECTORY constant is not defined.");
return NULL;
#else
return PyUnicode_DecodeFSDefault(LF_SOURCE_DIRECTORY);
#endif
}

PyObject* py_package_directory(PyObject* self, PyObject* args) {
#ifndef LF_PACKAGE_DIRECTORY
// This should not occur.
PyErr_SetString(PyExc_RuntimeError, "LF_PACKAGE_DIRECTORY constant is not defined.");
return NULL;
#else
return PyUnicode_DecodeFSDefault(LF_PACKAGE_DIRECTORY);
#endif
}

/**
* Parse Python's 'argv' (from sys.argv()) into a pair of C-style
* 'argc' (the size of command-line parameters array)
Expand Down Expand Up @@ -299,12 +319,15 @@ PyObject* py_main(PyObject* self, PyObject* py_args) {
* @see schedule_copy
* @see request_stop
*/
static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = {{"start", py_main, METH_VARARGS, NULL},
{"schedule_copy", py_schedule_copy, METH_VARARGS, NULL},
{"tag", py_lf_tag, METH_NOARGS, NULL},
{"tag_compare", py_tag_compare, METH_VARARGS, NULL},
{"request_stop", py_request_stop, METH_NOARGS, "Request stop"},
{NULL, NULL, 0, NULL}};
static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = {
{"start", py_main, METH_VARARGS, NULL},
{"schedule_copy", py_schedule_copy, METH_VARARGS, NULL},
{"tag", py_lf_tag, METH_NOARGS, NULL},
{"tag_compare", py_tag_compare, METH_VARARGS, NULL},
{"request_stop", py_request_stop, METH_NOARGS, "Request stop"},
{"source_directory", py_source_directory, METH_NOARGS, "Source directory path for .lf file"},
{"package_directory", py_package_directory, METH_NOARGS, "Root package directory path"},
{NULL, NULL, 0, NULL}};

/**
* Define the Lingua Franca module.
Expand Down

0 comments on commit 8fe0436

Please sign in to comment.