def test_warns_if_line_format_is_incorrect(self): with warnings.catch_warnings(record=True) as w: parse_dotenv('lol$wut') self.assertEqual(len(w), 1) self.assertTrue(w[0].category is SyntaxWarning) self.assertEqual(str(w[0].message), "Line 'lol$wut' doesn't match format")
def test_warns_if_line_format_is_incorrect(self): with warnings.catch_warnings(record=True) as w: parse_dotenv('lol$wut') self.assertEqual(len(w), 1) self.assertTrue(w[0].category is SyntaxWarning) self.assertEqual( str(w[0].message), "Line 'lol$wut' doesn't match format" )
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) try: load_dotenv('.env') app.config.update(parse_dotenv('.env')) except TypeError: print('Error parsing .env') app.config['MONGODB_SETTINGS'] = { 'db': app.config['MONGO_DBNAME'], 'host': app.config['MONGO_URI'] } config[config_name].init_app(app) bootstrap.init_app(app) mail.init_app(app) moment.init_app(app) db.init_app(app) login_manager.init_app(app) from main import main as main_blueprint app.register_blueprint(main_blueprint) from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') # attach routes and custom error pages here return app
def __init__(self, dotenv_path): if dotenv is None: raise ImportError( 'install configstore[dotenv] to use the dotenv backend') with open(dotenv_path) as file: content = file.read() self.config = dotenv.parse_dotenv(content)
def env_check(): parsed = parse_dotenv(env_file) env_var_dict = dict(parsed).items() env_var_list = list(parsed) for k, v in env_var_list: for local_k, local_v in env_var_list: if local_k == k: if local_v == v: sys.stdout.write('same value') else: sys.stdout.write('different value')
def sync_env(): # env_var_dict = dict(parsed).items() mem_file = StringIO() # todo add remote environment path get(remote_path, mem_file) remote_env = mem_file.getvalue() local_env = list(parse_dotenv(env_file)) for k, v in remote_env: for local_k, local_v in local_env: if local_k == k: if local_v == v: continue else: config("set", k, v)
def set_environment_variables(): logger = getLogger() try: import dotenv from path import Path with open(Path(__file__).dirname().abspath() / '.env') as dot_env: env = dotenv.parse_dotenv(dot_env.read()) # log any updates to the environment logger.warning('----> Overrides from .env') pad = max(len(_) for _ in os.environ) + 2 if env: for k, v in sorted(env.items()): v = os.environ.setdefault(k, v) logger.warning(' --> {k:·<{pad}} {v}'.format(pad=pad, k=k, v=v)) else: logger.warning(' --> no overrides from .env') except (ImportError, FileNotFoundError): pass os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') DATABASE_URL = os.environ.get('DATABASE_URL') ENVIRONMENT = os.environ.get('ENVIRONMENT') if DATABASE_URL and not os.environ.get('DATABASE_AWS_CHECK'): if ENVIRONMENT != 'PRODUCTION' and 'localhost' not in DATABASE_URL: confirm = input('say yes >>> ') if confirm != 'yes': exit(1) logger.critical(' USING PRODUCTION DATABASE '.center(80, '-')) logger.critical(DATABASE_URL) logger.critical(' USING PRODUCTION DATABASE '.center(80, '-'))
def test_expands_variables_found_in_values(self): env = parse_dotenv("FOO=test\nBAR=$FOO") self.assertEqual(env, {'FOO': 'test', 'BAR': 'test'})
def test_parses_escaped_double_quotes(self): env = parse_dotenv('FOO="escaped\"bar"') self.assertEqual(env, {'FOO': 'escaped"bar'})
def test_parses_double_quoted_values(self): env = parse_dotenv('FOO="bar"') self.assertEqual(env, {'FOO': 'bar'})
def test_allows_hash_in_quoted_values(self): env = parse_dotenv('foo="bar#baz" # comment ') self.assertEqual(env, {'foo': 'bar#baz'})
# Standard Library import os import sys # External Libraries from logging import getLogger if __name__ == '__main__': logger = getLogger() try: import dotenv with open('.env') as dot_env: env = dotenv.parse_dotenv(dot_env.read()) # log any updates to the environment logger.warn('----> Overrides from .env') if env: for k, v in sorted(env.items()): v = os.environ.setdefault(k, v) logger.warn(' --> %-20s - %s', k, v) else: logger.warn(' --> no overrides from .env') except (ImportError, FileNotFoundError): pass os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lindy.lindyfocus.settings')
def test_does_not_expand_escaped_variables(self): env = parse_dotenv('FOO="foo\\$BAR"') self.assertEqual(env, {'FOO': 'foo$BAR'}) env = parse_dotenv('FOO="foo\${BAR}"') self.assertEqual(env, {'FOO': 'foo${BAR}'})
def test_expands_undefined_variables_to_an_empty_string(self): self.assertEqual(parse_dotenv('BAR=$FOO'), {'BAR': ''})
def test_expands_variables_from_environ_if_not_found_in_local_env(self): os.environ.setdefault('FOO', 'test') env = parse_dotenv('BAR=$FOO') self.assertEqual(env, {'BAR': 'test'})
def test_expands_variables_wrapped_in_brackets(self): env = parse_dotenv("FOO=test\nBAR=${FOO}bar") self.assertEqual(env, {'FOO': 'test', 'BAR': 'testbar'})
def test_parses_empty_values(self): env = parse_dotenv('FOO=') self.assertEqual(env, {'FOO': ''})
def test_parses_escaped_double_quotes(self): env = parse_dotenv('FOO="escaped\\"bar"') self.assertEqual(env, {'FOO': 'escaped"bar'})
def test_parses_single_quoted_values(self): env = parse_dotenv("FOO='bar'") self.assertEqual(env, {'FOO': 'bar'})
def test_expands_variables_in_double_quoted_values(self): env = parse_dotenv("FOO=test\nBAR=\"quote $FOO\"") self.assertEqual(env, {'FOO': 'test', 'BAR': 'quote test'})
def test_parses_key_with_dot_in_the_name(self): env = parse_dotenv('FOO.BAR=foobar') self.assertEqual(env, {'FOO.BAR': 'foobar'})
def test_does_not_expand_variables_in_single_quoted_values(self): env = parse_dotenv("BAR='quote $FOO'") self.assertEqual(env, {'BAR': 'quote $FOO'})
def test_ignores_empty_lines(self): env = parse_dotenv("\n \t \nfoo=bar\n \nfizz=buzz") self.assertEqual(env, {'foo': 'bar', 'fizz': 'buzz'})
def test_parses_export_keyword(self): env = parse_dotenv('export FOO=bar') self.assertEqual(env, {'FOO': 'bar'})
def test_parses_values_with_spaces_around_equal_sign(self): env = parse_dotenv('FOO =bar') self.assertEqual(env, {'FOO': 'bar'}) env = parse_dotenv('FOO= bar') self.assertEqual(env, {'FOO': 'bar'})
def test_strips_unquoted_values(self): env = parse_dotenv('foo=bar ') self.assertEqual(env, {'foo': 'bar'}) # not 'bar '
def test_parses_hash_in_quoted_values(self): env = parse_dotenv('foo="ba#r"') self.assertEqual(env, {'foo': 'ba#r'}) env = parse_dotenv('foo="ba#r"') self.assertEqual(parse_dotenv("foo='ba#r'"), {'foo': 'ba#r'})
def test_parses_unquoted_values(self): env = parse_dotenv('FOO=bar') self.assertEqual(env, {'FOO': 'bar'})
def test_ignores_inline_comments(self): env = parse_dotenv('foo=bar # this is foo') self.assertEqual(env, {'foo': 'bar'})
def test_ignores_comment_lines(self): env = parse_dotenv("\n\n\n # HERE GOES FOO \nfoo=bar") self.assertEqual(env, {'foo': 'bar'})
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) try: load_dotenv('.env') app.config.update(parse_dotenv('.env')) except TypeError: print('Error parsing .env') app.config['SECRET_KEY'] app.config['SESSION_TYPE']= 'filesystem' app.config['MONGODB_SETTINGS'] = { 'db': app.config['MONGO_DBNAME'], 'host': app.config['MONGO_URI'] } #MAIL app.config.update( DEBUG=True, #EMAIL SETTINGS MAIL_SERVER= app.config['MAIL_SERVER'], MAIL_PORT = app.config['MAIL_PORT'], MAIL_USE_TLS = False, MAIL_USE_SSL = True, MAIL_USERNAME = app.config['MAIL_USERNAME'], MAIL_PASSWORD = app.config['MAIL_PASSWORD'] ) mail = Mail(app) config[config_name].init_app(app) bootstrap.init_app(app) # mail.init_app(app) moment.init_app(app) db.init_app(app) login_manager.init_app(app) Session(app) # additional _init_.py file in each of these modules from main import main as main_blueprint app.register_blueprint(main_blueprint) from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') from .browse import browse as browse_blueprint app.register_blueprint(browse_blueprint) from .literature import lit as lit_blueprint app.register_blueprint(lit_blueprint) from .preferences import pref as pref_blueprint app.register_blueprint(pref_blueprint) from .search import search as search_blueprint app.register_blueprint(search_blueprint) from .user import user as user_blueprint app.register_blueprint(user_blueprint) # attach routes and custom error pages here return app
def test_source_env_relative(self): fname = '~/.envrc.user' env = parse_dotenv(f'[[ -f {fname} ]] && source_env {fname}') self.assertEqual(env, {'source_env': [f'{os.path.expanduser(fname)}']})
def test_replaces_from_previous_value(self): env = parse_dotenv('foo=bar\nbaz=$foo') self.assertEqual(env, {'foo': 'bar', 'baz': 'bar'})
def test_source_env_absolute(self): fname = '.envrc.local' env = parse_dotenv(f'[[ -f {fname} ]] && source_env {fname}') self.assertEqual(env, {'source_env': ['.envrc.local']})