<commit_before>""" This module contains a function to extract secrets from environment or a
secret file.
"""

import os
import json
import logging
from api_etl.settings import BASE_DIR

logger = logging.getLogger(__name__)


with open(os.path.join(BASE_DIR, 'secret.json')) as secrets_file:
    secrets = json.load(secrets_file)


def get_secret(setting, my_secrets=secrets, env=False):
    try:
        value = my_secrets[setting]
        if env:
            os.environ[setting] = value
        return value
    except KeyError:
        logger.warning("Impossible to get %s", setting)<commit_msg>Minor update: secrets handling<commit_after>""" This module contains a function to extract secrets from environment or a
secret file.
"""

import os
from os import path
import json
import logging
from api_etl.settings import BASE_DIR

logger = logging.getLogger(__name__)

try:
    with open(path.join(BASE_DIR, 'secret.json')) as secrets_file:
        secrets = json.load(secrets_file)
except FileNotFoundError:
    secrets = {}
    logger.info("No file")


def get_secret(setting, my_secrets=secrets, env=True):
    """
    Tries to find secrets either in secret file, or in environment variables.
    env > secret file
    Then, set it as environment variable and returns value.
    """
    value = None
    # Try to get value from env then from file
    try:
        value = os.environ[setting]
        return value
    except KeyError:
        logger.debug("Impossible to get %s from environment" % setting)

    try:
        value = my_secrets[setting]
    except KeyError:
        logger.debug("Impossible to get %s from file" % setting)

    # If value found, set it back as env
    if value and env:
        os.environ[setting] = value
        return value
    else:
        logger.warning("%s not found." % setting)
{'fraction_non_alphanumeric': 0.08507135016465422, 'fraction_numerical': 0.0, 'mean_word_length': 3.133786848072562, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '322095', 'n_tokens_mistral': 531, 'n_tokens_neox': 522, 'n_words': 186}