blob: f05be55562588294c0a1bef8257149368ae86fcf [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
48 if "'module' object has no attribute '__path__'" in error\
49 and self.count == 0:
50 self.count += 1
51 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