From a4613f535f348f4caff14cb9066da899b53a04d8 Mon Sep 17 00:00:00 2001 From: Mitchell Stokes Date: Thu, 30 Apr 2020 22:22:03 -0700 Subject: [PATCH] config: Allow a single equal sign (=) between variable names and values This allows for more INI-style syntax as proposed by #523. Sections are still not supported. --- dtool/src/prc/configPage.cxx | 12 ++++++++++-- tests/prc/test_config_page.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/dtool/src/prc/configPage.cxx b/dtool/src/prc/configPage.cxx index af53f661228..bc5d5c4d0d4 100644 --- a/dtool/src/prc/configPage.cxx +++ b/dtool/src/prc/configPage.cxx @@ -397,7 +397,7 @@ read_prc_line(const string &line) { EVP_VerifyUpdate((EVP_MD_CTX *)_md_ctx, line.data(), line.size()); #endif // HAVE_OPENSSL - // Separate the line into a variable and a value. + // Parse leading whitespace size_t p = 0; while (p < line.length() && isspace((unsigned char)line[p])) { p++; @@ -408,8 +408,9 @@ read_prc_line(const string &line) { return; } + // Separate the line into a variable and a value (ASCII 61 is "=") size_t variable_begin = p; - while (p < line.length() && !isspace((unsigned char)line[p])) { + while (p < line.length() && !(isspace((unsigned char)line[p]) || line[p] == 61)) { p++; } size_t variable_end = p; @@ -417,6 +418,13 @@ read_prc_line(const string &line) { while (p < line.length() && isspace((unsigned char)line[p])) { p++; } + if (line[p] == 61) { + // if we stopped on an =, consume it and keep grabbing whitespace + p++; + while (p < line.length() && isspace((unsigned char)line[p])) { + p++; + } + } size_t value_begin = p; // Is there an embedded comment on this line? diff --git a/tests/prc/test_config_page.py b/tests/prc/test_config_page.py index 5ca6cdb6ef6..aa0e51144a2 100644 --- a/tests/prc/test_config_page.py +++ b/tests/prc/test_config_page.py @@ -10,3 +10,19 @@ def test_load_unload_page(): assert core.unload_prc_file(page) assert var.value == 1 + +def test_ini_syntax(): + page = core.load_prc_file_data("test_ini_syntax", "test-var=true\ntest-var2 = foo") + assert page + + import panda3d + print(panda3d.__path__) + + for decl in page.declarations: + print(decl.variable) + + var = core.ConfigVariableBool("test-var", False) + assert var.value == True + + var = core.ConfigVariableString("test-var2", "") + assert var.value == "foo"