Merge pull request #400 from pycontribs/toml-checker

Toml checker
diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml
index 9396d55..7a3b380 100644
--- a/.pre-commit-hooks.yaml
+++ b/.pre-commit-hooks.yaml
@@ -70,6 +70,12 @@
     entry: check-symlinks
     language: python
     types: [symlink]
+-   id: check-toml
+    name: Check Toml
+    description: This hook checks toml files for parseable syntax.
+    entry: check-toml
+    language: python
+    types: [toml]
 -   id: check-vcs-permalinks
     name: Check vcs permalinks
     description: Ensures that links to vcs websites are permalinks.
diff --git a/README.md b/README.md
index 1a152de..3614ea9 100644
--- a/README.md
+++ b/README.md
@@ -42,6 +42,7 @@
 - `check-json` - Attempts to load all json files to verify syntax.
 - `check-merge-conflict` - Check for files that contain merge conflict strings.
 - `check-symlinks` - Checks for symlinks which do not point to anything.
+- `check-toml` - Attempts to load all TOML files to verify syntax.
 - `check-vcs-permalinks` - Ensures that links to vcs websites are permalinks.
 - `check-xml` - Attempts to load all xml files to verify syntax.
 - `check-yaml` - Attempts to load all yaml files to verify syntax.
diff --git a/pre_commit_hooks/check_toml.py b/pre_commit_hooks/check_toml.py
new file mode 100644
index 0000000..e16e17c
--- /dev/null
+++ b/pre_commit_hooks/check_toml.py
@@ -0,0 +1,28 @@
+from __future__ import print_function
+
+import argparse
+import sys
+from typing import Optional
+from typing import Sequence
+
+import toml
+
+
+def main(argv=None):  # type: (Optional[Sequence[str]]) -> int
+    parser = argparse.ArgumentParser()
+    parser.add_argument('filenames', nargs='*', help='Filenames to check.')
+    args = parser.parse_args(argv)
+
+    retval = 0
+    for filename in args.filenames:
+        try:
+            with open(filename) as f:
+                toml.load(f)
+        except toml.TomlDecodeError as exc:
+            print('{}: {}'.format(filename, exc))
+            retval = 1
+    return retval
+
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/setup.cfg b/setup.cfg
index 96d5f5a..18d3492 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -26,6 +26,7 @@
 install_requires =
     flake8
     ruamel.yaml>=0.15
+    toml
     six
     typing; python_version<"3.5"
 python_requires = >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
@@ -43,6 +44,7 @@
     check-json = pre_commit_hooks.check_json:main
     check-merge-conflict = pre_commit_hooks.check_merge_conflict:main
     check-symlinks = pre_commit_hooks.check_symlinks:main
+    check-toml = pre_commit_hooks.check_toml:main
     check-vcs-permalinks = pre_commit_hooks.check_vcs_permalinks:main
     check-xml = pre_commit_hooks.check_xml:main
     check-yaml = pre_commit_hooks.check_yaml:main
diff --git a/tests/check_toml_test.py b/tests/check_toml_test.py
new file mode 100644
index 0000000..1172c40
--- /dev/null
+++ b/tests/check_toml_test.py
@@ -0,0 +1,32 @@
+from __future__ import absolute_import
+from __future__ import unicode_literals
+
+from pre_commit_hooks.check_toml import main
+
+
+def test_toml_good(tmpdir):
+    filename = tmpdir.join('f')
+    filename.write("""
+key = # INVALID
+
+= "no key name"  # INVALID
+""")
+    ret = main((filename.strpath,))
+    assert ret == 1
+
+
+def test_toml_bad(tmpdir):
+    filename = tmpdir.join('f')
+    filename.write(
+        """
+# This is a TOML document.
+
+title = "TOML Example"
+
+[owner]
+name = "John"
+dob = 1979-05-27T07:32:00-08:00 # First class dates
+""",
+    )
+    ret = main((filename.strpath,))
+    assert ret == 0