Small RNA changes

* rna_validate_identifier now checks identifiers are not python keywords such as if, and, from (builtins like max, object and sort are ok)
* rna_validate_identifier prints an error explaining why it fails
* renamed Struct's "from" to "base" - to point to the struct inherited from.
* renamed ImageUsers's "pass" and "layer" to "renderPass" and "renderLayer"
* use the identifier as the key for ENUM's (matching structs and properties)
This commit is contained in:
Campbell Barton
2008-12-18 23:34:19 +00:00
parent 1dcbafa4e4
commit 9178dc8596
4 changed files with 48 additions and 16 deletions

View File

@@ -166,17 +166,44 @@ static int rna_find_sdna_member(SDNA *sdna, const char *structname, const char *
return 0;
}
static int rna_validate_identifier(const char *identifier)
static int rna_validate_identifier(const char *identifier, char *error)
{
int a=0;
/* list from http://docs.python.org/reference/lexical_analysis.html#id5 */
static char *kwlist[] = {
"and", "as", "assert", "break",
"class", "continue", "def", "del",
"elif", "else", "except", "exec",
"finally", "for", "from", "global",
"if", "import", "in", "is",
"lambda", "not", "or", "pass",
"print", "raise", "return", "try",
"while", "with", "yield", NULL
};
if (!isalpha(identifier[0])) {
strcpy(error, "first character failed isalpha() check");
return 0;
}
for(a=1; identifier[a] != '\0'; a++) {
if (identifier[a]=='_') continue;
if (isalnum(identifier[a])==0) return 0;
if (identifier[a]=='_') {
continue;
}
if (isalnum(identifier[a])==0) {
strcpy(error, "one of the characters failed an isalnum() check and is not an underscore");
return 0;
}
}
for(a=0; kwlist[a]; a++) {
if (strcmp(identifier, kwlist[a]) == 0) {
strcpy(error, "this keyword is reserved by python");
return 0;
}
}
return 1;
@@ -299,8 +326,9 @@ StructRNA *RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *
PropertyRNA *prop, *propfrom;
if(DefRNA.preprocess) {
if (rna_validate_identifier(identifier) == 0) {
fprintf(stderr, "RNA_def_struct: struct identifier \"%s\" is an invalid name\n", identifier);
char error[512];
if (rna_validate_identifier(identifier, error) == 0) {
fprintf(stderr, "RNA_def_struct: struct identifier \"%s\" error - %s\n", identifier, error);
DefRNA.error= 1;
}
}
@@ -524,9 +552,10 @@ PropertyRNA *RNA_def_property(StructRNA *srna, const char *identifier, int type,
PropertyRNA *prop;
if(DefRNA.preprocess) {
char error[512];
if (rna_validate_identifier(identifier) == 0) {
fprintf(stderr, "RNA_def_property: property identifier \"%s\" is an invalid name\n", identifier);
if (rna_validate_identifier(identifier, error) == 0) {
fprintf(stderr, "RNA_def_property: property identifier \"%s\" - %s\n", identifier, error);
DefRNA.error= 1;
return NULL;
}

View File

@@ -72,11 +72,13 @@ static void rna_def_imageuser(BlenderRNA *brna)
RNA_def_property_range(prop, -MAXFRAMEF, MAXFRAMEF);
RNA_def_property_ui_text(prop, "Fields per Frame", "The number of fields per rendered frame (2 fields is 1 image).");
prop= RNA_def_property(srna, "layer", PROP_INT, PROP_UNSIGNED);
prop= RNA_def_property(srna, "renderLayer", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "layer");
RNA_def_property_flag(prop, PROP_NOT_EDITABLE); /* image_multi_cb */
RNA_def_property_ui_text(prop, "Layer", "Layer in multilayer image.");
prop= RNA_def_property(srna, "pass", PROP_INT, PROP_UNSIGNED);
prop= RNA_def_property(srna, "renderPass", PROP_INT, PROP_UNSIGNED);
RNA_def_property_int_sdna(prop, NULL, "pass");
RNA_def_property_flag(prop, PROP_NOT_EDITABLE); /* image_multi_cb */
RNA_def_property_ui_text(prop, "Pass", "Pass in multilayer image.");
}

View File

@@ -326,8 +326,9 @@ static void rna_def_key(BlenderRNA *brna)
rna_def_ipo_common(srna);
prop= RNA_def_property(srna, "from", PROP_POINTER, PROP_NONE);
RNA_def_property_ui_text(prop, "From", "Datablock using these shape keys.");
prop= RNA_def_property(srna, "user", PROP_POINTER, PROP_NONE);
RNA_def_property_pointer_sdna(prop, NULL, "from");
RNA_def_property_ui_text(prop, "User", "Datablock using these shape keys.");
prop= RNA_def_property(srna, "relative", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "type", KEY_RELATIVE);

View File

@@ -52,7 +52,7 @@ static int rna_Struct_name_length(PointerRNA *ptr)
return strlen(((StructRNA*)ptr->data)->name);
}
static void *rna_Struct_from_get(PointerRNA *ptr)
static void *rna_Struct_base_get(PointerRNA *ptr)
{
return ((StructRNA*)ptr->data)->from;
}
@@ -400,11 +400,11 @@ static void rna_def_struct(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Identifier", "Unique name used in the code and scripting.");
RNA_def_struct_name_property(srna, prop);
prop= RNA_def_property(srna, "from", PROP_POINTER, PROP_NONE);
prop= RNA_def_property(srna, "base", PROP_POINTER, PROP_NONE);
RNA_def_property_flag(prop, PROP_NOT_EDITABLE);
RNA_def_property_struct_type(prop, "Struct");
RNA_def_property_pointer_funcs(prop, "rna_Struct_from_get", NULL, NULL);
RNA_def_property_ui_text(prop, "From", "Struct definition this is derived from.");
RNA_def_property_pointer_funcs(prop, "rna_Struct_base_get", NULL, NULL);
RNA_def_property_ui_text(prop, "base", "Struct definition this is derived from.");
prop= RNA_def_property(srna, "name_property", PROP_POINTER, PROP_NONE);
RNA_def_property_flag(prop, PROP_NOT_EDITABLE);
@@ -551,12 +551,12 @@ static void rna_def_enum_property(BlenderRNA *brna, StructRNA *srna)
RNA_def_property_flag(prop, PROP_NOT_EDITABLE);
RNA_def_property_string_funcs(prop, "rna_EnumPropertyItem_name_get", "rna_EnumPropertyItem_name_length", NULL);
RNA_def_property_ui_text(prop, "Name", "Human readable name.");
RNA_def_struct_name_property(srna, prop);
prop= RNA_def_property(srna, "identifier", PROP_STRING, PROP_NONE);
RNA_def_property_flag(prop, PROP_NOT_EDITABLE);
RNA_def_property_string_funcs(prop, "rna_EnumPropertyItem_identifier_get", "rna_EnumPropertyItem_identifier_length", NULL);
RNA_def_property_ui_text(prop, "Identifier", "Unique name used in the code and scripting.");
RNA_def_struct_name_property(srna, prop);
prop= RNA_def_property(srna, "value", PROP_INT, PROP_UNSIGNED);
RNA_def_property_flag(prop, PROP_NOT_EDITABLE);