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
=== modified file 'pocketlint/formatcheck.py'
--- pocketlint/formatcheck.py 2013-11-06 15:21:20 +0000
+++ pocketlint/formatcheck.py 2014-02-05 15:51:44 +0000
@@ -389,6 +389,23 @@
389 'File does not ends with an empty line.',389 'File does not ends with an empty line.',
390 icon='info')390 icon='info')
391391
392 def check_regex_line(self, line_no, line):
393 """Check that line does not match the regular expression.
394
395 This can be used for custom checks.
396 """
397 if not self.options:
398 return
399 patterns = getattr(self.options, 'regex_line', [])
400
401 for pattern, message in patterns:
402 if re.search(pattern, line):
403 self.message(
404 line_no,
405 'Line contains flagged text. %s' % (message),
406 icon='info',
407 )
408
392409
393class AnyTextChecker(BaseChecker, AnyTextMixin):410class AnyTextChecker(BaseChecker, AnyTextMixin):
394 """Verify the text of the document."""411 """Verify the text of the document."""
@@ -400,6 +417,7 @@
400 self.check_length(line_no, line)417 self.check_length(line_no, line)
401 self.check_trailing_whitespace(line_no, line)418 self.check_trailing_whitespace(line_no, line)
402 self.check_conflicts(line_no, line)419 self.check_conflicts(line_no, line)
420 self.check_regex_line(line_no, line)
403421
404 self.check_windows_endlines()422 self.check_windows_endlines()
405423
@@ -416,6 +434,7 @@
416 self.check_trailing_whitespace(line_no, line)434 self.check_trailing_whitespace(line_no, line)
417 self.check_tab(line_no, line)435 self.check_tab(line_no, line)
418 self.check_conflicts(line_no, line)436 self.check_conflicts(line_no, line)
437 self.check_regex_line(line_no, line)
419438
420 self.check_windows_endlines()439 self.check_windows_endlines()
421440
@@ -554,6 +573,7 @@
554 line_no += 1573 line_no += 1
555 self.check_trailing_whitespace(line_no, line)574 self.check_trailing_whitespace(line_no, line)
556 self.check_conflicts(line_no, line)575 self.check_conflicts(line_no, line)
576 self.check_regex_line(line_no, line)
557577
558578
559class CSSChecker(BaseChecker, AnyTextMixin):579class CSSChecker(BaseChecker, AnyTextMixin):
@@ -590,6 +610,7 @@
590 self.check_length(line_no, line)610 self.check_length(line_no, line)
591 self.check_trailing_whitespace(line_no, line)611 self.check_trailing_whitespace(line_no, line)
592 self.check_conflicts(line_no, line)612 self.check_conflicts(line_no, line)
613 self.check_regex_line(line_no, line)
593 self.check_tab(line_no, line)614 self.check_tab(line_no, line)
594615
595 def check_css_coding_conventions(self):616 def check_css_coding_conventions(self):
@@ -675,6 +696,7 @@
675 self.encoding = match.group(1).lower()696 self.encoding = match.group(1).lower()
676 self.check_pdb(line_no, line)697 self.check_pdb(line_no, line)
677 self.check_conflicts(line_no, line)698 self.check_conflicts(line_no, line)
699 self.check_regex_line(line_no, line)
678 self.check_ascii(line_no, line)700 self.check_ascii(line_no, line)
679701
680 def check_pdb(self, line_no, line):702 def check_pdb(self, line_no, line):
@@ -786,6 +808,7 @@
786 self.check_length(line_no, line)808 self.check_length(line_no, line)
787 self.check_trailing_whitespace(line_no, line)809 self.check_trailing_whitespace(line_no, line)
788 self.check_conflicts(line_no, line)810 self.check_conflicts(line_no, line)
811 self.check_regex_line(line_no, line)
789 self.check_tab(line_no, line)812 self.check_tab(line_no, line)
790813
791814
@@ -802,6 +825,7 @@
802 line_no += 1825 line_no += 1
803 self.check_trailing_whitespace(line_no, line)826 self.check_trailing_whitespace(line_no, line)
804 self.check_conflicts(line_no, line)827 self.check_conflicts(line_no, line)
828 self.check_regex_line(line_no, line)
805 self.check_tab(line_no, line)829 self.check_tab(line_no, line)
806 last_lineno = line_no830 last_lineno = line_no
807 self.check_load()831 self.check_load()
@@ -860,6 +884,7 @@
860 self.check_trailing_whitespace(line_no, line)884 self.check_trailing_whitespace(line_no, line)
861 self.check_tab(line_no, line)885 self.check_tab(line_no, line)
862 self.check_conflicts(line_no, line)886 self.check_conflicts(line_no, line)
887 self.check_regex_line(line_no, line)
863888
864 if self.isTransition(line_no - 1):889 if self.isTransition(line_no - 1):
865 self.check_transition(line_no - 1)890 self.check_transition(line_no - 1)
@@ -1082,6 +1107,7 @@
1082 self.check_length(line_no, line)1107 self.check_length(line_no, line)
1083 self.check_trailing_whitespace(line_no, line)1108 self.check_trailing_whitespace(line_no, line)
1084 self.check_conflicts(line_no, line)1109 self.check_conflicts(line_no, line)
1110 self.check_regex_line(line_no, line)
10851111
10861112
1087def get_option_parser():1113def get_option_parser():
10881114
=== modified file 'pocketlint/tests/test_css.py'
--- pocketlint/tests/test_css.py 2013-09-05 12:41:03 +0000
+++ pocketlint/tests/test_css.py 2014-02-05 15:51:44 +0000
@@ -101,7 +101,7 @@
101class TestText(CheckerTestCase, TestAnyTextMixin):101class TestText(CheckerTestCase, TestAnyTextMixin):
102 """Verify text integration."""102 """Verify text integration."""
103103
104 def create_and_check(self, file_name, text):104 def create_and_check(self, file_name, text, options=None):
105 """Used by the TestAnyTextMixin tests."""105 """Used by the TestAnyTextMixin tests."""
106 checker = CSSChecker(file_name, text, self.reporter)106 checker = CSSChecker(file_name, text, self.reporter, options)
107 checker.check_text()107 checker.check_text()
108108
=== modified file 'pocketlint/tests/test_go.py'
--- pocketlint/tests/test_go.py 2013-10-19 14:34:40 +0000
+++ pocketlint/tests/test_go.py 2014-02-05 15:51:44 +0000
@@ -15,8 +15,8 @@
15class TestText(CheckerTestCase, TestAnyTextMixin):15class TestText(CheckerTestCase, TestAnyTextMixin):
16 """Verify text integration."""16 """Verify text integration."""
1717
18 def create_and_check(self, file_name, text):18 def create_and_check(self, file_name, text, options=None):
19 checker = GOChecker(file_name, text, self.reporter)19 checker = GOChecker(file_name, text, self.reporter, options)
20 checker.check()20 checker.check()
2121
22 def test_long_length(self):22 def test_long_length(self):
2323
=== modified file 'pocketlint/tests/test_javascript.py'
--- pocketlint/tests/test_javascript.py 2013-10-09 12:22:58 +0000
+++ pocketlint/tests/test_javascript.py 2014-02-05 15:51:44 +0000
@@ -96,9 +96,9 @@
96class TestText(CheckerTestCase, TestAnyTextMixin):96class TestText(CheckerTestCase, TestAnyTextMixin):
97 """Verify text integration."""97 """Verify text integration."""
9898
99 def create_and_check(self, file_name, text):99 def create_and_check(self, file_name, text, options=None):
100 """Used by the TestAnyTextMixin tests."""100 """Used by the TestAnyTextMixin tests."""
101 checker = JavascriptChecker(file_name, text, self.reporter)101 checker = JavascriptChecker(file_name, text, self.reporter, options)
102 checker.check_text()102 checker.check_text()
103103
104 def test_code_with_debugger(self):104 def test_code_with_debugger(self):
105105
=== modified file 'pocketlint/tests/test_python.py'
--- pocketlint/tests/test_python.py 2013-02-16 18:03:51 +0000
+++ pocketlint/tests/test_python.py 2014-02-05 15:51:44 +0000
@@ -248,9 +248,9 @@
248 # pep8 checks this.248 # pep8 checks this.
249 pass249 pass
250250
251 def create_and_check(self, file_name, text):251 def create_and_check(self, file_name, text, options=None):
252 """Used by the TestAnyTextMixin tests."""252 """Used by the TestAnyTextMixin tests."""
253 checker = PythonChecker(file_name, text, self.reporter)253 checker = PythonChecker(file_name, text, self.reporter, options)
254 checker.check_text()254 checker.check_text()
255255
256 def test_code_without_issues(self):256 def test_code_without_issues(self):
257257
=== modified file 'pocketlint/tests/test_sql.py'
--- pocketlint/tests/test_sql.py 2013-01-17 22:08:07 +0000
+++ pocketlint/tests/test_sql.py 2014-02-05 15:51:44 +0000
@@ -15,8 +15,8 @@
15class TestSQL(CheckerTestCase, TestAnyTextMixin):15class TestSQL(CheckerTestCase, TestAnyTextMixin):
16 """Verify text integration."""16 """Verify text integration."""
1717
18 def create_and_check(self, file_name, text):18 def create_and_check(self, file_name, text, options=None):
19 checker = SQLChecker(file_name, text, self.reporter)19 checker = SQLChecker(file_name, text, self.reporter, options)
20 checker.check()20 checker.check()
2121
22 def test_long_length(self):22 def test_long_length(self):
2323
=== modified file 'pocketlint/tests/test_text.py'
--- pocketlint/tests/test_text.py 2013-01-20 21:49:28 +0000
+++ pocketlint/tests/test_text.py 2014-02-05 15:51:44 +0000
@@ -11,13 +11,13 @@
11 AnyTextChecker,11 AnyTextChecker,
12 get_option_parser,12 get_option_parser,
13)13)
14from pocketlint.tests import CheckerTestCase14from pocketlint.tests import Bunch, CheckerTestCase
1515
1616
17class TestAnyTextMixin:17class TestAnyTextMixin:
18 """A mixin that provides common text tests."""18 """A mixin that provides common text tests."""
1919
20 def create_and_check(self, file_name, text):20 def create_and_check(self, file_name, text, options=None):
21 raise NotImplemented21 raise NotImplemented
2222
23 def test_without_conflicts(self):23 def test_without_conflicts(self):
@@ -60,6 +60,29 @@
60 [(1, 'Line contains a tab character.')],60 [(1, 'Line contains a tab character.')],
61 self.reporter.messages)61 self.reporter.messages)
6262
63 def test_regex_line(self):
64 """
65 A list of regex and corresponding error messages can be passed to
66 check each line.
67 """
68 options = Bunch(
69 max_line_length=80,
70 regex_line=[
71 ('.*marker.*', 'Explanation.'),
72 ('.*sign.*', 'Message.'),
73 ])
74
75 self.create_and_check(
76 'bogus',
77 'with marker here\n other sign here', options)
78
79 self.assertEqual([
80 (1, 'Line contains flagged text. Explanation.'),
81 (2, 'Line contains flagged text. Message.'),
82 ],
83 self.reporter.messages,
84 )
85
6386
64class TestText(CheckerTestCase, TestAnyTextMixin):87class TestText(CheckerTestCase, TestAnyTextMixin):
65 """Verify text integration."""88 """Verify text integration."""
6689
=== modified file 'pocketlint/tests/test_xml.py'
--- pocketlint/tests/test_xml.py 2013-01-27 13:31:27 +0000
+++ pocketlint/tests/test_xml.py 2014-02-05 15:51:44 +0000
@@ -110,9 +110,9 @@
110class TestText(CheckerTestCase, TestAnyTextMixin):110class TestText(CheckerTestCase, TestAnyTextMixin):
111 """Verify text integration."""111 """Verify text integration."""
112112
113 def create_and_check(self, file_name, text):113 def create_and_check(self, file_name, text, options=None):
114 """Used by the TestAnyTextMixin tests."""114 """Used by the TestAnyTextMixin tests."""
115 checker = XMLChecker(file_name, text, self.reporter)115 checker = XMLChecker(file_name, text, self.reporter, options)
116 checker.check_text()116 checker.check_text()
117117
118 def test_long_length(self):118 def test_long_length(self):

Subscribers

People subscribed via source and target branches

to all changes: