mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Adding detect-raw-datetime-manipulation option
This commit is contained in:
parent
2dbaced650
commit
6932b83509
8 changed files with 161 additions and 1 deletions
43
pre_commit_hooks/detect_datetime_raw_manipulation.py
Normal file
43
pre_commit_hooks/detect_datetime_raw_manipulation.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import sys
|
||||
|
||||
DT_MANIPULATION_RE = re.compile(
|
||||
r'[+-]=?\s?datetime.timedelta\(|[+-]=?\s?timedelta\(|.replace\(.*tzinfo=|datetime\(.*tzinfo=',
|
||||
)
|
||||
|
||||
|
||||
def _check_file(filename):
|
||||
return_value = 0
|
||||
with open(filename, 'r') as f:
|
||||
for i, line in enumerate(f, 1):
|
||||
if DT_MANIPULATION_RE.search(line):
|
||||
if line.strip().endswith(' # safe_dt_op') or line.strip().startswith('#'):
|
||||
continue
|
||||
|
||||
sys.stdout.write('{}:{}: {}'.format(filename, i, line))
|
||||
sys.stdout.flush()
|
||||
|
||||
return_value += 1
|
||||
|
||||
return return_value
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*')
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
return_value = 0
|
||||
for filename in args.filenames:
|
||||
result = _check_file(filename)
|
||||
return_value |= 1 if result > 0 else 0
|
||||
|
||||
return return_value
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue