blob: 503acbe1d12dede255d0920de87f70d8de5baef3 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2import logging
kelvin-onlab2f4ef842015-01-19 12:49:33 -08003"""
adminbae64d82013-08-01 10:50:15 -07004Created on 24-Oct-2012
kelvin-onlab2f4ef842015-01-19 12:49:33 -08005
6author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ),
7 Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07008
9
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
kelvin-onlab2f4ef842015-01-19 12:49:33 -080013 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
kelvin-onlab2f4ef842015-01-19 12:49:33 -080021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
adminbae64d82013-08-01 10:50:15 -070024
kelvin-onlab2f4ef842015-01-19 12:49:33 -080025"""
kelvin-onlab2f4ef842015-01-19 12:49:33 -080026
27class Component( object ):
28
29 """
adminbae64d82013-08-01 10:50:15 -070030 This is the tempalte class for components
kelvin-onlab2f4ef842015-01-19 12:49:33 -080031 """
32 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070033 self.default = ''
kelvin-onlab2f4ef842015-01-19 12:49:33 -080034 self.wrapped = sys.modules[ __name__ ]
35
36 def __getattr__( self, name ):
37 """
adminbae64d82013-08-01 10:50:15 -070038 This will invoke, if the attribute wasn't found the usual ways.
Jon Hall20ead012015-02-18 14:00:42 -080039 Here it will look for assert_attribute and will execute when
40 AttributeError occurs.
41 It will return the result of the assert_attribute.
kelvin-onlab2f4ef842015-01-19 12:49:33 -080042 """
adminbae64d82013-08-01 10:50:15 -070043 try:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080044 return getattr( self.wrapped, name )
Jon Hall20ead012015-02-18 14:00:42 -080045 except AttributeError as error:
46 main.log.error( str(error.__class__) + " " + str(error) )
adminbae64d82013-08-01 10:50:15 -070047 try:
Jon Hall20ead012015-02-18 14:00:42 -080048 def experimentHandling( *args, **kwargs ):
adminbae64d82013-08-01 10:50:15 -070049 if main.EXPERIMENTAL_MODE == main.TRUE:
Jon Hall20ead012015-02-18 14:00:42 -080050 result = self.experimentRun( *args, **kwargs )
51 main.log.info( "EXPERIMENTAL MODE. API " +
52 str( name ) +
53 " not yet implemented. " +
54 "Returning dummy values" )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080055 return result
adminbae64d82013-08-01 10:50:15 -070056 else:
57 return main.FALSE
58 return experimentHandling
kelvin-onlab2f4ef842015-01-19 12:49:33 -080059 except TypeError as e:
Jon Hall20ead012015-02-18 14:00:42 -080060 main.log.error( "Arguments for experimental mode does not" +
61 " have key 'retruns'" + e )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080062
63 def connect( self ):
64
65 vars( main )[ self.name + 'log' ] = logging.getLogger( self.name )
66
67 session_file = main.logdir + "/" + self.name + ".session"
68 self.log_handler = logging.FileHandler( session_file )
69 self.log_handler.setLevel( logging.DEBUG )
70
71 vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG )
72 _formatter = logging.Formatter(
73 "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" )
74 self.log_handler.setFormatter( _formatter )
75 vars( main )[ self.name + 'log' ].addHandler( self.log_handler )
76 # Adding header for the component log
77 vars( main )[ self.name + 'log' ].info( main.logHeader )
adminbae64d82013-08-01 10:50:15 -070078 # Opening the session log to append command's execution output
kelvin-onlab2f4ef842015-01-19 12:49:33 -080079 self.logfile_handler = open( session_file, "a" )
80
adminbae64d82013-08-01 10:50:15 -070081 return "Dummy"
kelvin-onlab2f4ef842015-01-19 12:49:33 -080082
83 def execute( self, cmd ):
adminbae64d82013-08-01 10:50:15 -070084 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -080085 # import commands
86 # return commands.getoutput( cmd )
87
88 def disconnect( self ):
89 return main.TRUE
90
91 def config( self ):
adminbae64d82013-08-01 10:50:15 -070092 self = self
93 # Need to update the configuration code
kelvin-onlab2f4ef842015-01-19 12:49:33 -080094
95 def cleanup( self ):
adminbae64d82013-08-01 10:50:15 -070096 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -080097
98 def log( self, message ):
99 """
100 Here finding the for the component to which the
adminbae64d82013-08-01 10:50:15 -0700101 log message based on the called child object.
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800102 """
103 vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" )
104
105 def close_log_handles( self ):
106 vars( main )[ self.name + 'log' ].removeHandler( self.log_handler )
adminbae64d82013-08-01 10:50:15 -0700107 if self.logfile_handler:
108 self.logfile_handler.close()
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800109
110 def get_version( self ):
adminbae64d82013-08-01 10:50:15 -0700111 return "Version unknown"
112
Jon Hall20ead012015-02-18 14:00:42 -0800113 def experimentRun( self, *args, **kwargs ):
114 # FIXME handle *args
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800115 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
116 return args[ "RETURNS" ]
adminbae64d82013-08-01 10:50:15 -0700117
118
119if __name__ != "__main__":
120 import sys
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800121 sys.modules[ __name__ ] = Component()
122