Guest User

Untitled

a guest
Apr 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. """
  2. pyhtml.py
  3.  
  4. HTML utility functions
  5.  
  6. TODO: Finish converting all functions the Tag subclasses.
  7. """
  8. import types
  9. import sys
  10.  
  11. class Tag:
  12. """ Dynamic tag building class.
  13.  
  14. name = Tag identifier (p, html, head, b, strong, etc)
  15. value = Contents of the tag.
  16. attrs = String, List or Dictionary of attributes to include in the tag.
  17. close = Boolean, weather a closing tag should be output or not, defaults
  18. to True.
  19. """
  20. def __init__(self, name, value="", attrs=None, close=True):
  21. self.name = name
  22. self.value = value
  23. self.close = close
  24. self.attrs = " "
  25. self.addAttrs(attrs)
  26.  
  27. def addAttrs(self, attrs):
  28. if attrs and type(attrs) == types.StringType:
  29. self.attrs += attrs
  30. elif attrs and type(attrs) == types.ListType:
  31. for attr in attrs:
  32. self.attrs += "%s " % attr
  33. elif attrs and type(attrs) == types.DictionaryType:
  34. for key in attrs:
  35. self.attrs += '%s="%s" ' % (key, attrs[key])
  36.  
  37. def __closedStr(self):
  38. out = ""
  39. if self.attrs:
  40. out = '<%s %s>%s</%s>' % (self.name, self.attrs, self.value, self.name)
  41. else:
  42. out = '<%s>%s</%s>' % (self.name, self.value, self.name)
  43. return out
  44.  
  45. def __openStr(self):
  46. if self.attrs:
  47. out = '<%s %s />' % (self.name, self.attrs)
  48. else:
  49. out = '<%s />' % (self.name)
  50. return out
  51.  
  52. def __str__(self):
  53. if self.close:
  54. return self.__closedStr()
  55. else:
  56. return self.__openStr()
  57.  
  58. class Form(Tag):
  59. def __init__(self, method="", action="get", content="", attrs=None):
  60. Tag.__init__(self, name="form", value=content, attrs=attrs, close=True)
  61. Tag.addAttrs(self, {'method':method, 'action':action})
  62.  
  63. class Option(Tag):
  64. def __init__(self, value="", content="", selected=False):
  65. Tag.__init__(self, name="option", value="content", attrs={'value':value})
  66. if selected:
  67. Tag.addAttrs(self, "SELECTED")
  68.  
  69. class Select:
  70. def __init__(self, content="", name=""):
  71. self.content = content
  72. self.name = name
  73. def __str__(self):
  74. return '<select name="%s">%s</select>' % (self.name, self.content)
  75.  
  76. class P:
  77. def __init__(self, content="", extra=""):
  78. self.content = content
  79. self.extra = extra
  80. def __str__(self):
  81. if self.extra:
  82. return '<p>%s</p>' % self.content
  83. else:
  84. return '<p %s>%s</p>' % (self.extra, self.content)
  85.  
  86. class TableDataCell:
  87. pass
  88.  
  89. class TableHeaderCell:
  90. pass
  91.  
  92. class TableRow:
  93. pass
  94.  
  95. class TableHeader(TableRow):
  96. pass
  97.  
  98. def option(value="", txt="", selected=False):
  99. is_selected = ""
  100. if selected:
  101. is_selected = "SELECTED"
  102. return '<option value="%s" %s>%s</option>' % (value, is_selected, txt)
  103.  
  104. def form(method="", action="GET", style="",content=""):
  105. return '<form method="%s" action="%s" style="%s">%s</form>"' % (method, action, style, content)
  106.  
  107. def select(name="", content=""):
  108. return '<select name="%s">%s</select>' % (name, content)
  109.  
  110. def pstyle(content, style):
  111. return '<p style="%s">%s</p>' % (style, content)
  112.  
  113. def pclass(content, klass):
  114. return '<p class="%s">%s</p>' % (klass, content)
  115.  
  116. def pid(content, id):
  117. return '<p id="%s">%s</p>' % (id, content)
  118.  
  119. def pstyleclass(content, style, klass):
  120. return '<p class="%s" style="%s">%s</p>' % (klass, style, content)
  121.  
  122. def pstyleid(content, style, id):
  123. return '<p id="%s" style="%s">%s</p>' % (id, style, content)
  124.  
  125. def pclassid(content, klass, id):
  126. return '<p id="%s" class="%s">%s</p>' % (id, klass, content)
  127.  
  128. def pclassstyleid(content, klass, style, id):
  129. return '<p id="%s" class="%s" style="%s">%s</p>' % (id, style, klass, content)
  130.  
  131. def p(content="", style=None, klass=None, id=None):
  132. if style and not (klass or id):
  133. return pstyle(content, style)
  134. elif klass and not (style or id):
  135. return pclass(content, klass)
  136. elif id and not (style or klass):
  137. return pid(content, id)
  138. elif (style and klass) and not id:
  139. return pstyleclass(content, style, klass)
  140. elif (style and id) and not klass:
  141. return pstyleid(content, id)
  142. elif (klass and id) and not style:
  143. return pclassid(content, klass, id)
  144. elif (klass and style and id):
  145. return pclassstyleid(content, klass, style, id)
  146. else:
  147. return '<p>%s</p>' % content
  148.  
  149. def pstyle(content="",style=""):
  150. return '<p style="%s">%s</p>' % (content, style)
  151.  
  152. def ahref(location="", name=""):
  153. return '<a href="%s">%s</a>' % (location, name)
  154.  
  155. def th(content):
  156. return '<th>%s</th>' % content
  157.  
  158. def tr(content):
  159. return '<tr>%s</tr>' % content
  160.  
  161. def td(content):
  162. return '<td>%s</td>' % content
  163.  
  164. def table_from_dict(content):
  165. """ Accepts a dictionary with the following keys:
  166. cols = optional integer
  167. rows = optionsl integer
  168. table_header = option list of strings
  169. table_rows = required list of lists, e.g [['one','two'],['three','four']]
  170. border = optional integer or string
  171. style = optional string
  172. class = optional string
  173. id = optional string
  174.  
  175. A sample dictionary using all the keys would be:
  176.  
  177. {'cols':3, 'rows':3,
  178. 'table_header':['head1','head2','head3'],
  179. 'table_rows':[['one','two','three'],
  180. ['four','five','six'],
  181. ['seven','eight','nine']],
  182. 'border':0,
  183. 'style':"text-align: center",
  184. 'class':'tableclassname',
  185. 'id':'atableid'}
  186.  
  187. Raises:
  188. pythml.InvalidTableDictionary if table_rows key is missing
  189. """
  190. class InvalidTableDictionary(Exception):
  191. def __init__(self, value):
  192. self.value = value
  193. def __str__(self):
  194. return repr(self.value)
  195.  
  196. num_rows = 0
  197. num_cols = 0
  198. border = 0
  199. style = ""
  200. klass = ""
  201. id = ""
  202.  
  203. if not content.has_key('table_rows'):
  204. raise InvalidTableDictionary(1)
  205.  
  206. if content.has_key('border'):
  207. border = 'border="%s"' % str(border)
  208. if content.has_key('style'):
  209. style = 'style="%s"' % content['style']
  210. if content.has_key('class'):
  211. klass = 'class="%s"' % content['class']
  212. if content.has_key('id'):
  213. id = 'id="%s"' % content['id']
  214.  
  215. # if cols & rows supplied, use them
  216. if content.has_key('cols') and content.has_key('rows'):
  217. num_rows = content['rows']
  218. num_cols = content['cols']
  219. else: # calculate our own rows
  220. # find the no. of cols
  221. for row in content['table_rows']:
  222. if len(row) > num_cols:
  223. num_cols = len(row)
  224. # find the no. of rows
  225. num_rows = len(content['table_rows'])
  226. if content.has_key('table_header'):
  227. num_rows += 1
  228.  
  229. out = '<table cols="%i" rows="%i" %s %s %s %s>' % (num_cols, num_rows, border,
  230. id, klass, style)
  231. if content.has_key('table_header'):
  232. s = ""
  233. for item in content['table_header']:
  234. s += th(item)
  235. out += tr(s)
  236. for row in content['table_rows']:
  237. s = ""
  238. for item in row:
  239. s += td(item)
  240. out += tr(s)
  241. out += '</table>'
  242. return out
  243.  
  244. def table_from_string(content):
  245. return '<table>%s</table>' % content
  246.  
  247. def table(content):
  248. if type(content) is types.DictionaryType:
  249. return table_from_dict(content)
  250. elif type(content) is types.StringType:
  251. return table_from_string(content)
  252. return None
  253.  
  254. def html(content):
  255. return '<html>%s</html>' % content
  256.  
  257. def head(content):
  258. return '<head>%s</head>' % content
  259.  
  260. def body(content):
  261. return '<body>%</body>' % content
  262.  
  263. def input(type="", name="", value=""):
  264. return '<input type="%s" name="%s" value="%s"' % (type, name, value)
Add Comment
Please, Sign In to add comment