Passed
Push — master ( fe6f5e...f6312c )
by srz
16:29 queued 10:28
created

editorconfig-self-lint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 35
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A EditorConfig.__next__() 0 5 2
A EditorConfig.readline() 0 5 2
A EditorConfig.__init__() 0 4 1
A EditorConfig.__iter__() 0 2 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A main() 0 8 2
1
#!/usr/bin/env python
2
#
3
# editorconfig-lint.py
4
#
5
# Copyright (C) 2019, Takazumi Shirayanagi
6
# This software is released under the new BSD License,
7
# see LICENSE
8
#
9
10
import sys
11
import os
12
13
try:
14
    from configparser import ConfigParser
15
except ImportError:
16
    from ConfigParser import SafeConfigParser as ConfigParser
17
18
19
class EditorConfig(object):
20
    def __init__(self, path):
21
        self.name = os.path.basename(path)
22
        self.fp = open(path)
23
        self.first_head = True
24
25
    def readline(self):
26
        if self.first_head:
27
            self.first_head = False
28
            return '[global]\n'
29
        return self.fp.readline()
30
31
    def __iter__(self):
32
        return self
33
34
    def __next__(self):
35
        line = self.readline()
36
        if not line:
37
            raise StopIteration
38
        return line
39
40
    next = __next__  # For Python 2 compatibility.
41
42
43
def main():
44
    path = sys.argv[1]
45
    ini = ConfigParser()
46
    if not os.path.exists(path):
47
        sys.stderr.write('%s not found...' % path)
48
        sys.exit(2)
49
    ini.read_file(EditorConfig(path))
50
    sys.exit(0)
51
52
53
if __name__ == '__main__':
54
    main()
55