def _save_profile_settings(self): """Overwrite the settings file with the current values""" config = confparser() config.read_dict(self._config) with open_for_safe_write(self.settings) as ini: config.write(ini)
def _save_profile_settings(self): """Overwrite the settings file with the current values""" config = confparser() config.read_dict(self._config) # type checker can't handle @singledispatch... # noinspection PyTypeChecker with open_atomic(self.settings) as ini: config.write(ini)
def __init__(self, profiles_dir, name=FALLBACK_PROFILE, copy_profile=None, create_on_enoent=True): """ :param Path profiles_dir: :param str name: :param Profile copy_profile: :param bool create_on_enoent: Whether to create the profile folder if it does not already exist """ self.name = name self.folder = profiles_dir / name # type: Path if not self.folder.exists(): if create_on_enoent: # create the directory if it doesn't exist self.folder.mkdir() # since we're creating a new profile, check to see if # we're cloning an already existing one if copy_profile is not None: import shutil for fpath in (copy_profile.folder / fname for fname in ProfileFiles): #copy the other profile's files to our new profile dir shutil.copy(str(fpath), str(self.folder)) del shutil else: raise exceptions.ProfileDoesNotExistError(name) for f in [self.folder / p for p in ProfileFiles]: if not f.exists(): # if the file doesn't exist (perhaps because this is a new profile) # create empty placeholder for it f.touch() if f.name == SETTINGS: # put default vals in the ini file c = confparser() c.read_dict(self.__default_settings) with f.open('w') as ini: c.write(ini) # we don't worry about the json files, but we need to load in # the values from the ini file and store it. self._config = self.load_profile_settings()
def load_profile_settings(self): config = confparser() config.read(str(self.settings)) # for now, just turn the config parser into a dict and return it; # the checks below should deal with most of the conversions we'd need # to worry about sett={} for sec, sub in self.__default_settings.items(): sett[sec]={} for k,v in sub.items(): if isinstance(v, bool): # fallback to default vals if key or section is missing sett[sec][k] = config.getboolean(sec, k, fallback=v) elif isinstance(v, int): sett[sec][k] = config.getint(sec, k, fallback=v) elif isinstance(v, float): sett[sec][k] = config.getfloat(sec, k, fallback=v) else: # strings for everyone else sett[sec][k] = config.get(sec, k, fallback=v) return sett
def load_profile_settings(self): config = confparser() config.read(str(self.settings)) # for now, just turn the config parser into a dict and return it; # the checks below should deal with most of the conversions we'd need # to worry about sett = {} for sec, sub in self.__default_settings.items(): sett[sec] = {} for k, v in sub.items(): if isinstance(v, bool): # fallback to default vals if key or section is missing sett[sec][k] = config.getboolean(sec, k, fallback=v) elif isinstance(v, int): sett[sec][k] = config.getint(sec, k, fallback=v) elif isinstance(v, float): sett[sec][k] = config.getfloat(sec, k, fallback=v) else: # strings for everyone else sett[sec][k] = config.get(sec, k, fallback=v) return sett
def __init__(self, profiles_dir, name=FALLBACK_PROFILE, copy_profile=None, create_on_enoent=True): """ :param Path profiles_dir: :param str name: :param Profile copy_profile: :param bool create_on_enoent: Whether to create the profile folder if it does not already exist """ self.name = name self._overrides = {} # type: dict [str, dir_override] ##================================= ## Path checks/creation ##--------------------------------- # todo: pull 'profiles_dir' from ModManager's profile AppFolder dynamically (so that the profiles directory can be changed without having to update the 'folder' attribute of every Profile as well). self.folder = profiles_dir / name # type: Path if not self.folder.exists(): if create_on_enoent: # create the directory if it doesn't exist self.folder.mkdir() # since we're creating a new profile, check to see if # we're cloning an already existing one if copy_profile is not None: import shutil for fpath in (copy_profile.folder / fname for fname in ProfileFiles): #copy the other profile's files to our new profile dir shutil.copy(str(fpath), str(self.folder)) del shutil else: raise exceptions.ProfileDoesNotExistError(name) for f in [self.folder / p for p in ProfileFiles]: if not f.exists(): # if the file doesn't exist (perhaps because this is a new profile) # create empty placeholder for it f.touch() if f.name == SETTINGS: # put default vals in the ini file c = confparser() c.read_dict(self.__default_settings) with f.open('w') as ini: c.write(ini) ##================================= ## Load config ##--------------------------------- # we don't worry about the json files, but we need to load in # the values from the ini file and store it. self._config = self.load_profile_settings() # self.LOGGER << "Loaded profile-specific settings: {}".format(self.settings) ##================================= ## Extract overrides ##--------------------------------- for k in Profile.__default_settings[kstr_section.OVERRIDES]: self._overrides[k] = dir_override( self.get_setting(kstr_section.OVERRIDES, k), self.get_setting(kstr_section.OVR_ENABLED, k))