From a7ccd1fa7254b1060afb7490329d33178e705afd Mon Sep 17 00:00:00 2001
From: Guido Sterbini <sterbini@hpc-201-11-01-a.cr.cnaf.infn.it>
Date: Mon, 5 Jul 2021 11:57:15 +0200
Subject: [PATCH] Adding the py_to_yaml

---
 docs/source/api.rst    |  5 +++++
 tree_maker/__init__.py |  1 +
 tree_maker/general.py  | 11 +++++++++++
 3 files changed, 17 insertions(+)

diff --git a/docs/source/api.rst b/docs/source/api.rst
index 7cd5726..868ddb6 100644
--- a/docs/source/api.rst
+++ b/docs/source/api.rst
@@ -7,6 +7,11 @@ NodeJob.py
 .. autoclass:: tree_maker.NodeJob.NodeJob
     :members:
 
+general.py
+------
+.. automodule:: tree_maker.general
+    :members:
+
 tag.py
 ------
 .. automodule:: tree_maker.tag
diff --git a/tree_maker/__init__.py b/tree_maker/__init__.py
index fa42a9e..e882033 100644
--- a/tree_maker/__init__.py
+++ b/tree_maker/__init__.py
@@ -11,6 +11,7 @@ from .general import tree_from_yaml
 from .general import tree_from_json
 from .general import from_yaml
 from .general import from_json
+from .general import py_to_yaml
 from .tag import * 
 from .tag_json import * 
 
diff --git a/tree_maker/general.py b/tree_maker/general.py
index f7f8ce0..bcdddb0 100644
--- a/tree_maker/general.py
+++ b/tree_maker/general.py
@@ -10,14 +10,17 @@ import orjson
 ryaml = ruamel.yaml.YAML()
 
 def tree_from_yaml(filename='tree.yaml'): 
+    '''Import the tree structure from the yaml formatted *filename*'''
     with open(filename, "r") as file:
         return DictImporter(nodecls=NodeJob).import_(yaml.load(file, Loader=yaml.FullLoader))
         
 def tree_from_json(filename='tree.json'): 
+    '''Import the tree structure from the json formatted *filename*'''
     with open(filename, "r") as file:
         return DictImporter(nodecls=NodeJob).import_(orjson.loads(file.read()))
         
 def from_yaml(filename):
+    '''Load the *filename* yaml file'''
     try:
         with open(filename, 'r') as file:
             return ryaml.load(file)
@@ -26,9 +29,17 @@ def from_yaml(filename):
         return {}
 
 def from_json(filename, verbose=False):
+    '''Load the *filename* json file'''
     try:
         with open(filename, 'r') as file:
             return orjson.loads(file.read())
     except Exception as e:
         if verbose: print(e)
         return {}
+
+def py_to_yaml(filename):
+    '''Convert the *filename*.py (containg dictionaries) to an
+    equivalent *filename*.yaml'''
+    import filename as my_dict
+    with open(filename+'.yaml', 'w') as fid:
+        yaml.dump(my_dict, fid)
-- 
GitLab