Exemple #1
0
def main(module, config, logger=None, verbose=False):
    if logger == None:
        logger = Logger("runner_main.log", "runner.py > main", autosave=True)
    else:
        logger.context = "runner.py > main()"
    classes = utils.get_classes(module)
    errors = utils.parse_config(config, classes)
    for i in errors:
        if type(i) == ConfigError:
            logger.error("ConfigError: " + str(i), True)
        elif type(i) == PropertyError:
            logger.warning("PropertyError: " + str(i), True)
    w = World(initial_food=config.get("World").get("initial_food", 100),
              food_function=config.get("World").get("food_function",
                                                    functions.base))
    for ccllss in classes:
        w.creatures[ccllss.__name__] = []
        n_of_class = config.get("World",
                                {}).get("initial",
                                        {}).get("n_" + ccllss.__name__, 1)
        for i in range(n_of_class):
            c = ccllss(name=ccllss.__name__,
                       species=ccllss.__name__,
                       reproduction_chance=config.get("Creatures", {}).get(
                           ccllss.__name__, {}).get("reproduction_chance",
                                                    0.1),
                       death_chance=config.get("Creatures", {}).get(
                           ccllss.__name__, {}).get("death_chance", 0.1),
                       speed=config.get("Creatures",
                                        {}).get(ccllss.__name__,
                                                {}).get("speed", 1),
                       death_age=config.get("Creatures",
                                            {}).get(ccllss.__name__,
                                                    {}).get("death_age", 100),
                       age_increments=config.get("Creatures", {}).get(
                           ccllss.__name__, {}).get("age_increments", 0.1),
                       energy=config.get("Creatures",
                                         {}).get(ccllss.__name__,
                                                 {}).get("energy", 100))
            w.creatures[ccllss.__name__].append(c)
        logger.debug(
            "Added " + str(n_of_class) + " instances of " + ccllss.__name__ +
            " to the world", verbose)
    data = do_iterations(w, config.get("iterations"), logger, config)
    logger.save()
    display_data(data)
Exemple #2
0
def exec_sql(sql_command="SELECT * FROM tasks", verbose=True, logger: Logger = None):
    """Execute sql_command on database and return None or whatever is returned from the database. If sql_command is not specified, all tasks will get returned"""
    result = None
    if logger != None and type(logger) == Logger:
        logger.set_context("exec_sql")
    elif verbose == True:
        logger = Logger("exec_sql.log", "exec_sql", True, True)
    try:
        connection = pymysql.connect(host='localhost',
                                     user='******',
                                     password='******',
                                     db='stater',
                                     charset='utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor)
    except OperationalError as e:
        logger.handle_exception(e)
        raise AuthenticationError("Access denied to database")
    if verbose and logger != None:
        logger.success("connected to database", False)
    if verbose and logger != None:
        logger.debug("executing SQL-Command: " + sql_command)
    with connection.cursor() as cursor:
        cursor.execute(sql_command)
        if "SELECT" in sql_command:
            result = cursor.fetchall()
        else:
            connection.commit()
    if verbose and logger != None:
        logger.debug("SQL query returned: " + str(result))
    try:
        connection.close()
        if verbose and logger != None:
            logger.success("Shut down connection to database", False)
    except Exception as e:
        if logger != None and type(logger) == Logger:
            logger.handle_exception(e)
        else:
            print(e)
    finally:
        return result
Exemple #3
0
)
parser.add_argument("--filename",
                    "--fname",
                    nargs="?",
                    type=str,
                    help="filename to save the file under")

arguments = parser.parse_args()
args = vars(arguments)
VERBOSE = args.get("verbose", False)

log = Logger("workflow.log", "workflow.py", True, True)
utils.log_header(log, DESCRIPTION, VERBOSE)

try:
    log.debug("Debug mode activated.", VERBOSE)
    log.debug("Args: ", VERBOSE)
    for i in args:
        log.debug(i + ": " + str(args[i]), VERBOSE)
    if not args.get("test", False):
        download.main(args, logger=log)
    if args.get("facerec", None) != None and not args.get("test", False):
        if "all" in args.get("facerec", []):
            # pass all videos from utils.getVideos() to facerec
            facerec.main({"files": utils.getVideos()})
        else:
            # pass all videos from args to facerec
            facerec.main({"files": args.get("facerec", [])})
    if not args.get("test", False):
        convert.main(args, log)
    if not args.get("test", False):