Commit 19c0f590 authored by mouadh's avatar mouadh

db_config_file

parent 18470cf9
include *.rst *.txt *.ini *.py *.cfg *.yml include *.rst *.txt *.ini *.py *.cfg *.yml
recursive-include olapy * recursive-include olapy *
recursive-include cubes_templates * recursive-include cubes_templates *
recursive-include olapy/config *
prune tmp prune tmp
prune instance prune instance
prune .git prune .git
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<olapy>
<database>
<!--<sgbd>postgres</sgbd>-->
<user_name>postgres</user_name>
<passowrd>root</passowrd>
<host>localhost</host>
</database>
</olapy>
import psycopg2 as pg import psycopg2 as pg
# postgres connection # postgres connection
USERNAME = 'postgres' from olapy.core.mdx.tools.olapy_config_file_parser import DbConfigParser
PASSWORD = 'root'
HOST = 'localhost'
class MyDB(object): class MyDB(object):
"""Connect to sql database (postgres only right now).""" """Connect to sql database (postgres only right now)."""
@staticmethod
def get_db_credentials():
db_config = DbConfigParser()
if db_config.config_file_exist():
# many databases in the future maybe
return db_config.get_db_credentials()[0]
else:
raise Exception('Missing database config file')
def __init__(self, def __init__(self,
username=USERNAME, username=get_db_credentials()['user_name'],
password=PASSWORD, password=get_db_credentials()['passowrd'],
db=None, db=None,
host=HOST): host=get_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))
......
from __future__ import absolute_import, division, print_function
import os
from lxml import etree
class DbConfigParser:
# TODO one config file (I will try to merge dimensions between them in web part)
def __init__(self,
config_path = None,
file_name='olapy-config.xml'):
"""
:param cube_path: path to cube (csv folders)
:param file_name: config file name (DEFAULT = cubes-config.xml)
"""
if config_path is None:
from os.path import expanduser
home_directory = expanduser("~")
self.cube_path = os.path.join(home_directory, 'olapy-data')
else:
self.cube_path = config_path
self.file_name = file_name
def config_file_exist(self):
"""
Check whether the config file exists or not.
:return: True | False
"""
return os.path.isfile(os.path.join(self.cube_path, self.file_name))
def get_db_credentials(self):
"""
Get all db credentials in the config file.
:return: list of cube name as key and cube source as value (csv or postgres) (right now only postgres is supported)
"""
with open(os.path.join(self.cube_path, self.file_name)) as config_file:
parser = etree.XMLParser()
tree = etree.parse(config_file, parser)
try:
return [
{'sgbd': db.find('sgbd').text,
'user_name': db.find('user_name').text,
'passowrd': db.find('passowrd').text,
'host': db.find('host').text,
}
for db in tree.xpath('/olapy/database')
]
except:
raise ('missed name or source tags')
...@@ -4,6 +4,7 @@ from __future__ import absolute_import, division, print_function ...@@ -4,6 +4,7 @@ from __future__ import absolute_import, division, print_function
import os import os
import zipfile import zipfile
from os.path import expanduser from os.path import expanduser
from shutil import copyfile
from pip.download import PipSession from pip.download import PipSession
from pip.req import parse_requirements from pip.req import parse_requirements
...@@ -11,7 +12,7 @@ from setuptools import find_packages, setup ...@@ -11,7 +12,7 @@ from setuptools import find_packages, setup
RUNNING_TOX = 'RUNNING_TOX' in os.environ RUNNING_TOX = 'RUNNING_TOX' in os.environ
from setuptools.command.install import install # from setuptools.command.install import install
# class PostDevelopCommand(develop): # class PostDevelopCommand(develop):
...@@ -27,27 +28,27 @@ from setuptools.command.install import install ...@@ -27,27 +28,27 @@ from setuptools.command.install import install
# from manage import initdb # from manage import initdb
# initdb() # initdb()
# # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION # # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION
#
class PostInstallCommand(install): # class PostInstallCommand(install):
"""Post-installation for installation mode.""" # """Post-installation for installation mode."""
def run(self): # def run(self):
# PUT YOUR PRE-INSTALL SCRIPT HERE or CALL A FUNCTION # # PUT YOUR PRE-INSTALL SCRIPT HERE or CALL A FUNCTION
#
install.run(self) # install.run(self)
# initiate cubes examples # # initiate cubes examples
if RUNNING_TOX: # if RUNNING_TOX:
home_directory = os.environ.get('HOME_DIR') # home_directory = os.environ.get('HOME_DIR')
else: # else:
home_directory = expanduser("~") # home_directory = expanduser("~")
#
if not os.path.isdir(os.path.join(home_directory, 'olapy-data', 'cubes')): # if not os.path.isdir(os.path.join(home_directory, 'olapy-data', 'cubes')):
try: # try:
os.makedirs(os.path.join(home_directory, 'olapy-data', 'cubes')) # os.makedirs(os.path.join(home_directory, 'olapy-data', 'cubes'))
zip_ref = zipfile.ZipFile('cubes_templates/cubes_temp.zip', 'r') # zip_ref = zipfile.ZipFile('cubes_templates/cubes_temp.zip', 'r')
zip_ref.extractall(os.path.join(home_directory, 'olapy-data', 'cubes')) # zip_ref.extractall(os.path.join(home_directory, 'olapy-data', 'cubes'))
zip_ref.close() # zip_ref.close()
except: # except:
raise Exception('unable to create cubes directory !') # raise Exception('unable to create cubes directory !')
session = PipSession() session = PipSession()
...@@ -66,10 +67,10 @@ setup( ...@@ -66,10 +67,10 @@ setup(
long_description=open('README.rst').read(), long_description=open('README.rst').read(),
install_requires=install_requires, install_requires=install_requires,
include_package_data=False, include_package_data=False,
cmdclass={ # cmdclass={
# 'develop': PostDevelopCommand, # # 'develop': PostDevelopCommand,
'install': PostInstallCommand, # 'install': PostInstallCommand,
}, # },
classifiers=[ classifiers=[
"Programming Language :: Python", "Programming Language :: Python",
'Development Status :: 3 - Alpha', 'Development Status :: 3 - Alpha',
...@@ -82,4 +83,21 @@ setup( ...@@ -82,4 +83,21 @@ setup(
# # generate and set secret key # # generate and set secret key
# os.environ["SECRET_KEY"] # os.environ["SECRET_KEY"]
if RUNNING_TOX:
home_directory = os.environ.get('HOME_DIR')
else:
home_directory = expanduser("~")
if not os.path.isdir(os.path.join(home_directory, 'olapy-data', 'cubes')):
try:
os.makedirs(os.path.join(home_directory, 'olapy-data', 'cubes'))
zip_ref = zipfile.ZipFile('cubes_templates/cubes_temp.zip', 'r')
zip_ref.extractall(os.path.join(home_directory, 'olapy-data', 'cubes'))
zip_ref.close()
except:
raise Exception('unable to create cubes directory !')
if not os.path.isfile(os.path.join(home_directory, 'olapy-data','olapy-config.xml')):
copyfile('config/olapy-config.xml', os.path.join(home_directory, 'olapy-data','olapy-config.xml'))
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