def __persistence(self, secret): from pymongo import MongoClient from pydotenvs import load_env load_env() try: persist = {'host': environ['HOST'], 'port': environ['PORT'] if environ['PORT'] != '' else 27017, 'db': environ['DB_NAME'] if environ['DB_NAME'] != '' else 'polysecrets', 'collection': environ['secrets'] if environ['secrets'] != '' else 'secrets', 'user': environ['DB_USER'], 'pass': environ['DB_PASS'], 'authSource': environ['AUTH_SOURCE'] if environ['AUTH_SOURCE'] != '' else 'admin' } if 'mongod://' in persist['host']: client = MongoClient(persist['host']) else: client = MongoClient(host=persist['host'], port=persist['port'], username=persist['user'], password=persist['pass'], authSource=persist['authSource']) db = client[persist['db']] collection = db[persist['collection']] _prev_used_secret = collection.find_one({'secret': secret}) if _prev_used_secret is not None: return False collection.insert_one({'secret': secret, 'createdAt': datetime.now()}) return secret except KeyError as ke: print('Unable to parse .env file for MongoDB credentials. Error: ', ke, 'environment variable could not be found.') sys.exit(200)
from mongrations import Mongrations, Database from pydotenvs import load_env, load_env_object load_env( '.env-example') # by default it looks for .env in the current directory # connection_object = load_env_object() # connect via dictionary of environment variables [ i.e Mongrations(config) ] class Mongration(Database): def __init__(self): super(Database, self).__init__() def up(self): self.db['test_collection'].insert_one({'hello': 'world'}) def down(self): self.db['test_collection'].delete_one({'hello': 'world'}) # To use connection object (parameter): connection_obj = connection_object Mongrations(Mongration, 'sync')
import sys, os from sanic import Sanic from pydotenvs import load_env from pathlib import Path from .htmlparser import Parser from .emitter import EventEmitter, EventListener from .responses import html from .javascript.browser.index import WebBrowswer load_env() app = Sanic(__name__) app.static('/static', os.path.abspath(os.curdir + '/public')) webbrowser = WebBrowswer() emitter = EventEmitter() listener = EventListener() emitter.subscribe(listener.func, 'func') def _get_environment_variables_(): host = os.environ.get('PYREACT_HOST', '127.0.0.1') port = os.environ.get('PYREACT_PORT', 8000) env = os.environ.get('PYREACT_ENV', 'dev') return host, port, env @app.listener('after_server_start') async def _open_in_browser_(app, loop): host, port, _ = _get_environment_variables_() print('[PLACEHOLDER FOR AFTER_SERVER_START LISTENER]') # webbrowser.open(f'http://{host}:{port}')