aboutsummaryrefslogtreecommitdiffstats
path: root/cucutags.py
blob: 4ad62f70d47a8b074549db5e3f626d8a89552c24 (plain) (blame)
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
#!/usr/bin/python
import os
import io
import re
import logging
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
                    level=logging.INFO)


# Press "{sequence}" [@./common_steps/app.py:17]
# Start {app:w} via {type:w} [@./common_steps/app.py:42]
# Make sure that {app:w} is running [@./common_steps/app.py:59]
# {app:w} should start [@./common_steps/app.py:66]
# folder select dialog with name "{name}" is displayed
# [@./common_steps/dialogs.py:8]
# folder select dialog is displayed [@./common_steps/dialogs.py:13]
# in folder select dialog I choose "{name}" [@./common_steps/dialogs.py:19]
# file select dialog with name "{name}" is displayed
# [@./common_steps/dialogs.py:24]
# file select dialog is displayed [@./common_steps/dialogs.py:29]
# in file select dialog I select "{name}" [@./common_steps/dialogs.py:35]
# in file save dialog I save file to "{path}" clicking "{button}"
# [@./common_steps/dialogs.py:54]
# I open GApplication menu [@./common_steps/gmenu.py:12]
# I close GApplication menu [@./common_steps/gmenu.py:27]
# I click menu "{name}" in GApplication menu [@./common_steps/gmenu.py:34]
# I get submenus from GApplication [@./common_steps/gmenu.py:45]


class Target(object):
    pattern = re.compile(r"^\s*@(step|when|then)\(u'(.*)'\)")
    result = 'targets'

    def __init__(self, text, filename, lineno):
        self.text = text
        self.filename = filename
        self.lineno = int(lineno)

    def __unicode__(self):
        return "%s [@%s:%d]" % (self.text, self.filename, self.lineno)

    def __str__(self):
        return self.__unicode__().encode("utf-8")

    def ismatch(self, feature):
        return True


# sed -n -e \
#     's/^\s\+\(\*\|[Ww]hen\|[Ss]tep\|[Tt]hen\|[Gg]iven\)\s\+\(.*\)\s*$/\2/p' \
#     "$1" |sort -u
class Feature(object):
    pattern = \
        re.compile(r'^\s+(\*|[Ww]hen|[Ss]tep|[Tt]hen|[Gg]iven)\s+(.*)\s*$')
    result = 'features'

    def __init__(self, text, filename, lineno):
        self.text = text

    def __unicode__(self):
        return self.text

    def __str__(self):
        return self.__unicode__().encode("utf-8")


def ishidden(filename):
    """Is file hidden on the given OS.

    Later we can add some magic for non-Unix filesystems.
    """
    return filename[0] == "."


def process_file(cdir, filename):
    PATTERNS = {'.py': Target, '.feature': Feature}
    out = {
        'targets': [],
        'features': []
    }
    file_ext = os.path.splitext(filename)[1]
    if file_ext in PATTERNS.keys():
        ftype = PATTERNS[file_ext]

        logging.debug("cdir = %s, file = %s", cdir, filename)
        ffname = os.path.join(cdir, filename)
        with io.open(ffname) as f:
            lineno = 0
            for line in f.readlines():
                lineno += 1
                matches = ftype.pattern.search(line)
                if matches:
                    logging.debug("key = %s", ftype.result)
                    logging.debug("value = %s", matches.group(2))
                    obj = ftype(matches.group(2), ffname, lineno)
                    out[ftype.result].append(obj)

    len_results = len(out['targets']) + len(out['features'])
    if len_results:
        logging.debug("len out = %d", len_results)

    return out


def walker(startdir):
    feature_list = []
    target_list = []

    for root, dirs, files in os.walk(startdir):
        for directory in dirs:
            if ishidden(directory):
                dirs.remove(directory)

        for f in files:
            new_out = process_file(root, f)
            feature_list.extend(new_out['features'])
            target_list.extend(new_out['targets'])

    return feature_list, target_list


def match(feat, targlist):
    for trg in targlist:
        if trg.ismatch(feat):
            return trg

    return None


def matcher(features, targets):
    out = []
    for feat in features:
        trg = match(feat, targets)
        if trg:
            out.append((feat, trg.filename, trg.lineno,))

    return out


if __name__ == "__main__":
    raw = walker(os.curdir)
    res = matcher(raw[0], raw[1])
    for r in res:
        print("%s\t%s\t%s" % r)