def sort_back_test_data(filename, sort_by, reverse=True): from kairos import tools from tv import tv debug.file_name = 'sort_back_test_data.log' log = debug.create_log() log.setLevel(20) config = tools.get_config() log.setLevel(config.getint('logging', 'level')) # log.info("{} {} {}".format(filename, sort_by, reverse)) path = os.path.join(os.path.curdir, filename) if os.path.exists(path): try: import json with open(path, 'r') as stream: data = json.load(stream) data = tv.back_test_sort(data, sort_by, reverse) with open(path, 'w') as stream: stream.write(json.dumps(data, indent=4)) except Exception as e: log.exception(e) else: log.error("{} doesn't exist".format(filename)) exit(1)
def clean_browser_data(): debug.file_name = 'clean_browser_data.log' log = debug.create_log() log.setLevel(20) from kairos import tools config = tools.get_config() log.setLevel(config.getint('logging', 'level')) driver_type = 'chromedriver' driver_count = sum(1 for process in psutil.process_iter() if process.name().startswith(driver_type)) if config.has_option('webdriver', 'user_data_directory'): user_data_directory = config.get('webdriver', 'user_data_directory') user_data_base_dir, tail = os.path.split(user_data_directory) with os.scandir(user_data_base_dir) as user_data_directories: for entry in user_data_directories: # remove all directories that start with 'kairos_' followed by a number path = os.path.join(user_data_base_dir, entry) if (entry.name.startswith('kairos_') and not tools.path_in_use( path, log)) or (entry.name.startswith('kairos') and driver_count == 0): if entry.name != user_data_directory: try: shutil.rmtree(path) log.info("{} removed".format(entry.name)) except Exception as e: log.exception(e) elif entry.name.startswith('kairos_'): log.info( "{} is in use and cannot be removed (close all instances of {} and try again)" .format(entry.name, driver_type)) log.info("cleaning complete") else: log.info("skipping. User data directory is not set in kairos.cfg")
def test_mongodb(): # set log name before importing... the import creates the log whatever it's file name debug.file_name = 'test_mongodb.log' log = debug.create_log() log.setLevel(20) from kairos import tools config = tools.get_config() log.setLevel(config.getint('logging', 'level')) connection_string = config.get('mongodb', 'connection_string') collection = config.get('mongodb', 'collection') from kairos import mongodb mongodb.test(connection_string, collection, log) exit(0)
# File: tools.py import os from kairos import debug from collections import OrderedDict from configparser import RawConfigParser log = debug.create_log() def get_config(current_dir): config = RawConfigParser( allow_no_value=True, strict=False, empty_lines_in_values=False, dict_type=ConfigParserMultiValues, converters={"list": ConfigParserMultiValues.getlist}) config_file = os.path.join(current_dir, "kairos.cfg") if os.path.exists(config_file): config.read(config_file) if config.getboolean('logging', 'clear_on_start_up'): debug.clear_log() log.setLevel(config.getint('logging', 'level')) else: log.error("File " + config_file + " does not exist") log.exception(FileNotFoundError) exit(0) return config def chunks(collection, size): """Yield successive n-sized chunks from l."""
def create_log(mode='a'): return debug.create_log(mode)