def test_app(): # Create the new board & update the board id environment variable file_path = find_dotenv('.env') load_dotenv(file_path, override=True) os.environ['FLASK_SKIP_LOGIN'] = "******" os.environ['MONGO_DB'] = "test_db" mongo_srv = os.getenv('MONGO_SRV') mongo_user = os.getenv('MONGO_USER') mongo_pwd = os.getenv('MONGO_PWD') mongo_connection = os.getenv('MONGO_CONNECTION') testClient = ToDoMongoClient(mongo_user, mongo_pwd, mongo_srv, "test_db", mongo_connection) admin_user = testClient.add_user("admin_user", "Admin") writer_user = testClient.add_user("writer_user", "Writer") reader_user = testClient.add_user("reader_user", "Reader") application = create_app() # start the app in its own thread. thread = Thread(target=lambda: application.run(use_reloader=False)) thread.daemon = True thread.start() yield application # Tear Down thread.join(1) testClient.delete_user(admin_user.id) testClient.delete_user(writer_user.id) testClient.delete_user(reader_user.id)
def __init__(self, ): load_dotenv() service_account = os.getenv('service_account') file_path = os.path.abspath( os.path.join(os.getcwd(), os.getenv('service_account_file_path'))) self.ee = authenticate_ee(file_path=file_path, service_account=service_account)
def main(): if isfile(os.getenv("MA_SELECT_ENV_FILE", "")): load_dotenv(os.environ["MA_SELECT_ENV_FILE"]) apply_dotenv_vars() args = parse_command_line_args() errorfiles, outputfiles = get_all_jobfiles(args.start_pid, args.base_dir) todo_jobs, dones, job_infos, leftover_text = split_all(errorfiles) #TODO consider todo_jobs and dones! job_infos = merge_job_infos(job_infos) assert len([i["jobid"] for i in job_infos]) == len(set([i["jobid"] for i in job_infos])) for info in job_infos: if info["jobid"] in dones.keys(): info["finished_at"] = dones[info["jobid"]] elif not "state" in info: info["state"] = {i: get_status(i, status_attempts=1, silent=True, detailed=True) for i in info["external_id"]} # assert len([i for i in job_infos if not i.get("finished_at")]) == len(job_infos)-len(dones) states = [{k:v for k,v in i.get("state", {}).items() if v != "unknown"} if any(v for v in i.get("state", {}).values() if v != "unknown") else {max(i.get("external_id")): "success"} if i.get("finished_at") else {} for i in job_infos] states = [(dict([max([(k, v) for k, v in i.items() if v == j], key=lambda x:x[0]) for j in set(i.values())]), dict(Counter(list(i.values())))) for i in states] #the highest ids per-state, number of states for state in states: if "success" in state[1] and len(state[0]) > 1: assert all(i > int({v: k for k, v in state[0].items()}["success"]) for i in [int(k) for k,v in state[0].items() if v != "success"]), "There is a succeeded task-id but it's not the last one!" for single_state, infos in zip(states, job_infos): infos["single_state"] = single_state for show in [j for j in ["running", "enqueued", "failed", "success"] if j in set(flatten([i[1].keys() for i in states]))]: toshow = [i for i in job_infos if show in i["single_state"][0].values()] if toshow: print("The following runs are "+show+":") print("\n".join([f" {r['output'].ljust(max(len(i['output']) for i in toshow))} (pid {','.join(r['external_id'])})" for r in toshow])) job_infos = [i for i in job_infos if not show in i["single_state"][0].values()] return
def load(self): ''' Protected method to read the file for the first time. ''' settings_file = os.environ.get(ENVIRONMENT_VARIABLE) path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'configuration.ini') if settings_file and os.path.isfile(settings_file): settings_files = [path, settings_file] else: settings_files = [path] setattr(self, 'SETTINGS_FILES', settings_files) parser = ConfigParser.SafeConfigParser() # TODO: fail gracefully when no header is detected at the beginning. parser.read(settings_files) set_settings_from_configuration(self, parser) dotenv_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), '.env') if os.path.isfile(dotenv_path): load_dotenv(dotenv_path) set_settings_from_environment(self, getattr(self, 'ENVIRON').split(',')) else: LOGGER.warning('There is no .env file in the configuration folder.')
def client(): file_path = find_dotenv('.env.test') load_dotenv(file_path, override=True, verbose=True) with mongomock.patch(servers=(('fakemongo.com', 27017), )): test_app = create_app() with test_app.test_client() as client: yield client
def client(): # Use our test integration config instead of the 'real' version file_path = find_dotenv('.env.test') load_dotenv(file_path, override=True) # Create the new app. test_app = app.create_app() # Use the app to create a test_client that can be used in our tests. with test_app.test_client() as client: yield client
def load_data(root_dir, mode, overide=None): dotenv_path = 'num_config.env' load_dotenv(dotenv_path=dotenv_path) curr_round = os.getenv('LATEST_ROUND') data_path = root_dir + '/numerai_dataset_' + str(curr_round) if overide: data = dt.fread(overide).to_pandas() elif mode == 'train': data = dt.fread(data_path + '/numerai_training_data.csv').to_pandas() elif mode == 'test': data = dt.fread(data_path + '/numerai_tournament_data.csv').to_pandas() return data
def client(): # Use our test integration config instead of the 'real' version file_path = find_dotenv('.env.test') load_dotenv(file_path, override=True) with mongomock.patch(servers=(('test.mongodb.net', 27017), )): # Create the new app. test_app = create_app() # Use the app to create a test_client that can be used in our tests. with test_app.test_client() as client: yield client
def __init__(self, default_variables: dict = None, env_file: str = None): """ :param env_file: """ if default_variables is None: default_variables = {} self.default_variables = default_variables self.variables = {} if env_file is not None: if os.path.isfile(env_file): load_dotenv(env_file) else: logging.error("env file is not found")
def read_env( path: _StrType = None, recurse: _BoolType = True, verbose: _BoolType = False, override: _BoolType = False, raise_error_if_not_found: _BoolType = False, ) -> None: """Read a .env file into os.environ. If .env is not found in the directory from which this method is called, the default behavior is to recurse up the directory tree until a .env file is found. If you do not wish to recurse up the tree, you may pass False as a second positional argument. """ if path is None: # By default, start search from the same directory this function is called current_frame = inspect.currentframe() if not current_frame: raise RuntimeError("Could not get current call frame.") frame = typing.cast(types.FrameType, current_frame.f_back) caller_dir = Path(frame.f_code.co_filename).parent.resolve() start = caller_dir / ".env" else: if Path(path).is_dir(): raise ValueError("path must be a filename, not a directory.") start = Path(path) is_env_file_found = False # TODO: Remove str casts when we drop Python 3.5 if recurse: start_dir, env_name = os.path.split(str(start)) if not start_dir: # Only a filename was given start_dir = os.getcwd() for dirname in _walk_to_root(start_dir): check_path = Path(dirname) / env_name if check_path.exists(): is_env_file_found = True load_dotenv(str(check_path), verbose=verbose, override=override) break else: if start.exists(): is_env_file_found = True load_dotenv(str(start), verbose=verbose, override=override) if verbose: print("environ: is_env_file_found={}".format(is_env_file_found)) if raise_error_if_not_found and not is_env_file_found: raise OSError("File not found: {}".format(path or ".env"))
def get_filename(variables, get_dependencies=True, doprint=True): if not isinstance(variables, dict): variables = dict([[j.strip() for j in i.split(":")] for i in variables.split(",")]) try: path = generated_paths[LAST_RESULT].format_map( {k.lower(): v for k, v in variables.items()}) except KeyError: warnings.warn("Assuming that generated_paths is sorted!") for key in list(generated_paths.keys())[::-1]: try: path = generated_paths[key].format_map( {k.lower(): v for k, v in variables.items()}) except KeyError: pass else: break if not get_dependencies: if doprint: print(path) return path else: if isfile(os.getenv("MA_SELECT_ENV_FILE", "")): load_dotenv(os.environ["MA_SELECT_ENV_FILE"]) apply_dotenv_vars() fname = join(os.getenv("MA_BASE_DIR"), path) with open(fname, "rb") as rfile: loadeds = next(ijson.items(rfile, "loaded_files")) dependencies = list({ k: v["path"] for k, v in loadeds.items() if k not in ["raw_descriptions", "description_languages", "title_languages"] }.values()) diff_dirs = [i for i in dependencies if dirname(dirname(path)) in i][0] pre_dir = diff_dirs[:diff_dirs.find(dirname(dirname(path)))] if doprint: print("Data-Dir:", pre_dir) print( "Files:", ", ".join([i.replace(pre_dir, "") for i in dependencies] + [path])) print( "Copy-command:", f"rsync -az --progress --relative grid:{' :'.join([i.replace(pre_dir, pre_dir+'.'+os.sep) for i in dependencies]+[pre_dir+'.'+os.sep+path])} " + os.environ["MA_BASE_DIR"]) print("Env-vars:", get_envvars(path)[0]) return path, pre_dir, dependencies
def main(): load_dotenv("../../.env") credential = AzureKeyCredential(os.environ.get("COGNITIVE_SERVICE_KEY")) form_recognizer_client = FormRecognizerClient( endpoint="https://ocrdemo1.cognitiveservices.azure.com/", credential=credential ) with open("../../data/invoice2.png", "rb") as f: invoice = f.read() poller = form_recognizer_client.begin_recognize_content(invoice) page = poller.result() img = imread("../../data/invoice2.png") img = draw_blocks(img, page) cv2.imshow("img", img) cv2.waitKey(0)
def configure(gcalendar: GCalendar = None) -> Flask: if not gcalendar: logging.warn("Trying to import gcalendar...") load_dotenv() GCALENDAR_ID = getenv("GCALENDAR_ID") GTIMEZONE = getenv("GTIMEZONE") gcalendar = GCalendar(GCALENDAR_ID, timezone=GTIMEZONE) if gcalendar: api.config["gcalendar"] = gcalendar else: raise Exception( "Cannot configure gcalendar. (Probably missing some env)") return api
def getDB(self): load_dotenv() user = os.getenv("DBUSER") password = os.getenv("DBPASSWD") host = os.getenv("DBIP") port = int(os.getenv("DBPORT")) database = os.getenv("DBNAME") try: conn = mariadb.connect(user=user, password=password, host=host, port=port, database=database) conn.autocommit = True cur = conn.cursor() return cur except mariadb.Error as e: print(f"Error connecting to MariaDB Platform: {e}") return False
def read_env( path: str = None, recurse: bool = True, stream: str = None, verbose: bool = False, override: bool = False, ) -> DotEnv: """Read a .env file into os.environ. If .env is not found in the directory from which this method is called, the default behavior is to recurse up the directory tree until a .env file is found. If you do not wish to recurse up the tree, you may pass False as a second positional argument. """ # By default, start search from the same file this function is called if path is None: current_frame = inspect.currentframe() if not current_frame: raise RuntimeError("Could not get current call frame.") frame = current_frame.f_back caller_dir = os.path.dirname(frame.f_code.co_filename) # Will be a directory start = os.path.join(os.path.abspath(caller_dir)) else: # Could be directory or a file start = path if recurse: env_name = os.path.basename(start) if os.path.isfile( start) else ".env" for dirname in _walk_to_root(start): check_path = os.path.join(dirname, env_name) if os.path.exists(check_path): return load_dotenv(check_path, stream=stream, verbose=verbose, override=override) else: if path is None: start = os.path.join(start, ".env") return load_dotenv(start, stream=stream, verbose=verbose, override=override)
def app_with_temp_db(): file_path = find_dotenv('.env') load_dotenv(file_path, override=True, verbose=True) # Update the mongo db name environment variable test_db = "Todo_e2e_tests" os.environ['MONGO_DATABASE_NAME'] = test_db # construct the new application application = create_app() # start the app in its own thread. thread = Thread(target=lambda: application.run(use_reloader=False)) thread.daemon = True thread.start() yield application # Tear Down thread.join(1) mongoClient = pymongo.MongoClient(f'{os.environ["MONGO_CONNECTION_STRING"]}') mongoClient.drop_database(test_db)
def configure(): noa_username = Prompt.ask("Enter your Noa Workbook username") noa_password = Prompt.ask( "Enter your password [dim italic](input will be hidden)[/dim italic]", password=True, ) dotenv_location = Path(__file__).parent.parent / ".env" set_key( dotenv_path=dotenv_location, key_to_set=NOA_USERNAME_KEY, value_to_set=noa_username, ) set_key( dotenv_path=dotenv_location, key_to_set=NOA_PASSWORD_KEY, value_to_set=noa_password, ) load_dotenv(override=True) client = NoaWorkbook() try: default_activity = client.get_day_visualization() except JSONDecodeError: print("[red]Wrong username or password[/red]") return 1 console = Console(highlight=False) console.print( dedent( f""" [green]Success![/green] You are good to go :partying_face: Your hours will be logged to the following task when using [yellow]tt-a[/yellow] * Client: {default_activity.customer_name} * Job: {default_activity.job_name} * Task: {default_activity.task_description} """ ) )
def apply_dotenv_vars(ENV_PREFIX="MA"): if os.getenv(ENV_PREFIX + "_SELECT_ENV_FILE"): assert isfile(os.getenv(ENV_PREFIX + "_SELECT_ENV_FILE")) load_dotenv(os.getenv(ENV_PREFIX + "_SELECT_ENV_FILE")) curr_envvars = { k: v for k, v in os.environ.items() if k.startswith(ENV_PREFIX + "_") } curr_envvars = {k: os.path.expandvars(v) for k, v in curr_envvars.items()} #replace envvars curr_envvars = { k: os.path.expandvars( os.path.expandvars(re.sub(r"{([^-\s]*?)}", r"$\1", v))) for k, v in curr_envvars.items() } #replace {ENV_VAR} with $ENV_VAR and then apply them envfile = curr_envvars.get(ENV_PREFIX + "_" + "ENV_FILE") if envfile and not isfile(envfile) and isfile( join(curr_envvars.get(ENV_PREFIX + "_" + "CONFIGDIR", ""), envfile)): curr_envvars[ENV_PREFIX + "_" + "ENV_FILE"] = join( curr_envvars.get(ENV_PREFIX + "_" + "CONFIGDIR"), envfile) for k, v in curr_envvars.items(): os.environ[k] = v
def read_env( path: typing.Optional[_StrType] = None, recurse: _BoolType = True, verbose: _BoolType = False, override: _BoolType = False, ) -> None: """Read a .env file into os.environ. If .env is not found in the directory from which this method is called, the default behavior is to recurse up the directory tree until a .env file is found. If you do not wish to recurse up the tree, you may pass False as a second positional argument. """ if path is None: # By default, start search from the same directory this function is called current_frame = inspect.currentframe() if current_frame is None: raise RuntimeError("Could not get current call frame.") frame = current_frame.f_back assert frame is not None caller_dir = Path(frame.f_code.co_filename).parent.resolve() start = caller_dir / ".env" else: if Path(path).is_dir(): raise ValueError("path must be a filename, not a directory.") start = Path(path) if recurse: start_dir, env_name = os.path.split(start) if not start_dir: # Only a filename was given start_dir = os.getcwd() for dirname in _walk_to_root(start_dir): check_path = Path(dirname) / env_name if check_path.exists(): load_dotenv(check_path, verbose=verbose, override=override) return else: load_dotenv(str(start), verbose=verbose, override=override)
import os from dotenv.main import load_dotenv from flask import url_for BASEDIR = os.path.abspath(os.path.dirname(__file__)) dotenv_path = os.path.join(BASEDIR, '.env') load_dotenv(dotenv_path) class Config(object): SECRET_KEY = os.environ.get("SECRET_KEY") or "ssshhhhh" SQLALCHEMY_DATABASE_URI = os.environ.get( "DATABASE_URI" ) or 'postgresql://[email protected]:5432/vital_records_printing' # suppress warning SQLALCHEMY_TRACK_MODIFICATIONS = True # location of certificate image directory from within static folder (default: "img/certificate") CERT_IMAGE_STATIC_DIRECTORY = os.environ.get('CERT_IMAGE_STATIC_DIRECTORY') or \ os.path.join("img", "certificate") # absolute path to mounted certificate image directory (".../app/static/img/certificate") CERT_IMAGE_DIRECTORY = os.path.join(BASEDIR, "app", "static", CERT_IMAGE_STATIC_DIRECTORY) class DevelopmentConfig(Config):
def read_env( path: _StrType = None, recurse: _BoolType = True, verbose: _BoolType = False, override: _BoolType = False, ) -> None: """Read a .env file into os.environ. If .env is not found in the directory from which this method is called, the default behavior is to recurse up the directory tree until a .env file is found. If you do not wish to recurse up the tree, you may pass False as a second positional argument. """ PATH_SEPARATOR_PATTERN = r"\\|\/" if path and os.path.isdir(str(path)): raise ValueError( "The specified path '{}' is a directory. Ensure that you pass a filename." .format(path)) original_path = path # use .env as path if path is None or empty if not path: path = ".env" # make path absolute if it is relative, using the directory of the calling script as base if not os.path.isabs(path): def merge_absolute_and_relative_path(absolute_path, relative_path): absolute_path_items = re.split(PATH_SEPARATOR_PATTERN, absolute_path) relative_path_items = re.split(PATH_SEPARATOR_PATTERN, relative_path) popped = False for absolute_path_item in absolute_path_items: if absolute_path_item == relative_path_items[0]: relative_path_items.pop(0) popped = True elif popped: break return os.path.sep.join(absolute_path_items + relative_path_items) current_frame = inspect.currentframe() if not current_frame: raise RuntimeError("Could not get current call frame.") frame = typing.cast(types.FrameType, current_frame.f_back) caller_dir = os.path.dirname(frame.f_code.co_filename) path = merge_absolute_and_relative_path(caller_dir, path) # walk up the directory tree starting in the path directory and try to find an environment file. # if recurse is False, stop after the first directory. def look_for_environment_file_in_parents(path): path_items = re.split(PATH_SEPARATOR_PATTERN, os.path.abspath(path)) basename = path_items[-1] parents = path_items[:-1] depth = len(parents) while depth >= 0: path_to_check = os.path.sep.join( parents[:depth]) + os.path.sep + basename if os.path.isfile(path_to_check): return path_to_check if recurse: depth -= 1 continue else: return None env_file_to_load = look_for_environment_file_in_parents(path) # load the env file if we found one if env_file_to_load: load_dotenv(env_file_to_load, verbose=verbose, override=override) else: raise ValueError( "Could not find environment file for path '{}', recurse = {}.". format(original_path, recurse))
# Displays information about a block returned by text detection and text analysis import cv2 from src.aws.helpers import process_text_analysis from dotenv.main import load_dotenv load_dotenv("../../.env") def main(): bucket = 'felix-ml-sagemaker' document = 'invoice2.png' img, block_count = process_text_analysis(bucket, document) print("Blocks detected: " + str(block_count)) cv2.imshow("img", img) cv2.waitKey(0) if __name__ == "__main__": main()
def endpoint_url(): load_dotenv() load_env_vars() endpoint_url = os.environ['KESHER_API_GW'] return endpoint_url[:-1]
import os from flask import Flask, request from flask_pymongo import PyMongo from dotenv.main import load_dotenv # IMPORTING FROM MY FUNCTIONS from components.model.model_predict import model_pred from components.core.database import save_users_images, get_user_data, get_user_image_id, get_user_id # MongoDB Details Are Saved In ENV File load_dotenv('/components/utils/.env') MONGO_DB_CREDENTIAL = os.getenv('MONGO_DB_CREDENTIAL') app = Flask(__name__) app.config['MONGO_URI'] = MONGO_DB_CREDENTIAL mongo = PyMongo(app) # SAVE THE USER INFORMATION TO MONGO_DB @app.route('/', methods=['POST']) def save_user_diabetic(): if request.method == 'GET': return 'Send Your Post Request Here' image = request.files['image'] user_name = request.values['user_name'] save_users_images(mongo, image, user_name) id = get_user_id(mongo, user_name) return 'User Registration Completed with Image Id {}'.format(id), 200
import os import fitz from dotenv.main import load_dotenv from progress.bar import FillingSquaresBar from pymongo import MongoClient load_dotenv('.env.local') dir = os.getenv('CLIENTS_DIR_PATH') tbn_dir = os.getenv('TBN_DIR') mongo_uri = os.getenv('MONGO_URI') client = MongoClient(mongo_uri) db = client.buffalographics def tbn_from_pdf(obj_path, ext='.jpg', cache_dirname='tbn'): pdf_path = os.path.join(dir, obj_path) tbn = obj_path.replace(' ', '_').replace('/', '-').replace('.pdf', ext).lower() cache_path = os.path.join( os.getcwd(), '__pycache__', cache_dirname, ) if os.path.isdir(cache_path) is False: os.mkdir(cache_path) cache_file = os.path.join(cache_path, tbn).lower() if os.path.isfile(cache_file) is False:
from os import environ from dotenv.main import load_dotenv # dotenv load_dotenv(verbose=True) def _fix_booleans(var): FALSIES = ['False', 'false', '0', 0, 'No', 'no'] TRUTHIES = ['True', 'true', '1', 1, 'Yes', 'yes'] if var in FALSIES: return False if var in TRUTHIES: return True else: return var def get_env_variable(var_name, default=None): """ Get the environment variable or return exception """ if default is not None: return _fix_booleans(environ.get(var_name, default)) try: return _fix_booleans(environ[var_name]) except KeyError: error_msg = "Please set the %s environment variable" % var_name raise KeyError(error_msg)
import os import discord from discord.ext import commands from dotenv.main import load_dotenv load_dotenv(override=True) import constants DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') def main(): intents = discord.Intents.default() intents.members = True client = commands.Bot(constants.BOT_PREFIX, intents=intents, help_command=None, case_insensitive=True) # Get the modules of all cogs whose directory structure is modules/<module_name>/cog.py for folder in os.listdir("modules"): if os.path.exists(os.path.join("modules", folder, "cog.py")): client.load_extension(f"modules.{folder}.cog") @client.event async def on_ready(): await client.change_presence( activity=discord.Activity(name="the House Cup", type=5)) for guild in client.guilds: print( f"{client.user.name} has connected to the following guild: {guild.name} (id: {guild.id})" )
def __init__(self): load_dotenv() user=os.getenv("INFLUX_DB_USER") password=os.getenv("INFLUX_DB_PASSWORD") self.client = InfluxDBClient('localhost', 8086, user, password, 'pi_influxdb')
import discord from discord.ext import commands import os import pymongo from datetime import datetime import dotenv from dotenv.main import load_dotenv """Loads environment variables from .env. Initializes TOKEN as bot token. """ load_dotenv() DISCORD_TOKEN = os.environ.get('TOKEN') bot = commands.Bot(command_prefix='.') @bot.event async def on_ready(): launch_time = datetime.now() print(f"Started at {launch_time}") # clear commands and its error handling @bot.command() @commands.has_permissions(manage_messages=True) async def clear(ctx, amount: int): await ctx.channel.purge(limit=amount)
from h1st_contrib.utils.log import STDOUT_HANDLER from h1st_contrib.utils import s3 from h1st_contrib.pred_maint.data_mgmt import (EquipmentParquetDataSet, EQUIPMENT_INSTANCE_ID_COL, DATE_COL) # noqa: E501 __all__ = ( 'H1ST_MODELS_S3_DIR_PATH', 'H1ST_BATCH_OUTPUT_S3_DIR_PATH', 'BaseFaultPredictor', ) load_dotenv(dotenv_path='.env', stream=None, verbose=True, override=False, interpolate=True, encoding='utf-8') _S3_BUCKET: Optional[str] = os.environ.get('H1ST_PMFP_S3_BUCKET') MODELS_S3_PREFIX: str = '.h1st/models' H1ST_MODELS_S3_DIR_PATH: str = f's3://{_S3_BUCKET}/{MODELS_S3_PREFIX}' BATCH_OUTPUT_S3_PREFIX: str = '.h1st/batch-output' H1ST_BATCH_OUTPUT_S3_DIR_PATH: str = f's3://{_S3_BUCKET}/{BATCH_OUTPUT_S3_PREFIX}' # noqa: E501 class BaseFaultPredictor(Model): # pylint: disable=too-many-ancestors """Base Fault Prediction model class."""