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
| #! /usr/bin/env python3
class findBlocks():
"""
Looking for docstring using triple quotes "
or C-like comments one several lines.
"""
def __init__(self, pathOfTheFile):
self.pathOfTheFile = pathOfTheFile
def analyse(self):
blocksToFind = {
'docString': {
'open': '"""',
'close': '"""'
},
'multiComment': {
'open': '/*',
'close': '*/'
}
}
lineNo = 0
lastBlockOpened = ''
with open(
self.pathOfTheFile,
mode = 'r',
encoding = 'utf8'
) as fileToAnalyse:
for oneLine in fileToAnalyse.readlines():
print(
oneLine.rstrip(),
sep="\n"
)
lineNo += 1
# One block has been found.
if lastBlockOpened:
blockInfo = blocksToFind[lastBlockOpened]
position = self.lookFor(
stringToFind = blockInfo['close'],
stringToAnalyse = oneLine
)
if position > -1:
print(
'\t===> CLOSE {0} AT {1} IN LINE {2} : '.format(
lastBlockOpened, position, lineNo
)
)
lastBlockOpened = ''
# No block has been found.
else:
lastPosition = -1
lastBlockName = ''
for blockName, blockInfo in blocksToFind.items():
position = self.lookFor(
stringToFind = blockInfo['open'],
stringToAnalyse = oneLine
)
if position > -1 \
and (lastPosition == -1 or lastPosition > position):
lastPosition = position
lastBlockName = blockName
if lastBlockName:
lastBlockOpened = lastBlockName
print(
'\t===> OPEN {0} AT {1} IN LINE {2} : '.format(
lastBlockName, lastPosition, lineNo
)
)
def lookFor(self, stringToFind,
stringToAnalyse,
start = 0):
return stringToAnalyse.find(stringToFind, start)
##############
# SOME TESTS #
##############
if __name__ == '__main__':
pathForTest = "test_1.txt"
findBlocks(pathForTest).analyse() |
Partager