#!/usr/bin/python
# -----------------------------------------------------------------------
# Copyright (C) 2003 Gustavo Sverzut Barbieri.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
# -----------------------------------------------------------------------
#
# This code is part of the pytvgrab project:
# http://pytvgrab.sourceforge.net
#
# -----------------------------------------------------------------------
# Subversion Information, do not edit
#
# $Rev: 291 $
# $LastChangedDate: 2004-11-21 23:37:37 +1100 (Sun, 21 Nov 2004) $
# $LastChangedRevision: 291 $
# $LastChangedBy: ottrey $
#
# $Log: $
#
class Tag:
"""
This class represents a HTML Tag.
It have a name (name), attributes (attrs) and children,
which are other Tag elements.
"""
__author__ = "Gustavo Sverzut Barbieri <gustavo@linuxdicas.com.br>"
__revision__ = "$Revision: 1.1 $"
verbose = 0
name = ""
attrs = [ ]
cdata = ""
children = [ ]
tabstop = 3
table_decor = " border=\"3\" cellpadding=\"5\" " + \
" cellspacing=\"2\" style=\"" + \
"border: #ff0000 2px dashed; margin: 5px\""
def __init__( self, name, attrs=None, cdata="", children=None, verbose=0 ):
"""
name: the name of this tag.
attrs: the attributes of this tag. It's a list of tuples
( 'name', 'value' )
cdata: the contents data of this tag.
children: the children of this tag (other Tag()s).
verbose: set this to 1, then when printing the tag <table>, it will add
some color to it's border, so you can understand the document
layout (some are a hell of table inside other tables!)
"""
self.name = name
self.cdata = cdata
self.attrs = attrs or [ ]
self.children = children or [ ]
self.verbose = verbose
# __init__()
def getAttribute( self, name ):
"""
Return the attribute value or None, if the attribute isn't present
"""
for attr in self.attrs:
if attr[ 0 ] == name:
return attr[ 1 ]
return None
# getAttribute()
def setAttribute( self, name, value ):
"""
Change the attribute value or add it to the attribute list if it
was not present.
If value==None, then the attribute will be removed from list.
"""
v = self.getAttribute( name )
if v != None:
self.attrs.remove( ( name, v ) )
if value != None:
self.attrs += [ ( name, value ) ]
# setAttribute()
def delAttribute( self, name ):
"""
Remove, if exists, the attribute called 'name'
"""
self.setAttribute( name, None )
# delAttribute()
def __str__( self, indent = "" ):
"""
String representation of this Tag. It will recurse into children, so
calling it on the root tag will give you the whole document.
"""
s = u"%s<%s" % ( indent, self.name )
if self.attrs:
s += " "
for a in self.attrs:
s += "%s=\"%s\" " % ( a[ 0 ], a[ 1 ] )
# for
s = s[ : -1 ] # remove trailing space
if self.verbose:
# Change table appearence, so you can understand the
# document's layout
if self.name == "table":
s += self.table_decor
s += ">\n"
if self.cdata:
s+= "%s%s\n" % ( indent + " " * self.tabstop, self.cdata )
indent += " " * self.tabstop
for c in self.children:
# Propagate verbosity
v = c.verbose
c.verbose = self.verbose
# Recurse into children
s += c.__str__( indent )
# Back up verbosity
c.verbose = v
s += "%s</%s>\n" % ( indent[ : - self.tabstop ], self.name )
return s
# __str__()
def __repr__( self ):
return "<Tag(): name=\"%s\" attrs=\"%s\" cdata=\"%s\" #children=%s>" % \
( self.name, self.attrs, self.cdata, len( self.children ) )
# __repr__()
# Tag
# -------------- Unit Tests -------------- #
using_unittest2=False
try:
import unittest2 as unittest
using_unittest2=True
except:
import unittest
class Tag_UnitTest(unittest.TestCase):
def setUp(self):
self.i1 = Tag( "a", [ ( "href", "link.html" ), ( "title", "link" ) ], "a link" )
self.o1 = "<a href=\"link.html\" title=\"link\">\n%sa link\n</a>\n" % ( " " * Tag.tabstop )
self.i2 = Tag( "div", children=[ Tag( "b", cdata="bold text" ) ] )
self.o2 = "<div>\n%s<b>\n%sbold text\n%s</b>\n</div>\n" % ( " " * Tag.tabstop,
" " * ( Tag.tabstop * 2 ),
" " * Tag.tabstop )
# setUp()
def test01(self): v=str( self.i1 ); assert v == self.o1, v
def test02(self): v=str( self.i2 ); assert v == self.o2, v
if using_unittest2 or __name__ == '__main__':
unittest.main()
# -------------- Unit Tests -------------- #
|