pytron-web/pytron_run/ai_manager/__init__.py

38 lines
956 B
Python

from os.path import dirname, basename, isfile, getmtime
from importlib import import_module
from glob import glob
modules = glob(dirname(__file__)+"/*/ai.py")
__all__ = []
class UploadedAi:
def __init__(self, name, module, constructor, date):
self.name = name
self.module = module
self.constructor = constructor
self.date = date
for path in [f for f in modules if isfile(f)]:
# Remove the .py
toto = '.'.join(path.split('.')[:-1])
# Compute the name
name = toto.split('/')[-2]
# Find the module
try:
module = import_module('.'.join(toto.split('/')[-3:]))
except:
continue
# Find the constructor
constructor = getattr(module, 'Ai')
# Moment when the file was last modified
date = getmtime(path)
# Append to the list of uploaded ais
__all__.append(UploadedAi(name, module, constructor, date))
__all__ = sorted(__all__, key = lambda ai: ai.name)