sdbus++: Refactor utilities shared by templates

These utilities are used by more than one template.
Moved to python classes for reusability.

Tested by generating files using 2 yaml files with properties and methods.

Signed-off-by: Ramin Izadpanah <iramin@google.com>
Change-Id: Ifa08c068afa3ce46104cd44a79982cf04b745350
diff --git a/tools/sdbusplus/interface.py b/tools/sdbusplus/interface.py
index 69d08db..a35c99a 100644
--- a/tools/sdbusplus/interface.py
+++ b/tools/sdbusplus/interface.py
@@ -28,6 +28,12 @@
 
         super(Interface, self).__init__(**kwargs)
 
+        self.namespaces = self.name.split('.')
+        self.classname = self.namespaces.pop()
+
+    def cppNamespace(self):
+        return "::".join(self.namespaces) + "::server::" + self.classname
+
     def enum_includes(self, inc_list):
         includes = []
         namespaces = []
diff --git a/tools/sdbusplus/method.py b/tools/sdbusplus/method.py
index 15f8e4f..0753939 100644
--- a/tools/sdbusplus/method.py
+++ b/tools/sdbusplus/method.py
@@ -22,6 +22,35 @@
         return self.render(loader, "method.prototype.hpp.mako", method=self,
                            interface=interface, ptype=ptype, post=str.rstrip)
 
