HOPS
HOPS class reference
MHO_PyContainerStoreInterface.hh
Go to the documentation of this file.
1 #ifndef MHO_PyContainerStoreInterface_HH__
2 #define MHO_PyContainerStoreInterface_HH__
3 
6 
7 #include "MHO_ContainerStore.hh"
9 
10 //need these extras to be able to translate between nl:json and py:dict or py::object
11 #include "pybind11_json/pybind11_json.hpp"
12 #include <pybind11/embed.h>
13 #include <pybind11/pybind11.h>
14 namespace py = pybind11;
15 namespace nl = nlohmann;
16 using namespace pybind11::literals;
17 
18 namespace hops
19 {
20 
30 {
31  public:
32  MHO_PyContainerStoreInterface(): fContainerStore(nullptr){}; //dummy constructor
33  MHO_PyContainerStoreInterface(MHO_ContainerStore* conStore): fContainerStore(conStore){};
35 
36  bool IsValid()
37  {
38  if(fContainerStore != nullptr)
39  {
40  return true;
41  }
42  return false;
43  }
44 
45  std::size_t GetNObjects()
46  {
47  if(fContainerStore)
48  {
49  return fContainerStore->GetNObjects();
50  }
51  return 0;
52  }
53 
54  bool IsObjectPresent(const std::string& uuid_string) const
55  {
56  if(fContainerStore != nullptr)
57  {
58  MHO_UUID uuid;
59  bool ok = uuid.from_string(uuid_string);
60  if(!ok)
61  {
62  msg_error("python_bindings", "error could not convert: " << uuid_string << " to valid UUID" << eom);
63  }
64  return fContainerStore->IsObjectPresent(uuid);
65  }
66  return false;
67  }
68 
69  std::string GetObjectTypeUUID(const std::string& uuid_string) const
70  {
71  MHO_UUID type_uuid;
72  if(fContainerStore != nullptr)
73  {
74  MHO_UUID uuid;
75  bool ok = uuid.from_string(uuid_string);
76  type_uuid = fContainerStore->GetObjectTypeUUID(uuid);
77  }
78  return type_uuid.as_string(); //empyt if uuid was invalid
79  }
80 
81  template< typename XClassType > MHO_PyTableContainer< XClassType >* GetObject(const std::string& uuid_string)
82  {
83  MHO_UUID uuid;
84  bool ok = uuid.from_string(uuid_string);
85  if(!ok)
86  {
87  msg_error("python_bindings", "error could not convert: " << uuid_string << " to valid UUID" << eom);
88  }
89  XClassType* obj = fContainerStore->GetObject< XClassType >(uuid);
90  auto ext_ptr = GetExtension(obj);
91  if(ext_ptr != nullptr)
92  {
93  return ext_ptr;
94  }
95  else
96  {
97  msg_error("python_bindings", "object with uuid: " << uuid_string << " is either not present, or is not a: "<< MHO_ClassIdentity::ClassName<XClassType>() << eom);
98  return nullptr;
99  }
100  }
101 
102  //return a list of (UUID <-> typename <-> shortname) tuples
103  py::list GetObjectList()
104  {
105  mho_json info_obj;
106  std::vector< std::tuple< std::string, std::string, std::string > > info = fContainerStore->GetAllObjectInfo();
107  for(std::size_t i = 0; i < info.size(); i++)
108  {
109  mho_json item_info;
110  item_info["type_uuid"] = std::get< 0 >(info[i]);
111  item_info["object_uuid"] = std::get< 1 >(info[i]);
112  item_info["shortname"] = std::get< 2 >(info[i]);
113  info_obj.push_back(item_info);
114  }
115  py::list ret_obj = info_obj;
116  return ret_obj;
117  };
118 
119  //publically EXPOSED for C++ lambda's
120  //DO NOT EXPOSE THIS CLASS TO PYTHON
121  MHO_ContainerStore* GetContainerStore() { return fContainerStore; }
122 
123  private:
124  //put the extension building/retrieval in this template
125  template< typename XClassType > MHO_PyTableContainer< XClassType >* GetExtension(XClassType* obj)
126  {
127  if(obj != nullptr)
128  {
129  if(obj->template HasExtension< MHO_PyTableContainer< XClassType > >())
130  {
131  return obj->template AsExtension< MHO_PyTableContainer< XClassType > >();
132  }
133  else
134  {
135  return obj->template MakeExtension< MHO_PyTableContainer< XClassType > >();
136  }
137  }
138  return nullptr;
139  }
140 
141  //pointer to the parameter store
142  MHO_ContainerStore* fContainerStore;
143 };
144 
145 inline void DeclarePyContainerStoreInterface(py::module& m, std::string pyclass_name)
146 {
147  py::class_< MHO_PyContainerStoreInterface >(m, pyclass_name.c_str())
148  //no __init__ def here, as this class is not meant to be constructable on the python side
150  "check if the underlying container store object exists and is valid")
152  "get the number of objects present in the container store")
153  .def("is_object_present", &hops::MHO_PyContainerStoreInterface::IsObjectPresent,
154  "check if an object with the passed UUID is present", py::arg("uuid"))
155  .def("get_object_id_list", &hops::MHO_PyContainerStoreInterface::GetObjectList,
156  "get a list of the object information (list of dict's containing each object's type_uuid, object_uuid, and "
157  "shortname")
158  .def(
159  "get_object", //lambda for returing either object data or none type
160  [=](MHO_PyContainerStoreInterface& m, std::string object_uuid) -> py::object {
161  MHO_UUID uuid;
162  bool is_valid = uuid.from_string(object_uuid);
163  MHO_ContainerStore* cStore = m.GetContainerStore();
164  if(is_valid && cStore != nullptr && cStore->IsObjectPresent(uuid)) //use MHO_UUID, not string
165  {
166  MHO_UUID type_id = cStore->GetObjectTypeUUID(uuid);
167 
168  if(type_id == cStore->GetTypeUUID< visibility_type >())
169  {
170  return py::cast( *(m.GetObject< visibility_type >(object_uuid) ) );
171  }
172  if(type_id == cStore->GetTypeUUID< weight_type >())
173  {
174  return py::cast( *(m.GetObject< weight_type >(object_uuid) ) );
175  }
176  if(type_id == cStore->GetTypeUUID< station_coord_type >())
177  {
178  return py::cast( *(m.GetObject< station_coord_type >(object_uuid) ) );
179  }
180  if(type_id == cStore->GetTypeUUID< visibility_store_type >())
181  {
182  return py::cast( *(m.GetObject< visibility_store_type >(object_uuid) ) );
183  }
184 
185  if(type_id == cStore->GetTypeUUID< weight_store_type >())
186  {
187  return py::cast( *(m.GetObject< weight_store_type >(object_uuid) ) );
188  }
189 
190  if(type_id == cStore->GetTypeUUID< phasor_type >())
191  {
192  return py::cast( *(m.GetObject< phasor_type >(object_uuid) ) );
193  }
194 
195  if(type_id == cStore->GetTypeUUID< multitone_pcal_type >())
196  {
197  return py::cast( *(m.GetObject< multitone_pcal_type >(object_uuid) ) );
198  }
199 
200  if(type_id == cStore->GetTypeUUID< MHO_ObjectTags >())
201  {
202  //handle tag data type
203  MHO_ObjectTags* tags = cStore->GetObject< MHO_ObjectTags >(uuid); //use MHO_UUID, not string
204  if(tags != nullptr)
205  {
206  mho_json meta_data = tags->GetMetaDataAsJSON();
207  std::set< MHO_UUID > tagged_ids = tags->GetTaggedObjectUUIDSet();
208  std::vector< std::string > id_list;
209  for(auto it = tagged_ids.begin(); it != tagged_ids.end(); it++)
210  {
211  id_list.push_back(it->as_string());
212  }
213  //append the tagged object uuid list
214  meta_data["tagged_object_uuid_list"] = id_list;
215  //convert to py::dict
216  py::dict dict_obj = meta_data;
217  return dict_obj;
218  }
219  else
220  {
221  py::print("MHO_ObjectTags object with uuid: ", object_uuid, " cannot be loaded.");
222  }
223  }
224  }
225  py::print("object uuid ", object_uuid, " is not recognized, returning None.");
226  return py::object(py::cast(nullptr));
227  },
228  py::return_value_policy::reference, "return the object matching the specified uuid", py::arg("uuid"));
229 }
230 
231 } // namespace hops
232 
233 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
Class MHO_ContainerStore.
Definition: MHO_ContainerStore.hh:32
bool IsObjectPresent(const MHO_UUID &obj_id) const
Checks if an object with a given UUID is present in the container store.
Definition: MHO_ContainerStore.cc:18
MHO_UUID GetTypeUUID()
get the type uuid for a specific type (if it is supported) - if unsupported uuid will be zero
Definition: MHO_ContainerStore.hh:396
Class MHO_ObjectTags.
Definition: MHO_ObjectTags.hh:28
python binding to the MHO_ContainerStore
Definition: MHO_PyContainerStoreInterface.hh:30
MHO_ContainerStore * GetContainerStore()
Definition: MHO_PyContainerStoreInterface.hh:121
std::size_t GetNObjects()
Definition: MHO_PyContainerStoreInterface.hh:45
std::string GetObjectTypeUUID(const std::string &uuid_string) const
Definition: MHO_PyContainerStoreInterface.hh:69
bool IsValid()
Definition: MHO_PyContainerStoreInterface.hh:36
bool IsObjectPresent(const std::string &uuid_string) const
Definition: MHO_PyContainerStoreInterface.hh:54
py::list GetObjectList()
Definition: MHO_PyContainerStoreInterface.hh:103
MHO_PyContainerStoreInterface()
Definition: MHO_PyContainerStoreInterface.hh:32
MHO_PyContainerStoreInterface(MHO_ContainerStore *conStore)
Definition: MHO_PyContainerStoreInterface.hh:33
virtual ~MHO_PyContainerStoreInterface()
Definition: MHO_PyContainerStoreInterface.hh:34
MHO_PyTableContainer< XClassType > * GetObject(const std::string &uuid_string)
Definition: MHO_PyContainerStoreInterface.hh:81
python bindings for template MHO_TableContainer objects This extension which allows us to present the...
Definition: MHO_PyTableContainer.hh:40
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
std::string as_string() const
Definition: MHO_UUID.hh:125
bool from_string(const std::string &uuid_str)
Definition: MHO_UUID.hh:143
Definition: MHO_ChannelLabeler.hh:17
void DeclarePyContainerStoreInterface(py::module &m, std::string pyclass_name)
Definition: MHO_PyContainerStoreInterface.hh:145