elog-gen.py: Dynamically determine the namespaces

Namespaces should be determined by the directory tree where the
yaml files are located.

Change-Id: I82729728b6aebf4c48a0ec967f2d08c1cbf8b7f3
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/tools/elog-gen.py b/tools/elog-gen.py
index 3cf396c..6ca8455 100755
--- a/tools/elog-gen.py
+++ b/tools/elog-gen.py
@@ -57,15 +57,17 @@
 
 
 def get_error_yaml_files(i_yaml_dir, i_test_dir):
-    yaml_files = []
+    yaml_files = dict()
     if i_yaml_dir != "None":
         for root, dirs, files in os.walk(i_yaml_dir):
             for files in filter(lambda file:
                                 file.endswith('.errors.yaml'), files):
-                yaml_files.append(os.path.join(root, files))
+                splitdir = root.split(i_yaml_dir)[1]
+                yaml_files[(os.path.join(root, files))] = splitdir
     for root, dirs, files in os.walk(i_test_dir):
         for files in filter(lambda file: file.endswith('.errors.yaml'), files):
-            yaml_files.append(os.path.join(root, files))
+            splitdir = root.split(i_test_dir)[1]
+            yaml_files[(os.path.join(root, files))] = splitdir
     return yaml_files
 
 
@@ -92,7 +94,7 @@
 
 
 def gen_elog_hpp(i_yaml_dir, i_test_dir, i_output_hpp,
-                 i_template_dir, i_elog_mako, i_error_namespace):
+                 i_template_dir, i_elog_mako):
     r"""
     Read  yaml file(s) under input yaml dir, grab the relevant data and call
     the mako template to generate the output header file.
@@ -112,6 +114,7 @@
     meta = dict()  # The meta data names associated (ERRNO, FILE_NAME, ...)
     meta_data = dict()  # The meta data info (type, format)
     parents = dict()
+    namespace = dict()
 
     error_yamls = get_error_yaml_files(i_yaml_dir, i_test_dir)
 
@@ -135,13 +138,15 @@
 
         get_elog_data(error_yaml,
                       meta_yaml,
-                      # 3rd arg is a tuple
+                      error_yamls[error_yaml],
+                      # Last arg is a tuple
                       (errors,
                        error_msg,
                        error_lvl,
                        meta,
                        meta_data,
-                       parents))
+                       parents,
+                       namespace))
 
     if(not check_error_inheritance(errors, parents)):
         print("Error - failed to validate error inheritance")
@@ -157,13 +162,14 @@
     f.write(template.render(
             errors=errors, error_msg=error_msg,
             error_lvl=error_lvl, meta=meta,
-            meta_data=meta_data, error_namespace=i_error_namespace,
+            meta_data=meta_data, error_namespace=namespace,
             parents=parents))
     f.close()
 
 
 def get_elog_data(i_elog_yaml,
                   i_elog_meta_yaml,
+                  i_namespace,
                   o_elog_data):
     r"""
     Parse the error and metadata yaml files in order to pull out
@@ -172,9 +178,11 @@
     Description of arguments:
     i_elog_yaml                 error yaml file
     i_elog_meta_yaml            metadata yaml file
+    i_namespace                 namespace data
     o_elog_data                 error metadata
     """
-    errors, error_msg, error_lvl, meta, meta_data, parents = o_elog_data
+    errors, error_msg, error_lvl, meta, meta_data, parents, namespace = \
+        o_elog_data
     ifile = yaml.safe_load(open(i_elog_yaml))
     mfile = yaml.safe_load(open(i_elog_meta_yaml))
     for i in mfile:
@@ -207,6 +215,7 @@
             meta_data[str_short]['str_short'] = str_short
             meta_data[str_short]['type'] = get_cpp_type(j['type'])
         meta[i['name']] = tmp_meta
+        namespace[i['name']] = i_namespace
 
     # Debug
     # for i in errors:
@@ -240,18 +249,13 @@
                       default="phosphor-logging/templates/",
                       help="Base directory of files to process")
 
-    parser.add_option("-n", "--namespace", dest="error_namespace",
-                      default="example/xyz/openbmc_project/Example",
-                      help="Error d-bus namespace")
-
     (options, args) = parser.parse_args(i_args)
 
     gen_elog_hpp(options.yamldir,
                  options.testdir,
                  options.output_hpp,
                  options.templatedir,
-                 options.elog_mako,
-                 options.error_namespace)
+                 options.elog_mako)
 
 # Only run if it's a script
 if __name__ == '__main__':
diff --git a/tools/phosphor-logging/templates/elog-gen-template.mako.hpp b/tools/phosphor-logging/templates/elog-gen-template.mako.hpp
index f0b0818..097b865 100644
--- a/tools/phosphor-logging/templates/elog-gen-template.mako.hpp
+++ b/tools/phosphor-logging/templates/elog-gen-template.mako.hpp
@@ -17,7 +17,7 @@
 
     % for index, name in enumerate(errors):
 <%
-    namespaces = error_namespace.split('/')
+    namespaces = error_namespace[name].split('/')
     ## In case someone provided a error_namespace ending with '/', remove the
     ## last split string, which would be an empty string.
     if not namespaces[-1]: