diff --git a/aiida_project/commands/main.py b/aiida_project/commands/main.py index fdcd450..5de45a8 100644 --- a/aiida_project/commands/main.py +++ b/aiida_project/commands/main.py @@ -59,16 +59,9 @@ def init(shell: Optional[ShellType] = None): default=detected_shell, ) - if shell_str == "fish": - print( - "[bold red]Error:[/] `fish` is not yet supported. " - "If this is Julian: you better get to it 😜" # Nhehehe - ) - return - config.set_key("aiida_project_shell", shell_str) + config.write_key("aiida_project_shell", shell_str) - env_file_path = Path.home() / Path(f".{shell_str}rc") if "Created by `aiida-project init`" in env_file_path.read_text(): print( @@ -85,14 +78,11 @@ def init(shell: Optional[ShellType] = None): f"\n# Created by `aiida-project init` on " f"{datetime.now().strftime('%d/%m/%y %H:%M')}\n" ) - handle.write(f"export $(grep -v '^#' {config.Config.env_file} | xargs)") - handle.write(CDA_FUNCTION) - - config.set_key( + config.write_key("aiida_project_dir", config.aiida_project_dir.as_posix()) + config.write_key( "aiida_venv_dir", os.environ.get("WORKON_HOME", config.aiida_venv_dir.as_posix()), ) - config.set_key("aiida_project_dir", config.aiida_project_dir.as_posix()) print("\nāœØšŸš€ AiiDA-project has been initialised! šŸš€āœØ\n") print("[bold blue]Info:[/] For the changes to take effect, run the following command:") print(f"\n source {env_file_path.resolve()}\n") @@ -121,6 +111,8 @@ def create( config = ProjectConfig() if config.is_not_initialised(): return + else: + config = config.from_env_file() venv_path = config.aiida_venv_dir / Path(name) project_path = config.aiida_project_dir / Path(name) diff --git a/aiida_project/config.py b/aiida_project/config.py index ebcb90c..d10350a 100644 --- a/aiida_project/config.py +++ b/aiida_project/config.py @@ -28,8 +28,8 @@ class ShellType(str, Enum): class ProjectConfig(BaseSettings): """Configuration class for configuring `aiida-project`.""" - aiida_venv_dir: Path = Path(Path.home(), ".aiida_venvs") - aiida_project_dir: Path = Path(Path.home(), "project") + aiida_venv_dir: Path = Path.home() / Path(".aiida_venvs") + aiida_project_dir: Path = Path.home() / Path("aiida_projects") # ? Make hidden? aiida_default_python_path: Optional[Path] = None aiida_project_structure: dict = DEFAULT_PROJECT_STRUCTURE aiida_project_shell: str = "bash" @@ -44,10 +44,23 @@ def is_not_initialised(self): print("[bold blue]Info:[/bold blue] Please run `aiida-project init` to get started.") return True - def set_key(self, key, value): + @classmethod + def from_env_file(cls, env_path: Optional[Path] = None): + """Populate config instance from env file.""" + + if env_path is None: + env_path = Path.home() / Path(".aiida_project.env") + # ? Currently works only with default path of Config class... + config_dict = {k:v for k,v in dotenv.dotenv_values(env_path).items()} + config_instance = cls.parse_obj(config_dict) + return config_instance + + # ? Renamed these methods, as they actually write to the env file, and we + # ? might implement a method that sets the key of the ProjectConfig class? + def write_key(self, key, value): dotenv.set_key(self.Config.env_file, key, value) - def get_key(self, key): + def read_key(self, key): return dotenv.get_key(key)