Commit 51f76a47 authored by Vladislav Rykov's avatar Vladislav Rykov
Browse files

database configuration added

parent 6220f3d4
{
"name" : "gateway",
"user" : "pi",
"password" : "dev",
"host" : "localhost",
"port" : 5432
}
from binascii import hexlify from binascii import hexlify
import os import os
import psycopg2
import json
def rand_str(length): def rand_str(length):
if length % 2 == 0: if length % 2 == 0:
...@@ -39,12 +41,18 @@ def prep_id_range(devlist): ...@@ -39,12 +41,18 @@ def prep_id_range(devlist):
s += str(r[-1])+']' s += str(r[-1])+']'
return s return s
import psycopg2
# decorator implementation # decorator implementation
def with_psql(f): def with_psql(f):
def _with_psql(*args, **kwargs): def _with_psql(*args, **kwargs):
conn = psycopg2.connect('dbname=gateway') db_conf = read_json_file('db.conf')
conn = psycopg2.connect(
database = db_conf['name'],
user = db_conf['user'],
password = db_conf['password'],
host = db_conf['host'],
port = db_conf['port']
)
cur = conn.cursor() cur = conn.cursor()
try: try:
...@@ -61,3 +69,12 @@ def with_psql(f): ...@@ -61,3 +69,12 @@ def with_psql(f):
return res return res
return _with_psql return _with_psql
def read_json_file(path):
json_dict = None
try:
with open(path) as json_file:
json_dict = json.load(json_file)
except Exception, e:
print("{} : {}".format(path, e))
return json_dict
No preview for this file type
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment