experiment.py 3.87 KB
import argparse
import os
from utils import SubCommandRunner
#from decouple import Config, RepositoryEnv
import decouple
import json
import core.env

# TODO:
# Load automatically if exists the persistency file
# Create a class that is a sigleton with configuration variables

VOLIA_ENV_VAR="VOLIA_EXPERIMENT"


def init():
    """
    Initialize volia
    """

    directory = VOLIA_HOME

    if not os.path.isdir(directory):
        os.mkdir(directory)

    #config = decouple.AutoConfig(search_path=directory)
    #config = Config(RepositoryEnv(directory))
    
    persistency_file = os.path.join(directory, "persistency.json")
    env_file = os.path.join(directory, ".env")

    with open(env_file, "w") as f:
        f.write(f"VOLIA_PERSISTENCY=\"{persistency_file}\"")
    #exit(1)
    basic_persistency = {
        "current_experiment": None
    }

    with open(persistency_file, "w") as f:
        json.dump(basic_persistency, f, indent=4)
    


def create():
    pass

def show():
    print("Volia env var : " + VOLIA_ENV_VAR)
    print("Value : " + str(os.getenv(VOLIA_ENV_VAR)))
    config = decouple.AutoConfig(search_path="")
    pass

def activate():
    pass

def deactivate():
    #pycrosskit.envariables.SysEnv.set_var(VOLIA_ENV_VAR, "UNE VARIABLE")
    print("VARIABLE: " + pycrosskit.envariables.SysEnv.get_var(VOLIA_ENV_VAR, delete=True))
    print("Desactivation")
    print("New var : " + os.getenv(VOLIA_ENV_VAR))
    pass

def remove():
    pass


if __name__ == '__main__':

    # Load VOLIA_HOME env var
    VOLIA_HOME = os.getenv("VOLIA_HOME")

    if VOLIA_HOME is None:
        raise Exception("VOLIA_HOME is not defined. Please, define where you want to initialize VOLIA_HOME.\nWe suggest to set this variable to following value: /home/[your name]/.volia.")

    # Load environment file if exists
    # TODO: Pas besoin de INIT finalement.
    CONFIG = None
    PERSISTENCY_DATA = None
    env_file = os.path.join(VOLIA_HOME, ".env")

    if not os.path.isfile(env_file):
        print("VOLIA is not initialized. Initializing.")
        init()
    
    # Load le fichier
    CONFIG = decouple.AutoConfig(search_path=VOLIA_HOME)
    # Then loading the persistency file
    persistency_file = os.path.join(VOLIA_HOME, "persistency.json")
    PERSISTENCY_DATA = core.env.Env(persistency_file)

    # PARSER
    parser = argparse.ArgumentParser(description="...")
    subparsers = parser.add_subparsers(title="actions")

    # init : initialize the volia environment tool
    parser_init = subparsers.add_parser("init", help="init volia environment")
    parser_init.set_defaults(which="init")

    # create a new env
    parser_create = subparsers.add_parser("create", help="create an experimental environment")
    parser_create.set_defaults(which="create")

    # activate env
    parser_activate = subparsers.add_parser("activate", help="activate an experimental environment")
    parser_activate.set_defaults(which="activate")
    '''
    Pour activater, il suffit d'ajouter la variable d'environnement.
    '''
    # deactivate env
    parser_deactivate = subparsers.add_parser("deactivate", help="deactivate an experimental environment")
    parser_deactivate.set_defaults(which="deactivate")
    '''
    Pour d├ęsactiver il suffit d'enlever la variable d'environnement.
    '''

    # remove env
    parser_remove = subparsers.add_parser("remove", help="remove an experimental environment")
    parser_remove.set_defaults(which="remove")
    
    # show env
    parser_show = subparsers.add_parser("show", help="show the list of experimental environment")
    parser_show.set_defaults(which="show")

    # Parse
    args = parser.parse_args()

    # Run commands
    runner = SubCommandRunner({
        "create": create,
        "activate": activate,
        "deactivate": deactivate,
        "remove": remove,
        "show" : show,
        "init" : init
    })

    runner.run(args.which, args.__dict__, remove="which")