Commit 22984080 authored by David Hendriks's avatar David Hendriks
Browse files

added help functionality, doing some small fixes now

parent 75b4803f
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -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):
    """
+49 −4
Original line number Diff line number Diff line
@@ -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)
@@ -331,3 +339,40 @@ static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
     */
    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
+41 −0
Original line number Diff line number Diff line
@@ -240,3 +240,44 @@ int run_binary_with_logfile(char * argstring,
    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);
    return 0;
}
 No newline at end of file
+5 −4
Original line number Diff line number Diff line
@@ -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