Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions aiida_project/commands/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions aiida_project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)


Expand Down