Merge lp:~adiroiban/pocket-lint/line-regex into lp:pocket-lint

Proposed by Adi Roiban
Status: Merged
Merged at revision: 446
Proposed branch: lp:~adiroiban/pocket-lint/line-regex
Merge into: lp:pocket-lint
Diff against target: 241 lines (+63/-14)
8 files modified
pocketlint/formatcheck.py (+26/-0)
pocketlint/tests/test_css.py (+2/-2)
pocketlint/tests/test_go.py (+2/-2)
pocketlint/tests/test_javascript.py (+2/-2)
pocketlint/tests/test_python.py (+2/-2)
pocketlint/tests/test_sql.py (+2/-2)
pocketlint/tests/test_text.py (+25/-2)
pocketlint/tests/test_xml.py (+2/-2)
To merge this branch: bzr merge lp:~adiroiban/pocket-lint/line-regex
Reviewer Review Type Date Requested Status
Curtis Hovey code Approve
Review via email: mp+204985@code.launchpad.net

Description of the change

This branch add a simple way of creating custom line checks based on regex.

Please let me know if you find this useful and I can improve its quality.

Thanks!

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'pocketlint/formatcheck.py'
2--- pocketlint/formatcheck.py 2013-11-06 15:21:20 +0000
3+++ pocketlint/formatcheck.py 2014-02-05 15:51:44 +0000
4@@ -389,6 +389,23 @@
5 'File does not ends with an empty line.',
6 icon='info')
7
8+ def check_regex_line(self, line_no, line):
9+ """Check that line does not match the regular expression.
10+
11+ This can be used for custom checks.
12+ """
13+ if not self.options:
14+ return
15+ patterns = getattr(self.options, 'regex_line', [])
16+
17+ for pattern, message in patterns:
18+ if re.search(pattern, line):
19+ self.message(
20+ line_no,
21+ 'Line contains flagged text. %s' % (message),
22+ icon='info',
23+ )
24+
25
26 class AnyTextChecker(BaseChecker, AnyTextMixin):
27 """Verify the text of the document."""
28@@ -400,6 +417,7 @@
29 self.check_length(line_no, line)
30 self.check_trailing_whitespace(line_no, line)
31 self.check_conflicts(line_no, line)
32+ self.check_regex_line(line_no, line)
33
34 self.check_windows_endlines()
35
36@@ -416,6 +434,7 @@
37 self.check_trailing_whitespace(line_no, line)
38 self.check_tab(line_no, line)
39 self.check_conflicts(line_no, line)
40+ self.check_regex_line(line_no, line)
41
42 self.check_windows_endlines()
43
44@@ -554,6 +573,7 @@
45 line_no += 1
46 self.check_trailing_whitespace(line_no, line)
47 self.check_conflicts(line_no, line)
48+ self.check_regex_line(line_no, line)
49
50
51 class CSSChecker(BaseChecker, AnyTextMixin):
52@@ -590,6 +610,7 @@
53 self.check_length(line_no, line)
54 self.check_trailing_whitespace(line_no, line)
55 self.check_conflicts(line_no, line)
56+ self.check_regex_line(line_no, line)
57 self.check_tab(line_no, line)
58
59 def check_css_coding_conventions(self):
60@@ -675,6 +696,7 @@
61 self.encoding = match.group(1).lower()
62 self.check_pdb(line_no, line)
63 self.check_conflicts(line_no, line)
64+ self.check_regex_line(line_no, line)
65 self.check_ascii(line_no, line)
66
67 def check_pdb(self, line_no, line):
68@@ -786,6 +808,7 @@
69 self.check_length(line_no, line)
70 self.check_trailing_whitespace(line_no, line)
71 self.check_conflicts(line_no, line)
72+ self.check_regex_line(line_no, line)
73 self.check_tab(line_no, line)
74
75
76@@ -802,6 +825,7 @@
77 line_no += 1
78 self.check_trailing_whitespace(line_no, line)
79 self.check_conflicts(line_no, line)
80+ self.check_regex_line(line_no, line)
81 self.check_tab(line_no, line)
82 last_lineno = line_no
83 self.check_load()
84@@ -860,6 +884,7 @@
85 self.check_trailing_whitespace(line_no, line)
86 self.check_tab(line_no, line)
87 self.check_conflicts(line_no, line)
88+ self.check_regex_line(line_no, line)
89
90 if self.isTransition(line_no - 1):
91 self.check_transition(line_no - 1)
92@@ -1082,6 +1107,7 @@
93 self.check_length(line_no, line)
94 self.check_trailing_whitespace(line_no, line)
95 self.check_conflicts(line_no, line)
96+ self.check_regex_line(line_no, line)
97
98
99 def get_option_parser():
100
101=== modified file 'pocketlint/tests/test_css.py'
102--- pocketlint/tests/test_css.py 2013-09-05 12:41:03 +0000
103+++ pocketlint/tests/test_css.py 2014-02-05 15:51:44 +0000
104@@ -101,7 +101,7 @@
105 class TestText(CheckerTestCase, TestAnyTextMixin):
106 """Verify text integration."""
107
108- def create_and_check(self, file_name, text):
109+ def create_and_check(self, file_name, text, options=None):
110 """Used by the TestAnyTextMixin tests."""
111- checker = CSSChecker(file_name, text, self.reporter)
112+ checker = CSSChecker(file_name, text, self.reporter, options)
113 checker.check_text()
114
115=== modified file 'pocketlint/tests/test_go.py'
116--- pocketlint/tests/test_go.py 2013-10-19 14:34:40 +0000
117+++ pocketlint/tests/test_go.py 2014-02-05 15:51:44 +0000
118@@ -15,8 +15,8 @@
119 class TestText(CheckerTestCase, TestAnyTextMixin):
120 """Verify text integration."""
121
122- def create_and_check(self, file_name, text):
123- checker = GOChecker(file_name, text, self.reporter)
124+ def create_and_check(self, file_name, text, options=None):
125+ checker = GOChecker(file_name, text, self.reporter, options)
126 checker.check()
127
128 def test_long_length(self):
129
130=== modified file 'pocketlint/tests/test_javascript.py'
131--- pocketlint/tests/test_javascript.py 2013-10-09 12:22:58 +0000
132+++ pocketlint/tests/test_javascript.py 2014-02-05 15:51:44 +0000
133@@ -96,9 +96,9 @@
134 class TestText(CheckerTestCase, TestAnyTextMixin):
135 """Verify text integration."""
136
137- def create_and_check(self, file_name, text):
138+ def create_and_check(self, file_name, text, options=None):
139 """Used by the TestAnyTextMixin tests."""
140- checker = JavascriptChecker(file_name, text, self.reporter)
141+ checker = JavascriptChecker(file_name, text, self.reporter, options)
142 checker.check_text()
143
144 def test_code_with_debugger(self):
145
146=== modified file 'pocketlint/tests/test_python.py'
147--- pocketlint/tests/test_python.py 2013-02-16 18:03:51 +0000
148+++ pocketlint/tests/test_python.py 2014-02-05 15:51:44 +0000
149@@ -248,9 +248,9 @@
150 # pep8 checks this.
151 pass
152
153- def create_and_check(self, file_name, text):
154+ def create_and_check(self, file_name, text, options=None):
155 """Used by the TestAnyTextMixin tests."""
156- checker = PythonChecker(file_name, text, self.reporter)
157+ checker = PythonChecker(file_name, text, self.reporter, options)
158 checker.check_text()
159
160 def test_code_without_issues(self):
161
162=== modified file 'pocketlint/tests/test_sql.py'
163--- pocketlint/tests/test_sql.py 2013-01-17 22:08:07 +0000
164+++ pocketlint/tests/test_sql.py 2014-02-05 15:51:44 +0000
165@@ -15,8 +15,8 @@
166 class TestSQL(CheckerTestCase, TestAnyTextMixin):
167 """Verify text integration."""
168
169- def create_and_check(self, file_name, text):
170- checker = SQLChecker(file_name, text, self.reporter)
171+ def create_and_check(self, file_name, text, options=None):
172+ checker = SQLChecker(file_name, text, self.reporter, options)
173 checker.check()
174
175 def test_long_length(self):
176
177=== modified file 'pocketlint/tests/test_text.py'
178--- pocketlint/tests/test_text.py 2013-01-20 21:49:28 +0000
179+++ pocketlint/tests/test_text.py 2014-02-05 15:51:44 +0000
180@@ -11,13 +11,13 @@
181 AnyTextChecker,
182 get_option_parser,
183 )
184-from pocketlint.tests import CheckerTestCase
185+from pocketlint.tests import Bunch, CheckerTestCase
186
187
188 class TestAnyTextMixin:
189 """A mixin that provides common text tests."""
190
191- def create_and_check(self, file_name, text):
192+ def create_and_check(self, file_name, text, options=None):
193 raise NotImplemented
194
195 def test_without_conflicts(self):
196@@ -60,6 +60,29 @@
197 [(1, 'Line contains a tab character.')],
198 self.reporter.messages)
199
200+ def test_regex_line(self):
201+ """
202+ A list of regex and corresponding error messages can be passed to
203+ check each line.
204+ """
205+ options = Bunch(
206+ max_line_length=80,
207+ regex_line=[
208+ ('.*marker.*', 'Explanation.'),
209+ ('.*sign.*', 'Message.'),
210+ ])
211+
212+ self.create_and_check(
213+ 'bogus',
214+ 'with marker here\n other sign here', options)
215+
216+ self.assertEqual([
217+ (1, 'Line contains flagged text. Explanation.'),
218+ (2, 'Line contains flagged text. Message.'),
219+ ],
220+ self.reporter.messages,
221+ )
222+
223
224 class TestText(CheckerTestCase, TestAnyTextMixin):
225 """Verify text integration."""
226
227=== modified file 'pocketlint/tests/test_xml.py'
228--- pocketlint/tests/test_xml.py 2013-01-27 13:31:27 +0000
229+++ pocketlint/tests/test_xml.py 2014-02-05 15:51:44 +0000
230@@ -110,9 +110,9 @@
231 class TestText(CheckerTestCase, TestAnyTextMixin):
232 """Verify text integration."""
233
234- def create_and_check(self, file_name, text):
235+ def create_and_check(self, file_name, text, options=None):
236 """Used by the TestAnyTextMixin tests."""
237- checker = XMLChecker(file_name, text, self.reporter)
238+ checker = XMLChecker(file_name, text, self.reporter, options)
239 checker.check_text()
240
241 def test_long_length(self):

Subscribers

People subscribed via source and target branches

to all changes: