From 22b5cb59d65dc62bc7dbb4b54d78712a1f42fff7 Mon Sep 17 00:00:00 2001 From: Martin Beracochea Date: Fri, 2 Feb 2024 13:30:39 +0000 Subject: [PATCH] ENV loading prototype --- .env.example | 2 ++ genomeuploader/genome_upload.py | 41 ++++++++++++++++++++++++++++----- pyproject.toml | 3 ++- 3 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..41fba3f --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +ENA_WEBIN="" +ENA_WEBIN_PASSWORD="" \ No newline at end of file diff --git a/genomeuploader/genome_upload.py b/genomeuploader/genome_upload.py index 6d0627c..36e918d 100755 --- a/genomeuploader/genome_upload.py +++ b/genomeuploader/genome_upload.py @@ -20,9 +20,12 @@ import argparse import re import json -import pandas as pd +from pathlib import Path from datetime import date, datetime as dt +import pandas as pd +from dotenv import load_dotenv + import xml.etree.ElementTree as ET import xml.dom.minidom as minidom @@ -825,8 +828,33 @@ def __init__(self, argv=sys.argv[1:]): self.genomeMetadata = self.args.genome_info self.genomeType = "bins" if self.args.bins else "MAGs" self.live = True if self.args.live else False - self.username = self.args.webin - self.password = self.args.password + + if self.args.webin and self.args.password: + self.username = self.args.webin + self.password = self.args.password + else: + # Config file # + user_config = Path.home() / ".genome_uploader.config" + if user_config.exists(): + logger.debug("Loading the env variables from {user_config}") + load_dotenv(str(user_config)) + else: + cwd_config = Path.cwd() / ".genome_uploader.config" + if not cwd_config.exists(): + logger.debug(f"Loading the variables from the current directory {Path.cwd()}.genome_uploader.config") + load_dotenv(str(cwd_config)) + else: + logger.debug("Trying to load env variables from the .env file") + # from a local .env file + load_dotenv() + + self.username = os.getenv("ENA_WEBIN") + self.password = os.getenv("ENA_WEBIN_PASSWORD") + + if not self.username or not self.password: + logger.error("ENA Webin username or password are empty") + sys.exit(1) + self.tpa = True if self.args.tpa else False self.centre_name = self.args.centre_name self.force = True if self.args.force else False @@ -855,9 +883,10 @@ def parse_args(self, argv): "option allows to validate samples beforehand") parser.add_argument('--tpa', action='store_true', help="Select if uploading TPA-generated genomes") - parser.add_argument('--webin', required=True, help="Webin id") - parser.add_argument('--password', required=True, help="Webin password") - parser.add_argument('--centre_name', required=True, help="Name of the centre uploading genomes") + # Users can provide their credentials and centre name manually or using a config file + parser.add_argument('--webin', required=False, help="Webin id") + parser.add_argument('--password', required=False, help="Webin password") + parser.add_argument('--centre_name', required=False, help="Name of the centre uploading genomes") args = parser.parse_args(argv) diff --git a/pyproject.toml b/pyproject.toml index 28eefce..bf5256f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,8 @@ classifiers = [ dependencies = [ "requests==2.26.0", - "pandas==1.4.1" + "pandas==1.4.1", + "python-dotenv==1.0.1" ] [project.optional-dependencies]