fix [#30589] RNA function descriptions not showing in Python console on autocomplete
authorCampbell Barton <ideasman42@gmail.com>
Tue, 20 Mar 2012 07:41:47 +0000 (07:41 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 20 Mar 2012 07:41:47 +0000 (07:41 +0000)
show rna function description and arguments now.

source/blender/makesrna/RNA_access.h
source/blender/makesrna/intern/rna_access.c
source/blender/python/intern/bpy_rna.c
source/blender/windowmanager/intern/wm_operators.c

index 596e348e3f6fdbba68c2081619a3d17a1e80b4d7..f4c3e4ed86d437c7bc8ed0a2cb7fbe0de3293df0 100644 (file)
@@ -951,6 +951,13 @@ int RNA_property_is_idprop(PropertyRNA *prop);
 /* python compatible string representation of this property, (must be freed!) */
 char *RNA_property_as_string(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop);
 char *RNA_pointer_as_string(struct bContext *C, PointerRNA *ptr);
+char *RNA_pointer_as_string_keywords_ex(struct bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
+                                        const short skip_optional_value, const short all_args,
+                                        PropertyRNA *iterprop);
+char *RNA_pointer_as_string_keywords(struct bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
+                                     const short skip_optional_value, const short all_args);
+char *RNA_function_as_string_keywords(struct bContext *C, FunctionRNA *func, PointerRNA *ptr_default,
+                                     const short as_function, const short all_args);
 
 /* Function */
 
index 582f4b32962036e747bd31ce1eeeae118378f7b5..2a14b951ae3c6e2d5dbc6f3e53e0601fc3777f5f 100644 (file)
@@ -4507,6 +4507,118 @@ char *RNA_pointer_as_string(bContext *C, PointerRNA *ptr)
        return cstring;
 }
 
+
+/* context and ptr_default can be NULL */
+char *RNA_pointer_as_string_keywords_ex(bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
+                                        const short as_function, const short all_args,
+                                        PropertyRNA *iterprop)
+{
+       const char *arg_name = NULL;
+
+       PropertyRNA *prop;
+
+       DynStr *dynstr= BLI_dynstr_new();
+       char *cstring, *buf;
+       int first_iter = TRUE, ok = TRUE;
+       int flag;
+
+       /* only to get the orginal props for comparisons */
+       PropertyRNA *prop_default;
+       char *buf_default;
+
+       RNA_PROP_BEGIN(ptr, propptr, iterprop) {
+               prop = propptr.data;
+
+               flag = RNA_property_flag(prop);
+
+               if (as_function && (flag & PROP_OUTPUT)) {
+                       continue;
+               }
+
+               arg_name = RNA_property_identifier(prop);
+
+               if (strcmp(arg_name, "rna_type") == 0) {
+                       continue;
+               }
+
+               if (as_function && (flag & PROP_REQUIRED)) {
+                       /* required args don't have useful defaults */
+                       BLI_dynstr_appendf(dynstr, first_iter ? "%s":", %s", arg_name);
+                       first_iter = FALSE;
+               }
+               else {
+                       if (as_function && RNA_property_type(prop) == PROP_POINTER) {
+                               /* don't expand pointers for functions */
+                               if (flag & PROP_NEVER_NULL) {
+                                       /* we cant really do the right thing here. arg=arg?, hrmf! */
+                                       buf = BLI_strdup(arg_name);
+                               }
+                               else {
+                                       buf = BLI_strdup("None");
+                               }
+                       }
+                       else {
+                               buf = RNA_property_as_string(C, ptr, prop);
+                       }
+
+                       ok = TRUE;
+
+                       if (all_args == FALSE && ptr_default) {
+                               /* not verbose, so only add in attributes that use non-default values
+                                * slow but good for tooltips */
+                               prop_default= RNA_struct_find_property(ptr_default, arg_name);
+
+                               if (prop_default) {
+                                       buf_default= RNA_property_as_string(C, ptr_default, prop_default);
+
+                                       if (strcmp(buf, buf_default) == 0)
+                                               ok = FALSE; /* values match, don't bother printing */
+
+                                       MEM_freeN(buf_default);
+                               }
+                       }
+                       if (ok) {
+                               BLI_dynstr_appendf(dynstr, first_iter ? "%s=%s":", %s=%s", arg_name, buf);
+                               first_iter = FALSE;
+                       }
+
+                       MEM_freeN(buf);
+               }
+       }
+       RNA_PROP_END;
+
+       cstring = BLI_dynstr_get_cstring(dynstr);
+       BLI_dynstr_free(dynstr);
+       return cstring;
+}
+
+char *RNA_pointer_as_string_keywords(bContext *C, PointerRNA *ptr, PointerRNA *ptr_default,
+                                     const short as_function, const short all_args)
+{
+       PropertyRNA *iterprop;
+
+       iterprop = RNA_struct_iterator_property(ptr->type);
+
+       return RNA_pointer_as_string_keywords_ex(C, ptr, ptr_default, as_function, all_args,
+                                                iterprop);
+}
+
+char *RNA_function_as_string_keywords(bContext *C, FunctionRNA *func, PointerRNA *ptr_default,
+                                     const short as_function, const short all_args)
+{
+       PointerRNA funcptr;
+       PropertyRNA *iterprop;
+
+       RNA_pointer_create(NULL, &RNA_Function, func, &funcptr);
+
+       iterprop = RNA_struct_find_property(&funcptr, "parameters");
+
+       RNA_struct_iterator_property(funcptr.type);
+
+       return RNA_pointer_as_string_keywords_ex(C, &funcptr, ptr_default, as_function, all_args,
+                                                iterprop);
+}
+
 char *RNA_property_as_string(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
 {
        int type = RNA_property_type(prop);
index e882d6ccd34afe8aba6b848c449ba4dd8d652f4d..35c8cc60a5389ee66c621b3f087b7f27f0e5b560 100644 (file)
@@ -3938,6 +3938,12 @@ static PyGetSetDef pyrna_struct_getseters[] = {
        {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
 };
 
+static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *closure);
+
+static PyGetSetDef pyrna_func_getseters[] = {
+       {(char *)"__doc__", (getter)pyrna_func_doc_get, (setter)NULL, NULL, NULL},
+       {NULL, NULL, NULL, NULL, NULL} /* Sentinel */
+};
 
 PyDoc_STRVAR(pyrna_prop_collection_keys_doc,
 ".. method:: keys()\n"
@@ -5150,6 +5156,22 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject
        Py_RETURN_NONE;
 }
 
+static PyObject *pyrna_func_doc_get(BPy_FunctionRNA *self, void *UNUSED(closure))
+{
+       PyObject *ret;
+       char *args;
+
+       args = RNA_function_as_string_keywords(NULL, self->func, NULL, TRUE, TRUE);
+
+       ret = PyUnicode_FromFormat("%.200s.%.200s(%.200s)\n%s",
+                                  RNA_struct_identifier(self->ptr.type),
+                                  RNA_function_identifier(self->func),
+                                  args, RNA_function_ui_description(self->func));
+
+       MEM_freeN(args);
+
+       return ret;
+}
 
 /* subclasses of pyrna_struct_Type which support idprop definitions use this as a metaclass */
 /* note: tp_base member is set to &PyType_Type on init */
@@ -5726,7 +5748,7 @@ PyTypeObject pyrna_func_Type = {
   /*** Attribute descriptor and subclassing stuff ***/
        NULL,                       /* struct PyMethodDef *tp_methods; */
        NULL,                       /* struct PyMemberDef *tp_members; */
-       NULL,                       /* struct PyGetSetDef *tp_getset; */
+       pyrna_func_getseters,       /* struct PyGetSetDef *tp_getset; */
        NULL,                       /* struct _typeobject *tp_base; */
        NULL,                       /* PyObject *tp_dict; */
        NULL,                       /* descrgetfunc tp_descr_get; */
index ba9ff89f175642d9c3406d1b95b8a87ba5fc01fd..7ee29945f57840fa8b46cc3e10ec933bb572a695 100644 (file)
@@ -512,68 +512,29 @@ void WM_operator_bl_idname(char *to, const char *from)
  */
 char *WM_operator_pystring(bContext *C, wmOperatorType *ot, PointerRNA *opptr, int all_args)
 {
-       const char *arg_name= NULL;
        char idname_py[OP_MAX_TYPENAME];
 
-       PropertyRNA *prop, *iterprop;
-
        /* for building the string */
        DynStr *dynstr= BLI_dynstr_new();
-       char *cstring, *buf;
-       int first_iter=1, ok= 1;
-
+       char *cstring;
+       char *cstring_args;
 
        /* only to get the orginal props for comparisons */
        PointerRNA opptr_default;
-       PropertyRNA *prop_default;
-       char *buf_default;
-       if(all_args==0 || opptr==NULL) {
+
+       if (all_args==0 || opptr==NULL) {
                WM_operator_properties_create_ptr(&opptr_default, ot);
 
                if(opptr==NULL)
                        opptr = &opptr_default;
        }
 
-
        WM_operator_py_idname(idname_py, ot->idname);
        BLI_dynstr_appendf(dynstr, "bpy.ops.%s(", idname_py);
 
-       iterprop= RNA_struct_iterator_property(opptr->type);
-
-       RNA_PROP_BEGIN(opptr, propptr, iterprop) {
-               prop= propptr.data;
-               arg_name= RNA_property_identifier(prop);
-
-               if (strcmp(arg_name, "rna_type")==0) continue;
-
-               buf= RNA_property_as_string(C, opptr, prop);
-               
-               ok= 1;
-
-               if(!all_args) {
-                       /* not verbose, so only add in attributes that use non-default values
-                        * slow but good for tooltips */
-                       prop_default= RNA_struct_find_property(&opptr_default, arg_name);
-
-                       if(prop_default) {
-                               buf_default= RNA_property_as_string(C, &opptr_default, prop_default);
-
-                               if(strcmp(buf, buf_default)==0)
-                                       ok= 0; /* values match, don't bother printing */
-
-                               MEM_freeN(buf_default);
-                       }
-
-               }
-               if(ok) {
-                       BLI_dynstr_appendf(dynstr, first_iter?"%s=%s":", %s=%s", arg_name, buf);
-                       first_iter = 0;
-               }
-
-               MEM_freeN(buf);
-
-       }
-       RNA_PROP_END;
+       cstring_args = RNA_pointer_as_string_keywords(C, opptr, &opptr_default, FALSE, all_args);
+       BLI_dynstr_append(dynstr, cstring_args);
+       MEM_freeN(cstring_args);
 
        if(all_args==0 || opptr==&opptr_default )
                WM_operator_properties_free(&opptr_default);