A corporation manager and dashboard for EVE Online
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

42 lignes
875 B

  1. # -*- coding: utf-8 -*-
  2. import sqlite3
  3. from flask import g
  4. from werkzeug.local import LocalProxy
  5. __all__ = ["Database"]
  6. class Database:
  7. path = None
  8. def __init__(self):
  9. if self.path is None:
  10. raise RuntimeError("Database.path not set")
  11. self._conn = sqlite3.connect(self.path)
  12. import traceback
  13. def __enter__(self):
  14. return self._conn.__enter__()
  15. def __exit__(self, exc_type, exc_value, trace):
  16. return self._conn.__exit__(exc_type, exc_value, trace)
  17. @classmethod
  18. def _get(cls):
  19. if not hasattr(g, "_db"):
  20. g._db = cls()
  21. return g._db
  22. @classmethod
  23. def pre_hook(cls):
  24. g.db = LocalProxy(cls._get)
  25. @classmethod
  26. def post_hook(cls, exc):
  27. if hasattr(g, "_db"):
  28. g._db.close()
  29. def close(self):
  30. return self._conn.close()