import configparser
import os
import sys
def resource_path(relative_path):
"""
Get absolute path to resource.
PyInstaller unpacks data into a temporary folder and stores the path in _MEIPASS.
Use this method to get the _MEIPASS directory in packed-mode and use the local directory in unpacked mode.
:param relative_path: The relative path to the configuration file (relative to the package folder)
"""
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath('.'), relative_path)
def read_config(config_file):
"""
Read values from a configuration file (declared in a basic configuration language).
:param config_file: The configuration file in the package root folder
:return: The configuration file content
"""
parser = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
try:
with open(config_file, encoding='UTF-8') as f:
parser.read_file(f)
except IOError:
print('Error: Could not read from config file: "{0}"'.format(config_file))
sys.exit(1)
return parser
Leave a Reply