Package: badger-config-handler
Badger_Config_HandlerA python library for handling code-declared and file-defined settings.Supports saving to JSON and YAML files.InstallationInstall fromPyPipipinstallbadger-config-handlerRules and limitationssettings are declared in the class and defined insetup()settingsMUSTbe declared with a type hint.  example:my_var: intsettings can only be ofallowed data typesettings not declared in code are ignored in the config file (and are removed on the next save, same for commented out settings)settings can beNoneif they are set to null in the config, regardles of the type hintsettings without a default value set insetup()are not saved to the config file, but they can still be set from the config fileTheroot_pathandparent_sectionpropertys are NOT available insetup()Example configfrompathlibimportPathfrombadger_config_handlerimportBadger_Config_Base,Badger_Config_SectionclassSub_Section(Badger_Config_Section):section_var:strsection_float:floatsection_path:Pathdefsetup(self):self.section_var="section"self.section_float=20.3self.section_path="relative/sub/path"defpost_process(self):self.section_path=self.make_absolute_to_root(relative_path=self.section_path,enforce_in_root=True)# becomes "path/to/project/root/relative/sub/path"defpre_process(self):self.section_path=self.make_relative_to_root(absolute_path=self.section_path)# turns back to "relative/sub/path"classbase(Badger_Config_Base):my_var:strmy_int:intmy_none:strsub_section:Sub_Section# NOT Badger_Config_Sectiondefsetup(self):self.my_var="test"self.my_int=50self.my_none=Noneself.sub_section=Sub_Section(section_name="sub")config=My_Base(config_file_path="path/to/config.json",root_path="path/to/project/root")config.save()config.load()config.sync()Allowed data typesnativethe file handlers have native support for these types and are used as is,
no conversion is done on these valuesstringintfloatboolNone / nullsupportedBadger_Config_Sectionconverted usingserialize:{VAR}.to_dict()de-serialize:Badger_Config_Section.from_dict({VAR})datetime.datetimeconverted usingserialize:{VAR}.isoformat()de-serialize:datetime.fromisoformat({VAR})pathlib.Pathconverted usingserialize:str({VAR})de-serialize:pathlib.Path({VAR})CollectionsNOTE:It is recommended to use aConfig Sectioninstead of Collections.If collections are used items should be ofnativetype only,
if they are not ofnativetype they are serialized but can NOT be de-serialize.Code using these values must handle these cases.dictlistConfig BaseProperty's_config_file_pathpath to the config fileALLOWED_FILE_TYPESall allowed file extensionsFunction'ssetup()seeConfig_Section.setup()save()Saves config to filesteps:pre_process()save to filepost_process()load()Load config from fileParameters:paramtyperequireddefaultNoteauto_createboolTrueCreate config file if not foundsafe_loadseeboolTrue! UNTESTED !sync()Sync config with fileruns:load()if file exists then continue else stop heresave()load()this adds new config fields to the file or removes old onesParameters:same asload()Config SectionProperty'ssection_namename of the current sectionroot_pathby default the project root path or overridden by the parent sectionparent_sectionreference to the parent section (if it exists)Function'ssetup()Replacement for__init__()should be used to set default valuesNOTE: the propertysroot_pathandparent_sectionare NOT available during thispre_process()Pre process values beforesave()Warning: the function should be written in a way that running it multiple times in a row doesn't cause problemsuseful for:converting unsupported data type to anativeorsupportedtypeconvertingabsolute paths to relative(keeps them short in the config file)post_process()post process values afterload()Warning: the function should be written in a way that running it multiple times in a row doesn't cause problemsuseful for:creating unsupported data type fromnativeorsupportedtypeconvertingrelative paths to absolute(keeps them short in the config file)make_absolute_to_root(Path, bool)Help function to make a setting Path absolute to the sectionsroot_pathPaths settings can escape the root_path using something like../../secrets.json.To avoid that useenforce_in_root.Parameters:paramtyperequireddefaultrelative_pathPathxenforce_in_rootboolTruemake_relative_to_root(Path)Help function to make a setting Path relative to the sectionsroot_pathParameters:paramtyperequireddefaultabsolute_pathPathxto_dict(bool)converts all values to a dictionaryParameters:paramtyperequireddefaultconvert_to_nativeboolTruefrom_dict()Reconstruct Config Section from dictionaryParameters:paramtypedescriptionrequireddefaultdatadict[str,native]the dict representation of this section (as generated fromto_dict(True))xsafe_loadbool! UNTESTED !True -> Only variables that already exist in the class are set (useshasattr)False -> New variables can be set from config fileTruedanger_convertbool! UNTESTED !For details see docs of_native_to_var()False