<s> """<STR_LIT>""" <EOL> from cStringIO import StringIO <EOL> try : <EOL> from lxml import etree <EOL> except ImportError : <EOL> etree = None <EOL> from zope . interface import classProvides <EOL> from nagcat import errors , filters , log <EOL> class XPathFilter ( filters . _Filter ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( filters . IFilter ) <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , test , default , arguments ) : <EOL> super ( XPathFilter , self ) . __init__ ( test , default , arguments ) <EOL> if not etree : <EOL> raise errors . InitError ( "<STR_LIT>" ) <EOL> try : <EOL> self . xpath = etree . XPath ( self . arguments ) <EOL> except etree . XPathSyntaxError , ex : <EOL> raise errors . InitError ( <EOL> "<STR_LIT>" % ( self . arguments , ex ) ) <EOL> @ errors . callback <EOL> def filter ( self , result ) : <EOL> def format ( data ) : <EOL> if etree . iselement ( data ) : <EOL> ret = etree . tostring ( data , pretty_print = True ) <EOL> else : <EOL> ret = str ( data ) <EOL> return ret . strip ( ) <EOL> log . debug ( "<STR_LIT>" , self . arguments ) <EOL> try : <EOL> root = etree . fromstring ( result ) <EOL> except etree . XMLSyntaxError , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ex ) <EOL> data = self . xpath ( root ) <EOL> if isinstance ( data , list ) and data : <EOL> return "<STR_LIT:\n>" . join ( [ format ( x ) for x in data ] ) <EOL> elif isinstance ( data , list ) : <EOL> if self . default is not None : <EOL> return self . default <EOL> else : <EOL> raise errors . TestCritical ( <EOL> "<STR_LIT>" % self . arguments ) <EOL> else : <EOL> return format ( data ) <EOL> class XSLTFilter ( filters . _Filter ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( filters . IFilter ) <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , test , default , arguments ) : <EOL> super ( XSLTFilter , self ) . __init__ ( test , default , arguments ) <EOL> if not etree : <EOL> raise errors . InitError ( "<STR_LIT>" ) <EOL> try : <EOL> if self . arguments . startswith ( "<STR_LIT:/>" ) : <EOL> document = etree . parse ( self . arguments ) <EOL> elif self . arguments . rstrip ( ) . startswith ( "<STR_LIT:<>" ) : <EOL> document = etree . fromstring ( self . arguments ) <EOL> else : <EOL> raise errors . InitError ( <EOL> "<STR_LIT>" % self . arguments ) <EOL> self . xslt = etree . XSLT ( document ) <EOL> except IOError , ex : <EOL> raise errors . InitError ( "<STR_LIT>" % ex ) <EOL> except etree . XMLSyntaxError , ex : <EOL> raise errors . InitError ( "<STR_LIT>" % ex ) <EOL> except etree . XSLTParseError , ex : <EOL> raise errors . InitError ( "<STR_LIT>" % ex ) <EOL> @ errors . callback <EOL> def filter ( self , result ) : <EOL> try : <EOL> input = etree . fromstring ( result ) <EOL> except etree . XMLSyntaxError , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ex ) <EOL> try : <EOL> output = self . xslt ( input ) <EOL> except etree . XSLTApplyError , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ex ) <EOL> return etree . tostring ( output , pretty_print = True ) <EOL> class HtmlFilter ( filters . _Filter ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( filters . IFilter ) <EOL> name = "<STR_LIT:html>" <EOL> def __init__ ( self , test , default , arguments ) : <EOL> super ( HtmlFilter , self ) . __init__ ( test , default , arguments ) <EOL> if not etree : <EOL> raise errors . InitError ( "<STR_LIT>" ) <EOL> assert default is None <EOL> assert not arguments <EOL> @ errors . callback <EOL> def filter ( self , result ) : <EOL> try : <EOL> parser = etree . HTMLParser ( remove_comments = True ) <EOL> html = etree . parse ( StringIO ( result ) , parser ) <EOL> except etree . XMLSyntaxError , ex : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ex ) <EOL> return etree . tostring ( html , pretty_print = True ) </s>
