Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ros2 #40

Merged
merged 28 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ea6f107
don't warn about missing findpackage
nathanhhughes Dec 2, 2023
f3f63ee
(wip) start on ros2 interface
nathanhhughes Dec 2, 2023
19fe974
add new launch extension package
nathanhhughes Jul 10, 2024
3a3c156
drop broken unit test install
nathanhhughes Jul 10, 2024
2a3ac3d
switch config tag format and work on parsing correctly
nathanhhughes Nov 30, 2024
a6e389d
work on performing substitutions on parsed param fields
nathanhhughes Dec 2, 2024
a74c0cb
allow optional new YAML merging behavior
nathanhhughes Dec 23, 2024
832edfa
(wip) start on context
nathanhhughes Jan 2, 2025
803ea39
outline basic commandline infrastructure
nathanhhughes Jan 2, 2025
c8aaf9b
remove rclcpp and add context command line function
nathanhhughes Jan 3, 2025
0242800
fix linting warnings and add more coverage for external registry
nathanhhughes Jan 3, 2025
ebda7b3
split parsing and utils tests
nathanhhughes Jan 3, 2025
189f53b
forward extend_sequence arg and add merge tests
nathanhhughes Jan 7, 2025
018e47b
add additional yaml parsing test coverage
nathanhhughes Jan 8, 2025
921f14f
fix broken test and make merge test more complicated
nathanhhughes Jan 8, 2025
0e05454
(wip) initial implementation of parsing
nathanhhughes Jan 9, 2025
922a9e4
condense files and allow for multiple tokens
nathanhhughes Jan 9, 2025
b433de0
implement custom parser because ros can't escape correctly
nathanhhughes Jan 9, 2025
f00a2c8
drop debug print
nathanhhughes Jan 9, 2025
355e6bb
add ability to namespace files
nathanhhughes Jan 9, 2025
f00edbc
drop launch extension
nathanhhughes Jan 10, 2025
d640177
add command line tests and fix application order
nathanhhughes Jan 13, 2025
a58abd7
fix function name and add cli docs
nathanhhughes Jan 13, 2025
1e9db51
mention multiple flags
nathanhhughes Jan 13, 2025
f542cb7
add full api to context plus quick documentation
nathanhhughes Jan 13, 2025
3f044fd
add doc links
nathanhhughes Jan 13, 2025
eb34098
remove logging TODOs
nathanhhughes Jan 22, 2025
e49a47d
log to stderr for errors and warnings by default
nathanhhughes Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


} // 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
Loading