kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import logging |
| 3 | """ |
| 4 | Created on 24-Oct-2012 |
| 5 | |
| 6 | author:s: Anil Kumar ( anilkumar.s@paxterrasolutions.com ), |
| 7 | Raghav Kashyap( raghavkashyap@paxterrasolutions.com ) |
| 8 | |
| 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 |
| 13 | ( at your option ) any later version. |
| 14 | |
| 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 |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | |
| 24 | |
| 25 | """ |
| 26 | import re |
| 27 | from logging import Logger |
| 28 | |
| 29 | |
| 30 | class Component( object ): |
| 31 | |
| 32 | """ |
| 33 | This is the tempalte class for components |
| 34 | """ |
| 35 | def __init__( self ): |
| 36 | self.default = '' |
| 37 | self.wrapped = sys.modules[ __name__ ] |
| 38 | |
| 39 | def __getattr__( self, name ): |
| 40 | """ |
| 41 | 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. |
| 44 | """ |
| 45 | try: |
| 46 | return getattr( self.wrapped, name ) |
| 47 | except AttributeError: |
| 48 | try: |
| 49 | def experimentHandling( **kwargs ): |
| 50 | if main.EXPERIMENTAL_MODE == main.TRUE: |
| 51 | result = self.experimentRun( **kwargs ) |
| 52 | main.log.info( "EXPERIMENTAL MODE. API " + str( |
| 53 | name ) + " not yet implemented. Returning dummy values" ) |
| 54 | return result |
| 55 | else: |
| 56 | return main.FALSE |
| 57 | return experimentHandling |
| 58 | 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 ) |
| 78 | # Opening the session log to append command's execution output |
| 79 | self.logfile_handler = open( session_file, "a" ) |
| 80 | |
| 81 | return "Dummy" |
| 82 | |
| 83 | def execute( self, cmd ): |
| 84 | return main.TRUE |
| 85 | # import commands |
| 86 | # return commands.getoutput( cmd ) |
| 87 | |
| 88 | def disconnect( self ): |
| 89 | return main.TRUE |
| 90 | |
| 91 | def config( self ): |
| 92 | self = self |
| 93 | # Need to update the configuration code |
| 94 | |
| 95 | def cleanup( self ): |
| 96 | return main.TRUE |
| 97 | |
| 98 | def log( self, message ): |
| 99 | """ |
| 100 | Here finding the for the component to which the |
| 101 | log message based on the called child object. |
| 102 | """ |
| 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 ) |
| 107 | if self.logfile_handler: |
| 108 | self.logfile_handler.close() |
| 109 | |
| 110 | def get_version( self ): |
| 111 | return "Version unknown" |
| 112 | |
| 113 | def experimentRun( self, **kwargs ): |
| 114 | args = utilities.parse_args( [ "RETURNS" ], **kwargs ) |
| 115 | return args[ "RETURNS" ] |
| 116 | |
| 117 | |
| 118 | if __name__ != "__main__": |
| 119 | import sys |
| 120 | sys.modules[ __name__ ] = Component() |
| 121 | |