-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchange_file_style.py
138 lines (113 loc) · 3.99 KB
/
change_file_style.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# coding=utf-8
import os
basedir = os.getcwdu()
file_type = ['.py']
# coding=utf-8
import os
import codecs
import chardet
from docopt import docopt
import argparse
detail = False
basedir = os.getcwdu()
file_type = ['.py']
class LineCount(object):
"""
Line Count by Python , line can count code files and code lines.
Usage:
line [--type=<file>]
line --verbose
line (-h | --help)
line (-v | --version)
Options:
-h --help Show this screen.
-v --version Show version.
--verbose Show detail.
-t file, --type=<file> Select file ext.
"""
def __init__(self, root_dir=basedir, detail=False, file_type=file_type):
self.root_dir = root_dir
self.detail = detail
self.file_num = 0
self.line_num = 0
self.file_type = file_type
def find_file(self):
"""
Count file num
"""
for root, dirs, files in os.walk(self.root_dir):
for filename in files:
if os.path.splitext(filename)[1] in self.file_type:
self.file_num += 1
new_file_path = os.path.join(root, filename)
with open(new_file_path) as f:
comment = f.readline()
if comment == '# coding=utf-8\n' or comment == '# -*- coding: utf-8 -*-\n' or comment == '#coding=utf-8\n':
continue
f.seek(0)
page = f.read()
with open(new_file_path, 'w') as f:
f.seek(0)
f.write('# -*- coding: utf-8 -*-\n')
f.write(page)
print '['+new_file_path+']'
self.count_line(new_file_path)
fc = FileConvert(new_file_path, new_file_path)
fc.convert()
print("Search in %-70s" % self.root_dir)
print("file count: %d\nline count: %d" % (self.file_num, self.line_num))
def count_line(self, filename):
"""
Count file line
"""
line_num = 0
with open(filename, 'r') as f:
while 1:
data = f.readline()
line_num += 1
if not data:
break
if self.detail:
print('%-80s%d' % (filename, line_num))
self.line_num += line_num
class FileConvert(object):
"""
File Style by Python , detect your file character and change it to utf-8 and other things.
Usage:
filestyle <src> <dest> [<origin>] [<final>] [<tabsize>]
filestyle (-h | --help)
filestyle (-v | --version)
Options:
-h --help Show this screen.
-v --version Show version.
"""
def __init__(self, src, dest, origin='utf-8', final='utf-8', tabsize=4):
self.src = src
self.dest = dest
self.origin = origin
self.final = final
self.tabsize = tabsize
self.content = []
def read_file(self):
if not self.origin:
self.origin = chardet.detect(open(self.src).read())['encoding']
with codecs.open(self.src, 'rb', encoding=self.origin) as f:
for line in f.xreadlines():
single = line.replace('\r\n', '\n').replace('\t', ' '*self.tabsize)
self.content.append(single)
def write_file(self):
with codecs.open(self.dest, 'wb', encoding=self.final) as f:
for line in self.content:
f.write(line.decode(self.origin))
def convert(self):
self.read_file()
self.write_file()
if __name__ == '__main__':
arguments = docopt(LineCount.__doc__, version='v1.0.0')
if arguments['--verbose']:
detail = True
elif arguments['--type']:
ext_file = arguments['--type']
file_type = open(ext_file, 'r').read().split('\n')
lc = LineCount(file_type=file_type, detail=detail)
lc.find_file()