Skip to content

Commit

Permalink
ENV loading prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
mberacochea committed Feb 2, 2024
1 parent 56fa1be commit 22b5cb5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ENA_WEBIN=""
ENA_WEBIN_PASSWORD=""
41 changes: 35 additions & 6 deletions genomeuploader/genome_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 22b5cb5

Please sign in to comment.