from configparser import RawConfigParser from os.path import splitext, basename, dirname, abspath class Configuration: """ Configuration manager that read the configuration from multiples nfo files that are commons for all interfaces The configuration will be read and overridden in the following order: NFO.txt -> nfo.txt -> directory_name.txt -> video_file.txt -> video_name.txt (value in the rightmost file override values in preceding files) Your interface may add to this list from either side. Refer to the plugin's description. A plugin can also override completely this configuration by using other means. For example the cli plugins takes command line arguments with more importance than any nfo file. Attributes: CONFIG_FILE Filename for the base configuration file that should be used to set global behavior for prismedia and its plugins """ CONFIG_FILE = "prismedia.config" # TODO: replace with "config.txt"? something else? def __init__(self): self.root_path = dirname(abspath(__file__)) self.config_parser = RawConfigParser() self.configuration_file_list = [] self.base_configuration_file = self.root_path + "/config/" + self.CONFIG_FILE self.config_parser.read(self.base_configuration_file) def read_commons_nfo(self, video_path, video_name): video_directory = dirname(video_path) directory_name = basename(video_directory) video_file = splitext(basename(video_path))[0] self.configuration_file_list.append(video_directory + "/" + "NFO.txt") self.configuration_file_list.append(video_directory + "/" + "nfo.txt") self.configuration_file_list.append(video_directory + "/" + directory_name + ".txt") self.configuration_file_list.append(video_directory + "/" + video_file + ".txt") if video_name and video_name != video_file: self.configuration_file_list.append(video_directory + "/" + video_name + ".txt") self.config_parser.read(self.configuration_file_list) # Do not use this in actual production ready code. Prismedia should not write any file # This can be used in local to see how a plugin's variable needs to be written in the global NFO # I am afraid that trying to save a plugin info will also save the current config for the video def _write_config(self): """ Write the content of the ConfigParser in a file. """ # Do not use `assert` since we want to do the opposite and optimize the other way around if not __debug__: raise AssertionError("The method `Configuration._write_config` should not be called in production") with open(self.base_configuration_file + "_generated", "w") as configFile: self.config_parser.write(configFile) configuration_instance = Configuration()