Repository: chimpler/pyhocon

Issue:
How to dump HOCON file?
Hi,

Just wonder how to do this with pyhocon?

https://marcinkubala.wordpress.com/2013/10/09/typesafe-config-hocon/

Thanks.

Discussion:

Patch:
diff --git a/pyhocon/config_parser.py b/pyhocon/config_parser.py
index a3a2191..1c966d6 100644
--- a/pyhocon/config_parser.py
+++ b/pyhocon/config_parser.py
@@ -83,6 +83,16 @@ class ConfigFactory(object):
         """
         return ConfigParser().parse(content, basedir, resolve)
 
+    @staticmethod
+    def from_dict(dictionary):
+        """Convert dictionary (and ordered dictionary) into a ConfigTree
+        :param dictionary: dictionary to convert
+        :type dictionary: dict
+        :return: Config object
+        :type return: Config
+        """
+        return ConfigTree(dictionary)
+
 
 class ConfigParser(object):
     """
diff --git a/pyhocon/config_tree.py b/pyhocon/config_tree.py
index 92b98a0..a11433d 100644
--- a/pyhocon/config_tree.py
+++ b/pyhocon/config_tree.py
@@ -18,6 +18,7 @@ class ConfigTree(OrderedDict):
 
     def __init__(self, *args, **kwds):
         super(ConfigTree, self).__init__(*args, **kwds)
+
         for key, value in self.items():
             if isinstance(value, ConfigValues):
                 value.parent = self
diff --git a/pyhocon/tool.py b/pyhocon/tool.py
index 267a862..35010bd 100644
--- a/pyhocon/tool.py
+++ b/pyhocon/tool.py
@@ -9,6 +9,7 @@ LOG_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
 
 
 class HOCONConverter(object):
+
     @staticmethod
     def to_json(config, indent=2, level=0):
         """Convert HOCON input into a JSON output


Test patch:
diff --git a/tests/test_config_parser.py b/tests/test_config_parser.py
index 16bd518..4c02aed 100644
--- a/tests/test_config_parser.py
+++ b/tests/test_config_parser.py
@@ -3,6 +3,10 @@ from pyparsing import ParseSyntaxException, ParseException
 import pytest
 from pyhocon import ConfigFactory, ConfigSubstitutionException
 from pyhocon.exceptions import ConfigMissingException, ConfigWrongTypeException
+try:  # pragma: no cover
+    from collections import OrderedDict
+except ImportError:  # pragma: no cover
+    from ordereddict import OrderedDict
 
 
 class TestConfigParser(object):
@@ -1143,3 +1147,22 @@ with-escaped-newline-escape-sequence: \"\"\"
         assert config['with-escaped-backslash'] == '\n\\\\\n'
         assert config['with-newline-escape-sequence'] == '\n\\n\n'
         assert config['with-escaped-newline-escape-sequence'] == '\n\\\\n\n'
+
+    def test_from_dict_with_dict(self):
+        d = {
+            'banana': 3,
+            'apple': 4,
+            'pear': 1,
+            'orange': 2,
+        }
+        config = ConfigFactory.from_dict(d)
+        assert config == d
+
+    def test_from_dict_with_ordered_dict(self):
+        d = OrderedDict()
+        d['banana'] = 3
+        d['apple'] = 4
+        d['pear'] = 1
+        d['orange'] = 2
+        config = ConfigFactory.from_dict(d)
+        assert config == d