def _load_mode(self, mode_string): """Loads a mode, reads in its config, and creates the Mode object. Args: mode: String name of the mode you're loading. This is the name of the mode's folder in your game's machine_files/modes folder. """ self.log.debug('Processing mode: %s', mode_string) config = dict() # find the folder for this mode: mode_path = os.path.join( self.machine.machine_path, self.machine.config['media_controller']['paths']['modes'], mode_string) if not os.path.exists(mode_path): mode_path = os.path.abspath( os.path.join( 'mpf', self.machine.config['media_controller']['paths']['modes'], mode_string)) # Is there an MPF default config for this mode? If so, load it first mpf_mode_config = os.path.join( 'mpf', self.machine.config['media_controller']['paths']['modes'], mode_string, 'config', mode_string + '.yaml') if os.path.isfile(mpf_mode_config): config = Config.load_config_file(mpf_mode_config) # Now figure out if there's a machine-specific config for this mode, and # if so, merge it into the config mode_config_folder = os.path.join( self.machine.machine_path, self.machine.config['media_controller']['paths']['modes'], mode_string, 'config') found_file = False for path, _, files in os.walk(mode_config_folder): for file in files: file_root, file_ext = os.path.splitext(file) if file_root == mode_string: config = Util.dict_merge( config, Config.load_config_file(os.path.join(path, file))) found_file = True break if found_file: break return Mode(self.machine, config, mode_string, mode_path)
def load_config_file(filename, verify_version=True, halt_on_error=True): config = FileManager.load(filename, verify_version, halt_on_error) if 'config' in config: path = os.path.split(filename)[0] for file in Util.string_to_list(config['config']): full_file = os.path.join(path, file) config = Util.dict_merge(config, Config.load_config_file(full_file)) return config
def _load_machine_config(self): for num, config_file in enumerate(self.options['configfile']): if not (config_file.startswith('/') or config_file.startswith('\\')): config_file = os.path.join(self.machine_path, self.config['mpf']['paths']['config'], config_file) self.log.info("Machine config file #%s: %s", num+1, config_file) self.config = Util.dict_merge(self.config, Config.load_config_file(config_file))
def _load_machine_config(self): for num, config_file in enumerate(self.options['configfile']): if not (config_file.startswith('/') or config_file.startswith('\\')): config_file = os.path.join(self.machine_path, self.config['media_controller']['paths']['config'], config_file) self.log.info("Machine config file #%s: %s", num+1, config_file) self.config = Util.dict_merge(self.config, Config.load_config_file(config_file))
def _load_mode(self, mode_string): """Loads a mode, reads in its config, and creates the Mode object. Args: mode: String name of the mode you're loading. This is the name of the mode's folder in your game's machine_files/modes folder. """ self.log.debug('Processing mode: %s', mode_string) config = dict() # find the folder for this mode: mode_path = os.path.join(self.machine.machine_path, self.machine.config['media_controller']['paths']['modes'], mode_string) if not os.path.exists(mode_path): mode_path = os.path.abspath(os.path.join('mpf', self.machine.config['media_controller']['paths']['modes'], mode_string)) # Is there an MPF default config for this mode? If so, load it first mpf_mode_config = os.path.join( 'mpf', self.machine.config['media_controller']['paths']['modes'], mode_string, 'config', mode_string + '.yaml') if os.path.isfile(mpf_mode_config): config = Config.load_config_file(mpf_mode_config) # Now figure out if there's a machine-specific config for this mode, and # if so, merge it into the config mode_config_folder = os.path.join(self.machine.machine_path, self.machine.config['media_controller']['paths']['modes'], mode_string, 'config') found_file = False for path, _, files in os.walk(mode_config_folder): for file in files: file_root, file_ext = os.path.splitext(file) if file_root == mode_string: config = Util.dict_merge(config, Config.load_config_file(os.path.join(path, file))) found_file = True break if found_file: break return Mode(self.machine, config, mode_string, mode_path)
def _get_merged_settings(self, section_name): # Returns a dict_merged dict of a config section from the machine-wide # config with the mode-specific config merged in. if section_name in self.machine.config: return_dict = copy.deepcopy(self.machine.config[section_name]) else: return_dict = CaseInsensitiveDict() if section_name in self.config: return_dict = Util.dict_merge(return_dict, self.config[section_name], combine_lists=False) return return_dict
def process_config(config_spec, source, target=None): config_spec = yaml.load(config_spec) processed_config = source for k in config_spec.keys(): if k in source: processed_config[k] = Config.validate_config_item( config_spec[k], source[k]) else: log.debug('Processing default settings for key "%s:"', k) processed_config[k] = Config.validate_config_item( config_spec[k]) if target: processed_config = Util.dict_merge(target, processed_config) return processed_config
def _load_mode(self, mode_string): """Loads a mode, reads in its config, and creates the Mode object. Args: mode: String name of the mode you're loading. This is the name of the mode's folder in your game's machine_files/modes folder. """ if self.debug: self.log.debug('Processing mode: %s', mode_string) config = dict() # find the folder for this mode: mode_path = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string) if not os.path.exists(mode_path): mode_path = os.path.abspath(os.path.join('mpf', self.machine.config['mpf']['paths']['modes'], mode_string)) # Is there an MPF default config for this mode? If so, load it first mpf_mode_config = os.path.join( 'mpf', self.machine.config['mpf']['paths']['modes'], mode_string, 'config', mode_string + '.yaml') if os.path.isfile(mpf_mode_config): config = Config.load_config_file(mpf_mode_config) # Now figure out if there's a machine-specific config for this mode, and # if so, merge it into the config mode_config_folder = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string, 'config') found_file = False for path, _, files in os.walk(mode_config_folder): for file in files: file_root, file_ext = os.path.splitext(file) if file_root == mode_string: config = Util.dict_merge(config, Config.load_config_file(os.path.join(path, file))) found_file = True break if found_file: break if 'code' in config['mode']: # need to figure out if this mode code is in the machine folder or # the default mpf folder mode_code_file = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string, 'code', config['mode']['code'].split('.')[0] + '.py') if os.path.isfile(mode_code_file): # code is in the machine folder import_str = (self.machine.config['mpf']['paths']['modes'] + '.' + mode_string + '.code.' + config['mode']['code'].split('.')[0]) i = __import__(import_str, fromlist=['']) if self.debug: self.log.debug("Loading Mode class code from %s", mode_code_file) mode_object = getattr(i, config['mode']['code'].split('.')[1])( self.machine, config, mode_string, mode_path) else: # code is in the mpf folder import_str = ('mpf.' + self.machine.config['mpf']['paths']['modes'] + '.' + mode_string + '.code.' + config['mode']['code'].split('.')[0]) i = __import__(import_str, fromlist=['']) if self.debug: self.log.debug("Loading Mode class code from %s", import_str) mode_object = getattr(i, config['mode']['code'].split('.')[1])( self.machine, config, mode_string, mode_path) else: # no code specified, so using the default Mode class if self.debug: self.log.debug("Loading default Mode class code") mode_object = Mode(self.machine, config, mode_string, mode_path) return mode_object
def process_config2(self, config_spec, source, section_name=None, target=None, result_type='dict'): # config_spec, str i.e. "device:shot" # source is dict # section_name is str used for logging failures if not section_name: section_name = config_spec validation_failure_info = (config_spec, section_name) orig_spec = config_spec config_spec = config_spec.split(':') this_spec = self.machine.config['config_validator'] for i in range(len(config_spec)): this_spec = this_spec[config_spec[i]] self.check_for_invalid_sections(this_spec, source, validation_failure_info) processed_config = source for k in this_spec.keys(): if k in source: # validate the entry that exists if type(this_spec[k]) is dict: # This means we're looking for a list of dicts final_list = list() if k in source: for i in source[k]: # individual step final_list.append(self.process_config2( orig_spec + ':' + k, source=i, section_name=k)) processed_config[k] = final_list elif result_type == 'list': # spec is dict # item is source processed_config = self.validate_config_item2( spec=this_spec[k], item=source[k], validation_failure_info=(validation_failure_info, k)) else: processed_config[k] = self.validate_config_item2( this_spec[k], item=source[k], validation_failure_info=(validation_failure_info, k)) else: # create the default entry if type(this_spec[k]) is dict: processed_config[k] = list() else: if result_type == 'list': processed_config = self.validate_config_item2( this_spec[k], validation_failure_info=(validation_failure_info, k)) else: processed_config[k] = self.validate_config_item2( this_spec[k], validation_failure_info=(validation_failure_info, k)) if target: processed_config = Util.dict_merge(target, processed_config) #if result_type == 'list': #quit() return processed_config
def get_merged_config(self): merged_config = self.config for key in self.child_files: merged_config = Util.dict_merge(merged_config, self.child_files[key].get_merged_config()) return merged_config
def process_config2(self, config_spec, source, section_name=None, target=None, result_type='dict'): # config_spec, str i.e. "device:shot" # source is dict # section_name is str used for logging failures if not section_name: section_name = config_spec validation_failure_info = (config_spec, section_name) orig_spec = config_spec config_spec = config_spec.split(':') this_spec = self.machine.config['config_validator'] for i in range(len(config_spec)): this_spec = this_spec[config_spec[i]] self.check_for_invalid_sections(this_spec, source, validation_failure_info) processed_config = source for k in this_spec.keys(): if k in source: # validate the entry that exists if type(this_spec[k]) is dict: # This means we're looking for a list of dicts final_list = list() if k in source: for i in source[k]: # individual step final_list.append( self.process_config2(orig_spec + ':' + k, source=i, section_name=k)) processed_config[k] = final_list elif result_type == 'list': # spec is dict # item is source processed_config = self.validate_config_item2( spec=this_spec[k], item=source[k], validation_failure_info=(validation_failure_info, k)) else: processed_config[k] = self.validate_config_item2( this_spec[k], item=source[k], validation_failure_info=(validation_failure_info, k)) else: # create the default entry if type(this_spec[k]) is dict: processed_config[k] = list() else: if result_type == 'list': processed_config = self.validate_config_item2( this_spec[k], validation_failure_info=(validation_failure_info, k)) else: processed_config[k] = self.validate_config_item2( this_spec[k], validation_failure_info=(validation_failure_info, k)) if target: processed_config = Util.dict_merge(target, processed_config) #if result_type == 'list': #quit() return processed_config
def _load_mode(self, mode_string): """Loads a mode, reads in its config, and creates the Mode object. Args: mode: String name of the mode you're loading. This is the name of the mode's folder in your game's machine_files/modes folder. """ if self.debug: self.log.debug('Processing mode: %s', mode_string) config = dict() # Find the folder for this mode. First check the machine folder/modes, # if that's not a valid folder, check the mpf/modes folder. mode_path = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string) if not os.path.exists(mode_path): mode_path = os.path.abspath(os.path.join('mpf', self.machine.config['mpf']['paths']['modes'], mode_string)) # Is there an MPF default config for this mode? If so, load it first mpf_mode_config = os.path.join( 'mpf', self.machine.config['mpf']['paths']['modes'], mode_string, 'config', mode_string + '.yaml') if os.path.isfile(mpf_mode_config): config = Config.load_config_file(mpf_mode_config) # Now figure out if there's a machine-specific config for this mode, # and if so, merge it into the config mode_config_folder = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string, 'config') found_file = False for path, _, files in os.walk(mode_config_folder): for file in files: file_root, file_ext = os.path.splitext(file) if file_root == mode_string: config = Util.dict_merge(config, Config.load_config_file(os.path.join(path, file))) found_file = True break if found_file: break # Figure out where the code is for this mode. # If a custom 'code' setting exists, first look in the machine folder # for it, and if it's not there, then look in mpf/modes for it. if 'code' in config['mode']: mode_code_file = os.path.join(self.machine.machine_path, self.machine.config['mpf']['paths']['modes'], mode_string, 'code', config['mode']['code'].split('.')[0] + '.py') if os.path.isfile(mode_code_file): # code is in the machine folder import_str = (self.machine.config['mpf']['paths']['modes'] + '.' + mode_string + '.code.' + config['mode']['code'].split('.')[0]) i = __import__(import_str, fromlist=['']) if self.debug: self.log.debug("Loading Mode class code from %s", mode_code_file) mode_object = getattr(i, config['mode']['code'].split('.')[1])( self.machine, config, mode_string, mode_path) else: # code is in the mpf folder import_str = ('mpf.' + self.machine.config['mpf']['paths']['modes'] + '.' + mode_string + '.code.' + config['mode']['code'].split('.')[0]) i = __import__(import_str, fromlist=['']) if self.debug: self.log.debug("Loading Mode class code from %s", import_str) mode_object = getattr(i, config['mode']['code'].split('.')[1])( self.machine, config, mode_string, mode_path) else: # no code specified, so using the default Mode class if self.debug: self.log.debug("Loading default Mode class code") mode_object = Mode(self.machine, config, mode_string, mode_path) return mode_object