Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- pyhtml.py
- HTML utility functions
- TODO: Finish converting all functions the Tag subclasses.
- """
- import types
- import sys
- class Tag:
- """ Dynamic tag building class.
- name = Tag identifier (p, html, head, b, strong, etc)
- value = Contents of the tag.
- attrs = String, List or Dictionary of attributes to include in the tag.
- close = Boolean, weather a closing tag should be output or not, defaults
- to True.
- """
- def __init__(self, name, value="", attrs=None, close=True):
- self.name = name
- self.value = value
- self.close = close
- self.attrs = " "
- self.addAttrs(attrs)
- def addAttrs(self, attrs):
- if attrs and type(attrs) == types.StringType:
- self.attrs += attrs
- elif attrs and type(attrs) == types.ListType:
- for attr in attrs:
- self.attrs += "%s " % attr
- elif attrs and type(attrs) == types.DictionaryType:
- for key in attrs:
- self.attrs += '%s="%s" ' % (key, attrs[key])
- def __closedStr(self):
- out = ""
- if self.attrs:
- out = '<%s %s>%s</%s>' % (self.name, self.attrs, self.value, self.name)
- else:
- out = '<%s>%s</%s>' % (self.name, self.value, self.name)
- return out
- def __openStr(self):
- if self.attrs:
- out = '<%s %s />' % (self.name, self.attrs)
- else:
- out = '<%s />' % (self.name)
- return out
- def __str__(self):
- if self.close:
- return self.__closedStr()
- else:
- return self.__openStr()
- class Form(Tag):
- def __init__(self, method="", action="get", content="", attrs=None):
- Tag.__init__(self, name="form", value=content, attrs=attrs, close=True)
- Tag.addAttrs(self, {'method':method, 'action':action})
- class Option(Tag):
- def __init__(self, value="", content="", selected=False):
- Tag.__init__(self, name="option", value="content", attrs={'value':value})
- if selected:
- Tag.addAttrs(self, "SELECTED")
- class Select:
- def __init__(self, content="", name=""):
- self.content = content
- self.name = name
- def __str__(self):
- return '<select name="%s">%s</select>' % (self.name, self.content)
- class P:
- def __init__(self, content="", extra=""):
- self.content = content
- self.extra = extra
- def __str__(self):
- if self.extra:
- return '<p>%s</p>' % self.content
- else:
- return '<p %s>%s</p>' % (self.extra, self.content)
- class TableDataCell:
- pass
- class TableHeaderCell:
- pass
- class TableRow:
- pass
- class TableHeader(TableRow):
- pass
- def option(value="", txt="", selected=False):
- is_selected = ""
- if selected:
- is_selected = "SELECTED"
- return '<option value="%s" %s>%s</option>' % (value, is_selected, txt)
- def form(method="", action="GET", style="",content=""):
- return '<form method="%s" action="%s" style="%s">%s</form>"' % (method, action, style, content)
- def select(name="", content=""):
- return '<select name="%s">%s</select>' % (name, content)
- def pstyle(content, style):
- return '<p style="%s">%s</p>' % (style, content)
- def pclass(content, klass):
- return '<p class="%s">%s</p>' % (klass, content)
- def pid(content, id):
- return '<p id="%s">%s</p>' % (id, content)
- def pstyleclass(content, style, klass):
- return '<p class="%s" style="%s">%s</p>' % (klass, style, content)
- def pstyleid(content, style, id):
- return '<p id="%s" style="%s">%s</p>' % (id, style, content)
- def pclassid(content, klass, id):
- return '<p id="%s" class="%s">%s</p>' % (id, klass, content)
- def pclassstyleid(content, klass, style, id):
- return '<p id="%s" class="%s" style="%s">%s</p>' % (id, style, klass, content)
- def p(content="", style=None, klass=None, id=None):
- if style and not (klass or id):
- return pstyle(content, style)
- elif klass and not (style or id):
- return pclass(content, klass)
- elif id and not (style or klass):
- return pid(content, id)
- elif (style and klass) and not id:
- return pstyleclass(content, style, klass)
- elif (style and id) and not klass:
- return pstyleid(content, id)
- elif (klass and id) and not style:
- return pclassid(content, klass, id)
- elif (klass and style and id):
- return pclassstyleid(content, klass, style, id)
- else:
- return '<p>%s</p>' % content
- def pstyle(content="",style=""):
- return '<p style="%s">%s</p>' % (content, style)
- def ahref(location="", name=""):
- return '<a href="%s">%s</a>' % (location, name)
- def th(content):
- return '<th>%s</th>' % content
- def tr(content):
- return '<tr>%s</tr>' % content
- def td(content):
- return '<td>%s</td>' % content
- def table_from_dict(content):
- """ Accepts a dictionary with the following keys:
- cols = optional integer
- rows = optionsl integer
- table_header = option list of strings
- table_rows = required list of lists, e.g [['one','two'],['three','four']]
- border = optional integer or string
- style = optional string
- class = optional string
- id = optional string
- A sample dictionary using all the keys would be:
- {'cols':3, 'rows':3,
- 'table_header':['head1','head2','head3'],
- 'table_rows':[['one','two','three'],
- ['four','five','six'],
- ['seven','eight','nine']],
- 'border':0,
- 'style':"text-align: center",
- 'class':'tableclassname',
- 'id':'atableid'}
- Raises:
- pythml.InvalidTableDictionary if table_rows key is missing
- """
- class InvalidTableDictionary(Exception):
- def __init__(self, value):
- self.value = value
- def __str__(self):
- return repr(self.value)
- num_rows = 0
- num_cols = 0
- border = 0
- style = ""
- klass = ""
- id = ""
- if not content.has_key('table_rows'):
- raise InvalidTableDictionary(1)
- if content.has_key('border'):
- border = 'border="%s"' % str(border)
- if content.has_key('style'):
- style = 'style="%s"' % content['style']
- if content.has_key('class'):
- klass = 'class="%s"' % content['class']
- if content.has_key('id'):
- id = 'id="%s"' % content['id']
- # if cols & rows supplied, use them
- if content.has_key('cols') and content.has_key('rows'):
- num_rows = content['rows']
- num_cols = content['cols']
- else: # calculate our own rows
- # find the no. of cols
- for row in content['table_rows']:
- if len(row) > num_cols:
- num_cols = len(row)
- # find the no. of rows
- num_rows = len(content['table_rows'])
- if content.has_key('table_header'):
- num_rows += 1
- out = '<table cols="%i" rows="%i" %s %s %s %s>' % (num_cols, num_rows, border,
- id, klass, style)
- if content.has_key('table_header'):
- s = ""
- for item in content['table_header']:
- s += th(item)
- out += tr(s)
- for row in content['table_rows']:
- s = ""
- for item in row:
- s += td(item)
- out += tr(s)
- out += '</table>'
- return out
- def table_from_string(content):
- return '<table>%s</table>' % content
- def table(content):
- if type(content) is types.DictionaryType:
- return table_from_dict(content)
- elif type(content) is types.StringType:
- return table_from_string(content)
- return None
- def html(content):
- return '<html>%s</html>' % content
- def head(content):
- return '<head>%s</head>' % content
- def body(content):
- return '<body>%</body>' % content
- def input(type="", name="", value=""):
- return '<input type="%s" name="%s" value="%s"' % (type, name, value)
Add Comment
Please, Sign In to add comment