From 22984080214ec02d09238e2a57bb4578adf04be2 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Wed, 11 Dec 2019 15:48:33 +0000 Subject: [PATCH] added help functionality, doing some small fixes now --- binarycpython/utils/functions.py | 40 ++++++++++++++ src/binary_c_python.c | 53 +++++++++++++++++-- src/binary_c_python_api.c | 41 ++++++++++++++ .../python_API_test.py | 9 ++-- 4 files changed, 135 insertions(+), 8 deletions(-) rename python_API_test.py => tests/python_API_test.py (90%) diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 07873c3f3..bf04fb022 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -42,6 +42,46 @@ def get_arg_keys(): return get_defaults().keys() +def get_help(param_name): + """ + Function that returns the help info for a given parameter. + + Binary_c will output things in the following order; + - Did you mean? + - binary_c help for variable + - default + - available macros + + + + """ + + if param_name in get_arg_keys(): + help_info = binary_c_python_api.return_help(param_name) + + # print(help_info) + + # print(help_info) + # print(help_info.split('\n')) + print(repr(help_info)) + + cleaned = [el for el in help_info.split('\n') if not el==''] + + did_you_mean_nr = [i for i, el in enumerate(cleaned) if el.startswith('Did you mean')][0] + parameter_line_nr = [i for i, el in enumerate(cleaned) if el.startswith('binary_c help')][0] + default_line_nr = [i for i, el in enumerate(cleaned) if el.startswith('Default')][0] + # macros_line_nr = [i for i, el in enumerate(cleaned) if el.startswith('available')][0] + + # print(cleaned) + # print(info_line) + + pass + + +get_help('RLOF_method') + + + def run_system(**kwargs): """ diff --git a/src/binary_c_python.c b/src/binary_c_python.c index 9504c50fe..bd0ffd741 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -41,6 +41,9 @@ static char function_prototype_docstring[] = "The prototype for a binary_c python function"; static char return_arglines_docstring[] = "Return the default args for a binary_c system"; +static char return_help_info_docstring[] = + "Return the help info for a given parameter"; + static struct libbinary_c_store_t *store = NULL; // Initialize pyobjects @@ -53,6 +56,8 @@ static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *ar static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args); static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args); static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args); +static PyObject* binary_c_return_help_info(PyObject *self, PyObject *args); + /* * Python 3 interface is described at @@ -69,12 +74,14 @@ static PyMethodDef module_methods[] = { create_binary_docstring }, #endif + {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring}, + {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring}, + {"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring}, {"run_binary_with_logfile", binary_c_run_binary_with_logfile, METH_VARARGS, run_binary_with_logdocstring}, {"run_binary_custom_logging", binary_c_run_binary_custom_logging, METH_VARARGS, run_binary_custom_loggingdocstring}, - {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring}, - {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring}, {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring}, + {"return_help", binary_c_return_help_info, METH_VARARGS, return_help_info_docstring}, {NULL, NULL, 0, NULL} }; @@ -114,7 +121,8 @@ PyMODINIT_FUNC initbinary_c(void) #ifdef __DEPRECATED -static PyObject* binary_c_create_binary(PyObject *self, PyObject *args){ +static PyObject* binary_c_create_binary(PyObject *self, PyObject *args) +{ double var1, var2; char * empty_str = ""; @@ -141,7 +149,7 @@ static PyObject* binary_c_create_binary(PyObject *self, PyObject *args){ return ret; } -#endif +#endif //__DEPRECATED static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args) @@ -330,4 +338,41 @@ static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args) * return the return_error_string as well! */ return return_string; +} + +static PyObject* binary_c_return_help_info(PyObject *self, PyObject *args) +{ + /* Parse the input tuple */ + char *argstring; + + if(!PyArg_ParseTuple(args, "s", &argstring)) + { + return NULL; + } + else + { + char * buffer; + char * error_buffer; + size_t nbytes; + int out MAYBE_UNUSED = return_help_info(argstring, + &buffer, + &error_buffer, + &nbytes); + + /* copy the buffer to a python string */ + PyObject * return_string = Py_BuildValue("s", buffer); + PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); + + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error in binary_c run : %s\n", + error_buffer); + } + + Safe_free(buffer); + Safe_free(error_buffer); + + return return_string; + } } \ No newline at end of file diff --git a/src/binary_c_python_api.c b/src/binary_c_python_api.c index 98aa26883..f8e1d11bf 100644 --- a/src/binary_c_python_api.c +++ b/src/binary_c_python_api.c @@ -235,6 +235,47 @@ int run_binary_with_logfile(char * argstring, /* set raw_buffer_size = -1 to prevent it being freed */ stardata->tmpstore->raw_buffer_size = -1; + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); + binary_c_free_store_contents(store); + return 0; +} + + +int return_help_info(char * argstring, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes) +{ + /* memory for N binary systems */ + struct libbinary_c_stardata_t *stardata; + struct libbinary_c_store_t * store = NULL; + + /* make new stardata */ + stardata = NULL; + binary_c_new_system(&stardata, + NULL, + NULL, + &store, + &argstring, + -1); + + /* output to strings */ + stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; + + /* Ask the help api */ + binary_c_help(stardata, argstring); + + /* get buffer pointer */ + binary_c_buffer_info(stardata,buffer,nbytes); + + /* get error buffer pointer */ + binary_c_error_buffer(stardata,error_buffer); + + /* set raw_buffer_size = -1 to prevent it being freed */ + stardata->tmpstore->raw_buffer_size = -1; + /* free stardata (except the buffer) */ binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_store_contents(store); diff --git a/python_API_test.py b/tests/python_API_test.py similarity index 90% rename from python_API_test.py rename to tests/python_API_test.py index 8fe7e62dc..bb92a2fde 100755 --- a/python_API_test.py +++ b/tests/python_API_test.py @@ -7,7 +7,6 @@ import binary_c_python_api # module. ############################################################ - def run_test_binary(): m1 = 15.0 # Msun m2 = 14.0 # Msun @@ -33,7 +32,9 @@ def run_test_binary(): print(output) -# binary_star = binary_c_python_api.new_system() +def run_help(): + out = binary_c_python_api.return_help('M_1') + print(out) + +run_help() -# print(binary_star) -run_test_binary() \ No newline at end of file -- GitLab