Skip to content

Commit

Permalink
Config: Change syntax and updates all configs (#1730)
Browse files Browse the repository at this point in the history
Change config file syntax to:

```
[
{
  "options": 
  {
    "filename": true,
    "camera-direction": "-1,-0.5,-1",
    "hdri-ambient": true,
    "translucency-support": true,
    "animation-progress": true
  }
},
{
  "match": ".*obj",
  "options": 
  {
    "axis": true,
    "tone-mapping": true
  }
}
]
```

also adds a test and updates doc
  • Loading branch information
mwestphal authored Nov 28, 2024
1 parent e2ac4dd commit c3eb2e1
Show file tree
Hide file tree
Showing 37 changed files with 336 additions and 121 deletions.
72 changes: 56 additions & 16 deletions application/F3DConfigFileTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,79 @@ F3DOptionsTools::OptionsEntries F3DConfigFileTools::ReadConfigFiles(const std::s
{
file >> json;
}
catch (const std::exception& ex)
catch (const nlohmann::json::parse_error& ex)
{
f3d::log::error(
"Unable to parse the configuration file ", configFilePath.string(), " , ignoring it");
f3d::log::error(ex.what());
continue;
}

// For each config "pattern"
for (const auto& configBlock : json.items())
try
{
// Add each config entry into an option dict
F3DOptionsTools::OptionsDict entry;
for (const auto& item : configBlock.value().items())
// For each config block in the main array
for (const auto& configBlock : json)
{
if (item.value().is_number() || item.value().is_boolean())
// Recover match if any
std::string match;
try
{
match = configBlock.at("match");
}
catch (nlohmann::json::out_of_range&)
{
// No match defined, use a catch all regex
match = ".*";
}

// Recover options if any
nlohmann::ordered_json optionsBlock;
try
{
optionsBlock = configBlock.at("options");
}
catch (nlohmann::json::out_of_range&)
{
entry[item.key()] = nlohmann::to_string(item.value());
}
else if (item.value().is_string())

if (optionsBlock.empty())
{
entry[item.key()] = item.value().get<std::string>();
// To help users figure out issues with configuration files
f3d::log::warn("A config block in config file: ", configFilePath.string(),
" does not contains options, ignoring block");
}
else
{
f3d::log::error(item.key(), " from ", configFilePath.string(),
" must be a string, a boolean or a number, ignoring entry");
continue;
// Add each options config entry into an option dict
F3DOptionsTools::OptionsDict entry;
for (const auto& item : optionsBlock.items())
{
if (item.value().is_number() || item.value().is_boolean())
{
entry[item.key()] = nlohmann::to_string(item.value());
}
else if (item.value().is_string())
{
entry[item.key()] = item.value().get<std::string>();
}
else
{
f3d::log::error(item.key(), " from ", configFilePath.string(),
" must be a string, a boolean or a number, ignoring entry");
continue;
}
}

// Emplace the option dict for that pattern match into the config entries vector
confEntries.emplace_back(entry, configFilePath, match);
}
}

// Emplace the option dict for that pattern into the config entries vector
confEntries.emplace_back(entry, configFilePath, configBlock.key());
}
catch (const nlohmann::json::type_error& ex)
{
f3d::log::warn("Error processing config file: ", configFilePath.string(),
", configuration may be incorrect");
f3d::log::error(ex.what());
}
}
return confEntries;
Expand Down
6 changes: 6 additions & 0 deletions application/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,12 @@ f3d_test(NAME TestInvalidMultiFileMode DATA mb/recursive ARGS --multi-file-mode=
f3d_test(NAME TestVerboseUnnamedCamera DATA Cameras.gltf ARGS --verbose REGEXP "1: unnamed_1" NO_BASELINE)
f3d_test(NAME TestVerboseUnnamedAnimation DATA BoxAnimated.gltf ARGS --verbose REGEXP "0: unnamed_0" NO_BASELINE)

# Test invalid value in config file
f3d_test(NAME TestConfigFileInvalidOptions DATA cow.vtp CONFIG ${F3D_SOURCE_DIR}/testing/configs/invalid_options.json REGEXP "Error processing config file" NO_BASELINE)

# Test invalid value in config file
f3d_test(NAME TestConfigFileNoOptions DATA cow.vtp CONFIG ${F3D_SOURCE_DIR}/testing/configs/no_options.json REGEXP "does not contains options" NO_BASELINE)

# Test invalid value in config file
f3d_test(NAME TestConfigFileInvalidValue DATA cow.vtp CONFIG ${F3D_SOURCE_DIR}/testing/configs/invalid_value.json REGEXP "must be a string, a boolean or a number" NO_BASELINE)

Expand Down
63 changes: 39 additions & 24 deletions doc/user/CONFIGURATION_FILE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,52 @@ in order to provide different default values for the different filetypes.

Using a command-line option will override similar option set in any config files.

Some options are only taken into account on the first load and not on subsequent loads,
Some options are only taken into account on the first load and not on subsequent loads,
when switching between files.

A typical config file may look like this:

```javascript
[
{
".*": {
"bg-color": "0.7,0.7,0.7",
"color": "0.5,0.1,0.1",
"anti-aliasing": true,
"timer": true,
"progress": true,
"axis": true,
"bar": true,
"roughness": 0.2,
"grid": true,
"scalar-coloring": true
},
".*vt.": {
"edges": true
},
".*gl(tf|b)": {
"raytracing": true,
"denoise": true,
"samples": 3
},
".*mhd": {
"volume": true
}
"options":
{
"bg-color": "0.7,0.7,0.7",
"color": "0.5,0.1,0.1",
"anti-aliasing": true,
"timer": true,
"progress": true,
"axis": true,
"bar": true,
"roughness": 0.2,
"grid": true,
"scalar-coloring": true
}
},
{
"match": ".*vt.",
"options":
{
"edges": true
}
},
{
"match": ".*gl(tf|b)",
"options":
{
"raytracing": true,
"denoise": true,
"samples": 3
}
},
{
"match": ".*mhd",
"options":
{
"volume": true
}
}
]
```
Here, the first block defines a basic configuration with many desired options for all files.
The second block specifies that all files ending with vt., eg: vtk, vtp, vtu, ... will be shown with edges visibility turned on.
Expand Down
10 changes: 7 additions & 3 deletions doc/user/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ Alternatively, you can add your plugin directly in the
multiple plugins in a single comma-separated list, like in the example below:

```
[
{
".*(file_extension)": {
"load-plugins": "plugin1", "plugin2"
}
"match": ".*(file_extension)",
"options":
{
"load-plugins": "plugin1", "plugin2"
}
}
]
```

### Supported plugins
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[
{
".*(expl)":
"match": ".*(expl)",
"options":
{
"load-plugins": "example"
}
}
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[
{
".*(expl)":
"match": ".*(expl)",
"options":
{
"load-plugins": "example"
}
}
]
5 changes: 4 additions & 1 deletion plugins/alembic/configs/config.d/10_alembic.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
".*(abc)":
"match": ".*(abc)",
"options":
{
"scalar-coloring": true,
"load-plugins": "alembic"
}
}
]
5 changes: 4 additions & 1 deletion plugins/alembic/configs/thumbnail.d/10_alembic.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
".*(abc)":
"match": ".*(abc)",
"options":
{
"scalar-coloring": true,
"load-plugins": "alembic"
}
}
]
19 changes: 14 additions & 5 deletions plugins/assimp/configs/config.d/10_assimp.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
[
{
".*(fbx|dae|off|dxf|x|3mf)":
"match": ".*(fbx|dae|off|dxf|x|3mf)",
"options":
{
"load-plugins": "assimp"
},
".*(off|3mf)":
}
},
{
"match": ".*(off|3mf)",
"options":
{
"up": "+Z",
"camera-direction": "-1,1,-0.5"
},
".*(dxf)":
}
},
{
"match": ".*(dxf)",
"options":
{
"up": "-Z",
"camera-direction": "1,-1,0.5"
}
}
]
19 changes: 14 additions & 5 deletions plugins/assimp/configs/thumbnail.d/10_assimp.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
[
{
".*(fbx|dae|off|dxf|x|3mf)":
"match": ".*(fbx|dae|off|dxf|x|3mf)",
"options":
{
"load-plugins": "assimp"
},
".*(off|3mf)":
}
},
{
"match": ".*(off|3mf)",
"options":
{
"up": "+Z",
"camera-direction": "-1,1,-0.5"
},
".*(dxf)":
}
},
{
"match": ".*(dxf)",
"options":
{
"up": "-Z",
"camera-direction": "1,-1,0.5"
}
}
]
5 changes: 4 additions & 1 deletion plugins/draco/configs/config.d/10_draco.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
".*(drc)":
"match": ".*(drc)",
"options":
{
"scalar-coloring": true,
"load-plugins": "draco"
}
}
]
5 changes: 4 additions & 1 deletion plugins/draco/configs/thumbnail.d/10_draco.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
".*(drc)":
"match": ".*(drc)",
"options":
{
"scalar-coloring": true,
"load-plugins": "draco"
}
}
]
5 changes: 4 additions & 1 deletion plugins/exodus/configs/config.d/10_exodus.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
[
{
".*(ex2)":
"match": ".*(ex2)",
"options":
{
"scalar-coloring": true,
"load-plugins": "exodus",
"bar": true
}
}
]
5 changes: 4 additions & 1 deletion plugins/exodus/configs/thumbnail.d/10_exodus.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[
{
".*(ex2)":
"match": ".*(ex2)",
"options":
{
"scalar-coloring": true,
"load-plugins": "exodus"
}
}
]
Loading

0 comments on commit c3eb2e1

Please sign in to comment.