Skip to content

Commit

Permalink
Moved Col() and ColGroup() class from html/construct.py to html/tags/…
Browse files Browse the repository at this point in the history
…; Moved tests for constructing col and colgroup tags to separate file; Updated the docs accordingly
  • Loading branch information
bharadwajyarlagadda committed Sep 15, 2016
1 parent 99500ec commit a4b9142
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 215 deletions.
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ API Reference
:members:


.. autoclass:: korona.html.construct.Col
.. autoclass:: korona.html.tags.Col
:members:


.. autoclass:: korona.html.construct.ColGroup
.. autoclass:: korona.html.tags.ColGroup
:members:


Expand Down
4 changes: 2 additions & 2 deletions docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Korona can build <col> tag.

.. code-block:: python
from korona.html.construct import Col
from korona.html.tags import Col
attributes = {'align': 'char', 'char': '.', 'charoff': '2'}
Expand Down Expand Up @@ -370,7 +370,7 @@ Korona can build <colgroup> tag.

.. code-block:: python
from korona.html.construct import ColGroup
from korona.html.tags import ColGroup
attributes = {'align': 'char', 'char': '.', 'charoff': '2'}
Expand Down
139 changes: 0 additions & 139 deletions korona/html/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
)

from ..templates.html import (
col_tag,
colgroup_tag,
dd_tag,
del_tag,
details_tag,
Expand Down Expand Up @@ -39,143 +37,6 @@
CIRCLE_SHAPE_COORDINATES = 3


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".')


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".')


class DD(object):
"""Class for constructing dd tag.
Expand Down
2 changes: 2 additions & 0 deletions korona/html/tags/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
from .canvas import Canvas
from .caption import Caption
from .cite import Cite
from .col import Col
from .colgroup import ColGroup

from .heading import H1, H2, H3, H4, H5, H6
77 changes: 77 additions & 0 deletions korona/html/tags/col.py
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".')
74 changes: 74 additions & 0 deletions korona/html/tags/colgroup.py
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".')
42 changes: 42 additions & 0 deletions tests/test_html_tags/test_col_tag.py
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)
Loading

0 comments on commit a4b9142

Please sign in to comment.