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: "
98  << MHO_ClassIdentity::ClassName< XClassType >() << eom);
99  return nullptr;
100  }
101  }
102 
103  //return a list of (UUID <-> typename <-> shortname) tuples
104  py::list GetObjectList()
105  {
106  mho_json info_obj;
107  std::vector< std::tuple< std::string, std::string, std::string > > info = fContainerStore->GetAllObjectInfo();
108  for(std::size_t i = 0; i < info.size(); i++)
109  {
110  mho_json item_info;
111  item_info["type_uuid"] = std::get< 0 >(info[i]);
112  item_info["object_uuid"] = std::get< 1 >(info[i]);
113  item_info["shortname"] = std::get< 2 >(info[i]);
114  info_obj.push_back(item_info);
115  }
116  py::list ret_obj = info_obj;
117  return ret_obj;
118  };
119 
120  //publically EXPOSED for C++ lambda's
121  //DO NOT EXPOSE THIS CLASS TO PYTHON
122  MHO_ContainerStore* GetContainerStore() { return fContainerStore; }
123 
124  private:
125  //put the extension building/retrieval in this template
126  template< typename XClassType > MHO_PyTableContainer< XClassType >* GetExtension(XClassType* obj)
127  {
128  if(obj != nullptr)
129  {
130  if(obj->template HasExtension< MHO_PyTableContainer< XClassType > >())
131  {
132  return obj->template AsExtension< MHO_PyTableContainer< XClassType > >();
133  }
134  else
135  {
136  return obj->template MakeExtension< MHO_PyTableContainer< XClassType > >();
137  }
138  }
139  return nullptr;
140  }
141 
142  //pointer to the parameter store
143  MHO_ContainerStore* fContainerStore;
144 };
145 
146 inline void DeclarePyContainerStoreInterface(py::module& m, std::string pyclass_name)
147 {
148  py::class_< MHO_PyContainerStoreInterface >(m, pyclass_name.c_str())
149  //no __init__ def here, as this class is not meant to be constructable on the python side
151  "check if the underlying container store object exists and is valid")
153  "get the number of objects present in the container store")
154  .def("is_object_present", &hops::MHO_PyContainerStoreInterface::IsObjectPresent,
155  "check if an object with the passed UUID is present", py::arg("uuid"))
156  .def("get_object_id_list", &hops::MHO_PyContainerStoreInterface::GetObjectList,
157  "get a list of the object information (list of dict's containing each object's type_uuid, object_uuid, and "
158  "shortname")
159  .def(
160  "get_object", //lambda for returing either object data or none type
161  [=](MHO_PyContainerStoreInterface& m, std::string object_uuid) -> py::object {
162  MHO_UUID uuid;
163  bool is_valid = uuid.from_string(object_uuid);
164  MHO_ContainerStore* cStore = m.GetContainerStore();
165  if(is_valid && cStore != nullptr && cStore->IsObjectPresent(uuid)) //use MHO_UUID, not string
166  {
167  MHO_UUID type_id = cStore->GetObjectTypeUUID(uuid);
168 
169  if(type_id == cStore->GetTypeUUID< visibility_type >())
170  {
171  return py::cast(*(m.GetObject< visibility_type >(object_uuid)));
172  }
173  if(type_id == cStore->GetTypeUUID< weight_type >())
174  {
175  return py::cast(*(m.GetObject< weight_type >(object_uuid)));
176  }
177  if(type_id == cStore->GetTypeUUID< station_coord_type >())
178  {
179  return py::cast(*(m.GetObject< station_coord_type >(object_uuid)));
180  }
181  if(type_id == cStore->GetTypeUUID< visibility_store_type >())
182  {
183  return py::cast(*(m.GetObject< visibility_store_type >(object_uuid)));
184  }
185 
186  if(type_id == cStore->GetTypeUUID< weight_store_type >())
187  {
188  return py::cast(*(m.GetObject< weight_store_type >(object_uuid)));
189  }
190 
191  if(type_id == cStore->GetTypeUUID< phasor_type >())
192  {
193  return py::cast(*(m.GetObject< phasor_type >(object_uuid)));
194  }
195 
196  if(type_id == cStore->GetTypeUUID< multitone_pcal_type >())
197  {
198  return py::cast(*(m.GetObject< multitone_pcal_type >(object_uuid)));
199  }
200 
201  if(type_id == cStore->GetTypeUUID< MHO_ObjectTags >())
202  {
203  //handle tag data type
204  MHO_ObjectTags* tags = cStore->GetObject< MHO_ObjectTags >(uuid); //use MHO_UUID, not string
205  if(tags != nullptr)
206  {
207  mho_json meta_data = tags->GetMetaDataAsJSON();
208  std::set< MHO_UUID > tagged_ids = tags->GetTaggedObjectUUIDSet();
209  std::vector< std::string > id_list;
210  for(auto it = tagged_ids.begin(); it != tagged_ids.end(); it++)
211  {
212  id_list.push_back(it->as_string());
213  }
214  //append the tagged object uuid list
215  meta_data["tagged_object_uuid_list"] = id_list;
216  //convert to py::dict
217  py::dict dict_obj = meta_data;
218  return dict_obj;
219  }
220  else
221  {
222  py::print("MHO_ObjectTags object with uuid: ", object_uuid, " cannot be loaded.");
223  }
224  }
225  }
226  py::print("object uuid ", object_uuid, " is not recognized, returning None.");
227  return py::object(py::cast(nullptr));
228  },
229  py::return_value_policy::reference, "return the object matching the specified uuid", py::arg("uuid"));
230 }
231 
232 } // namespace hops
233 
234 #endif
nlohmann::json mho_json
Definition: MHO_JSONHeaderWrapper.hh:5
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
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:394
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:122
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:104
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_AdhocFlagging.hh:18
void DeclarePyContainerStoreInterface(py::module &m, std::string pyclass_name)
Definition: MHO_PyContainerStoreInterface.hh:146