Repository: di/vladiate

Issue:
add a quiet option (-q)
Hi,

After the vladiate 0.0.20, I could get the shell exitcode for success or not.

Now I would like to add a quiet option (-q) so that when I call vladiate, it silently validates, if not it will throw an error.

Best

Discussion:
maleix: Thanks for the feedback di!
I updated the pull request.

What makes me doubt is how to get the `arguments` variable from the `_vladiate` function in main.py.
Making arguments global or passing it as parameter don't seem clean approaches.



Patch:
diff --git a/README.rst b/README.rst
index 22f8219..57abdf6 100644
--- a/README.rst
+++ b/README.rst
@@ -341,6 +341,10 @@ Running Vlads Programatically
       Whether to fail validation if there are fields in the file for which the
       `Vlad` does not have validators. Optional, defaults to `False`.
 
+  :``quiet=False``:
+      Whether to disable log output generated by validations.
+      Optional, defaults to `False`.
+
   For example:
 
 .. code:: python
@@ -380,6 +384,7 @@ Command Line Arguments
       -V, --version         show version number and exit
       -p PROCESSES, --processes=PROCESSES
                             attempt to use this number of processes, Default: 1
+      -q, --quiet           disable console log output generated by validations
 
 Authors
 -------
diff --git a/vladiate/main.py b/vladiate/main.py
index b9ca587..d04ba86 100644
--- a/vladiate/main.py
+++ b/vladiate/main.py
@@ -68,6 +68,16 @@ def parse_args():
         help="attempt to use this number of processes",
     )
 
+    # Disable vladiate classes console log output
+    parser.add_argument(
+        "-q",
+        "--quiet",
+        action="store_true",
+        dest="quiet",
+        default=False,
+        help="disable console log output generated by validations",
+    )
+
     return parser.parse_args()
 
 
@@ -162,7 +172,11 @@ def load_vladfile(path):
 
 def _vladiate(vlad):
     global result_queue
-    result_queue.put(vlad(vlad.source, validators=vlad.validators).validate())
+    result_queue.put(
+        vlad(
+            vlad.source, validators=vlad.validators, quiet=parse_args().quiet
+        ).validate()
+    )
 
 
 result_queue = Queue()
@@ -213,7 +227,7 @@ def main():
     all_passed = True
     if arguments.processes == 1:
         for vlad in vlad_classes:
-            passed = vlad(source=vlad.source).validate()
+            passed = vlad(source=vlad.source, quiet=arguments.quiet).validate()
             all_passed = all_passed and passed
 
     else:
diff --git a/vladiate/vlad.py b/vladiate/vlad.py
index c11c027..7e09efe 100644
--- a/vladiate/vlad.py
+++ b/vladiate/vlad.py
@@ -14,6 +14,7 @@ class Vlad(object):
         default_validator=EmptyValidator,
         delimiter=None,
         ignore_missing_validators=False,
+        quiet=False,
     ):
         self.logger = logs.logger
         self.failures = defaultdict(lambda: defaultdict(list))
@@ -24,6 +25,7 @@ class Vlad(object):
         self.delimiter = delimiter or getattr(self, "delimiter", ",")
         self.line_count = 0
         self.ignore_missing_validators = ignore_missing_validators
+        self.logger.disabled = quiet
 
         self.validators.update(
             {


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
index 60ea61a..838b552 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -119,6 +119,7 @@ def test_main_with_multiprocess(monkeypatch, get, expected):
             vladfile=stub(),
             vlads=["Something"],
             processes=2,
+            quiet=False,
         ),
     )
     monkeypatch.setattr("vladiate.main.find_vladfile", lambda *args, **kwargs: stub())
@@ -156,6 +157,7 @@ def test_main_with_vlads_in_args(monkeypatch):
             vladfile=stub(),
             vlads=["Something"],
             processes=1,
+            quiet=False,
         ),
     )
     monkeypatch.setattr("vladiate.main.find_vladfile", lambda *args, **kwargs: stub())
@@ -178,6 +180,7 @@ def test_main_no_vlads_in_args(monkeypatch):
             vladfile=stub(),
             vlads=[],
             processes=1,
+            quiet=False,
         ),
     )
     monkeypatch.setattr("vladiate.main.find_vladfile", lambda *args, **kwargs: stub())
diff --git a/tests/test_vlads.py b/tests/test_vlads.py
index dc174fc..e9b1453 100644
--- a/tests/test_vlads.py
+++ b/tests/test_vlads.py
@@ -110,6 +110,21 @@ def test_ignore_missing_validators():
     assert vlad.missing_validators == {"Column B"}
 
 
+def test_quiet(caplog):
+    source = LocalFile("vladiate/examples/vampires.csv")
+
+    class TestVlad(Vlad):
+        validators = {
+            "Column A": [UniqueValidator()],
+            "Column B": [SetValidator(["Vampire", "Not A Vampire"])],
+        }
+
+    vlad = TestVlad(source=source, quiet=True)
+
+    assert vlad.validate()
+    assert len(caplog.records) == 0
+
+
 def test_when_bad_is_non_iterable():
     source = String("Column A,Column B\n,foo")