2015-08-02 11:59:11 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
2016-04-15 09:23:00 -07:00
|
|
|
import sys
|
2015-08-02 11:59:11 -07:00
|
|
|
from configobj import ConfigObj
|
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
CONFIG_FILE = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__))+os.sep+".."+os.sep), "config.ini")
|
2015-08-02 11:59:11 -07:00
|
|
|
CFG = ConfigObj(CONFIG_FILE)
|
2016-12-23 00:53:39 -08:00
|
|
|
CFG.encoding = 'UTF-8'
|
|
|
|
|
2015-08-02 11:59:11 -07:00
|
|
|
|
|
|
|
def CheckSection(sec):
|
|
|
|
""" Check if INI section exists, if not create it """
|
|
|
|
try:
|
|
|
|
CFG[sec]
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
CFG[sec] = {}
|
|
|
|
return False
|
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
|
2015-08-02 11:59:11 -07:00
|
|
|
def check_setting_str(config, cfg_name, item_name, def_val, log=True):
|
|
|
|
try:
|
2016-08-02 22:35:58 -07:00
|
|
|
my_val = config[cfg_name][item_name].decode('UTF-8')
|
|
|
|
if my_val == u"":
|
2016-04-15 09:23:00 -07:00
|
|
|
my_val = def_val
|
|
|
|
config[cfg_name][item_name] = my_val
|
2015-08-02 11:59:11 -07:00
|
|
|
except:
|
|
|
|
my_val = def_val
|
|
|
|
try:
|
|
|
|
config[cfg_name][item_name] = my_val
|
|
|
|
except:
|
|
|
|
config[cfg_name] = {}
|
|
|
|
config[cfg_name][item_name] = my_val
|
|
|
|
return my_val
|
|
|
|
|
|
|
|
|
|
|
|
def check_setting_int(config, cfg_name, item_name, def_val):
|
|
|
|
try:
|
|
|
|
my_val = int(config[cfg_name][item_name])
|
|
|
|
except:
|
|
|
|
my_val = def_val
|
|
|
|
try:
|
|
|
|
config[cfg_name][item_name] = my_val
|
|
|
|
except:
|
|
|
|
config[cfg_name] = {}
|
|
|
|
config[cfg_name][item_name] = my_val
|
|
|
|
return my_val
|
|
|
|
|
|
|
|
CheckSection('General')
|
2016-04-15 09:23:00 -07:00
|
|
|
DB_ROOT = check_setting_str(CFG, 'General', 'DB_ROOT', "")
|
2015-08-03 16:44:05 -07:00
|
|
|
APP_DB_ROOT = check_setting_str(CFG, 'General', 'APP_DB_ROOT', os.getcwd())
|
2015-08-02 11:59:11 -07:00
|
|
|
MAIN_DIR = check_setting_str(CFG, 'General', 'MAIN_DIR', os.getcwd())
|
2016-04-15 09:23:00 -07:00
|
|
|
LOG_DIR = check_setting_str(CFG, 'General', 'LOG_DIR', os.getcwd())
|
2015-08-02 11:59:11 -07:00
|
|
|
PORT = check_setting_int(CFG, 'General', 'PORT', 8083)
|
|
|
|
NEWEST_BOOKS = check_setting_str(CFG, 'General', 'NEWEST_BOOKS', 60)
|
2015-08-02 12:23:24 -07:00
|
|
|
RANDOM_BOOKS = check_setting_int(CFG, 'General', 'RANDOM_BOOKS', 4)
|
2015-08-02 11:59:11 -07:00
|
|
|
|
|
|
|
CheckSection('Advanced')
|
2015-10-11 15:34:27 -07:00
|
|
|
TITLE_REGEX = check_setting_str(CFG, 'Advanced', 'TITLE_REGEX', '^(A|The|An|Der|Die|Das|Den|Ein|Eine|Einen|Dem|Des|Einem|Eines)\s+')
|
2015-10-11 15:41:41 -07:00
|
|
|
DEVELOPMENT = bool(check_setting_int(CFG, 'Advanced', 'DEVELOPMENT', 0))
|
2015-10-12 17:30:55 -07:00
|
|
|
PUBLIC_REG = bool(check_setting_int(CFG, 'Advanced', 'PUBLIC_REG', 0))
|
2016-04-15 10:39:25 -07:00
|
|
|
UPLOADING = bool(check_setting_int(CFG, 'Advanced', 'UPLOADING', 0))
|
2016-04-29 13:30:33 -07:00
|
|
|
ANON_BROWSE = bool(check_setting_int(CFG, 'Advanced', 'ANON_BROWSE', 0))
|
2015-08-02 11:59:11 -07:00
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
SYS_ENCODING = "UTF-8"
|
2015-08-02 11:59:11 -07:00
|
|
|
|
2016-04-15 09:23:00 -07:00
|
|
|
if DB_ROOT == "":
|
|
|
|
print "Calibre database directory (DB_ROOT) is not configured"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-12-23 00:53:39 -08:00
|
|
|
configval = {"DB_ROOT": DB_ROOT, "APP_DB_ROOT": APP_DB_ROOT, "MAIN_DIR": MAIN_DIR, "LOG_DIR": LOG_DIR, "PORT": PORT,
|
|
|
|
"NEWEST_BOOKS": NEWEST_BOOKS, "DEVELOPMENT": DEVELOPMENT, "TITLE_REGEX": TITLE_REGEX,
|
|
|
|
"PUBLIC_REG": PUBLIC_REG, "UPLOADING": UPLOADING, "ANON_BROWSE": ANON_BROWSE}
|
|
|
|
|
2015-08-02 11:59:11 -07:00
|
|
|
|
|
|
|
def save_config(configval):
|
2016-08-02 22:35:58 -07:00
|
|
|
new_config = ConfigObj(encoding='UTF-8')
|
2015-08-02 11:59:11 -07:00
|
|
|
new_config.filename = CONFIG_FILE
|
|
|
|
new_config['General'] = {}
|
|
|
|
new_config['General']['DB_ROOT'] = configval["DB_ROOT"]
|
2015-08-03 16:44:05 -07:00
|
|
|
new_config['General']['APP_DB_ROOT'] = configval["APP_DB_ROOT"]
|
2015-08-02 11:59:11 -07:00
|
|
|
new_config['General']['MAIN_DIR'] = configval["MAIN_DIR"]
|
2016-04-15 09:23:00 -07:00
|
|
|
new_config['General']['LOG_DIR'] = configval["LOG_DIR"]
|
2015-08-02 11:59:11 -07:00
|
|
|
new_config['General']['PORT'] = configval["PORT"]
|
|
|
|
new_config['General']['NEWEST_BOOKS'] = configval["NEWEST_BOOKS"]
|
|
|
|
new_config['Advanced'] = {}
|
|
|
|
new_config['Advanced']['TITLE_REGEX'] = configval["TITLE_REGEX"]
|
|
|
|
new_config['Advanced']['DEVELOPMENT'] = int(configval["DEVELOPMENT"])
|
2015-10-12 17:30:55 -07:00
|
|
|
new_config['Advanced']['PUBLIC_REG'] = int(configval["PUBLIC_REG"])
|
2016-04-15 10:39:25 -07:00
|
|
|
new_config['Advanced']['UPLOADING'] = int(configval["UPLOADING"])
|
2016-04-29 13:30:33 -07:00
|
|
|
new_config['Advanced']['ANON_BROWSE'] = int(configval["ANON_BROWSE"])
|
2015-08-02 11:59:11 -07:00
|
|
|
new_config.write()
|
|
|
|
return "Saved"
|
|
|
|
|
|
|
|
save_config(configval)
|