-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a script that generates credentials templates
- Loading branch information
1 parent
566d453
commit e7423c9
Showing
6 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
""" | ||
Utility script used for copying credentials templates to a user-defined path | ||
""" | ||
|
||
import click | ||
import dotenv | ||
import json | ||
import toml | ||
from pathlib import Path | ||
|
||
default_path = Path.home()/".earthdaily/credentials" | ||
|
||
default_configuration = { | ||
"EDS_AUTH_URL" : "https://..", | ||
"EDS_CLIENT_ID" : "123", | ||
"EDS_SECRET" : "123", | ||
} | ||
|
||
def write_json(json_path: Path) -> None: | ||
""" | ||
Write template JSON credentials file. | ||
Parameters | ||
---------- | ||
json_path : Path | ||
Path to output JSON file. | ||
""" | ||
print(f"Try to write credentials template to {json_path}") | ||
|
||
if json_path.exists(): | ||
print(f"{json_path} already exists") | ||
return | ||
|
||
with json_path.open("w") as f: | ||
json.dump(default_configuration, f) | ||
|
||
print(f"Credentials file written to {json_path}") | ||
print("Please edit it to insert your credentials") | ||
|
||
def write_toml(toml_path: Path) -> None: | ||
""" | ||
Write template TOML credentials file. | ||
Parameters | ||
---------- | ||
toml_path : Path | ||
Path to output TOML file. | ||
""" | ||
print(f"Try to write credentials template to {toml_path}") | ||
|
||
if toml_path.exists(): | ||
print(f"{toml_path} already exists") | ||
return | ||
|
||
with toml_path.open("w") as f: | ||
toml.dump({"default" : default_configuration}, f) | ||
|
||
print(f"Credentials file written to {toml_path}") | ||
print("Please edit it to insert your credentials") | ||
|
||
def write_env(env_path: Path) -> None: | ||
""" | ||
Write template .env credentials file. | ||
Parameters | ||
---------- | ||
env_path : Path | ||
Path to output .env file. | ||
""" | ||
print(f"Try to write credentials template to {env_path}") | ||
|
||
if env_path.exists(): | ||
print(f"{env_path} already exists") | ||
return | ||
|
||
with env_path.open("w") as f: | ||
for key, value in default_configuration.items(): | ||
line = f'{key}="{value}"\n' | ||
f.write(line) | ||
|
||
print(f"Credentials file written to {env_path}") | ||
print("Please edit it to insert your credentials") | ||
|
||
|
||
@click.command("Copy credentials templates in all accepted formats") | ||
@click.option("--json", "json_path", type = click.Path(path_type = Path, exists = False), required = False, help = "Path to the output JSON file containing the credentials keys (but no values)") | ||
@click.option("--toml", "toml_path", type = click.Path(path_type = Path, exists = False), required = False, help = "Path to the output TOML file containing the credentials keys (but no values)") | ||
@click.option("--env", "env_path", type = click.Path(path_type = Path, exists = False), required = False, help = "Path to the output .env file containing the credentials keys (but no values)") | ||
@click.option("--default", "default", is_flag=True, show_default=True, default = False, help=f"Copy the TOML template to {default_path}, with credential keys (and no values)") | ||
def cli(json_path:Path, toml_path: Path, env_path: Path, default: bool) -> None: | ||
|
||
if json_path is not None: | ||
write_json(json_path = json_path) | ||
|
||
if toml_path is not None: | ||
write_toml(toml_path = toml_path) | ||
|
||
if env_path is not None: | ||
write_env(env_path = env_path) | ||
|
||
if default: | ||
default_path.parent.mkdir(exist_ok = True, parents = True) | ||
write_toml(toml_path = default_path) | ||
|
||
if __name__ == "__main__": | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ dependencies: | |
- dask>=2024.4 | ||
- spyndex | ||
- dask-image | ||
- toml | ||
- toml | ||
- click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,6 @@ dependencies: | |
- dask-image | ||
- geocube | ||
- spyndex | ||
- sphinx-rtd-theme | ||
- sphinx-rtd-theme | ||
- toml | ||
- click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters