generator.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from functools import partial
  2. class generator(object):
  3. """description of class"""
  4. def tag(tagName, content, attributeName= None, attributeValue=None, close=True):
  5. # taking attribute into account
  6. if attributeName and attributeValue:
  7. if isinstance(attributeName, list) and isinstance(attributeValue, list):
  8. minLen = min(len(attributeName),len(attributeName))
  9. attrib =''
  10. for i in range(minLen):
  11. attrib = attrib+attributeName[i]+'="'+attributeValue[i]+'" '
  12. else:
  13. attrib = attributeName+'="'+attributeValue+'"'
  14. bBalise= '<'+tagName+' '+attrib+'>'
  15. else:
  16. bBalise= '<'+tagName+'>'
  17. # taking close into account
  18. eBalise= '</'+tagName+'>' if close else ''
  19. # return
  20. return bBalise+content+eBalise
  21. imgTagWidth = partial(tag, tagName='img', content='', attributeName= ['src','width'], close=False)
  22. imgTag = partial(tag, tagName='img', content='', attributeName= 'src', close=False)
  23. bold = partial(tag, tagName='b', attributeName= None, attributeValue=None, close=True)
  24. li = partial(tag, tagName='li', attributeName= None, attributeValue=None, close=True)
  25. ul = partial(tag, tagName='ul', attributeName= None, attributeValue=None, close=True)
  26. td = partial(tag, tagName='td', attributeName= None, attributeValue=None, close=True)
  27. tr = partial(tag, tagName='tr', attributeName= None, attributeValue=None, close=True)
  28. def listItem(nameItem_item):
  29. res = ''
  30. for it in nameItem_item:
  31. res+=generator.li(content=generator.bold(content=it[0]+':')+it[1])+'\n'
  32. return generator.ul(content='\n'+res)
  33. def table(tableElt):
  34. r = ''
  35. for row in tableElt:
  36. d = ''
  37. for elt in row: d+=generator.td(content=elt)
  38. r += generator.tr(content=d)+'\n'
  39. return generator.tag('table', '\n'+r)
  40. # --------------------------------------------------------------------------------------
  41. # test
  42. # --------------------------------------------------------------------------------------
  43. def test():
  44. print(generator.tag('H1', 'HDR DataBase'))
  45. print(generator.imgTag(attributeValue='../image/test.jpg'))
  46. print(generator.listItem([['toto','titi'],['momo','mimi'],['roro','riri']]))
  47. print(generator.table([['toto','titi'],['momo','mimi'],['roro','riri']]))