Skip to content

Commit

Permalink
PEP8.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphbean committed Mar 13, 2012
1 parent bc3020d commit 3c77ec8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
31 changes: 22 additions & 9 deletions tw2/forms/datagrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

__all__ = ["DataGrid", "Column"]


class Column(object):
"""Simple struct that describes a single DataGrid column.
Expand All @@ -18,11 +19,12 @@ class Column(object):

def __init__(self, name, getter=None, title=None, options=None):
if not name:
raise ValueError, 'name is required'
raise ValueError('name is required')

if getter:
if callable(getter):
self.getter = getter
else: # assume it's an attribute name
else: # assume it's an attribute name
self.getter = operator.attrgetter(getter)
else:
self.getter = attrwrapper(name)
Expand All @@ -33,19 +35,21 @@ def __init__(self, name, getter=None, title=None, options=None):
def get_option(self, name, default=NoDefault):
if name in self.options:
return self.options[name]
if default is NoDefault: # no such key and no default is given
if default is NoDefault: # no such key and no default is given
raise KeyError(name)
return default

def get_field(self, row, displays_on=None):
if getattr(self.getter, '__bases__', None) and issubclass(self.getter, twc.Widget) or\
if getattr(self.getter, '__bases__', None) and \
issubclass(self.getter, twc.Widget) or \
isinstance(self.getter, twc.Widget):
return self.getter.display(value=row, displays_on=displays_on)
return self.getter(row)

def __str__(self):
return "<Column %s>" % self.name


class DataGrid(twc.Widget):
"""Generic widget to present and manipulate data in a grid (tabular) form.
Expand All @@ -58,18 +62,27 @@ class DataGrid(twc.Widget):
or dynamically, via 'fields' key.
"""
resources = [twc.CSSLink(modname='tw2.forms', filename='static/datagrid/datagrid.css')]
resources = [
twc.CSSLink(modname='tw2.forms',
filename='static/datagrid/datagrid.css')
]
template = "tw2.forms.templates.datagrid"

css_class = twc.Param('CSS class name', default='grid', attribute=True, view_name='class')

css_class = twc.Param(
'CSS class name',
default='grid',
attribute=True,
view_name='class'
)
fields = twc.Param('Fields of the Grid', default=[], attribute=False)

@staticmethod
def get_field_getter(columns):
"""Return a function to access the fields of table by row, col."""
idx = {} # index columns by name
idx = {} # index columns by name
for col in columns:
idx[col.name] = col

def _get_field(row, col):
return idx[col].get_field(row)
return _get_field
Expand All @@ -83,7 +96,7 @@ def _parse(self, fields):
"""
columns = []
names = {} # keep track of names to ensure there are no dups
names = {} # keep track of names to ensure there are no dups
for n, col in enumerate(fields):
if not isinstance(col, Column):
if isinstance(col, str) or callable(col):
Expand Down
1 change: 1 addition & 0 deletions tw2/forms/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class DemoTextField(twf.TextField):
placeholder = "Search..."


class DemoChildren(twc.CompoundWidget):
title = twf.TextField()
priority = twf.SingleSelectField(options=['', 'Normal', 'High'])
Expand Down
7 changes: 5 additions & 2 deletions tw2/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class InputField(FormField):
value = twc.Param(attribute=True)
template = "tw2.forms.templates.input_field"


class PostlabeledInputField(InputField):
""" Inherits :class:`InputField`, but with a :attr:`text`
label that follows the input field """
Expand All @@ -42,14 +43,16 @@ class PostlabeledInputField(InputField):

class TextField(InputField):
size = twc.Param('Size of the field', default=None, attribute=True)
placeholder = twc.Param('Placeholder text (HTML5 Only)', attribute=True, default=None)
placeholder = twc.Param(
'Placeholder text (HTML5 Only)', attribute=True, default=None)
type = 'text'


class TextArea(FormField):
rows = twc.Param('Number of rows', default=None, attribute=True)
cols = twc.Param('Number of columns', default=None, attribute=True)
placeholder = twc.Param('Placeholder text (HTML5 Only)', attribute=True, default=None)
placeholder = twc.Param(
'Placeholder text (HTML5 Only)', attribute=True, default=None)
template = "tw2.forms.templates.textarea"


Expand Down

0 comments on commit 3c77ec8

Please sign in to comment.