blob: 6cf4cd8e3d61d747af97ae7f04ff4beb33665b4d [file] [log] [blame]
Chetan Gaonker3ff8eae2016-04-12 14:50:26 -07001#!/usr/bin/env python
2"""
3Created on 24-Oct-2012
4
5author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ),
6 Raghav Kashyap( raghavkashyap@paxterrasolutions.com )
7
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
12 ( at your option ) any later version.
13
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
20 along with TestON. If not, see <http://www.gnu.org/licenses/>.
21
22
23
24"""
25import logging
26from clicommon import *
27
28class Component( object ):
29
30 """
31 This is the tempalte class for components
32 """
33 def __init__( self ):
34 self.default = ''
35 self.wrapped = sys.modules[ __name__ ]
36 self.count = 0
37
38 def __getattr__( self, name ):
39 """
40 This will invoke, if the attribute wasn't found the usual ways.
41 Here it will look for assert_attribute and will execute when
42 AttributeError occurs.
43 It will return the result of the assert_attribute.
44 """
45 try:
46 return getattr( self.wrapped, name )
47 except AttributeError as error:
48 # NOTE: The first time we load a driver module we get this error
49 if "'module' object has no attribute '__path__'" in error:
50 pass
51 else:
52 main.log.error( str(error.__class__) + " " + str(error) )
53 try:
54 def experimentHandling( *args, **kwargs ):
55 if main.EXPERIMENTAL_MODE == main.TRUE:
56 result = self.experimentRun( *args, **kwargs )
57 main.log.info( "EXPERIMENTAL MODE. API " +
58 str( name ) +
59 " not yet implemented. " +
60 "Returning dummy values" )
61 return result
62 else:
63 return main.FALSE
64 return experimentHandling
65 except TypeError as e:
66 main.log.error( "Arguments for experimental mode does not" +
67 " have key 'retruns'" + e )
68
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 )
84 # Opening the session log to append command's execution output
85 self.logfile_handler = open( session_file, "a" )
86
87 return "Dummy"
88
89 def execute( self, cmd ):
90 return main.TRUE
91 # import commands
92 # return commands.getoutput( cmd )
93
94 def disconnect( self ):
95 return main.TRUE
96
97 def config( self ):
98 self = self
99 # Need to update the configuration code
100
101 def cleanup( self ):
102 return main.TRUE
103
104 def log( self, message ):
105 """
106 Here finding the for the component to which the
107 log message based on the called child object.
108 """
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 )
113 if self.logfile_handler:
114 self.logfile_handler.close()
115
116 def get_version( self ):
117 return "Version unknown"
118
119 def experimentRun( self, *args, **kwargs ):
120 # FIXME handle *args
121 args = utilities.parse_args( [ "RETURNS" ], **kwargs )
122 return args[ "RETURNS" ]
123
124
125if __name__ != "__main__":
126 import sys
127 sys.modules[ __name__ ] = Component()
128