-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Col() and ColGroup() class from html/construct.py to html/tags/…
…; Moved tests for constructing col and colgroup tags to separate file; Updated the docs accordingly
- Loading branch information
1 parent
99500ec
commit a4b9142
Showing
9 changed files
with
241 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module for constructing <col> tags.""" | ||
|
||
from __future__ import absolute_import | ||
|
||
from ...lib.utils import validate_attribute_values | ||
from ...templates.html.tags import col_tag | ||
|
||
|
||
class Col(object): | ||
"""Class for constructing col tag. | ||
Args: | ||
align (str): Specifies the alignment of the content related to a <col> | ||
element. | ||
char (str): Specifies the alignment of the content related to a <col> | ||
element to a character. | ||
charoff (int): Specifies the number of characters the content will be | ||
aligned from the character specified by the char attribute. | ||
span (int): Specifies the number of columns a <col> element should | ||
span. | ||
valign (str): Specifies the vertical alignment of the content related | ||
to a <col> element. | ||
width (str): Specifies the width of a <col> element. | ||
.. versionadded:: 0.2.0 | ||
""" | ||
def __init__(self, | ||
align=None, | ||
char=None, | ||
charoff=None, | ||
span=None, | ||
valign=None, | ||
width=None): | ||
self.tag = 'col' | ||
validate_attribute_values(tag=self.tag, | ||
attribute_name='align', | ||
value=align) | ||
self.validate_char_attribute(align=align, value=char) | ||
self.validate_charoff_attribute(align=align, char=char, value=charoff) | ||
validate_attribute_values(tag=self.tag, | ||
attribute_name='valign', | ||
value=valign) | ||
self.values = {'align': align, | ||
'char': char, | ||
'charoff': charoff, | ||
'span': span, | ||
'valign': valign, | ||
'width': width} | ||
|
||
def construct(self): | ||
"""Returns the constructed col tag <col>.""" | ||
return col_tag.render(self.values) | ||
|
||
def validate_char_attribute(self, align, value): | ||
"""Validates char attribute. The char attribute can only be used if | ||
the align attribute is set to "char". | ||
""" | ||
if not value: | ||
return | ||
|
||
if value and align != 'char': | ||
raise AttributeError('<col>: The char attribute can only be used ' | ||
'if the align attribute is set to "char".') | ||
|
||
def validate_charoff_attribute(self, align, char, value): | ||
"""Validates charoff attribute. The charoff attribute can only be | ||
used if the char attribute is specified and the align attribute is | ||
set to "char". | ||
""" | ||
if not value: | ||
return | ||
|
||
if value and (not char or align != 'char'): | ||
raise AttributeError('<col>: The charoff attribute can only be ' | ||
'used if the char attribute is specified and ' | ||
'the align attribute is set to "char".') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module for constructing <colgroup> tags.""" | ||
|
||
from __future__ import absolute_import | ||
|
||
from ...lib.utils import validate_attribute_values | ||
from ...templates.html.tags import colgroup_tag | ||
|
||
|
||
class ColGroup(object): | ||
"""Class for constructing colgroup tag. | ||
Args: | ||
align (str): Aligns the content in a column group. | ||
char (str): Aligns the content in a column group to a character. | ||
charoff (int): Sets the number of characters the content will be | ||
aligned from the character specified by the char attribute. | ||
span (int): Specifies the number of columns a column group should span. | ||
valign (str): Vertical aligns the content in a column group. | ||
width (str): Specifies the width of a column group. | ||
.. versionadded:: 0.2.0 | ||
""" | ||
def __init__(self, | ||
align=None, | ||
char=None, | ||
charoff=None, | ||
span=None, | ||
valign=None, | ||
width=None): | ||
self.tag = 'colgroup' | ||
validate_attribute_values(tag=self.tag, | ||
attribute_name='align', | ||
value=align) | ||
self.validate_char_attribute(align=align, value=char) | ||
self.validate_charoff_attribute(align=align, char=char, value=charoff) | ||
validate_attribute_values(tag=self.tag, | ||
attribute_name='valign', | ||
value=valign) | ||
self.values = {'align': align, | ||
'char': char, | ||
'charoff': charoff, | ||
'span': span, | ||
'valign': valign, | ||
'width': width} | ||
|
||
def construct(self): | ||
"""Returns the constructed colgroup tag <colgroup>.""" | ||
return colgroup_tag.render(self.values) | ||
|
||
def validate_char_attribute(self, align, value): | ||
"""Validates char attribute. The char attribute can only be used if | ||
the align attribute is set to "char". | ||
""" | ||
if not value: | ||
return | ||
|
||
if value and align != 'char': | ||
raise AttributeError('<colgroup>: The char attribute can only be ' | ||
'used if the align attribute is set to ' | ||
'"char".') | ||
|
||
def validate_charoff_attribute(self, align, char, value): | ||
"""Validates charoff attribute. The charoff attribute can only be | ||
used if the char attribute is specified and the align attribute is | ||
set to "char". | ||
""" | ||
if not value: | ||
return | ||
|
||
if value and (not char or align != 'char'): | ||
raise AttributeError('<colgroup>: The charoff attribute can only ' | ||
'be used if the char attribute is specified ' | ||
'and the align attribute is set to "char".') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from ..fixtures import parametrize | ||
|
||
from korona.html.tags import Col | ||
from korona.templates.html.tags import col_tag | ||
|
||
|
||
@parametrize('attributes', [ | ||
({'align': 'char'}), | ||
({'align': 'char', 'char': '.'}), | ||
({'align': 'char', 'char': '.', 'charoff': '2'}), | ||
({'align': 'left', 'span': '2'}), | ||
({'align': 'right', 'valign': 'top'}), | ||
({'width': '130'}) | ||
]) | ||
def test_construct_col_tag(attributes): | ||
"""Test for validating whether the col tag is constructed correctly or | ||
not. | ||
""" | ||
col = Col(**attributes) | ||
assert col.construct() == col_tag.render(attributes) | ||
|
||
|
||
@parametrize('attributes,exception,error_msg', [ | ||
({'char': '.'}, AttributeError, 'The char attribute can only be used'), | ||
({'charoff': '2'}, AttributeError, 'The charoff attribute can only be'), | ||
({'char': '.', 'charoff': '2'}, | ||
AttributeError, | ||
'The char attribute can only be used'), | ||
({'align': 'left', 'charoff': '2'}, | ||
AttributeError, | ||
'The charoff attribute can only be') | ||
]) | ||
def test_construct_col_tag_error(attributes, exception, error_msg): | ||
"""Test for validating col tag's attributes.""" | ||
with pytest.raises(exception) as exc: | ||
Col(**attributes) | ||
|
||
assert error_msg in str(exc) |
Oops, something went wrong.