Package: liteconfig
liteconfigLightweight and configurable .ini config parser with dot notation property access.Featuresdot notation:value = cfg.section.property;customizable parsing options;no dependencies inside, only plain Python;can handle text files, multiline strings or lists as input data;no singleton, you can use as much Config objects as you want;multiple encodings support, including Unicode;read/write config files.Default parsing options and their meaningdelimiter = '='delimiter between property and value is "=".before_delimiter = ' 'after_delimiter = ' 'strings which will precede and follow up delimiter character when exporting config to file.
For example, propertyiwith value of0will be exported asi = 0. And ifbefore_delimiterwill be set as:,
the same thing will be exported asi := 0.comment_markers = '#;'empty lines and lines beginning with "#" or ";" are ignored.parse_numbers = Truewill try to parse numeric values to int or float.parse_booleans = Truewill try to parse boolean values to bool.boolean_true = ('yes', 'true', 'on')case-insensitive tuple of string values, recognized as boolean "True".boolean_false = ('no', 'false', 'off')case-insensitive tuple of string values, recognized as boolean "False".encoding = 'utf-8'parser will try to read and write config files using this encoding.exceptions = FalseIf True, accessing nonexistent properties (or sections) of config will raiseAttributeError.
If False, nonexistent property will return None. Absent section will return special object Nothing, which can be tested against truth (and it will always return False). So you can use the construction likeifcfg.section.property:# do something with cfg.section.propertyelse:# handle nonexistencePublic methods of Config object__init__(input_data [, delimiter, comment_markers, parse_numbers, parse_booleans, boolean_true, boolean_false, encoding, exceptions])Instantiates Config object and parses input_data. Depending on type of input_data, instance will parse it as list, as multiline string or will interpret string as path to config file and read it.has_section(item)Return True or False depending on existence of config section.has_property(item [, section])Return True or False depending on existence of config property. Will search in all sections by default or in one concrete section if it is passed as second argument.write(file)Export config to file with the same settings as when object was instantiated.Error handlingAttempt to load nonexistent config file will raiseFileNotFoundError.Also may raisePermissionErrorif process does not have sufficient privileges to read or write file.If desired, access to nonexistent property (or section) will raiseAttributeError.Ifinput_datais not list nor string nor path to config file, will raiseValueError.Fail to decodeinput_datafile will result inUnicodeError.NotesWhen exporting config, boolean values willalwaysbe written likeTrueorFalse, regardless of initial readings (yes,no,on,offet cetera).Example===BEGIN config.ini===; liteconfig support very simple sectionless configs tooproperty=value[section]; this comment will be ignoredheads=tailstruth=lienokia=3310[misc]# this comment will be ignored tookill_all_humans=yespi=3.14159[юникод]文字=😉===END config.ini===Here we have: comments and empty lines, three sections and one "free" item. Some sections are named using ASCII symbols and one is Unicode. Also noticeheads=tailskey-value: it isn't necessary to have spaces before and after delimiter. Property and values can also be Unicode: cyrillic, hieroglyphs and emoji are welcome, thanks to Python3.Default behaviour is to try to represent all numbers as ints and floats, not strings. The same goes to boolean values: notice howyesbecameTrueforkill_all_humansproperty.importliteconfigcfg=liteconfig.Config('config.ini')print(cfg.property)# 'value'print(cfg.section.heads)# 'tails'print(cfg.юникод.文字)# '😉'print(cfg.section.nokia)# 3310print(type(cfg.section.nokia))# intprint(cfg.misc.kill_all_humans)# Trueprint(type(cfg.misc.kill_all_humans))# boolprint(cfg.misc.pi)# 3.14159print(type(cfg.misc.pi))# floatprint(cfg.nonexistent)# AttributeError exception or Noneprint(cfg.voidsection.nonexistent)# AttributeError exception or Nothing (boolean False)print(cfg.voidsection)# AttributeError exception or Nothing (boolean False)TO-DOParsers for various formats of hierarchical configs (with subsections).Support for versions of Python lesser than 3.6 (probably).History0.0.1: initial release.0.0.2: added option to raise exception or return None/False for absent properties or sections.0.0.3: minor fixes in documentation.0.1.0: added tests. Removed '1' and '0' from parsed booleans, as it conflicts with parsing numbers.0.1.1: minor fixes in documentation.0.1.2: code clean-up.1.0.0: bug-fixing. Tests re-written using pytest. Also, liteconfig learned how to keep comments when writing previously read config.