admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import logging |
| 3 | ''' |
| 4 | Created on 24-Oct-2012 |
| 5 | |
| 6 | @authors: 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 | |
| 27 | import re |
| 28 | from logging import Logger |
| 29 | |
| 30 | class Component(object): |
| 31 | ''' |
| 32 | This is the tempalte class for components |
| 33 | ''' |
| 34 | def __init__(self): |
| 35 | self.default = '' |
| 36 | self.wrapped = sys.modules[__name__] |
| 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 AttributeError occurs. |
| 42 | It will return the result of the assert_attribute. |
| 43 | ''' |
| 44 | try: |
| 45 | return getattr(self.wrapped, name) |
| 46 | except AttributeError: |
| 47 | try: |
| 48 | def experimentHandling(**kwargs): |
| 49 | if main.EXPERIMENTAL_MODE == main.TRUE: |
| 50 | result = self.experimentRun(**kwargs) |
| 51 | main.log.info("EXPERIMENTAL MODE. API "+str(name)+" not yet implemented. Returning dummy values") |
| 52 | return result |
| 53 | else: |
| 54 | return main.FALSE |
| 55 | return experimentHandling |
| 56 | except TypeError,e: |
| 57 | main.log.error("Arguments for experimental mode does not have key 'retruns'" + e) |
| 58 | |
| 59 | |
| 60 | def connect(self): |
| 61 | |
| 62 | vars(main)[self.name+'log'] = logging.getLogger(self.name) |
| 63 | |
| 64 | session_file = main.logdir+"/"+self.name+".session" |
| 65 | self.log_handler = logging.FileHandler(session_file) |
| 66 | self.log_handler.setLevel(logging.DEBUG) |
| 67 | |
| 68 | vars(main)[self.name+'log'].setLevel(logging.DEBUG) |
| 69 | _formatter = logging.Formatter("%(asctime)s %(name)-10s: %(levelname)-8s: %(message)s") |
| 70 | self.log_handler.setFormatter(_formatter) |
| 71 | vars(main)[self.name+'log'].addHandler(self.log_handler) |
| 72 | # Adding header for the component log |
| 73 | vars(main)[self.name+'log'].info(main.logHeader) |
| 74 | # Opening the session log to append command's execution output |
| 75 | self.logfile_handler = open(session_file,"a") |
| 76 | |
| 77 | return "Dummy" |
| 78 | |
| 79 | def execute(self,cmd): |
| 80 | return main.TRUE |
| 81 | #import commands |
| 82 | #return commands.getoutput(cmd) |
| 83 | |
| 84 | def disconnect(self): |
| 85 | return main.TRUE |
| 86 | |
| 87 | def config(self): |
| 88 | self = self |
| 89 | # Need to update the configuration code |
| 90 | |
| 91 | def cleanup(self): |
| 92 | return main.TRUE |
| 93 | |
| 94 | def log(self,message): |
| 95 | ''' |
| 96 | Here finding the for the component to which the |
| 97 | log message based on the called child object. |
| 98 | ''' |
| 99 | vars(main)[self.name+'log'].info("\n"+message+"\n") |
| 100 | |
| 101 | def close_log_handles(self) : |
| 102 | vars(main)[self.name+'log'].removeHandler(self.log_handler) |
| 103 | if self.logfile_handler: |
| 104 | self.logfile_handler.close() |
| 105 | |
| 106 | def get_version(self): |
| 107 | return "Version unknown" |
| 108 | |
| 109 | def experimentRun(self,**kwargs): |
| 110 | args = utilities.parse_args(["RETURNS"],**kwargs) |
| 111 | return args["RETURNS"] |
| 112 | |
| 113 | |
| 114 | if __name__ != "__main__": |
| 115 | import sys |
| 116 | sys.modules[__name__] = Component() |