forked from wcmbishop/moniteur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableau_db_connection.py
70 lines (50 loc) · 2.01 KB
/
tableau_db_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# tableau_db_connection.py
# =================================================
# Establishes connections to the Tableau PostgreSQL
# database.
#
# Database parameters are defined in
# moniteur_settings.py.
#
# =================================================
# =================================================
# imports
import psycopg2
import psycopg2.extras
import moniteur_settings
import logging
# =================================================
# Error logging
logging.basicConfig(filename='moniteur.log', level=logging.DEBUG)
# =================================================
# TABLEAU CONNECTION DECORATOR
# -------------------------------------------------
def tableau_db(func):
"""
Wrap a function in an idiomatic SQL transaction for interaction
with the Tableau 'workgroup' database. The wrapped function
should take a cursor as its first argument; other arguments will be
preserved.
"""
def new_func(*args, **kwargs):
conn = psycopg2.connect(database=moniteur_settings.TABLEAU_DB["dbname"],
user=moniteur_settings.TABLEAU_DB["user"],
password=moniteur_settings.TABLEAU_DB["password"],
host=moniteur_settings.TABLEAU_DB["host"],
port=moniteur_settings.TABLEAU_DB["port"])
# Define the cursor that will be passed to the wrapped functions.
# The cursor used is the 'RealDictCursor', which returns lists
# of dictionaries, each dictionary containing a row of data.
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
retval = func(cursor, *args, **kwargs)
except:
logging.info('Error connecting to the Tableau Postgres workgroup database.')
raise
finally:
cursor.close()
return retval
# Tidy up the help()-visible docstrings to be nice
new_func.__name__ = func.__name__
new_func.__doc__ = func.__doc__
return new_func