def _read_config(path, try_backup=False): """ Read the complete INI file and check its version number if OK, pass values to config-database """ global CFG, database, modified if try_backup or not os.path.exists(path): # Not found, try backup try: shutil.copyfile(path + '.bak', path) try_backup = True except IOError: pass if not os.path.exists(path): # No file found, create default INI file try: if not sabnzbd.WIN32: prev = os.umask(077) fp = open(path, "w") fp.write("__version__=%s\n[misc]\n[logging]\n" % CONFIG_VERSION) fp.close() if not sabnzbd.WIN32: os.umask(prev) except IOError: return False, 'Cannot create INI file %s' % path try: fp = open(path, 'rb') lines = fp.read().split('\n') if len(lines) == 1: fp.seek(0) lines = fp.read().split('\r') lines = [line.rstrip('\r\n') for line in lines] fp.close() try: # First try UTF-8 encoding CFG = configobj.ConfigObj(lines, default_encoding='utf-8', encoding='utf-8') except UnicodeDecodeError: # Failed, enable retry CFG = {} if not re.search(r'utf[ -]*8', CFG.get('__encoding__', ''), re.I): # INI file is still in 8bit ASCII encoding, so try Latin-1 instead CFG = configobj.ConfigObj(lines, default_encoding='cp1252', encoding='cp1252') except (IOError, configobj.ConfigObjError, UnicodeEncodeError), strerror: if try_backup: if isinstance(strerror, UnicodeEncodeError): strerror = 'Character encoding of the file is inconsistent' return False, '"%s" is not a valid configuration file<br>Error message: %s' % ( path, strerror) else: # Try backup file return _read_config(path, True)
def read_config(path): """ Read the complete INI file and check its version number if OK, pass values to config-database """ global CFG, database, modified if not os.path.exists(path): # No file found, create default INI file try: if not sabnzbd.WIN32: prev = os.umask(077) fp = open(path, "w") fp.write("__version__=%s\n[misc]\n[logging]\n" % CONFIG_VERSION) fp.close() if not sabnzbd.WIN32: os.umask(prev) except IOError: return False, 'Cannot create INI file %s' % path try: CFG = configobj.ConfigObj(path) try: if int(CFG['__version__']) > int(CONFIG_VERSION): return False, "Incorrect version number %s in %s" % ( CFG['__version__'], path) except (KeyError, ValueError): CFG['__version__'] = CONFIG_VERSION except configobj.ConfigObjError, strerror: return False, '"%s" is not a valid configuration file<br>Error message: %s' % ( path, strerror)
#!/usr/bin/env python2 import sys sys.path.append('/opt/sabnzbd') from os import path from sabnzbd import config from sabnzbd.utils import configobj print("Reading sabnzbd config file") config.read_config('/opt/sabnzbd-data/sabnzbd.ini') print("Reading sabnzbd_delta config file") configDelta = configobj.ConfigObj('/tmp/config/sabnzbd_delta.ini') print("Merging the sabnzbd configs") config.CFG.merge(configDelta) print("Writing resulting sabnzbd config file") with open('/opt/sabnzbd-data/sabnzbd.ini', 'w') as configfile: config.CFG.write(configfile) if path.exists('/opt/sabnzbd-data/nzbtomedia/autoProcessMedia.cfg'): print("Reading autoProcessMedia config file from existing file") autoProcessMediaPath='/opt/sabnzbd-data/nzbtomedia/autoProcessMedia.cfg' else: print("Reading autoProcessMedia config file from default file") autoProcessMediaPath='/opt/nzbToMedia/autoProcessMedia.cfg.spec' config = configobj.ConfigObj(autoProcessMediaPath)