+    def returns_as_list(self, interface, full=False):
+        return ", ".join([r.cppTypeParam(interface.name, full=full)
+                         for r in self.returns])
+
+    def cpp_return_type(self, interface):
+        if len(self.returns) == 0:
+            return "void"
+        elif len(self.returns) == 1:
+            return self.returns[0].cppTypeParam(interface.name)
+        else:
+            return "std::tuple<" + \
+                   self.returns_as_list() + \
+                   ">"
+
+    def parameter(self, interface, p, defaultValue=False):
+        r = "%s %s" % (p.cppTypeParam(interface.name), p.camelCase)
+        if defaultValue:
+            r += p.default_value()
+        return r
+
+    def get_parameters_str(self, interface, defaultValue=False):
+        return ",\n            ".join(
+            [self.parameter(interface, p, defaultValue)
+                for p in self.parameters])
+
+    def parameters_as_local(self, interface):
+        return "{};\n    ".join([self.parameter(interface, p)
+                                for p in self.parameters])
+
     def or_cpp_flags(self, flags):
         """Return the corresponding ORed cpp flags."""
         flags_dict = {"deprecated": "vtable::common_::deprecated",
diff --git a/tools/sdbusplus/property.py b/tools/sdbusplus/property.py
index 490fabf..e0c8fed 100644
--- a/tools/sdbusplus/property.py
+++ b/tools/sdbusplus/property.py
@@ -53,6 +53,12 @@
     def cppTypeParam(self, interface, full=False, server=True):
         return self.__cppTypeParam(interface, self.cppTypeName, full, server)
 
+    def default_value(self):
+        if self.defaultValue is not None:
+            return " = " + str(self.defaultValue)
+        else:
+            return ""
+
     def __cppTypeParam(self, interface, cppTypeName, full=False, server=True):
 
         ns_type = "server" if server else "client"
diff --git a/tools/sdbusplus/templates/interface.server.hpp.mako b/tools/sdbusplus/templates/interface.server.hpp.mako
index 389b872..2fcce29 100644
--- a/tools/sdbusplus/templates/interface.server.hpp.mako
+++ b/tools/sdbusplus/templates/interface.server.hpp.mako
@@ -11,15 +11,12 @@
 ${ m.cpp_prototype(loader, interface=interface, ptype='callback-hpp-includes') }
 % endfor
 <%
-    namespaces = interface.name.split('.')
-    classname = namespaces.pop()
+    namespaces = interface.namespaces
+    classname = interface.classname
 
     def setOfPropertyTypes():
         return set(p.cppTypeParam(interface.name) for p in
                    interface.properties);
-
-    def cppNamespace():
-        return "::".join(namespaces) + "::server::" + classname
 %>
 namespace sdbusplus
 {
@@ -210,17 +207,17 @@
 {
     % for e in interface.enums:
 template <>
-inline auto convert_from_string<${cppNamespace()}::${e.name}>(
+inline auto convert_from_string<${interface.cppNamespace()}::${e.name}>(
         const std::string& value)
 {
-    return ${cppNamespace()}::convert${e.name}FromString(value);
+    return ${interface.cppNamespace()}::convert${e.name}FromString(value);
 }
 
 template <>
-inline std::string convert_to_string<${cppNamespace()}::${e.name}>(
-        ${cppNamespace()}::${e.name} value)
+inline std::string convert_to_string<${interface.cppNamespace()}::${e.name}>(
+        ${interface.cppNamespace()}::${e.name} value)
 {
-    return ${cppNamespace()}::convert${e.name}ToString(value);
+    return ${interface.cppNamespace()}::convert${e.name}ToString(value);
 }
     % endfor
 } // namespace details
diff --git a/tools/sdbusplus/templates/method.prototype.hpp.mako b/tools/sdbusplus/templates/method.prototype.hpp.mako
index 6178f43..17c2019 100644
--- a/tools/sdbusplus/templates/method.prototype.hpp.mako
+++ b/tools/sdbusplus/templates/method.prototype.hpp.mako
@@ -1,20 +1,4 @@
 <%
-    def cpp_return_type():
-        if len(method.returns) == 0:
-            return "void"
-        elif len(method.returns) == 1:
-            return method.returns[0].cppTypeParam(interface.name)
-        else:
-            return "std::tuple<" + \
-                   returns_as_list() + \
-                   ">"
-
-    def parameters(defaultValue=False):
-        return ",\n            ".\
-            join([ parameter(p, defaultValue) for p in method.parameters ])
-
-    def parameters_as_local():
-        return "{};\n    ".join([ parameter(p) for p in method.parameters ])
 
     def parameters_as_list(transform=lambda p: p.camelCase):
         return ", ".join([ transform(p) for p in method.parameters ])
@@ -23,27 +7,11 @@
         return ", ".join([ p.cppTypeParam(interface.name, full=True)
                 for p in method.parameters ])
 
-    def parameter(p, defaultValue=False):
-        r = "%s %s" % (p.cppTypeParam(interface.name), p.camelCase)
-        if defaultValue:
-            r += default_value(p)
-        return r
-
-    def returns_as_list(full=False):
-        return ", ".join([ r.cppTypeParam(interface.name, full=full)
-                for r in method.returns ])
-
     def returns_as_tuple_index(tuple, pre="", post=""):
         return ", ".join([ "%sstd::move(std::get<%d>(%s))%s" %\
                 (pre,i,tuple,post) \
                 for i in range(len(method.returns))])
 
-    def default_value(p):
-        if p.defaultValue != None:
-            return " = " + str(p.defaultValue)
-        else:
-            return ""
-
     def interface_name():
         return interface.name.split('.').pop()
 
@@ -82,8 +50,8 @@
         % endfor
     % endif
          */
-        virtual ${cpp_return_type()} ${ method.camelCase }(
-            ${ parameters() }) = 0;
+        virtual ${method.cpp_return_type(interface)} ${ method.camelCase }(
+            ${ method.get_parameters_str(interface) }) = 0;
 ###
 ### Emit 'callback-header'
 ###
@@ -127,7 +95,7 @@
         }
 
     % if len(method.parameters) != 0:
-        ${parameters_as_local()}{};
+        ${method.parameters_as_local(interface)}{};
 
         m.read(${parameters_as_list()});
     % endif
@@ -180,7 +148,7 @@
         utility::tuple_to_array(std::make_tuple('\0'));
     % else:
         utility::tuple_to_array(message::types::type_id<
-                ${ returns_as_list(full=True) }>());
+                ${ method.returns_as_list(interface, full=True) }>());
     % endif
 }
 }