Debian Codegraph @

test/sanity/code-smell/no-unwanted-characters.py

raw
1 """Disallow use of unwanted Unicode characters."""
2
3 from __future__ import annotations
4
5 import re
6 import sys
7
8
9 def main():
10 """Main entry point."""
11 for path in sys.argv[1:] or sys.stdin.read().splitlines():
12 with open(path, 'rb') as path_fd:
13 for line, text in enumerate(path_fd.readlines()):
14 try:
15 text = text.decode('utf-8')
16 except UnicodeDecodeError as ex:
17 print('%s:%d:%d: UnicodeDecodeError: %s' % (path, line + 1, ex.start + 1, ex))
18 continue
19
20 match = re.search('(\u00a0)', text)
21
22 if match:
23 print('%s:%d:%d: use an ASCII space instead of a Unicode no-break space' % (path, line + 1, match.start(1) + 1))
24
25
26 if __name__ == '__main__':
27 main()