blob: 9199a4e78b569a717794d17bc51f0495b3ce0f4b [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__ ]
Jon Halleb3a6842015-02-18 15:17:51 -080035 self.count = 0
kelvin-onlab2f4ef842015-01-19 12:49:33 -080036
37 def __getattr__( self, name ):
38 """
adminbae64d82013-08-01 10:50:15 -070039 This will invoke, if the attribute wasn't found the usual ways.
Jon Hall20ead012015-02-18 14:00:42 -080040 Here it will look for assert_attribute and will execute when
41 AttributeError occurs.
42 It will return the result of the assert_attribute.
kelvin-onlab2f4ef842015-01-19 12:49:33 -080043 """
adminbae64d82013-08-01 10:50:15 -070044 try:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080045 return getattr( self.wrapped, name )
Jon Hall20ead012015-02-18 14:00:42 -080046 except AttributeError as error:
Jon Halleb3a6842015-02-18 15:17:51 -080047 # NOTE: The first time we load a driver module we get this error
Jon Hall0fc0d452015-07-14 09:49:58 -070048 if "'module' object has no attribute '__path__'" in error:
49 pass
Jon Halleb3a6842015-02-18 15:17:51 -080050 else:
51 main.log.error( str(error.__class__) + " " + str(error) )
adminbae64d82013-08-01 10:50:15 -070052 try:
Jon Hall20ead012015-02-18 14:00:42 -080053 def experimentHandling( *args, **kwargs ):
adminbae64d82013-08-01 10:50:15 -070054 if main.EXPERIMENTAL_MODE == main.TRUE:
Jon Hall20ead012015-02-18 14:00:42 -080055 result = self.experimentRun( *args, **kwargs )
56 main.log.info( "EXPERIMENTAL MODE. API " +
57 str( name ) +
58 " not yet implemented. " +
59 "Returning dummy values" )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080060 return result
adminbae64d82013-08-01 10:50:15 -070061 else:
62 return main.FALSE
63 return experimentHandling
kelvin-onlab2f4ef842015-01-19 12:49:33 -080064 except TypeError as e:
Jon Hall20ead012015-02-18 14:00:42 -080065 main.log.error( "Arguments for experimental mode does not" +
66 " have key 'retruns'" + e )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080067
68 def connect( self ):
69
70 vars( main )[ self.name + 'log' ] = logging.getLogger( self.name )
71
72 session_file = main.logdir + "/" + self.name + ".session"
73 self.log_handler = logging.FileHandler( session_file )
74 self.log_handler.setLevel( logging.DEBUG )
75
76 vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG )
77 _formatter = logging.Formatter(
78 "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" )
79 self.log_handler.setFormatter( _formatter )
80 vars( main )[ self.name + 'log' ].addHandler( self.log_handler )
81 # Adding header for the component log
82 vars( main )[ self.name + 'log' ].info( main.logHeader )
adminbae64d82013-08-01 10:50:15 -070083 # Opening the session log to append command's execution output
kelvin-onlab2f4ef842015-01-19 12:49:33 -080084 self.logfile_handler = open( session_file, "a" )
85
adminbae64d82013-08-01 10:50:15 -070086 return "Dummy"
kelvin-onlab2f4ef842015-01-19 12:49:33 -080087
88 def execute( self, cmd ):
adminbae64d82013-08-01 10:50:15 -070089 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -080090 # import commands
91 # return commands.getoutput( cmd )
92
93 def disconnect( self ):
94 return main.TRUE
95
96 def config( self ):
adminbae64d82013-08-01 10:50:15 -070097 self = self
98 # Need to update the configuration code
kelvin-onlab2f4ef842015-01-19 12:49:33 -080099
100 def cleanup( self ):
adminbae64d82013-08-01 10:50:15 -0700101 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800102
103 def log( self, message ):
104 """
105 Here finding the for the component to which the
adminbae64d82013-08-01 10:50:15 -0700106 log message based on the called child object.
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800107 """
108 vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" )
109
110 def close_log_handles( self ):
111 vars( main )[ self.name + 'log' ].removeHandler( self.log_handler )
adminbae64d82013-08-01 10:50:15 -0700112 if self.logfile_handler:
113 self.logfile_handler.close()
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800114
115 def get_version( self ):
adminbae64d82013-08-01 10:50:15 -0700116 return "Version unknown"
117
Jon Hall20ead012015-02-18 14:00:42 -0800118 def experimentRun( self, *args, **kwargs ):
119 # FIXME handle *args
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800120 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
121 return args[ "RETURNS" ]
adminbae64d82013-08-01 10:50:15 -0700122
123
124if __name__ != "__main__":
125 import sys
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800126 sys.modules[ __name__ ] = Component()
127