-
Notifications
You must be signed in to change notification settings - Fork 4
/
charvalidate.py
59 lines (46 loc) · 1.83 KB
/
charvalidate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Sloveniacontrol Ltd. (www.sloveniacontrol.si)
# This file is part of asterix-data.
#
# asterix-data is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# asterix-data is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with asterix-data. If not, see <http://www.gnu.org/licenses/>.
# Author:
# Zoran Bošnjak, Sloveniacontrol Ltd.
"""Find non ascii and tab characters in input files (validator)
"""
import os, sys
import argparse
def check(s):
"""return list of lines, containing problems"""
lines = s.splitlines()
lines = map(lambda line: any([ord(c)>127 or c=='\t' for c in line]), lines)
lines = enumerate(lines, 1)
problems = filter(lambda a: a[1], lines)
return map(lambda a: a[0], problems)
# main
parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='+', type=argparse.FileType('r'))
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='verbose output')
args = parser.parse_args()
data = map(lambda x: (x.name, check(x.read())), args.infile)
problems = sum(map(lambda a: len(list(a[1])), data))
if problems:
print(problems, 'lines found (non-ascii or tabs)')
print('---')
for filename, err in data:
if err:
print(filename, 'lines:', err)
sys.exit(1)
print('OK')