Exports config in a new file 'config.py'

This commit is contained in:
Fred Z 2018-07-27 22:27:42 +02:00
parent 1c868d21c0
commit cff82380b8
3 changed files with 37 additions and 24 deletions

28
config.py Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: freezed <freezed@users.noreply.github.com> 2018-07-27
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [ocp5](https://github.com/freezed/ocp5) project
"""
# DATABASE
DB_CONFIG = {
'host': 'localhost',
'user': 'loff',
'pass': 'loff',
'db': 'loff',
'char': 'utf8',
'file': 'create-db-loff.sql'
}
# API
FIELD_KEPT = [
'product_name',
'stores',
'nutrition_grades',
'categories_tags'
]

24
db.py
View File

@ -10,21 +10,13 @@ Connect to DB
"""
import pymysql.cursors
from config import DB_CONFIG
# CONFIG
CONFIG = {
'host': 'localhost',
'user': 'loff',
'pass': 'loff',
'db': 'loff',
'char': 'utf8',
'file': 'create-db-loff.sql'
}
DB_NOT_FOUND = True
# FUNCTION
def sql_create_db(filename=CONFIG['file']):
def sql_create_db(filename=DB_CONFIG['file']):
"""
Get the SQL instruction to create the DB for file
@ -53,10 +45,10 @@ def sql_create_db(filename=CONFIG['file']):
# WORK
# Connect to the database
CONNECTION = pymysql.connect(host=CONFIG['host'],
user=CONFIG['user'],
password=CONFIG['pass'],
charset=CONFIG['char'],
CONNECTION = pymysql.connect(host=DB_CONFIG['host'],
user=DB_CONFIG['user'],
password=DB_CONFIG['pass'],
charset=DB_CONFIG['char'],
cursorclass=pymysql.cursors.DictCursor)
try:
@ -67,9 +59,9 @@ try:
for idx, val in enumerate(db_list):
if CONFIG['db'] in db_list[idx].values():
if DB_CONFIG['db'] in db_list[idx].values():
DB_NOT_FOUND = False
cursor.execute("USE {}".format(CONFIG['db']))
cursor.execute("USE {}".format(DB_CONFIG['db']))
print('DB exist : ready to use it.')
# No DB, create it

View File

@ -12,14 +12,7 @@ nurition grade.
"""
import json
import requests
# CONFIG
FIELD_KEPT = [
'product_name',
'stores',
'nutrition_grades',
'categories_tags'
]
from config import FIELD_KEPT
def get_product(code):