Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit ef4211b

Browse files
committed
Export ErrorRegistry to reStructuredText.
1 parent cd1747f commit ef4211b

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

pep257.py

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ class Error(object):
381381

382382
# should be overridden by inheriting classes
383383
code = None
384-
_message = None
384+
short_desc = None
385+
context = None
385386

386387
# Options that define how errors are printed:
387388
explain = False
@@ -401,7 +402,10 @@ def set_context(self, definition, explanation):
401402

402403
@property
403404
def message(self):
404-
return '%s: %s' % (self.code, self._message) % self.parameters
405+
ret = '%s: %s' % (self.code, self.short_desc)
406+
if self.context is not None:
407+
ret += ' (' + self.context % self.parameters + ')'
408+
return ret
405409

406410
@property
407411
def lines(self):
@@ -446,18 +450,19 @@ class ErrorRegistry(object):
446450
groups = []
447451

448452
class ErrorGroup(object):
449-
errors = []
450453

451454
def __init__(self, prefix, name):
452455
self.prefix = prefix
453456
self.name = name
457+
self.errors = []
454458

455-
def create_error(self, error_code, error_message):
459+
def create_error(self, error_code, error_desc, error_context=None):
456460
# TODO: check prefix
457461

458462
class _Error(Error):
459463
code = error_code
460-
_message = error_message
464+
short_desc = error_desc
465+
context = error_context
461466

462467
self.errors.append(_Error)
463468
return _Error
@@ -470,13 +475,26 @@ def create_group(cls, prefix, name):
470475

471476
@classmethod
472477
def get_error_codes(cls):
473-
# TODO: implement
474-
pass
478+
for group in cls.groups:
479+
for error in group.errors:
480+
yield error
475481

476482
@classmethod
477483
def to_rst(cls):
478-
# TODO: implement
479-
pass
484+
sep_line = '+' + 6 * '-' + '+' + '-' * 71 + '+\n'
485+
blank_line = '|' + 78 * ' ' + '|\n'
486+
table = ''
487+
for group in cls.groups:
488+
table += sep_line
489+
table += blank_line
490+
table += '|' + ('**%s**' % group.name).center(78) + '|\n'
491+
table += blank_line
492+
for error in group.errors:
493+
table += sep_line
494+
table += ('|' + error.code.center(6) + '| ' +
495+
error.short_desc.ljust(70) + '|\n')
496+
table += sep_line
497+
return table
480498

481499

482500
D1xx = ErrorRegistry.create_group('D1', 'Missing Docstrings')
@@ -487,17 +505,17 @@ def to_rst(cls):
487505

488506
D2xx = ErrorRegistry.create_group('D2', 'Whitespace Issues')
489507
D200 = D2xx.create_error('D200', 'One-line docstring should fit on one line '
490-
'with quotes, found %s')
508+
'with quotes', 'found %s')
491509
D201 = D2xx.create_error('D201', 'No blank lines allowed before function '
492-
'docstring, found %s')
510+
'docstring', 'found %s')
493511
D202 = D2xx.create_error('D202', 'No blank lines allowed after function '
494-
'docstring, found %s')
512+
'docstring', 'found %s')
495513
D203 = D2xx.create_error('D203', '1 blank line required before class '
496-
'docstring, found %s')
514+
'docstring', 'found %s')
497515
D204 = D2xx.create_error('D204', '1 blank line required after class '
498-
'docstring, found %s')
516+
'docstring', 'found %s')
499517
D205 = D2xx.create_error('D205', '1 blank line required between summary line '
500-
'and description, found %s')
518+
'and description', 'found %s')
501519
D206 = D2xx.create_error('D206', 'Docstring should be indented with spaces, '
502520
'not tabs')
503521
D207 = D2xx.create_error('D207', 'Docstring is under-indented')
@@ -508,15 +526,16 @@ def to_rst(cls):
508526
'docstring text')
509527

510528
D3xx = ErrorRegistry.create_group('D3', 'Quotes Issues')
511-
D300 = D3xx.create_error('D300', 'Use """triple double quotes""", found '
512-
'%s-quotes')
529+
D300 = D3xx.create_error('D300', 'Use """triple double quotes"""',
530+
'found %s-quotes')
513531
D301 = D3xx.create_error('D301', 'Use r""" if any backslashes in a docstring')
514532
D302 = D3xx.create_error('D302', 'Use u""" for Unicode docstrings')
515533

516534
D4xx = ErrorRegistry.create_group('D4', 'Docstring Content Issues')
517-
D400 = D4xx.create_error('D400', 'First line should end with a period, not %r')
518-
D401 = D4xx.create_error('D401', 'First line should be in imperative mood: '
519-
'%r, not %r')
535+
D400 = D4xx.create_error('D400', 'First line should end with a period',
536+
'not %r')
537+
D401 = D4xx.create_error('D401', 'First line should be in imperative mood',
538+
'%r, not %r')
520539
D402 = D4xx.create_error('D402', 'First line should not be the function\'s '
521540
'"signature"')
522541

