blob: 1e87d031f293ec01bdff1782f55ab2b24a42b50a [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"""
adminbae64d82013-08-01 10:50:15 -070026import re
27from logging import Logger
28
kelvin-onlab2f4ef842015-01-19 12:49:33 -080029
30class Component( object ):
31
32 """
adminbae64d82013-08-01 10:50:15 -070033 This is the tempalte class for components
kelvin-onlab2f4ef842015-01-19 12:49:33 -080034 """
35 def __init__( self ):
adminbae64d82013-08-01 10:50:15 -070036 self.default = ''
kelvin-onlab2f4ef842015-01-19 12:49:33 -080037 self.wrapped = sys.modules[ __name__ ]
38
39 def __getattr__( self, name ):
40 """
adminbae64d82013-08-01 10:50:15 -070041 This will invoke, if the attribute wasn't found the usual ways.
42 Here it will look for assert_attribute and will execute when 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 )
adminbae64d82013-08-01 10:50:15 -070047 except AttributeError:
48 try:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080049 def experimentHandling( **kwargs ):
adminbae64d82013-08-01 10:50:15 -070050 if main.EXPERIMENTAL_MODE == main.TRUE:
kelvin-onlab2f4ef842015-01-19 12:49:33 -080051 result = self.experimentRun( **kwargs )
52 main.log.info( "EXPERIMENTAL MODE. API " + str(
53 name ) + " not yet implemented. Returning dummy values" )
54 return result
adminbae64d82013-08-01 10:50:15 -070055 else:
56 return main.FALSE
57 return experimentHandling
kelvin-onlab2f4ef842015-01-19 12:49:33 -080058 except TypeError as e:
59 main.log.error(
60 "Arguments for experimental mode does not have key 'retruns'" +
61 e )
62
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
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800113 def experimentRun( self, **kwargs ):
114 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
115 return args[ "RETURNS" ]
adminbae64d82013-08-01 10:50:15 -0700116
117
118if __name__ != "__main__":
119 import sys
kelvin-onlab2f4ef842015-01-19 12:49:33 -0800120 sys.modules[ __name__ ] = Component()
121