def image(filename): '''image api call. sends the image filename to the dofler server to be parsed and added to the database. ''' if config.getboolean('Settings', 'client_validate'): try: image = Image.open(filename) except: return try: opener.open('%s/post/image' % config.get('Settings', 'dofler_address'), {'file': open(filename), 'filetype': filename.split('.')[-1]}) except: try: log.warn('Image %s upload failed. Size: %s kb' %\ (filename, os.path.getsize(filename) / 1024)) except: log.warn('Image %s doesnt exist. Cannot upload.' % filename)
def run(self): '''The service manager for the thread. This is where the child process is actually being spawned and maintained. ''' start = time.time() # Set the start timer interface = config.get('Parser: %s' % self.stanza, 'interface') timer = config.getint('Parser: %s' % self.stanza, 'timer') promisc = config.getboolean('Parser: %s' % self.stanza, 'promisc') # Replace the options in the command with the interface and promiscuous # settings as needed. cmd = self.cmd.replace('{INTERFACE}', interface)\ .replace('{PROMISC}', self.promisc[promisc]) # Yeah! Loops! while not self.breaker: # Here we actually start the child process and then run through the # output in a loop until either the process exits, the timer hits, # or someone sets the breaker flag. p = pexpect.spawn(cmd) while not self.breaker and (int(time.time()) - int(start)) < timer: try: line = p.readline() #print '%s:\t%s' % (self.stanza, line.strip('\r\n')) if line == '': if p.isalive(): time.sleep(0.1) else: break else: self.parse(line) except pexpect.TIMEOUT: pass # As we either broke out of the process or the timer hit, lets make # sure to terminate the process and reset the timer to the current # time. p.terminate() start = time.time()
import logging from dofler.config import config _loglevels = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warn': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL, } log = logging.getLogger('DoFler') # This is the console output handler if config.getboolean('Settings', 'log_to_console'): stderr = logging.StreamHandler() console_format = logging.Formatter('%(levelname)s %(message)s') stderr.setFormatter(console_format) log.setLevel(_loglevels[config.get('Settings', 'log_level')]) log.addHandler(stderr) # This is the file handler, and is optional. if config.getboolean('Settings', 'log_to_file'): hdlr = logging.FileHandler(config.get('Settings', 'log_file')) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) log.setLevel(_loglevels[config.get('Settings', 'log_level')]) log.addHandler(hdlr)