diff --git a/README.md b/README.md index dcc030e..3eb8997 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Add this to your `.pre-commit-config.yaml` - `name-tests-test` - Assert that files in tests/ end in _test.py - `pyflakes` - Run pyflakes on your python files - `requirements-txt-fixer` - Sorts entries in requirements.txt +- `double-quote-string-fixer` - This hook replaces double quoted strings with single quoted strings - `trailing-whitespace` - Trims trailing whitespace. ### As a standalone package diff --git a/hooks.yaml b/hooks.yaml index d575170..9e32ce7 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -79,6 +79,12 @@ entry: requirements-txt-fixer language: python files: requirements.*\.txt$ +- id: double-quote-string-fixer + name: Fix double quoted strings + description: This hook replaces double quoted strings with single quoted strings + entry: double-quote-string-fixer + language: python + files: \.py$ - id: trailing-whitespace name: Trim Trailing Whitespace description: This hook trims trailing whitespace. diff --git a/pre_commit_hooks/string_fixer.py b/pre_commit_hooks/string_fixer.py new file mode 100644 index 0000000..9d2213a --- /dev/null +++ b/pre_commit_hooks/string_fixer.py @@ -0,0 +1,49 @@ +from __future__ import absolute_import +from __future__ import print_function +from __future__ import unicode_literals + +import argparse +import re +import tokenize + + +double_quote_starts = tuple(s for s in tokenize.single_quoted if '"' in s) +compiled_tokenize_string = re.compile('(?