~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to CODE/TEST/PYTHON/get_code.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from inspect import EndOfBlock
 
2
import tokenize
 
3
 
 
4
class BlockFinder:
 
5
    """Provide a tokeneater() method to detect the end of a code block."""
 
6
    def __init__(self):
 
7
        self.indent = 0
 
8
        self.islambda = False
 
9
        self.started = False
 
10
        self.passline = False
 
11
        self.last = 1
 
12
 
 
13
    def tokeneater(self, type, token, srow_scol, erow_ecol, line):
 
14
        srow, scol = srow_scol
 
15
        erow, ecol = erow_ecol
 
16
        if not self.started:
 
17
            # look for the first "def", "class" or "lambda"
 
18
            if token in ("def", "class", "lambda"):
 
19
                if token == "lambda":
 
20
                    self.islambda = True
 
21
                self.started = True
 
22
            self.passline = True    # skip to the end of the line
 
23
        elif type == tokenize.NEWLINE:
 
24
            self.passline = False   # stop skipping when a NEWLINE is seen
 
25
            self.last = srow
 
26
            if self.islambda:       # lambdas always end at the first NEWLINE
 
27
                raise EndOfBlock
 
28
        elif self.passline:
 
29
            pass
 
30
        elif type == tokenize.INDENT:
 
31
            self.indent = self.indent + 1
 
32
            self.passline = True
 
33
        elif type == tokenize.DEDENT:
 
34
            self.indent = self.indent - 1
 
35
            # the end of matching indent/dedent pairs end a block
 
36
            # (note that this only works for "def"/"class" blocks,
 
37
            #  not e.g. for "if: else:" or "try: finally:" blocks)
 
38
            if self.indent <= 0:
 
39
                raise EndOfBlock
 
40
        elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
 
41
            # any other token on the same indentation level end the previous
 
42
            # block as well, except the pseudo-tokens COMMENT and NL.
 
43
            raise EndOfBlock
 
44
 
 
45
blockfinder = BlockFinder()
 
46
 
 
47
def get_next_block(lines):
 
48
    block = lines
 
49
    lineno = 0
 
50
    while block:
 
51
        lines,last = get_block(block)
 
52
        block      = block[last:]
 
53
        lineno    += last
 
54
        yield lineno,lines
 
55
 
 
56
def get_block(lines):
 
57
    """Extract the block of code at the top of the given list of lines."""
 
58
    try:
 
59
        tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
 
60
    except (EndOfBlock, IndentationError):
 
61
        pass
 
62
    return lines[:blockfinder.last], blockfinder.last
 
63
 
 
64
def getblock(lines):
 
65
    """Extract the block of code at the top of the given list of lines."""
 
66
    try:
 
67
        tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
 
68
    except (EndOfBlock, IndentationError):
 
69
        pass
 
70
    return lines[:blockfinder.last]
 
71
 
 
72
lines = file("get_code.py").readlines()
 
73
 
 
74
for n,(lineno,block) in enumerate(get_next_block(lines)):
 
75
    print n
 
76
    print "block",lineno,block[-1]