<s> import os <EOL> from ipaddress import ip_network <EOL> from lxml . etree import XPath , XPathSyntaxError <EOL> from twisted . internet import reactor <EOL> from twisted . python import usage <EOL> from twisted . plugin import getPlugins <EOL> from twisted . internet . task import LoopingCall <EOL> from twisted . application . service import MultiService <EOL> from twisted . application . internet import TCPClient <EOL> from twisted . application . internet import TCPServer <EOL> import comet <EOL> import comet . log as log <EOL> from comet . protocol import VOEventBroadcasterFactory <EOL> from comet . protocol import VOEventReceiverFactory <EOL> from comet . protocol import VOEventSubscriberFactory <EOL> from comet . utility import Event_DB , BaseOptions , WhitelistingFactory <EOL> from comet . validator import CheckIVORN , CheckPreviouslySeen , CheckSchema <EOL> import comet . plugins <EOL> from comet . icomet import IHandler , IHasOptions <EOL> from comet . handler import SpawnCommand , EventRelay <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> MAX_AGE = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> PRUNE_INTERVAL = <NUM_LIT:6> * <NUM_LIT> * <NUM_LIT> <EOL> DEFAULT_REMOTE_PORT = <NUM_LIT> <EOL> BCAST_TEST_INTERVAL = <NUM_LIT> <EOL> class Options ( BaseOptions ) : <EOL> optFlags = [ <EOL> [ "<STR_LIT>" , "<STR_LIT:r>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT:b>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT:v>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT:q>" , "<STR_LIT>" ] <EOL> ] <EOL> optParameters = [ <EOL> [ "<STR_LIT>" , None , os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , None , <NUM_LIT> , "<STR_LIT>" , int ] , <EOL> [ "<STR_LIT>" , None , DEFAULT_REMOTE_PORT , "<STR_LIT>" , int ] , <EOL> [ "<STR_LIT>" , None , BCAST_TEST_INTERVAL , "<STR_LIT>" , int ] , <EOL> [ "<STR_LIT>" , None , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , None , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , None , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , None , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , None , None , "<STR_LIT>" ] <EOL> ] <EOL> def __init__ ( self ) : <EOL> BaseOptions . __init__ ( self ) <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] = [ ] <EOL> self [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> def opt_quiet ( self ) : <EOL> self [ '<STR_LIT>' ] -= <NUM_LIT:1> <EOL> opt_q = opt_quiet <EOL> def opt_verbose ( self ) : <EOL> self [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> opt_v = opt_verbose <EOL> def opt_cmd ( self , cmd ) : <EOL> self [ "<STR_LIT>" ] . append ( SpawnCommand ( cmd ) ) <EOL> def opt_filter ( self , my_filter ) : <EOL> try : <EOL> XPath ( my_filter ) <EOL> except XPathSyntaxError : <EOL> raise usage . UsageError ( "<STR_LIT>" % my_filter ) <EOL> self [ '<STR_LIT>' ] . append ( my_filter ) <EOL> def opt_remote ( self , remote ) : <EOL> try : <EOL> host , port = remote . split ( "<STR_LIT::>" ) <EOL> except ValueError : <EOL> host , port = remote , DEFAULT_REMOTE_PORT <EOL> reactor . callWhenRunning ( <EOL> log . info , <EOL> "<STR_LIT>" % ( host , int ( port ) ) <EOL> ) <EOL> self [ '<STR_LIT>' ] . append ( ( host , int ( port ) ) ) <EOL> def opt_author_whitelist ( self , network ) : <EOL> reactor . callWhenRunning ( log . info , "<STR_LIT>" % network ) <EOL> self [ '<STR_LIT>' ] . append ( ip_network ( network , strict = False ) ) <EOL> def opt_subscriber_whitelist ( self , network ) : <EOL> reactor . callWhenRunning ( log . info , "<STR_LIT>" % network ) <EOL> self [ '<STR_LIT>' ] . append ( ip_network ( network , strict = False ) ) <EOL> def postOptions ( self ) : <EOL> BaseOptions . postOptions ( self ) <EOL> if self [ '<STR_LIT>' ] : <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] <EOL> else : <EOL> self [ '<STR_LIT>' ] = [ ip_network ( self [ '<STR_LIT>' ] , strict = False ) ] <EOL> if self [ '<STR_LIT>' ] : <EOL> self [ '<STR_LIT>' ] = self [ '<STR_LIT>' ] <EOL> else : <EOL> self [ '<STR_LIT>' ] = [ ip_network ( self [ '<STR_LIT>' ] , strict = False ) ] <EOL> if self [ '<STR_LIT>' ] >= <NUM_LIT:2> : <EOL> log . LEVEL = log . Levels . DEBUG <EOL> elif self [ '<STR_LIT>' ] == <NUM_LIT:1> : <EOL> log . LEVEL = log . Levels . INFO <EOL> else : <EOL> log . LEVEL = log . Levels . WARNING <EOL> for plugin in getPlugins ( IHandler , comet . plugins ) : <EOL> if self [ plugin . name ] : <EOL> if IHasOptions . providedBy ( plugin ) : <EOL> for name , _ , _ in plugin . get_options ( ) : <EOL> plugin . set_option ( name , self [ "<STR_LIT>" % ( plugin . name , name ) ] ) <EOL> self [ '<STR_LIT>' ] . append ( plugin ) <EOL> for plugin in getPlugins ( IHandler , comet . plugins ) : <EOL> Options . optFlags . append ( <EOL> [ plugin . name , None , "<STR_LIT>" % ( plugin . name , ) ] <EOL> ) <EOL> if IHasOptions . providedBy ( plugin ) : <EOL> for name , default , description in plugin . get_options ( ) : <EOL> Options . optParameters . append ( <EOL> [ "<STR_LIT>" % ( plugin . name , name ) , None , default , description ] <EOL> ) <EOL> def makeService ( config ) : <EOL> event_db = Event_DB ( config [ '<STR_LIT>' ] ) <EOL> LoopingCall ( event_db . prune , MAX_AGE ) . start ( PRUNE_INTERVAL ) <EOL> broker_service = MultiService ( ) <EOL> if config [ '<STR_LIT>' ] : <EOL> broadcaster_factory = VOEventBroadcasterFactory ( <EOL> config [ "<STR_LIT>" ] , config [ '<STR_LIT>' ] <EOL> ) <EOL> if log . LEVEL >= log . Levels . INFO : broadcaster_factory . noisy = False <EOL> broadcaster_whitelisting_factory = WhitelistingFactory ( <EOL> broadcaster_factory , config [ '<STR_LIT>' ] , "<STR_LIT>" <EOL> ) <EOL> if log . LEVEL >= log . Levels . INFO : broadcaster_whitelisting_factory . noisy = False <EOL> broadcaster_service = TCPServer ( <EOL> config [ '<STR_LIT>' ] , <EOL> broadcaster_whitelisting_factory <EOL> ) <EOL> broadcaster_service . setName ( "<STR_LIT>" ) <EOL> broadcaster_service . setServiceParent ( broker_service ) <EOL> config [ '<STR_LIT>' ] . append ( EventRelay ( broadcaster_factory ) ) <EOL> if config [ '<STR_LIT>' ] : <EOL> receiver_factory = VOEventReceiverFactory ( <EOL> local_ivo = config [ '<STR_LIT>' ] , <EOL> validators = [ <EOL> CheckPreviouslySeen ( event_db ) , <EOL> CheckSchema ( <EOL> os . path . join ( comet . __path__ [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> ) , <EOL> CheckIVORN ( ) <EOL> ] , <EOL> handlers = config [ '<STR_LIT>' ] <EOL> ) <EOL> if log . LEVEL >= log . Levels . INFO : receiver_factory . noisy = False <EOL> author_whitelisting_factory = WhitelistingFactory ( <EOL> receiver_factory , config [ '<STR_LIT>' ] , "<STR_LIT>" <EOL> ) <EOL> if log . LEVEL >= log . Levels . INFO : author_whitelisting_factory . noisy = False <EOL> receiver_service = TCPServer ( config [ '<STR_LIT>' ] , author_whitelisting_factory ) <EOL> receiver_service . setName ( "<STR_LIT>" ) <EOL> receiver_service . setServiceParent ( broker_service ) <EOL> for host , port in config [ "<STR_LIT>" ] : <EOL> subscriber_factory = VOEventSubscriberFactory ( <EOL> local_ivo = config [ "<STR_LIT>" ] , <EOL> validators = [ CheckPreviouslySeen ( event_db ) ] , <EOL> handlers = config [ '<STR_LIT>' ] , <EOL> filters = config [ '<STR_LIT>' ] <EOL> ) <EOL> if log . LEVEL >= log . Levels . INFO : subscriber_factory . noisy = False <EOL> remote_service = TCPClient ( host , port , subscriber_factory ) <EOL> remote_service . setName ( "<STR_LIT>" % ( host , port ) ) <EOL> remote_service . setServiceParent ( broker_service ) <EOL> if not broker_service . services : <EOL> reactor . callWhenRunning ( log . warn , "<STR_LIT>" ) <EOL> reactor . callWhenRunning ( reactor . stop ) <EOL> return broker_service </s>
