diff --git a/cellpy/cli.py b/cellpy/cli.py index 1caabe22..c715b967 100644 --- a/cellpy/cli.py +++ b/cellpy/cli.py @@ -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 @@ -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") @@ -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( @@ -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: @@ -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) @@ -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. diff --git a/cellpy/parameters/.cellpy_prms_default.conf b/cellpy/parameters/.cellpy_prms_default.conf index ce5f256f..a760a0d5 100644 --- a/cellpy/parameters/.cellpy_prms_default.conf +++ b/cellpy/parameters/.cellpy_prms_default.conf @@ -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 diff --git a/cellpy/parameters/prmreader.py b/cellpy/parameters/prmreader.py index f22947d2..f48da260 100644 --- a/cellpy/parameters/prmreader.py +++ b/cellpy/parameters/prmreader.py @@ -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= +# CELLPY_KEY_FILENAME= +# CELLPY_HOST= +# CELLPY_USER= +""" + # logger = logging.getLogger(__name__) yaml = YAML() @@ -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: @@ -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 @@ -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(): diff --git a/cellpy/parameters/prms.py b/cellpy/parameters/prms.py index 7c03cb08..877c614e 100644 --- a/cellpy/parameters/prms.py +++ b/cellpy/parameters/prms.py @@ -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) @@ -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