def update(_df): """Import data into the . Args: _df: Pandas DataFrame with the following headings ['language', 'key', 'translation'] Returns: None """ # Initialize key variables languages = {} headings_expected = [ 'language', 'key', 'translation'] headings_actual = [] valid = True count = 0 # Test columns for item in _df.columns: headings_actual.append(item) for item in headings_actual: if item not in headings_expected: valid = False if valid is False: log_message = ('''Imported data must have the following headings "{}"\ '''.format('", "'.join(headings_expected))) log.log2die(20082, log_message) # Process the DataFrame for _, row in _df.iterrows(): # Initialize variables at the top of the loop count += 1 code = row['language'].lower() key = str(row['key']) translation = str(row['translation']) # Store the idx_language value in a dictionary to improve speed idx_language = languages.get(code) if bool(idx_language) is False: idx_language = language.exists(code) if bool(idx_language) is True: languages[code] = idx_language else: log_message = ('''\ Language code "{}" on line {} of imported translation file not found during \ key-pair data importation. Please correct and try again. All other valid \ entries have been imported.\ '''.format(code, count)) log.log2see(20041, log_message) continue # Update the database if agent_xlate_exists(idx_language, key) is True: # Update the record update_row(key, translation, idx_language) else: # Insert a new record insert_row(key, translation, idx_language)
def query(self): """Query all remote targets for data. Args: None Returns: None """ # Check for lock and pid files if os.path.exists(self.lockfile_parent) is True: log_message = ('''\ Lock file {} exists. Multiple API daemons running API may have died \ catastrophically in the past, in which case the lockfile should be deleted.\ '''.format(self.lockfile_parent)) log.log2see(1083, log_message) if os.path.exists(self.pidfile_parent) is True: log_message = ('''\ PID file: {} already exists. Daemon already running? If not, it may have died \ catastrophically in the past in which case you should use --stop --force to \ fix.'''.format(self.pidfile_parent)) log.log2see(1084, log_message) ###################################################################### # # Assign options in format that the Gunicorn WSGI will accept # # NOTE! to get a full set of valid options pprint(self.cfg.settings) # in the instantiation of _StandaloneApplication. The option names # do not exactly match the CLI options found at # http://docs.gunicorn.org/en/stable/settings.html # ###################################################################### options = { 'bind': _ip_binding(self._agent_api_variable), 'accesslog': self.config.log_file_api(), 'errorlog': self.config.log_file_api(), 'capture_output': True, 'pidfile': self._pidfile_child, 'loglevel': self.config.log_level(), 'workers': _number_of_workers(), 'umask': 0o0007, } # Log so that user running the script from the CLI knows that something # is happening log_message = ( 'Pattoo API running on {}:{} and logging to file {}.' ''.format( self._agent_api_variable.ip_listen_address, self._agent_api_variable.ip_bind_port, self.config.log_file_api())) log.log2info(1022, log_message) # Run _StandaloneApplication(self._app, self.parent, options=options).run()
def contactable(self): """Check if target is contactable. Args: target_id: Target ID Returns: _contactable: True if a contactable """ # Define key variables _contactable = False result = None # Get target data target_name = self._snmp_ip_target # Try to reach target try: # If we can poll the SNMP sysObjectID, # then the target is contactable result = self.sysobjectid(check_reachability=True) if bool(result) is True: _contactable = True except Exception as exception_error: # Not contactable _contactable = False # Log a message log_message = ('''\ Unable to access target {} via SNMP. Make sure target is contactable and \ that the database\'s SNMP parameters for the target are correct. Fix, repeat \ your command AND make sure you set --.valid=True. Error: {}\ '''.format(target_name, exception_error)) log.log2see(55035, log_message) except: # Not contactable _contactable = False # Log a message log_message = ('Unexpected SNMP error for target {}' ''.format(target_name)) log.log2see(55036, log_message) # Return return _contactable
def test_log2see(self): """Testing function log2see.""" # Test should not cause script to crash log.log2see(self.code, self.message)