Skip to content

Commit

Permalink
make example env file when running cellpy setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jepegit committed Dec 26, 2023
1 parent f9a95eb commit 5f24578
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
42 changes: 42 additions & 0 deletions cellpy/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def save_prm_file(prm_filename):
prmreader._write_prm_file(prm_filename)


def dump_env_file(env_filename):
"""saves (writes) the env to file"""
print("dumping env file to", env_filename)
prmreader._write_env_file(env_filename)


def get_package_prm_dir():
"""gets the folder where the cellpy package lives"""
return pathlib.Path(cellpy.parameters.__file__).parent
Expand Down Expand Up @@ -228,6 +234,7 @@ def setup(
# generate variables
init_filename = prmreader.create_custom_init_filename()
user_dir, dst_file = prmreader.get_user_dir_and_dst(init_filename)
env_file = prmreader.get_env_file()

if dry_run:
click.echo("Create custom init filename and get user_dir and destination")
Expand Down Expand Up @@ -262,6 +269,9 @@ def setup(
click.echo(f"[cellpy] {dst_file} not found -> I will make one for you!")
reset = True

if not pathlib.Path(env_file).is_file():
click.echo(f"[cellpy] {env_file} not found -> I will make one, but you must edit it yourself!")

if interactive:
click.echo(" interactive mode ".center(80, "-"))
_update_paths(
Expand All @@ -273,6 +283,7 @@ def setup(
interactive=True,
)
_write_config_file(user_dir, dst_file, init_filename, dry_run)
_write_env_file(user_dir, env_file, dry_run)
_check(dry_run=dry_run)

else:
Expand All @@ -287,6 +298,7 @@ def setup(
silent=silent,
)
_write_config_file(user_dir, dst_file, init_filename, dry_run)
_write_env_file(user_dir, env_file, dry_run)
_check(dry_run=dry_run)


Expand Down Expand Up @@ -768,6 +780,36 @@ def _write_config_file(user_dir, dst_file, init_filename, dry_run):
)


def _write_env_file(user_dir, dst_file, dry_run):
click.echo(" update configuration ".center(80, "-"))
click.echo("[cellpy] (setup) Writing environment file:")
click.echo(f"\n {user_dir}\n")

if os.path.isfile(dst_file):
click.echo(f"[cellpy] (setup) Environment file {dst_file} already exists!")
return

try:
if dry_run:
click.echo(
f"*** dry-run: skipping actual saving of {dst_file} ***", color="red"
)
else:
click.echo(f"[cellpy] (setup) Saving file ({dst_file})")
dump_env_file(dst_file)

except ConfigFileNotWritten:
_txt = "[cellpy] (setup) No, that did not work either.\n"
_txt += "[cellpy] (setup) Well, guess you have to talk to the developers."
click.echo(_txt)
else:
click.echo(f"[cellpy] (setup) Environment file written!")
click.echo(
f"[cellpy] (setup) OK! Now you can edit it. For example by "
f"issuing \n\n [your-favourite-editor] {init_filename}\n"
)


def _get_default_editor():
"""
Return the default text editor.
Expand Down
1 change: 1 addition & 0 deletions cellpy/parameters/.cellpy_prms_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Reader:
capacity_interpolation_step: 2.0
use_cellpy_stat_file: false
auto_dirs: true
jupyter_executable:
Materials:
cell_class: LIB
default_material: silicon
Expand Down
38 changes: 36 additions & 2 deletions cellpy/parameters/prmreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@

DEFAULT_FILENAME = DEFAULT_FILENAME_START + "default" + DEFAULT_FILENAME_END

ENVIRONMENT_EXAMPLE = """
# This is an example of an environment file for cellpy.
# The environment file is used to set environment variables
# that are used by cellpy.
# The environment file should be located in the user directory
# (i.e. the directory returned by pathlib.Path.home()).
# The default environment file is named .env_cellpy, but you can
# change this in your config file.
# The environment file should contain the following variables:
# CELLPY_PASSWORD=<password>
# CELLPY_KEY_FILENAME=<key_filename>
# CELLPY_HOST=<host>
# CELLPY_USER=<user>
"""

# logger = logging.getLogger(__name__)

yaml = YAML()
Expand Down Expand Up @@ -89,7 +104,7 @@ def get_user_dir():


def _write_prm_file(file_name=None):
logging.debug("saving configuration to %s" % file_name)
logging.debug(f"saving configuration to {file_name}")
config_dict = _pack_prms()

try:
Expand All @@ -103,6 +118,24 @@ def _write_prm_file(file_name=None):
raise ConfigFileNotWritten


# TODO: make this alive by setting it to not dev:
def _write_env_file(env_file_name=None):
"""writes example environment file"""
dev = True
if env_file_name is None:
env_file_name = get_env_file_name()

logging.debug(f"saving environment arguments to {env_file_name}")
if dev:
print("---in-env-file------------------------------------")
print(ENVIRONMENT_EXAMPLE)
print("--------------------------------------------------")
return

with open(env_file_name, "w") as env_file:
env_file.write(ENVIRONMENT_EXAMPLE)


def _update_prms(config_dict, resolve_paths=True):
"""updates the prms with the values in the config_dict"""
# config_dict is your current config
Expand Down Expand Up @@ -331,7 +364,8 @@ def _save_current_prms_to_user_dir():

def get_env_file_name():
"""returns the location of the env-file"""
return pathlib.Path(prms.Paths.env_file)
env_file = pathlib.Path(prms.Paths.env_file)
return env_file


def info():
Expand Down
3 changes: 2 additions & 1 deletion cellpy/parameters/prms.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def keys(self):
return self.__dataclass_fields__.keys()


# If updating this, you will have to do a lot of tweaks.
# If updating PathsClass, you will have to do a lot of tweaks.
# .cellpy_prms_default.conf
# cli.py (_update_paths)
# test_cli_setup_interactive (NUMBER_OF_DIRS)
Expand Down Expand Up @@ -135,6 +135,7 @@ class ReaderClass(CellPyConfig):
auto_dirs: bool = (
True # v2.0 search in prm-file for res and hdf5 dirs in cellpy.get()
)
jupyter_executable: str = "jupyter"


@dataclass
Expand Down

0 comments on commit 5f24578

Please sign in to comment.