Commit 19c0f590 authored by mouadh's avatar mouadh

db_config_file

parent 18470cf9
include *.rst *.txt *.ini *.py *.cfg *.yml
recursive-include olapy *
recursive-include cubes_templates *
recursive-include olapy/config *
prune tmp
prune instance
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
# postgres connection
USERNAME = 'postgres'
PASSWORD = 'root'
HOST = 'localhost'
from olapy.core.mdx.tools.olapy_config_file_parser import DbConfigParser
class MyDB(object):
"""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,
username=USERNAME,
password=PASSWORD,
username=get_db_credentials()['user_name'],
password=get_db_credentials()['passowrd'],
db=None,
host=HOST):
host=get_db_credentials()['host']):
if db is None:
self.connection = pg.connect("user={0} password={1} host='{2}'".
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
import os
import zipfile
from os.path import expanduser
from shutil import copyfile
from pip.download import PipSession
from pip.req import parse_requirements
......@@ -11,7 +12,7 @@ from setuptools import find_packages, setup
RUNNING_TOX = 'RUNNING_TOX' in os.environ
from setuptools.command.install import install
# from setuptools.command.install import install
# class PostDevelopCommand(develop):
......@@ -27,27 +28,27 @@ from setuptools.command.install import install
# from manage import initdb
# initdb()
# # PUT YOUR POST-INSTALL SCRIPT HERE or CALL A FUNCTION
class PostInstallCommand(install):
"""Post-installation for installation mode."""
def run(self):
# PUT YOUR PRE-INSTALL SCRIPT HERE or CALL A FUNCTION
install.run(self)
# initiate cubes examples
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 !')
#
# class PostInstallCommand(install):
# """Post-installation for installation mode."""
# def run(self):
# # PUT YOUR PRE-INSTALL SCRIPT HERE or CALL A FUNCTION
#
# install.run(self)
# # initiate cubes examples
# 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 !')
session = PipSession()
......@@ -66,10 +67,10 @@ setup(
long_description=open('README.rst').read(),
install_requires=install_requires,
include_package_data=False,
cmdclass={
# 'develop': PostDevelopCommand,
'install': PostInstallCommand,
},
# cmdclass={
# # 'develop': PostDevelopCommand,
# 'install': PostInstallCommand,
# },
classifiers=[
"Programming Language :: Python",
'Development Status :: 3 - Alpha',
......@@ -82,4 +83,21 @@ setup(
# # generate and set 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