[annuaires_pg] lazy conn à la base pg
Beaucoup de scripts (via le binding) importent ce fichier et ne s'en servent en pratique que très rarement, on effectue donc une connexion à la base pg uniquement lors de l'appel aux fonctions le nécessitant.
This commit is contained in:
parent
092f355074
commit
88ba72332a
1 changed files with 23 additions and 16 deletions
|
@ -2,27 +2,29 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from functools import wraps
|
||||||
|
|
||||||
try:
|
conn = None
|
||||||
if __name__ == 'annuaires_pg_test':
|
def _need_conn(f):
|
||||||
conn = psycopg2.connect(user='crans', database='switchs', host='localhost')
|
"""Décorateur à appliquer aux fonctions nécessitant une connexion pgsql"""
|
||||||
else:
|
@wraps(f)
|
||||||
conn = psycopg2.connect(user='crans', database='switchs', host='pgsql.adm.crans.org')
|
def first_connect(*args, **kwargs):
|
||||||
|
global conn
|
||||||
# Population de la tâble avec les bâtiments
|
if conn == None:
|
||||||
cur = conn.cursor()
|
if __name__ == 'annuaires_pg_test':
|
||||||
|
host='localhost'
|
||||||
cur.execute("SELECT DISTINCT batiment FROM prises")
|
else:
|
||||||
bat_switchs = [i[0] for i in cur.fetchall()]
|
host='pgsql.adm.crans.org'
|
||||||
cur.close()
|
# "connecting …"
|
||||||
del cur
|
conn = psycopg2.connect(user='crans', database='switchs', host=host)
|
||||||
except psycopg2.OperationalError:
|
return f(*args, **kwargs)
|
||||||
bat_switchs = ["a", "b", "c", "g", "h", "i", "j", "m", "o", "p"]
|
return first_connect
|
||||||
|
|
||||||
|
bat_switchs = ["a", "b", "c", "g", "h", "i", "j", "m", "o", "p"]
|
||||||
|
|
||||||
bat_manuels = []
|
bat_manuels = []
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def chbre_prises(batiment, chambre = None):
|
def chbre_prises(batiment, chambre = None):
|
||||||
"""Correspondance chambre -> prise"""
|
"""Correspondance chambre -> prise"""
|
||||||
batiment = batiment.lower()
|
batiment = batiment.lower()
|
||||||
|
@ -44,6 +46,7 @@ def chbre_prises(batiment, chambre = None):
|
||||||
raise ValueError("Batiment inexistant")
|
raise ValueError("Batiment inexistant")
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def chbre_commentaire(batiment, chambre):
|
def chbre_commentaire(batiment, chambre):
|
||||||
""" Renvoie le commentaire associé à la chambre """
|
""" Renvoie le commentaire associé à la chambre """
|
||||||
global conn
|
global conn
|
||||||
|
@ -55,6 +58,7 @@ def chbre_commentaire(batiment, chambre):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
raise ValueError("Chambre inexistante")
|
raise ValueError("Chambre inexistante")
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def reverse(batiment, prise = None):
|
def reverse(batiment, prise = None):
|
||||||
"""Correspondance prise -> chambre"""
|
"""Correspondance prise -> chambre"""
|
||||||
batiment = batiment.lower()
|
batiment = batiment.lower()
|
||||||
|
@ -79,6 +83,7 @@ def reverse(batiment, prise = None):
|
||||||
raise ValueError("Batiment inexistant")
|
raise ValueError("Batiment inexistant")
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def is_crans(batiment, chambre):
|
def is_crans(batiment, chambre):
|
||||||
"""Chambre cablee au Cr@ns ?"""
|
"""Chambre cablee au Cr@ns ?"""
|
||||||
batiment = batiment.lower()
|
batiment = batiment.lower()
|
||||||
|
@ -87,6 +92,7 @@ def is_crans(batiment, chambre):
|
||||||
cur.execute("SELECT crans FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre))
|
cur.execute("SELECT crans FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre))
|
||||||
return cur.fetchone()[0]
|
return cur.fetchone()[0]
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def is_connected(batiment, chambre):
|
def is_connected(batiment, chambre):
|
||||||
"""Cablage physique effectue ?"""
|
"""Cablage physique effectue ?"""
|
||||||
batiment = batiment.lower()
|
batiment = batiment.lower()
|
||||||
|
@ -95,6 +101,7 @@ def is_connected(batiment, chambre):
|
||||||
cur.execute("SELECT cablage_effectue FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre))
|
cur.execute("SELECT cablage_effectue FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre))
|
||||||
return cur.fetchone()[0]
|
return cur.fetchone()[0]
|
||||||
|
|
||||||
|
@_need_conn
|
||||||
def crous_to_crans(batiment, chambre):
|
def crous_to_crans(batiment, chambre):
|
||||||
"""Passage d'une chambre de CROUS a Cr@ns"""
|
"""Passage d'une chambre de CROUS a Cr@ns"""
|
||||||
batiment = batiment.lower()
|
batiment = batiment.lower()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue