Commit 44a31ef6 authored by mouadh's avatar mouadh

db config file

parent 19c0f590
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<!--<sgbd>postgres</sgbd>--> <!--<sgbd>postgres</sgbd>-->
<user_name>postgres</user_name> <user_name>postgres</user_name>
<passowrd>root</passowrd> <password>root</password>
<host>localhost</host> <host>localhost</host>
</database> </database>
......
import psycopg2 as pg import psycopg2 as pg
# postgres connection # postgres connection
from olapy.core.mdx.tools.olapy_config_file_parser import DbConfigParser from olapy_config_file_parser import DbConfigParser
class MyDB(object): class MyDB(object):
"""Connect to sql database (postgres only right now).""" """Connect to sql database (postgres only right now)."""
@staticmethod # @staticmethod
def get_db_credentials(): # def db_credentials():
db_config = DbConfigParser() # db_config = DbConfigParser()
if db_config.config_file_exist(): # if db_config.config_file_exist():
# many databases in the future maybe # # many databases in the future maybe
return db_config.get_db_credentials()[0] # return db_config.get_db_credentials()[0]
else: # else:
raise Exception('Missing database config file') # raise Exception('Missing database config file')
def __init__(self, def __init__(self,
username=get_db_credentials()['user_name'], username='postgres',
password=get_db_credentials()['passowrd'], password='root',
db=None, db=None,
host=get_db_credentials()['host']): host='localhost'
):
# TODO temporary
db_config = DbConfigParser()
db_credentials = db_config.get_db_credentials()[0]
username = db_credentials['user_name']
password = db_credentials['password']
host = db_credentials['host']
if db is None: if db is None:
self.connection = pg.connect("user={0} password={1} host='{2}'". self.connection = pg.connect("user={0} password={1} host='{2}'".
format(username, password, host)) format(username, password, host))
else: else:
......
...@@ -45,9 +45,10 @@ class DbConfigParser: ...@@ -45,9 +45,10 @@ class DbConfigParser:
try: try:
return [ return [
{'sgbd': db.find('sgbd').text, {
# 'sgbd': db.find('sgbd').text,
'user_name': db.find('user_name').text, 'user_name': db.find('user_name').text,
'passowrd': db.find('passowrd').text, 'password': db.find('password').text,
'host': db.find('host').text, 'host': db.find('host').text,
} }
for db in tree.xpath('/olapy/database') for db in tree.xpath('/olapy/database')
......
Markdown is supported
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