Skip to content

Commit

Permalink
Add .enums dictionary to MessageDatabase
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed Oct 27, 2024
1 parent 9dc874a commit fc8fa7f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
3 changes: 2 additions & 1 deletion python/bindings/message_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,6 @@ void init_common_message_database(nb::module_& m)
.def("get_msg_def", nb::overload_cast<const std::string&>(&PyMessageDatabase::GetMsgDef, nb::const_), "msg_name"_a)
.def("get_msg_def", nb::overload_cast<int32_t>(&PyMessageDatabase::GetMsgDef, nb::const_), "msg_id"_a)
.def("get_enum_def", &PyMessageDatabase::GetEnumDefId, "enum_id"_a)
.def("get_enum_def", &PyMessageDatabase::GetEnumDefName, "enum_name"_a);
.def("get_enum_def", &PyMessageDatabase::GetEnumDefName, "enum_name"_a)
.def_prop_ro("enums", &PyMessageDatabase::GetEnumsByNameDict);
}
4 changes: 2 additions & 2 deletions python/bindings/message_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ nb::object convert_field(const FieldContainer& field, const PyMessageDatabase::C
if (field.fieldDef->type == FIELD_TYPE::ENUM)
{
const std::string& enumId = static_cast<const EnumField*>(field.fieldDef.get())->enumId;
auto it = parent_db->GetEnumsByIdMap().find(enumId);
if (it == parent_db->GetEnumsByIdMap().end())
auto it = parent_db->GetEnumsByIdDict().find(enumId);
if (it == parent_db->GetEnumsByIdDict().end())
{
throw std::runtime_error("Enum definition for " + field.fieldDef->name + " field with ID '" + enumId +
"' not found in the JSON database");
Expand Down
7 changes: 3 additions & 4 deletions python/bindings/oem_enums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ using namespace novatel::edie;

void init_novatel_oem_enums(nb::module_& m)
{
nb::module_ enums_mod = m.def_submodule("enums", "");
for (const auto& [_, enum_type] : MessageDbSingleton::get()->GetEnumsByIdMap())
nb::module_ enums_mod = m.def_submodule("enums", "Enumerations used by NovAtel OEM message fields.");
for (const auto& [name, enum_type] : MessageDbSingleton::get()->GetEnumsByNameDict()) //
{
nb::object name = enum_type.attr("_name");
enums_mod.attr(name) = enum_type;
enums_mod.attr(name.c_str()) = enum_type;
}
}
7 changes: 6 additions & 1 deletion python/bindings/py_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class PyMessageDatabase final : public MessageDatabase

explicit PyMessageDatabase(const MessageDatabase& message_db) : MessageDatabase(message_db) { UpdatePythonEnums(); }

[[nodiscard]] const std::unordered_map<std::string, nb::object>& GetEnumsByIdMap() const { return enums_by_id; }
[[nodiscard]] const std::unordered_map<std::string, nb::object>& GetEnumsByIdDict() const { return enums_by_id; }

[[nodiscard]] const std::unordered_map<std::string, nb::object>& GetEnumsByNameDict() const { return enums_by_name; }

private:
void GenerateMappings() override
Expand All @@ -33,6 +35,7 @@ class PyMessageDatabase final : public MessageDatabase
{
nb::object IntEnum = nb::module_::import_("enum").attr("IntEnum");
enums_by_id.clear();
enums_by_name.clear();
for (const auto& enum_def : EnumDefinitions())
{
nb::dict values;
Expand All @@ -42,10 +45,12 @@ class PyMessageDatabase final : public MessageDatabase
enum_type.attr("_name") = enum_name;
enum_type.attr("_id") = enum_def->_id;
enums_by_id[enum_def->_id.c_str()] = enum_type;
enums_by_name[enum_name] = enum_type;
}
}

std::unordered_map<std::string, nb::object> enums_by_id{};
std::unordered_map<std::string, nb::object> enums_by_name{};

public:
using Ptr = std::shared_ptr<PyMessageDatabase>;
Expand Down
31 changes: 31 additions & 0 deletions python/test/test_message_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
################################################################################
#
# COPYRIGHT NovAtel Inc, 2024. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
################################################################################=

import novatel_edie as ne


def test_message_db_enums(json_db):
assert json_db.enums["Datum"].WGS84 == 61
assert json_db.enums["Datum"].WGS84.name == "WGS84"
assert json_db.enums["Datum"].WGS84 == ne.enums.Datum.WGS84

0 comments on commit fc8fa7f

Please sign in to comment.