- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilePython.py
More file actions
Latest commit
183 lines (161 loc) · 5.2 KB
/
Copy pathfilePython.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 -*-
importos
fromsysimportargv
# import codecs
fromfileSimpleimportFile
fromfileSimple.fileListimportFileList
fromfileSimple.listFileimportListFile
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))
classFilePy (FileList):
def__init__ (self, file=None):
iffile:
ifpathRootnotinfile: file=pathPython+file
iffile[-3:] !='.py': file=file+'.py'
ifos.sep!='/': file=file.replace ('/', os.sep)
FileList.__init__ (self, '\n', file)
self.extension='py'
defclean (self):
self.text=self.text.strip()
self.replace ('\r')
self.replace ('(', ' (')
whileself.contain (' '): self.replace (' ',' ')
whileself.contain ('\n\n'): self.replace ('\n\n','\n')
self.replace (' (', ' (')
self.replace (' ()', '()')
blankSpaces='\n\t'
forcharinblankSpaces:
whileself.contain (char+'\n'): self.replace (char+'\n','\n')
whileself.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()
deffromFile (self):
FileList.fromFile (self)
self.clean()
defhelpPy (self):
self.file='b/'+self.title+'-help.txt'
self.dataFromFile()
rangePy=self.range()
rangePy.reverse()
forpinrangePy:
flag=False
for (word, width) inwordFunction:
ifword==self[p][:width] and'def __'notinself[p]:
flag=True
ifwordin'\t\treturn ':
self[p] =self[p] +'\n'
ifflag==False: self.pop (p)
self.toFile()
classFilePyVa (File):
def__init__ (self, file=None):
iffile:
ifpathRootnotinfile: file=pathPython+file
iffile[-3:] !='.py': file=file+'.py'
ifos.sep!='/': file=file.replace ('/', os.sep)
File.__init__ (self, file)
self.extension='py'
defclean (self):
self.text=self.text.strip()
self.replace ('\r')
self.replace ('(', ' (')
whileself.contain (' '): self.replace (' ',' ')
self.replace (' (', ' (')
self.replace (' ()', '()')
blankSpaces='\n\t'
forcharinblankSpaces:
whileself.contain (char+'\n'): self.replace (char+'\n','\n')
whileself.contain ('\n '): self.replace ('\n ','\n')
self.replace ('\ndef ', '\n\ndef ')
self.replace ('\n\tdef ', '\n\n\tdef ')
self.replace ('\nclass ', '\n\nclass ')
deffromFile (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()
"""
deftest (self):
self.title=self.title+'-bis'
print ('création de %s') %self.title
self.fileFromData()
self.toFile()
defhelp (self):
self.fromFile()
self.replace ('\n\n', '\n')
flist=FileList ('\n', 'b/'+self.title+'-help.txt')
flist.helpPy (self.text)
defgitConflict (self):
ifself.contain ('===') andself.contain ('>>>'): print ('conflit git dans le fichier', self.title)
# else: print ('pas de conflit dans le fichier', self.title)
defhelpPy (self, text):
self.fromText ('\n', text)
rangePy=self.range()
rangePy.reverse()
forpinrangePy:
flag=False
for (word, width) inwordFunction:
ifword==self[p][:width] and'def __'notinself[p]:
flag=True
ifwordin'\t\treturn ':
self[p] =self[p] +'\n'
ifflag==False: self.pop (p)
self.toFile()
setattr (FileList, 'helpPy', helpPy)
classListPy (ListFile):
def__init__ (self):
ListFile.__init__ (self)
self.path=pathPython
defget (self, TagNomfile=None, sens=True):
fordirpath, SousListDossiers, subListinos.walk (self.path):
ifnotsubList: continue
range_tag=range (len (subList) -1, -1, -1)
foriinrange_tag:
ifsubList[i][-3:] !='.py': trash=subList.pop (i)
ifTagNomfileandsens:
range_tag=range (len (subList) -1, -1, -1)
foriinrange_tag:
ifTagNomfilenotinsubList[i]: trash=subList.pop (i)
elifTagNomfile:
range_tag=range (len (subList) -1, -1, -1)
foriinrange_tag:
ifTagNomfileinsubList[i]: trash=subList.pop (i)
ifsubList:
forfileinsubList:
fileTmp=FilePy (os.path.join (dirpath, file))
self.add (fileTmp)
defgitConflict (self):
forfileinself:
file.fromFile()
file.gitConflict()
if__name__!='__main__': pass
eliflen (argv) <2: print (help)
elifargv[1] =='git':
pyList=ListPy()
pyList.get()
pyList.gitConflict()
else:
pyFile=FilePy (argv[1])
pyFile.help()