-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilePython.py
More file actions
183 lines (161 loc) · 5.2 KB
/
filePython.py
File metadata and controls
183 lines (161 loc) · 5.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import os
from sys import argv
# import codecs
from fileSimple import File
from fileSimple.fileList import FileList
from fileSimple.listFile import ListFile
help ="""lancement: python filePy
python mantisDeb
python debutils/debutils/text
pas besoin de rajouter l'extension .py.
si votre fichier est dans le dossier python, vous pouvez indiquer le chemin à partir de ce dossier.
précisez pathRoot et pathPython."""
pathRoot = 'C:\\Users\\deborah.powers' + os.sep
pathPython = pathRoot + 'python' + os.sep
wordImport = (('import ', 7), ('from ', 5))
wordFunction = (('import ', 7), ('from ', 5), ('class ', 6), ('def ', 4), ('\t""" ', 5), ('\treturn', 7), ('\tdef ', 5), ('\t\t""" ', 6), ('\t\treturn', 8))
class FilePy (FileList):
def __init__ (self, file =None):
if file:
if pathRoot not in file: file = pathPython + file
if file[-3:] != '.py': file = file + '.py'
if os.sep != '/': file = file.replace ('/', os.sep)
FileList.__init__ (self, '\n', file)
self.extension = 'py'
def clean (self):
self.text = self.text.strip()
self.replace ('\r')
self.replace ('(', ' (')
while self.contain (' '): self.replace (' ',' ')
while self.contain ('\n\n'): self.replace ('\n\n','\n')
self.replace (' (', ' (')
self.replace (' ()', '()')
blankSpaces = '\n \t'
for char in blankSpaces:
while self.contain (char +'\n'): self.replace (char +'\n','\n')
while self.contain ('\n '): self.replace ('\n ','\n')
self.replace ('\ndef ', '\n\ndef ')
self.replace ('\n\tdef ', '\n\n\tdef ')
self.replace ('\nclass ', '\n\nclass ')
self.fromText()
def fromFile (self):
FileList.fromFile (self)
self.clean()
def helpPy (self):
self.file = 'b/' + self.title + '-help.txt'
self.dataFromFile()
rangePy = self.range()
rangePy.reverse()
for p in rangePy:
flag = False
for (word, width) in wordFunction:
if word == self[p][:width] and 'def __' not in self[p]:
flag = True
if word in '\t\treturn ':
self[p] = self[p] +'\n'
if flag == False: self.pop (p)
self.toFile()
class FilePyVa (File):
def __init__ (self, file =None):
if file:
if pathRoot not in file: file = pathPython + file
if file[-3:] != '.py': file = file + '.py'
if os.sep != '/': file = file.replace ('/', os.sep)
File.__init__ (self, file)
self.extension = 'py'
def clean (self):
self.text = self.text.strip()
self.replace ('\r')
self.replace ('(', ' (')
while self.contain (' '): self.replace (' ',' ')
self.replace (' (', ' (')
self.replace (' ()', '()')
blankSpaces = '\n \t'
for char in blankSpaces:
while self.contain (char +'\n'): self.replace (char +'\n','\n')
while self.contain ('\n '): self.replace ('\n ','\n')
self.replace ('\ndef ', '\n\ndef ')
self.replace ('\n\tdef ', '\n\n\tdef ')
self.replace ('\nclass ', '\n\nclass ')
def fromFile (self):
File.fromFile (self)
self.clean()
"""
def fromFile (self):
if not os.path.exists (self.file): return
self.dataFromFile()
# ouvrir le file et recuperer le texte au format str
textBrut = open (self.file, 'rb')
tmpByte = textBrut.read()
encodingList = ('utf-8', 'ascii', 'ISO-8859-1')
for encoding in encodingList:
try: self.text = codecs.decode (tmpByte, encoding=encoding)
except UnicodeDecodeError: pass
else: break
textBrut.close()
self.clean()
"""
def test (self):
self.title = self.title +'-bis'
print ('création de %s') % self.title
self.fileFromData()
self.toFile()
def help (self):
self.fromFile()
self.replace ('\n\n', '\n')
flist = FileList ('\n', 'b/' + self.title + '-help.txt')
flist.helpPy (self.text)
def gitConflict (self):
if self.contain ('===') and self.contain ('>>>'): print ('conflit git dans le fichier', self.title)
# else: print ('pas de conflit dans le fichier', self.title)
def helpPy (self, text):
self.fromText ('\n', text)
rangePy = self.range()
rangePy.reverse()
for p in rangePy:
flag = False
for (word, width) in wordFunction:
if word == self[p][:width] and 'def __' not in self[p]:
flag = True
if word in '\t\treturn ':
self[p] = self[p] +'\n'
if flag == False: self.pop (p)
self.toFile()
setattr (FileList, 'helpPy', helpPy)
class ListPy (ListFile):
def __init__ (self):
ListFile.__init__ (self)
self.path = pathPython
def get (self, TagNomfile=None, sens=True):
for dirpath, SousListDossiers, subList in os.walk (self.path):
if not subList: continue
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if subList[i][-3:] != '.py': trash = subList.pop (i)
if TagNomfile and sens:
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if TagNomfile not in subList[i]: trash = subList.pop (i)
elif TagNomfile:
range_tag = range (len (subList) -1, -1, -1)
for i in range_tag:
if TagNomfile in subList[i]: trash = subList.pop (i)
if subList:
for file in subList:
fileTmp = FilePy (os.path.join (dirpath, file))
self.add (fileTmp)
def gitConflict (self):
for file in self:
file.fromFile()
file.gitConflict()
if __name__ != '__main__': pass
elif len (argv) <2: print (help)
elif argv[1] == 'git':
pyList = ListPy()
pyList.get()
pyList.gitConflict()
else:
pyFile = FilePy (argv[1])
pyFile.help()