<s> from abc import ABCMeta , abstractmethod <EOL> from urlparse import urlsplit <EOL> import sys <EOL> from splunklib . client import Service <EOL> from splunklib . modularinput . event_writer import EventWriter <EOL> from splunklib . modularinput . input_definition import InputDefinition <EOL> from splunklib . modularinput . validation_definition import ValidationDefinition <EOL> try : <EOL> import xml . etree . cElementTree as ET <EOL> except ImportError : <EOL> import xml . etree . ElementTree as ET <EOL> class Script ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = ABCMeta <EOL> def __init__ ( self ) : <EOL> self . _input_definition = None <EOL> self . _service = None <EOL> def run ( self , args ) : <EOL> """<STR_LIT>""" <EOL> return self . run_script ( args , EventWriter ( ) , sys . stdin ) <EOL> def run_script ( self , args , event_writer , input_stream ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> self . _input_definition = InputDefinition . parse ( input_stream ) <EOL> self . stream_events ( self . _input_definition , event_writer ) <EOL> event_writer . close ( ) <EOL> return <NUM_LIT:0> <EOL> elif str ( args [ <NUM_LIT:1> ] ) . lower ( ) == "<STR_LIT>" : <EOL> scheme = self . get_scheme ( ) <EOL> if scheme is None : <EOL> event_writer . log ( <EOL> EventWriter . FATAL , <EOL> "<STR_LIT>" ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> event_writer . write_xml_document ( scheme . to_xml ( ) ) <EOL> return <NUM_LIT:0> <EOL> elif args [ <NUM_LIT:1> ] . lower ( ) == "<STR_LIT>" : <EOL> validation_definition = ValidationDefinition . parse ( input_stream ) <EOL> try : <EOL> self . validate_input ( validation_definition ) <EOL> return <NUM_LIT:0> <EOL> except Exception as e : <EOL> root = ET . Element ( "<STR_LIT:error>" ) <EOL> ET . SubElement ( root , "<STR_LIT:message>" ) . text = e . message <EOL> event_writer . write_xml_document ( root ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> err_string = "<STR_LIT>" + '<STR_LIT:U+0020>' . join ( <EOL> args ) <EOL> event_writer . _err . write ( err_string ) <EOL> except Exception as e : <EOL> err_string = EventWriter . ERROR + e . message <EOL> event_writer . _err . write ( err_string ) <EOL> return <NUM_LIT:1> <EOL> @ property <EOL> def service ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _service is not None : <EOL> return self . _service <EOL> if self . _input_definition is None : <EOL> return None <EOL> splunkd_uri = self . _input_definition . metadata [ "<STR_LIT>" ] <EOL> session_key = self . _input_definition . metadata [ "<STR_LIT>" ] <EOL> splunkd = urlsplit ( splunkd_uri , allow_fragments = False ) <EOL> self . _service = Service ( <EOL> scheme = splunkd . scheme , <EOL> host = splunkd . hostname , <EOL> port = splunkd . port , <EOL> token = session_key , <EOL> ) <EOL> return self . _service <EOL> @ abstractmethod <EOL> def get_scheme ( self ) : <EOL> """<STR_LIT>""" <EOL> def validate_input ( self , definition ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ abstractmethod <EOL> def stream_events ( self , inputs , ew ) : <EOL> """<STR_LIT>""" </s>
