Flake8 corrections

remotes/origin/enhancement/email-actions
Jesús Espino 2014-05-22 13:14:28 +02:00
parent 5e95175654
commit bb73c2c06d
10 changed files with 61 additions and 42 deletions

View File

@ -6,6 +6,7 @@
import re
import markdown
# We can't re-use the built-in AutolinkPattern because we need to add protocols
# to links without them.
class AutolinkPattern(markdown.inlinepatterns.Pattern):
@ -20,6 +21,7 @@ class AutolinkPattern(markdown.inlinepatterns.Pattern):
el.text = markdown.util.AtomicString(m.group(2))
return el
class AutolinkExtension(markdown.Extension):
"""An extension that turns all URLs into links.

View File

@ -3,9 +3,9 @@
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import re
import markdown
# We can't re-use the built-in AutomailPattern because we need to add mailto:.
# We also don't care about HTML-encoding the email.
class AutomailPattern(markdown.inlinepatterns.Pattern):
@ -15,6 +15,7 @@ class AutomailPattern(markdown.inlinepatterns.Pattern):
el.text = markdown.util.AtomicString(m.group(2))
return el
class AutomailExtension(markdown.Extension):
"""An extension that turns all email addresses into links."""

View File

@ -1,4 +1,4 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# Tested on Markdown 2.3.1
#
@ -26,7 +26,6 @@
import re
import os
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
@ -167,7 +166,7 @@ class EmojifyPreprocessor(Preprocessor):
def emojify(match):
emoji = match.group(1)
if not emoji in emojis_set:
if emoji not in emojis_set:
return match.group(0)
url = emojis_path + emoji + u'.png'

View File

@ -1,4 +1,4 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# Tested on Markdown 2.3.1
#
@ -23,10 +23,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import re
import os
from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
from markdown.util import etree
@ -39,9 +35,7 @@ class MentionsExtension(Extension):
MENTION_RE = r'(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9-]+)'
mentionsPattern = MentionsPattern(MENTION_RE)
mentionsPattern.md = md
md.inlinePatterns.add('mentions',
mentionsPattern,
'_begin')
md.inlinePatterns.add('mentions', mentionsPattern, '_begin')
class MentionsPattern(Pattern):

View File

@ -1,4 +1,4 @@
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# Tested on Markdown 2.3.1
#
@ -23,10 +23,6 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import re
import os
from markdown.extensions import Extension
from markdown.inlinepatterns import Pattern
from markdown.util import etree
@ -43,9 +39,8 @@ class TaigaReferencesExtension(Extension):
TAIGA_REFERENCE_RE = r'(?<=^|(?<=[^a-zA-Z0-9-\[]))#(\d+)'
referencesPattern = TaigaReferencesPattern(TAIGA_REFERENCE_RE, self.project)
referencesPattern.md = md
md.inlinePatterns.add('taiga-references',
referencesPattern,
'_begin')
md.inlinePatterns.add('taiga-references', referencesPattern, '_begin')
class TaigaReferencesPattern(Pattern):
def __init__(self, pattern, project):
@ -73,7 +68,6 @@ class TaigaReferencesPattern(Pattern):
else:
return "#{}".format(obj_ref)
url = "/#/project/{}/{}/{}".format(
self.project.slug,
obj_section,

View File

@ -2,15 +2,17 @@
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import re
import markdown
class SemiSaneOListProcessor(markdown.blockprocessors.OListProcessor):
SIBLING_TAGS = ['ol']
class SemiSaneUListProcessor(markdown.blockprocessors.UListProcessor):
SIBLING_TAGS = ['ul']
class SemiSaneListExtension(markdown.Extension):
"""An extension that causes lists to be treated the same way GitHub does.

View File

