-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor/constants #39
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.8) | ||
project(frida_constants) | ||
|
||
# Find dependencies | ||
find_package(std_msgs REQUIRED) | ||
find_package(ament_cmake REQUIRED) | ||
find_package(ament_cmake_python REQUIRED) | ||
find_package(rclcpp REQUIRED) | ||
find_package(rclpy REQUIRED) | ||
|
||
ament_python_install_package(${PROJECT_NAME}) | ||
|
||
ament_package() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from enum import Enum | ||
|
||
import frida_constants.hri_constants as hri_constants | ||
from frida_constants.utils import parse_ros_config | ||
|
||
|
||
class ModuleNames(Enum): | ||
HRI = hri_constants.__name__ | ||
|
||
|
||
__all__ = [hri_constants.__name__, parse_ros_config.__name__, "ModuleNames"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
SPEAK_SERVICE = "/speech/speak" | ||
HEAR_SERVICE = "/speech/STT" | ||
KEYWORD_TOPIC = "/speech/kws" | ||
COMMAND_INTERPRETER_SERVICE = "/nlp/command_interpreter" | ||
DATA_EXTRACTOR_SERVICE = "/nlp/data_extractor" | ||
SENTENCE_BUILDER_SERVICE = "/nlp/sentence_builder" | ||
ITEM_CATEGORIZATION_SERVICE = "/nlp/item_categorization" | ||
CONVESATION_SERVICE = "/nlp/conversation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import importlib | ||
import inspect | ||
|
||
import yaml | ||
|
||
|
||
def get_constants_from_module(module_name): | ||
"""Get all constants from a module into a dict""" | ||
module = importlib.import_module(module_name) | ||
|
||
constants = { | ||
name: value | ||
for name, value in inspect.getmembers(module) | ||
if not name.startswith("__") | ||
and not inspect.ismodule(value) | ||
and not inspect.isfunction(value) | ||
} | ||
return constants | ||
|
||
|
||
# Contains all constants from all modules | ||
all_constants = {} | ||
|
||
|
||
# Get a constant value from a module | ||
def get_constant(constant_name: str, module_names: list[str]): | ||
for module_name in module_names: | ||
if constant_name in all_constants[module_name]: | ||
return all_constants[module_name][constant_name] | ||
|
||
raise ValueError(f"Constant {constant_name} not found in any of the modules.") | ||
|
||
|
||
def process_dict_config(config_object, module_names: list[str]): | ||
"""Recursively replace all "REPLACE" strings with the corresponding constant value""" | ||
for key, value in config_object.items(): | ||
if isinstance(value, dict): | ||
process_dict_config(value, module_names) | ||
elif isinstance(value, str): | ||
if value == "REPLACE": | ||
config_object[key] = get_constant(key, module_names) | ||
|
||
return config_object | ||
|
||
|
||
def parse_ros_config(config_path: str, module_names: list[str]): | ||
"""Process a ROS-formatted yaml config file, replacing all "REPLACE" strings with the corresponding constant value""" | ||
for module_name in module_names: | ||
if module_name not in all_constants: | ||
all_constants[module_name] = get_constants_from_module(module_name) | ||
|
||
with open(config_path, "r") as file: | ||
config = yaml.safe_load(file) | ||
|
||
return process_dict_config(config, module_names) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<package format="3"> | ||
<name>frida_constants</name> | ||
<version>0.1.0</version> | ||
<description>Package containing shared constants and utilities.</description> | ||
<maintainer email="[email protected]">RoBorregos</maintainer> | ||
<license>Apache-2.0</license> | ||
|
||
<!-- Build dependencies --> | ||
<buildtool_depend>ament_cmake</buildtool_depend> | ||
<buildtool_depend>ament_cmake_python</buildtool_depend> | ||
|
||
<depend>rclcpp</depend> | ||
<depend>rclpy</depend> | ||
|
||
<export> | ||
<build_type>ament_cmake</build_type> | ||
</export> | ||
</package> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
im not getting why are we changing this??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yaml config has keys with values "REPLACE".
parse_ros_config
updates such values, using the constants defined inModuleNames.HRI.value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lmao, seems like a lot isnt it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to have all in one place, why should we keep the input name in the config?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but seems ok either way, this also seems pretty elegant, I dont dislike it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feel free to close this, just wanted to know why
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so that we know which values to insert / which values are being replaced?