Skip to content

Commit

Permalink
Factor out strip_rst_directives function
Browse files Browse the repository at this point in the history
  • Loading branch information
erik committed Jan 11, 2019
1 parent fdcfef9 commit 4e2c8e9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/api/squabble.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ squabble package
squabble.reporter
squabble.rule
squabble.rules
squabble.util
7 changes: 7 additions & 0 deletions docs/api/squabble.util.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
squabble.util module
====================

.. automodule:: squabble.util
:members:
:undoc-members:
:show-inheritance:
11 changes: 3 additions & 8 deletions squabble/message.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import inspect
import logging
import re

from squabble import SquabbleException
from squabble.util import strip_rst_directives


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -134,7 +134,7 @@ def explain(cls):
... SELECT * FROM big_table;
... '''
>>> RstMessage().explain()
"Don't do this:\\n\\n SELECT * FROM big_table;"
"Don't do this:\\n SELECT * FROM big_table;"
"""
if not cls.__doc__:
return None
Expand All @@ -143,12 +143,7 @@ def explain(cls):
doc = inspect.cleandoc(cls.__doc__)

# replace ``.. code-block:: lang``
return re.sub(
r'^\s*\.\. code(-block)?:: (\w+)$',
'',
doc,
flags=re.MULTILINE
)
return strip_rst_directives(doc)

def asdict(self):
return {
Expand Down
3 changes: 2 additions & 1 deletion squabble/rules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect

from squabble.rule import Registry
from squabble.util import strip_rst_directives


class BaseRule:
Expand Down Expand Up @@ -55,7 +56,7 @@ def meta(cls):

help = None
if len(split_doc) == 2:
help = split_doc[1].strip()
help = strip_rst_directives(split_doc[1])

return {
'name': cls.__name__,
Expand Down
1 change: 0 additions & 1 deletion squabble/rules/require_foreign_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class RequireForeignKey(BaseRule):
the regex ``.*_id$``, but this is configurable.
.. code-block:: sql
CREATE TABLE comments (
post_id INT, -- warning here, this looks like a foreign key,
-- but no constraint was given
Expand Down
22 changes: 22 additions & 0 deletions squabble/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Odds and ends pieces that don't fit elsewhere, but aren't important
enough to have their own modules.
"""

import re


_RST_DIRECTIVE = re.compile(r'^\.\. [\w-]+:: \w+$\n', flags=re.MULTILINE)


def strip_rst_directives(string):
"""
Strip reStructuredText directives out of a block of text.
Lines containing a directive will be stripped out entirely
>>> strip_rst_directives('hello\\n.. code-block:: foo\\nworld')
'hello\\nworld'
"""

return re.sub(_RST_DIRECTIVE, '', string)

0 comments on commit 4e2c8e9

Please sign in to comment.