blob: b8a66e9a44380ca7d9e02525ce93ed0c8aa89245 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlab2f4ef842015-01-19 12:49:33 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 24-Oct-2012
kelvin-onlab2f4ef842015-01-19 12:49:33 -08004
5author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ),
6 Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07007
8
9 TestON is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 2 of the License, or
kelvin-onlab2f4ef842015-01-19 12:49:33 -080012 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070013
14 TestON is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
kelvin-onlab2f4ef842015-01-19 12:49:33 -080020 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070021
22
adminbae64d82013-08-01 10:50:15 -070023
kelvin-onlab2f4ef842015-01-19 12:49:33 -080024"""
Jon Hallfd8ce432015-07-21 13:07:53 -070025import logging
26
kelvin-onlab2f4ef842015-01-19 12:49:33 -080027
28class Component( object ):
29
30 """
adminbae64d82013-08-01 10:50:15 -070031 This is the tempalte class for components
kelvin-onlab2f4ef842015-01-19 12:49:33 -080032 """
33 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070034 self.default = ''
kelvin-onlab2f4ef842015-01-19 12:49:33 -080035 self.wrapped = sys.modules[ __name__ ]
Jon Halleb3a6842015-02-18 15:17:51 -080036 self.count = 0
kelvin-onlab2f4ef842015-01-19 12:49:33 -080037
38 def __getattr__( self, name ):
39 """
adminbae64d82013-08-01 10:50:15 -070040 This will invoke, if the attribute wasn't found the usual ways.
Jon Hall20ead012015-02-18 14:00:42 -080041 Here it will look for assert_attribute and will execute when
42 AttributeError occurs.
43 It will return the result of the assert_attribute.
kelvin-onlab2f4ef842015-01-19 12:49:33 -080044 """
adminbae64d82013-08-01 10:50:15 -070045 try:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080046 return getattr( self.wrapped, name )
Jon Hall20ead012015-02-18 14:00:42 -080047 except AttributeError as error:
Jon Halleb3a6842015-02-18 15:17:51 -080048 # NOTE: The first time we load a driver module we get this error
Jon Hall0fc0d452015-07-14 09:49:58 -070049 if "'module' object has no attribute '__path__'" in error:
50 pass
Jon Halleb3a6842015-02-18 15:17:51 -080051 else:
52 main.log.error( str(error.__class__) + " " + str(error) )
adminbae64d82013-08-01 10:50:15 -070053 try:
Jon Hall20ead012015-02-18 14:00:42 -080054 def experimentHandling( *args, **kwargs ):
adminbae64d82013-08-01 10:50:15 -070055 if main.EXPERIMENTAL_MODE == main.TRUE:
Jon Hall20ead012015-02-18 14:00:42 -080056 result = self.experimentRun( *args, **kwargs )
57 main.log.info( "EXPERIMENTAL MODE. API " +
58 str( name ) +
59 " not yet implemented. " +
60 "Returning dummy values" )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080061 return result
adminbae64d82013-08-01 10:50:15 -070062 else:
63 return main.FALSE
64 return experimentHandling
kelvin-onlab2f4ef842015-01-19 12:49:33 -080065 except TypeError as e:
Jon Hall20ead012015-02-18 14:00:42 -080066 main.log.error( "Arguments for experimental mode does not" +
67 " have key 'retruns'" + e )
kelvin-onlab2f4ef842015-01-19 12:49:33 -080068
69 def connect( self ):
70
71 vars( main )[ self.name + 'log' ] = logging.getLogger( self.name )
72
73 session_file = main.logdir + "/" + self.name + ".session"
74 self.log_handler = logging.FileHandler( session_file )
75 self.log_handler.setLevel( logging.DEBUG )
76
77 vars( main )[ self.name + 'log' ].setLevel( logging.DEBUG )
78 _formatter = logging.Formatter(
79 "%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s" )
80 self.log_handler.setFormatter( _formatter )
81 vars( main )[ self.name + 'log' ].addHandler( self.log_handler )
82 # Adding header for the component log
83 vars( main )[ self.name + 'log' ].info( main.logHeader )
adminbae64d82013-08-01 10:50:15 -070084 # Opening the session log to append command's execution output
kelvin-onlab2f4ef842015-01-19 12:49:33 -080085 self.logfile_handler = open( session_file, "a" )
86
adminbae64d82013-08-01 10:50:15 -070087 return "Dummy"
kelvin-onlab2f4ef842015-01-19 12:49:33 -080088
89 def execute( self, cmd ):
adminbae64d82013-08-01 10:50:15 -070090 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -080091 # import commands
92 # return commands.getoutput( cmd )
93
94 def disconnect( self ):
95 return main.TRUE
96
97 def config( self ):
adminbae64d82013-08-01 10:50:15 -070098 self = self
99 # Need to update the configuration code
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800100
101 def cleanup( self ):
adminbae64d82013-08-01 10:50:15 -0700102 return main.TRUE
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800103
104 def log( self, message ):
105 """
106 Here finding the for the component to which the
adminbae64d82013-08-01 10:50:15 -0700107 log message based on the called child object.
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800108 """
109 vars( main )[ self.name + 'log' ].info( "\n" + message + "\n" )
110
111 def close_log_handles( self ):
112 vars( main )[ self.name + 'log' ].removeHandler( self.log_handler )
adminbae64d82013-08-01 10:50:15 -0700113 if self.logfile_handler:
114 self.logfile_handler.close()
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800115
116 def get_version( self ):
adminbae64d82013-08-01 10:50:15 -0700117 return "Version unknown"
118
Jon Hall20ead012015-02-18 14:00:42 -0800119 def experimentRun( self, *args, **kwargs ):
120 # FIXME handle *args
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800121 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
122 return args[ "RETURNS" ]
adminbae64d82013-08-01 10:50:15 -0700123
124
125if __name__ != "__main__":
126 import sys
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800127 sys.modules[ __name__ ] = Component()
128