__init__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Deep Exploration Planner"""
  2. import os
  3. import logging
  4. from sqlalchemy import create_engine
  5. from sqlalchemy.orm import sessionmaker
  6. from dotenv import load_dotenv
  7. from apscheduler.schedulers.background import BackgroundScheduler
  8. from rival_regions_wrapper.rival_regions_wrapper import LocalAuthentication, RemoteAuthentication
  9. load_dotenv()
  10. # database
  11. ENGINE = create_engine(os.environ["DATABASE_URI"])
  12. SESSION = sessionmaker(bind=ENGINE)
  13. # scheduler
  14. SCHEDULER = BackgroundScheduler(
  15. daemon=True,
  16. job_defaults={'misfire_grace_time': 300},
  17. )
  18. SCHEDULER.start()
  19. # get logger
  20. LOGGER = logging.getLogger(__name__)
  21. LOGGER.setLevel(logging.INFO)
  22. SCHEDULER_LOGGER = logging.getLogger('apscheduler')
  23. SCHEDULER_LOGGER.setLevel(logging.DEBUG)
  24. # create file handler
  25. FILE_HANDLER = logging.FileHandler('output.log')
  26. FILE_HANDLER.setLevel(logging.DEBUG)
  27. # create console handler
  28. STREAM_HANDLER = logging.StreamHandler()
  29. STREAM_HANDLER.setLevel(logging.INFO)
  30. # create formatter and add it to the handlers
  31. FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  32. STREAM_HANDLER.setFormatter(FORMATTER)
  33. FILE_HANDLER.setFormatter(FORMATTER)
  34. # add the handlers to logger
  35. LOGGER.addHandler(STREAM_HANDLER)
  36. LOGGER.addHandler(FILE_HANDLER)
  37. SCHEDULER_LOGGER.addHandler(STREAM_HANDLER)
  38. SCHEDULER_LOGGER.addHandler(FILE_HANDLER)
  39. USERNAME = os.environ.get('username', None)
  40. PASSWORD = os.environ.get('password', None)
  41. LOGIN_METHOD = os.environ.get('login_method', None)
  42. class MissingEnvironError(Exception):
  43. """Error for missing environ"""
  44. if None in (USERNAME, PASSWORD, LOGIN_METHOD):
  45. raise MissingEnvironError(
  46. 'Load the following variables in your user environment:'
  47. 'username, password, login_method'
  48. )
  49. MIDDLEWARE = LocalAuthentication(USERNAME, PASSWORD, LOGIN_METHOD)
  50. # MIDDLEWARE = RemoteAuthentication(os.environ["API_URL"], os.environ["AUTHORIZATION"])