From 57464f604b2cb236a79bfc7a57bf27136c12f7d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Tue, 30 Jun 2020 13:27:28 +0200
Subject: [PATCH] hopefully fix coverage + add ujson support

---
 .travis.yml                             |  2 +-
 CHANGELOG.md                            |  1 +
 docs/configuration/sources.rst          |  4 ++++
 requirements.txt                        |  2 ++
 satella/__init__.py                     |  2 +-
 satella/configuration/sources/format.py | 27 +++++++++++++++++--------
 setup.py                                |  3 ++-
 unittest.cfg                            |  2 --
 8 files changed, 30 insertions(+), 13 deletions(-)
 delete mode 100644 unittest.cfg

diff --git a/.travis.yml b/.travis.yml
index 33f43e64..c97e5cd1 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,7 +9,7 @@ before_script:
   - chmod +x ./cc-test-reporter
   - ./cc-test-reporter before-build
   - pip install -r requirements.txt
-  - pip install coverage nose2 requests
+  - pip install coverage nose2
 jobs:
   include:
     - stage: test
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 783eb50a..7eb50e4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,2 +1,3 @@
 # v2.8.12
 
+* added optional `ujson` support
diff --git a/docs/configuration/sources.rst b/docs/configuration/sources.rst
index 1e613f6b..c62e5452 100644
--- a/docs/configuration/sources.rst
+++ b/docs/configuration/sources.rst
@@ -37,6 +37,10 @@ Then there are abstract sources of configuration.
 
 In order to actually load the configuration, use the method ``provide()``.
 
+Note that `FileSource` will try parsing the file with any modules, available, so if you
+want parsing for **yaml** and **toml**, you better install `pyyaml` and `toml` respectively.
+
+Note that JSON will be parsed using `ujson` if the module is available.
 
 JSON schema
 -----------
diff --git a/requirements.txt b/requirements.txt
index d36e77a2..70f28585 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,5 @@
 psutil
 pyyaml
 toml
+requests
+ujson
diff --git a/satella/__init__.py b/satella/__init__.py
index 386204b2..99ba6885 100644
--- a/satella/__init__.py
+++ b/satella/__init__.py
@@ -1 +1 @@
-__version__ = '2.8.12_a1'
+__version__ = '2.8.12_a2'
diff --git a/satella/configuration/sources/format.py b/satella/configuration/sources/format.py
index b443f0df..e39dff23 100644
--- a/satella/configuration/sources/format.py
+++ b/satella/configuration/sources/format.py
@@ -1,6 +1,5 @@
 import binascii
 import codecs
-import json
 import typing as tb
 
 from satella.coding.recast_exceptions import rethrow_as
@@ -60,14 +59,26 @@ class FormatSource(BaseSource):
             else:
                 return ret_val
 
+try:
+    import ujson
 
-@register_format_source
-class JSONSource(FormatSource):
-    """
-    Loads JSON strings
-    """
-    TRANSFORM = json.loads
-    EXTRA_EXCEPTIONS = [json.JSONDecodeError]
+    @register_format_source
+    class JSONSource(FormatSource):
+        """
+        Loads JSON strings
+        """
+        TRANSFORM = ujson.loads
+        EXTRA_EXCEPTIONS = [ValueError]
+except ImportError:
+    import json
+
+    @register_format_source
+    class JSONSource(FormatSource):
+        """
+        Loads JSON strings
+        """
+        TRANSFORM = json.loads
+        EXTRA_EXCEPTIONS = [json.JSONDecodeError]
 
 
 try:
diff --git a/setup.py b/setup.py
index dbf6e556..30953509 100644
--- a/setup.py
+++ b/setup.py
@@ -9,7 +9,7 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server',
             'psutil'
       ],
       tests_require=[
-          "nose2", "mock", "coverage", "nose2[coverage_plugin]", "requests"
+          "nose2", "mock", "coverage"
       ],
       test_suite='nose2.collector.collector',
       python_requires='!=2.7.*,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
@@ -17,6 +17,7 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server',
             'HTTPJSONSource': ['requests'],
             'YAMLSource': ['pyyaml'],
             'TOMLSource': ['toml'],
+            'Faster JSONSource': ['ujson'],
             'satella.cassandra': ['cassandra-driver']
       }
       )
diff --git a/unittest.cfg b/unittest.cfg
deleted file mode 100644
index 84743c4d..00000000
--- a/unittest.cfg
+++ /dev/null
@@ -1,2 +0,0 @@
-[coverage]
-always-on = True
-- 
GitLab