@ -20,6 +20,7 @@ SPACED_IMAGE_LINK_RE = markdown.inlinepatterns.IMAGE_LINK_RE.replace(
SPACED_IMAGE_REFERENCE_RE = markdown.inlinepatterns.IMAGE_REFERENCE_RE.replace(
r'\!' + BRK, r'\!' + BRK + SPACE)
class SpacedLinkExtension(markdown.Extension):
"""An extension that supports links and images with additional whitespace.

View File

@ -6,6 +6,7 @@ import markdown
STRIKE_RE = r'(~{2})(.+?)(~{2})' # ~~strike~~
class StrikethroughExtension(markdown.Extension):
"""An extension that supports PHP-Markdown style strikethrough.

View File

@ -5,20 +5,21 @@ from markdown.inlinepatterns import Pattern
from markdown.util import etree
import re
def build_url(label, base, end):
""" Build a url from the label, a base, and an end. """
clean_label = re.sub(r'([ ]+_)|(_[ ]+)|([ ]+)', '_', label)
return '%s%s%s'% (base, clean_label, end)
return '%s%s%s' % (base, clean_label, end)
class WikiLinkExtension(Extension):
def __init__(self, configs):
# set extension defaults
self.config = {
'base_url' : ['/', 'String to append to beginning or URL.'],
'end_url' : ['/', 'String to append to end of URL.'],
'html_class' : ['wikilink', 'CSS hook. Leave blank for none.'],
'build_url' : [build_url, 'Callable formats URL from label.'],
'base_url': ['/', 'String to append to beginning or URL.'],
'end_url': ['/', 'String to append to end of URL.'],
'html_class': ['wikilink', 'CSS hook. Leave blank for none.'],
'build_url': [build_url, 'Callable formats URL from label.'],
}
configs = dict(configs) or {}
# Override defaults with user settings

View File

@ -1,14 +1,8 @@
from unittest.mock import patch, MagicMock
import pytest
import taiga.base
from taiga.mdrender.extensions import mentions
from taiga.mdrender.extensions import emojify
from taiga.mdrender.service import render, cache_by_sha, get_diff_of_htmls, render_and_extract
from taiga.projects.references import services
from taiga.users.models import User
from datetime import datetime
@ -17,14 +11,17 @@ dummy_project = MagicMock()
dummy_project.id = 1
dummy_project.slug = "test"
def test_proccessor_valid_emoji():
result = emojify.EmojifyPreprocessor().run(["**:smile:**"])
assert result == ["**![smile](http://localhost:8000/static/img/emojis/smile.png)**"]
def test_proccessor_invalid_emoji():
result = emojify.EmojifyPreprocessor().run(["**:notvalidemoji:**"])
assert result == ["**:notvalidemoji:**"]
def test_proccessor_valid_user_mention():
with patch("taiga.mdrender.extensions.mentions.User") as mock:
instance = mock.objects.get.return_value
@ -33,6 +30,7 @@ def test_proccessor_valid_user_mention():
expected_result = "<p><strong><a alt=\"test name\" class=\"mention\" href=\"/#/profile/user1\" title=\"test name\">&commat;user1</a></strong></p>"
assert result == expected_result
def test_proccessor_invalid_user_mention():
with patch("taiga.mdrender.extensions.mentions.User") as mock:
mock.DoesNotExist = User.DoesNotExist
@ -40,6 +38,7 @@ def test_proccessor_invalid_user_mention():
result = render(dummy_project, "**@notvaliduser**")
assert result == '<p><strong>@notvaliduser</strong></p>'
def test_proccessor_valid_us_reference():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
instance = mock.return_value
@ -49,97 +48,117 @@ def test_proccessor_valid_us_reference():
expected_result = '<p><strong><a alt="test" class="reference user-story" href="/#/project/test/user-story/1" title="test">&num;1</a></strong></p>'
assert result == expected_result
def test_proccessor_valid_issue_reference():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
instance = mock.return_value
instance.content_type.model = "issue"
instance.content_object.subject = "test"
result = render(dummy_project, "**#1**")
expected_result = '<p><strong><a alt="test" class="reference issue" href="/#/project/test/issues/1" title="test">&num;1</a></strong></p>'
result = render(dummy_project, "**#2**")
expected_result = '<p><strong><a alt="test" class="reference issue" href="/#/project/test/issues/2" title="test">&num;2</a></strong></p>'
assert result == expected_result
def test_proccessor_valid_task_reference():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
instance = mock.return_value
instance.content_type.model = "task"
instance.content_object.subject = "test"
result = render(dummy_project, "**#1**")
expected_result = '<p><strong><a alt="test" class="reference task" href="/#/project/test/tasks/1" title="test">&num;1</a></strong></p>'
result = render(dummy_project, "**#3**")
expected_result = '<p><strong><a alt="test" class="reference task" href="/#/project/test/tasks/3" title="test">&num;3</a></strong></p>'
assert result == expected_result
def test_proccessor_invalid_type_reference():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
instance = mock.return_value
instance.content_type.model = "other"
instance.content_object.subject = "test"
result = render(dummy_project, "**#1**")
assert result == "<p><strong>#1</strong></p>"
result = render(dummy_project, "**#4**")
assert result == "<p><strong>#4</strong></p>"
def test_proccessor_invalid_reference():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
mock.return_value = None
result = render(dummy_project, "**#1**")
assert result == "<p><strong>#1</strong></p>"
result = render(dummy_project, "**#5**")
assert result == "<p><strong>#5</strong></p>"
def test_render_wiki_strong():
assert render(dummy_project, "**test**") == "<p><strong>test</strong></p>"
assert render(dummy_project, "__test__") == "<p><strong>test</strong></p>"
def test_render_wiki_italic():
assert render(dummy_project, "*test*") == "<p><em>test</em></p>"
assert render(dummy_project, "_test_") == "<p><em>test</em></p>"
def test_render_wiki_strike():
assert render(dummy_project, "~~test~~") == "<p><del>test</del></p>"
def test_render_absolute_link():
assert render(dummy_project, "[test](/test)") == "<p><a href=\"/test\">test</a></p>"
def test_render_relative_link():
assert render(dummy_project, "[test](test)") == "<p><a href=\"test\">test</a></p>"
def test_render_wikilink():
expected_result = "<p><a class=\"wikilink\" href=\"#/project/test/wiki/test\">test</a></p>"
assert render(dummy_project, "[[test]]") == expected_result
def test_render_wikilink_with_custom_title():
expected_result = "<p><a class=\"wikilink\" href=\"#/project/test/wiki/test\">custom</a></p>"
assert render(dummy_project, "[[test|custom]]") == expected_result
def test_render_reference_links():
expected_result = "<p>An <a href=\"http://example.com/\" title=\"Title\">example</a> of reference link</p>"
source = "An [example][id] of reference link\n [id]: http://example.com/ \"Title\""
assert render(dummy_project, source) == expected_result
def test_render_url_autolinks():
expected_result = "<p>Test the <a href=\"http://example.com/\">http://example.com/</a> autolink</p>"
source = "Test the http://example.com/ autolink"
assert render(dummy_project, source) == expected_result
def test_render_url_autolinks_without_http():
expected_result = "<p>Test the <a href=\"http://www.example.com\">www.example.com</a> autolink</p>"
source = "Test the www.example.com autolink"
assert render(dummy_project, source) == expected_result
def test_render_url_automail():
expected_result = "<p>Test the <a href=\"mailto:example@example.com\">example@example.com</a> automail</p>"
source = "Test the example@example.com automail"
assert render(dummy_project, source) == expected_result
def test_render_absolute_image():
assert render(dummy_project, "![test](/test.png)") == "<p><img alt=\"test\" src=\"/test.png\" /></p>"
def test_render_relative_image():
assert render(dummy_project, "![test](test.png)") == "<p><img alt=\"test\" src=\"test.png\" /></p>"
def test_render_triple_quote_code():
expected_result = "<div class=\"codehilite\"><pre><span class=\"n\">print</span><span class=\"p\">(</span><span class=\"s\">&quot;test&quot;</span><span class=\"p\">)</span>\n</pre></div>"
assert render(dummy_project, "```\nprint(\"test\")\n```") == expected_result
def test_render_triple_quote_and_lang_code():
expected_result = "<div class=\"codehilite\"><pre><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s\">&quot;test&quot;</span><span class=\"p\">)</span>\n</pre></div>"
assert render(dummy_project, "```python\nprint(\"test\")\n```") == expected_result
def test_cache_by_sha():
@cache_by_sha
def test_cache(project, text):
@ -151,18 +170,22 @@ def test_cache_by_sha():
result3 = test_cache(dummy_project, "test")
assert result1 == result3
def test_get_diff_of_htmls_insertions():
result = get_diff_of_htmls("", "<p>test</p>")
assert result == "<ins style=\"background:#e6ffe6;\">&lt;p&gt;test&lt;/p&gt;</ins>"
def test_get_diff_of_htmls_deletions():
result = get_diff_of_htmls("<p>test</p>", "")
assert result == "<del style=\"background:#ffe6e6;\">&lt;p&gt;test&lt;/p&gt;</del>"
def test_get_diff_of_htmls_modifications():
result = get_diff_of_htmls("<p>test1</p>", "<p>1test</p>")
assert result == "<span>&lt;p&gt;</span><ins style=\"background:#e6ffe6;\">1</ins><span>test</span><del style=\"background:#ffe6e6;\">1</del><span>&lt;/p&gt;</span>"
def test_render_and_extract_references():
with patch("taiga.mdrender.extensions.references.get_instance_by_ref") as mock:
instance = mock.return_value
@ -171,6 +194,7 @@ def test_render_and_extract_references():
(_, extracted) = render_and_extract(dummy_project, "**#1**")
assert extracted['references'] == [instance.content_object]
def test_render_and_extract_mentions():
with patch("taiga.mdrender.extensions.mentions.User") as mock:
instance = mock.objects.get.return_value