Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] DB config system #884

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ typings/
# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
# dotenv environment variables and user config file
.env

user.db.config.json

### Python ###
# Byte-compiled / optimized / DLL files
Expand Down
12 changes: 12 additions & 0 deletions ADD_TO_WIKI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
There is already `db.config.json` file at `/components/core` setting up default values for database connection. If you want to override any of the key, just make a `user.db.config.json` file in directory same as of `db.config.json` with keys you want to override. <br>

Template for `user.db.config.json` file:
```json
{
"BASSA_DB_NAME": "Bassa",
"BASSA_HOST": "localhost",
"BASSA_DB_USERNAME": "root",
"BASSA_DB_PASSWORD": ""
}
```

4 changes: 3 additions & 1 deletion components/core/DBCon.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import MySQLdb
from ConfReader import get_conf_reader
import os

_db=None
db_conf = get_conf_reader("db.config.json")

def get_db_con () :
global _db
if _db==None:
try:
_db=MySQLdb.connect("db", os.environ.get('BASSA_DB_USERNAME'), os.environ.get('BASSA_DB_PASSWORD'), os.environ.get('BASSA_DB_NAME'))
_db=MySQLdb.connect(host=db_conf["BASSA_HOST"], user=db_conf["BASSA_DB_USERNAME"], passwd=db_conf["BASSA_DB_PASSWORD"],db=db_conf["BASSA_DB_NAME"])
return _db
except:
return None
Expand Down
6 changes: 6 additions & 0 deletions components/core/db.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"BASSA_DB_NAME": "Bassa",
"BASSA_HOST": "localhost",
"BASSA_DB_USERNAME": "root",
"BASSA_DB_PASSWORD": ""
}
18 changes: 18 additions & 0 deletions components/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import shutil
import inspect
import platform
import json
from setuptools import setup
import setuptools
try:
Expand All @@ -11,6 +12,9 @@

EMAIL_CONF = 'email.conf'
DL_CONF = 'dl.conf'
DB_CONF = 'db.config.json'
USER_DB_CONF = 'user.db.config.json'

LINUX_CONFDIR = os.path.expanduser('~') + '/.config/bassa/'
WIN_CONFDIR = os.path.expanduser('~') + '/%app_data%/bassa/'
OSX_CONFDIR = os.path.expanduser('~') + '/.config/bassa/'
Expand Down Expand Up @@ -38,8 +42,22 @@ def read(file_name):

email_conf_location = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/" + EMAIL_CONF
dl_conf_location = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/" + DL_CONF
db_conf_location = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/" + DB_CONF
user_db_conf_location = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) + "/" + USER_DB_CONF
shutil.copyfile(email_conf_location, configdir + EMAIL_CONF)
shutil.copyfile(dl_conf_location, configdir + DL_CONF)
shutil.copyfile(db_conf_location, configdir + DB_CONF)
## merge db configs if user config exist
if os.path.exists(user_db_conf_location):
# userDbConf, defaultDbConf
with open(user_db_conf_location) as user_db_conf:
userDbConf = json.load(user_db_conf)
with open(configdir + DB_CONF) as default_db_conf:
defaultDbConf = json.load(default_db_conf)
for key in userDbConf:
defaultDbConf[key] = userDbConf[key]
with open(configdir + DB_CONF, 'w') as default_db_conf:
json.dump(defaultDbConf, default_db_conf, indent=4)

###/ Set configs ###

Expand Down