]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: return options as appropriate python type
authorSage Weil <sage@redhat.com>
Tue, 18 Dec 2018 20:11:30 +0000 (14:11 -0600)
committerSage Weil <sage@redhat.com>
Fri, 21 Dec 2018 04:11:40 +0000 (22:11 -0600)
Signed-off-by: Sage Weil <sage@redhat.com>
src/mgr/BaseMgrModule.cc
src/mgr/BaseMgrStandbyModule.cc
src/mgr/PyModule.cc
src/mgr/PyModule.h
src/mgr/PyModuleRunner.h
src/pybind/mgr/mgr_module.py

index 20a4e9f434fe77d156dc714774644cd07d940a9e..d9e7a33ca0e51e0935a1829c50da1afd158193c8 100644 (file)
@@ -399,7 +399,7 @@ ceph_get_module_option(BaseMgrModule *self, PyObject *args)
       what, &value);
   if (found) {
     dout(10) << __func__ << " " << what << " found: " << value.c_str() << dendl;
-    return PyString_FromString(value.c_str());
+    return self->this_module->py_module->get_typed_option_value(what, value);
   } else {
     dout(4) << __func__ << " " << what << " not found " << dendl;
     Py_RETURN_NONE;
index 0f1725fe87677d7cbbbdfc26273be891288d55b4..ee923552f40f7b2a5b8171517f9876ba28089c4d 100644 (file)
@@ -73,7 +73,7 @@ ceph_get_module_option(BaseMgrStandbyModule *self, PyObject *args)
   bool found = self->this_module->get_config(what, &value);
   if (found) {
     dout(10) << __func__ << " " << what << " found: " << value.c_str() << dendl;
-    return PyString_FromString(value.c_str());
+    return self->this_module->py_module->get_typed_option_value(what, value);
   } else {
     dout(4) << __func__ << " " << what << " not found " << dendl;
     Py_RETURN_NONE;
index 1233e72d9fe1067408ebaf92bc433d8c3584bf5b..562c6d418381a7362b574161d0d3b2b6c6270d82 100644 (file)
@@ -609,6 +609,38 @@ bool PyModule::is_option(const std::string &option_name)
   return options.count(option_name) > 0;
 }
 
+PyObject *PyModule::get_typed_option_value(const std::string& name,
+                                          const std::string& value)
+{
+  auto p = options.find(name);
+  if (p != options.end()) {
+    switch (p->second.type) {
+    case Option::TYPE_INT:
+    case Option::TYPE_UINT:
+    case Option::TYPE_SIZE:
+      return PyInt_FromString((char *)value.c_str(), nullptr, 0);
+    case Option::TYPE_SECS:
+    case Option::TYPE_FLOAT:
+      {
+       PyObject *s = PyString_FromString(value.c_str());
+       PyObject *f = PyFloat_FromString(s, nullptr);
+       Py_DECREF(s);
+       return f;
+      }
+    case Option::TYPE_BOOL:
+      if (value == "1" || value == "true" || value == "True" ||
+         value == "on" || value == "yes") {
+       Py_INCREF(Py_True);
+       return Py_True;
+      } else {
+       Py_INCREF(Py_False);
+       return Py_False;
+      }
+    }
+  }
+  return PyString_FromString(value.c_str());
+}
+
 int PyModule::load_subclass_of(const char* base_class, PyObject** py_class)
 {
   // load the base class
index 05b8b0fc121afeb113013dc16bbe9ebed5896e0b..412fa38f088abd9ed4837386c7031c97bfff2c88 100644 (file)
@@ -103,6 +103,10 @@ public:
     return options;
   }
 
+  PyObject *get_typed_option_value(
+    const std::string& option,
+    const std::string& value);
+
   int load(PyThreadState *pMainThreadState);
 #if PY_MAJOR_VERSION >= 3
   static PyObject* init_ceph_logger();
index fb58bf0c2323f1a0920d8034d80fce261068cb9c..08c85a13263606bb4d1353716dd2979a65fee655 100644 (file)
  */
 class PyModuleRunner
 {
-protected:
+public:
   // Info about the module we're going to run
   PyModuleRef py_module;
 
+protected:
   // Populated by descendent class
   PyObject *pClassInstance = nullptr;
 
index b620f630a46306b6188567834323ca97a675f29c..cec00ed8493afc70b951b9cf717816aaad58e844 100644 (file)
@@ -243,7 +243,7 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule):
         r = self._ceph_get_module_option(key)
         if r is None:
             final_key = key.split('/')[-1]
-            return str(self.MODULE_OPTION_DEFAULTS.get(final_key, default))
+            return self.MODULE_OPTION_DEFAULTS.get(final_key, default)
         else:
             return r
 
@@ -701,7 +701,7 @@ class MgrModule(ceph_module.BaseMgrModule):
         r = self._ceph_get_module_option(key)
         if r is None:
             final_key = key.split('/')[-1]
-            return str(self.MODULE_OPTION_DEFAULTS.get(final_key, default))
+            return self.MODULE_OPTION_DEFAULTS.get(final_key, default)
         else:
             return r