Skip to content

Commit

Permalink
Feature/ros2 (#40)
Browse files Browse the repository at this point in the history
* don't warn about missing findpackage

* (wip) start on ros2 interface

* add new launch extension package

* drop broken unit test install

* switch config tag format and work on parsing correctly

* work on performing substitutions on parsed param fields

* allow optional new YAML merging behavior

* (wip) start on context

* outline basic commandline infrastructure

* remove rclcpp and add context command line function

* fix linting warnings and add more coverage for external registry

* split parsing and utils tests

* forward extend_sequence arg and add merge tests

* add additional yaml parsing test coverage

* fix broken test and make merge test more complicated

* (wip) initial implementation of parsing

* condense files and allow for multiple tokens

* implement custom parser because ros can't escape correctly

* drop debug print

* add ability to namespace files

* drop launch extension

* add command line tests and fix application order

* fix function name and add cli docs

* mention multiple flags

* add full api to context plus quick documentation

* add doc links

* remove logging TODOs

* log to stderr for errors and warnings by default
  • Loading branch information
nathanhhughes authored Jan 22, 2025
1 parent 09f2d73 commit b210eb1
Show file tree
Hide file tree
Showing 29 changed files with 1,381 additions and 115 deletions.
5 changes: 4 additions & 1 deletion config_utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ option(CONFIG_UTILS_ENABLE_ROS "Export roscpp and build related code" ON)
option(CONFIG_UTILS_ENABLE_EIGEN "Export Eigen and build related code" ON)
option(CONFIG_UTILS_ENABLE_GLOG "Export glog and build related code" ON)
option(CONFIG_UTILS_BUILD_TESTS "Build unit tests" ON)
option(CONFIG_UTILS_INSTALL_TESTS "Install test executable" ON)
option(CONFIG_UTILS_BUILD_DEMOS "Build demo executables" ON)

find_package(yaml-cpp REQUIRED)
Expand All @@ -26,6 +25,9 @@ find_optional_pkgcfg(libglog CONFIG_UTILS_ENABLE_GLOG)
add_library(
${PROJECT_NAME}
src/asl_formatter.cpp
src/commandline.cpp
src/config_context.cpp # global singleton
src/context.cpp # parsing
src/conversions.cpp
src/external_registry.cpp
src/factory.cpp
Expand Down Expand Up @@ -74,6 +76,7 @@ if(CONFIG_UTILS_BUILD_DEMOS)
endif()

if(CONFIG_UTILS_BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory(test)
endif()
Expand Down
2 changes: 1 addition & 1 deletion config_utilities/cmake/OptionalPackage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# not the package should be used
macro(FIND_OPTIONAL package_name package_enabled)
if(${package_enabled})
find_package(${package_name})
find_package(${package_name} QUIET)
endif()

if(${${package_name}_FOUND})
Expand Down
10 changes: 7 additions & 3 deletions config_utilities/cmake/config_utilitiesConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@PACKAGE_INIT@

get_filename_component(config_utilities_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(config_utilities_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}"
PATH)
include(CMakeFindDependencyMacro)

find_dependency(yaml-cpp REQUIRED)
Expand All @@ -11,6 +12,11 @@ endif()

if(@ENABLE_roscpp@)
find_dependency(roscpp REQUIRED)
set(config_utilities_FOUND_CATKIN_PROJECT TRUE)
endif()

if(@ENABLE_rclcpp@)
find_dependency(rclcpp REQUIRED)
endif()

if(@ENABLE_libglog@)
Expand All @@ -24,6 +30,4 @@ endif()

set(config_utilities_LIBRARIES config_utilities::config_utilities)

set(config_utilities_FOUND_CATKIN_PROJECT TRUE)

check_required_components(config_utilities)
11 changes: 4 additions & 7 deletions config_utilities/include/config_utilities/internal/checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#pragma once

#include <algorithm>
#include <functional>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -82,7 +83,7 @@ struct CompareMessageTrait {
template <typename T, typename Compare>
class BinaryCheck : public CheckBase {
public:
BinaryCheck(const T& param, const T& value, const std::string name = "")
BinaryCheck(const T& param, const T& value, const std::string& name = "")
: param_(param), value_(value), name_(name) {}

bool valid() const override { return Compare{}(param_, value_); }
Expand Down Expand Up @@ -186,12 +187,8 @@ class CheckIsOneOf : public CheckBase {
: param_(param), candidates_(candidates), name_(name) {}

bool valid() const override {
for (const T& cadidate : candidates_) {
if (param_ == cadidate) {
return true;
}
}
return false;
// check that param matches any candidate
return std::any_of(candidates_.begin(), candidates_.end(), [this](const auto& c) { return c == param_; });
}

std::string message() const override {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/** -----------------------------------------------------------------------------
* Copyright (c) 2023 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* AUTHORS: Lukas Schmid <[email protected]>, Nathan Hughes <[email protected]>
* AFFILIATION: MIT-SPARK Lab, Massachusetts Institute of Technology
* YEAR: 2023
* LICENSE: BSD 3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* -------------------------------------------------------------------------- */

#pragma once

#include <string>

#include <yaml-cpp/yaml.h>

#include "config_utilities/factory.h"
#include "config_utilities/internal/visitor.h"

namespace config::internal {

/**
* @brief Context is a singleton that holds the raw parsed information used to generate configs
*/
class Context {
public:
~Context() = default;

static void update(const YAML::Node& other, const std::string& ns);

static void clear();

static YAML::Node toYaml();

template <typename BaseT, typename... ConstructorArguments>
static std::unique_ptr<BaseT> create(ConstructorArguments... args) {
return internal::ObjectWithConfigFactory<BaseT, ConstructorArguments...>::create(instance().contents_, args...);
}

template <typename BaseT, typename... ConstructorArguments>
static std::unique_ptr<BaseT> createNamespaced(const std::string& name_space, ConstructorArguments... args) {
const auto ns_node = internal::lookupNamespace(instance().contents_, name_space);
return internal::ObjectWithConfigFactory<BaseT, ConstructorArguments...>::create(ns_node, args...);
}

template <typename ConfigT>
static ConfigT loadConfig(const std::string& name_space = "") {
ConfigT config;
internal::Visitor::setValues(config, internal::lookupNamespace(instance().contents_, name_space), true);
return config;
}

private:
Context() = default;
static Context& instance();

YAML::Node contents_;
};

} // namespace config::internal
4 changes: 2 additions & 2 deletions config_utilities/include/config_utilities/internal/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

namespace config::internal {

// Enum for different severity levels of logging.
enum class Severity { kInfo, kWarning, kError, kFatal };
// Enum for different severity levels of logging. Enum values are used for comparing logging levels.
enum class Severity { kInfo = 0, kWarning = 1, kError = 2, kFatal = 3 };

std::string severityToString(const Severity severity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ namespace config::internal {

/**
* @brief Merges node b into a, overwriting values previously defined in a if they can not be
* merged. Modifies node a, whereas b is const.
* merged. Modifies node a, whereas b is const. Sequences can optionally be appended together at the same level of the
* YAML tree.
*/
void mergeYamlNodes(YAML::Node& a, const YAML::Node& b);
void mergeYamlNodes(YAML::Node& a, const YAML::Node& b, bool extend_sequences = false);

/**
* @brief Get a pointer to the final node of the specified namespace if it exists, where each map in the yaml is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ namespace config::internal {
*/
class StdoutLogger : public Logger {
public:
StdoutLogger() = default;
/**
* @brief Construct a logger to output to stdout or stderr depending on configuration
* @param min_severity Mininum severity to output
* @param stderr_severity Mininum severity to log to stderr instead of stdout
*/
StdoutLogger(Severity min_severity = Severity::kWarning, Severity stderr_severity = Severity::kError);

virtual ~StdoutLogger() = default;

protected:
void logImpl(const Severity severity, const std::string& message) override;

private:
const Severity min_severity_;
const Severity stderr_severity_;
// Factory registration to allow setting of formatters via Settings::setLogger().
inline static const auto registration_ = Registration<Logger, StdoutLogger>("stdout");

Expand Down
121 changes: 121 additions & 0 deletions config_utilities/include/config_utilities/parsing/commandline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/** -----------------------------------------------------------------------------
* Copyright (c) 2023 Massachusetts Institute of Technology.
* All Rights Reserved.
*
* AUTHORS: Lukas Schmid <[email protected]>, Nathan Hughes <[email protected]>
* AFFILIATION: MIT-SPARK Lab, Massachusetts Institute of Technology
* YEAR: 2023
* LICENSE: BSD 3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* -------------------------------------------------------------------------- */

#pragma once

#include <memory>
#include <string>

#include "config_utilities/factory.h"
#include "config_utilities/internal/visitor.h"
#include "config_utilities/internal/yaml_utils.h"

namespace config {
namespace internal {

/**
* @brief Parse and collate YAML node from arguments, optionally removing arguments
* @param argc Number of command line arguments
* @param argv Command line argument strings
*/
YAML::Node loadFromArguments(int& argc, char* argv[], bool remove_args);

} // namespace internal

/**
* @brief Loads a config based on collated YAML data specified via the command line
*
* See fromYaml() for more specific behavioral information.
*
* @tparam ConfigT The config type. This can also be a VirtualConfig<BaseT> or a std::vector<ConfigT>.
* @param argc Number of arguments.
* @param argv Actual command line arguments.
* @param name_space Optional namespace to use under the resolved YAML parameter tree.
* @returns The config.
*/
template <typename ConfigT>
ConfigT fromCLI(int argc, char* argv[], const std::string& name_space = "") {
// when parsing CLI locally we don't want to modify the arguments ever
const auto node = internal::loadFromArguments(argc, argv, false);

ConfigT config;
internal::Visitor::setValues(config, internal::lookupNamespace(node, name_space), true);
return config;
}

/**
* @brief Create a derived type object based on collated YAML data specified via the command line
*
* See createFromYaml() for more specific behavioral information.
*
* @tparam BaseT Type of the base class to be constructed.
* @tparam Args Other constructor arguments.
* @param argc Number of arguments.
* @param argv Actual command line arguments.
* @param args Other constructor arguments.
* @returns Unique pointer of type base that contains the derived object.
*/
template <typename BaseT, typename... ConstructorArguments>
std::unique_ptr<BaseT> createFromCLI(int argc, char* argv[], ConstructorArguments... args) {
// when parsing CLI locally we don't want to modify the arguments ever
const auto node = internal::loadFromArguments(argc, argv, false);
return internal::ObjectWithConfigFactory<BaseT, ConstructorArguments...>::create(node, args...);
}

/**
* @brief Create a derived type object based on collated YAML data specified via the command line
*
* See createFromYamlWithNamespace() for more specific behavioral information.
*
* @tparam BaseT Type of the base class to be constructed.
* @tparam Args Other constructor arguments.
* @param argc Number of arguments.
* @param argv Actual command line arguments.
* @param name_space Optionally specify a name space to create the object from.
* @param args Other constructor arguments.
* @returns Unique pointer of type base that contains the derived object.
*/
template <typename BaseT, typename... ConstructorArguments>
std::unique_ptr<BaseT> createFromCLIWithNamespace(int argc,
char* argv[],
const std::string& name_space,
ConstructorArguments... args) {
// when parsing CLI locally we don't want to modify the arguments ever
const auto node = internal::loadFromArguments(argc, argv, false);
const auto ns_node = internal::lookupNamespace(node, name_space);
return internal::ObjectWithConfigFactory<BaseT, ConstructorArguments...>::create(ns_node, args...);
}

} // namespace config
Loading

0 comments on commit b210eb1

Please sign in to comment.