test.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,29 @@ def nested():
4949
''
5050

5151

52-
@expect('D200: One-line docstring should fit on one line with quotes, found 3')
52+
@expect('D200: One-line docstring should fit on one line with quotes '
53+
'(found 3)')
5354
def asdlkfasd():
5455
"""
5556
Wrong.
5657
"""
5758

5859

59-
@expect('D201: No blank lines allowed before function docstring, found 1')
60+
@expect('D201: No blank lines allowed before function docstring (found 1)')
6061
def leading_space():
6162

6263
"""Leading space."""
6364

6465

65-
@expect('D202: No blank lines allowed after function docstring, found 1')
66+
@expect('D202: No blank lines allowed after function docstring (found 1)')
6667
def trailing_space():
6768
"""Leading space."""
6869

6970
pass
7071

7172

72-
@expect('D201: No blank lines allowed before function docstring, found 1')
73-
@expect('D202: No blank lines allowed after function docstring, found 1')
73+
@expect('D201: No blank lines allowed before function docstring (found 1)')
74+
@expect('D202: No blank lines allowed after function docstring (found 1)')
7475
def trailing_and_leading_space():
7576

7677
"""Trailing and leading space."""
@@ -79,15 +80,15 @@ def trailing_and_leading_space():
7980

8081

8182
expect('LeadingSpaceMissing',
82-
'D203: 1 blank line required before class docstring, found 0')
83+
'D203: 1 blank line required before class docstring (found 0)')
8384

8485

8586
class LeadingSpaceMissing:
8687
"""Leading space missing."""
8788

8889

8990
expect('TrailingSpace',
90-
'D204: 1 blank line required after class docstring, found 0')
91+
'D204: 1 blank line required after class docstring (found 0)')
9192

9293

9394
class TrailingSpace:
@@ -97,27 +98,27 @@ class TrailingSpace:
9798

9899

99100
expect('LeadingAndTrailingSpaceMissing',
100-
'D203: 1 blank line required before class docstring, found 0')
101+
'D203: 1 blank line required before class docstring (found 0)')
101102
expect('LeadingAndTrailingSpaceMissing',
102-
'D204: 1 blank line required after class docstring, found 0')
103+
'D204: 1 blank line required after class docstring (found 0)')
103104

104105

105106
class LeadingAndTrailingSpaceMissing:
106107
"""Leading and trailing space missing."""
107108
pass
108109

109110

110-
@expect('D205: 1 blank line required between summary line and description, '
111-
'found 0')
111+
@expect('D205: 1 blank line required between summary line and description '
112+
'(found 0)')
112113
def multi_line_zero_separating_blanks():
113114
"""Summary.
114115
Description.
115116
116117
"""
117118

118119

119-
@expect('D205: 1 blank line required between summary line and description, '
120-
'found 2')
120+
@expect('D205: 1 blank line required between summary line and description '
121+
'(found 2)')
121122
def multi_line_two_separating_blanks():
122123
"""Summary.
123124
@@ -206,12 +207,12 @@ def multiline():
206207
"""
207208

208209

209-
@expect('D300: Use """triple double quotes""", found \'\'\'-quotes')
210+
@expect('D300: Use """triple double quotes""" (found \'\'\'-quotes)')
210211
def lsfklkjllkjl():
211212
r'''Summary.'''
212213

213214

214-
@expect('D300: Use """triple double quotes""", found \'-quotes')
215+
@expect('D300: Use """triple double quotes""" (found \'-quotes)')
215216
def lalskklkjllkjl():
216217
r'Summary.'
217218

@@ -227,13 +228,13 @@ def lasewnlkjl():
227228
"""Юникод."""
228229

229230

230-
@expect("D400: First line should end with a period, not 'y'")
231+
@expect("D400: First line should end with a period (not 'y')")
231232
def lwnlkjl():
232233
"""Summary"""
233234

234235

235-
@expect("D401: First line should be in imperative mood: 'Return', not "
236-
"'Returns'")
236+
@expect("D401: First line should be in imperative mood ('Return', not "
237+
"'Returns')")
237238
def liouiwnlkjl():
238239
"""Returns foo."""
239240

@@ -263,7 +264,7 @@ def old_209():
263264
def oneliner_d102(): return
264265

265266

266-
@expect("D400: First line should end with a period, not 'r'")
267+
@expect("D400: First line should end with a period (not 'r')")
267268
def oneliner_withdoc(): """One liner"""
268269

269270

0 commit comments

Comments
 (0)