Figs

A towel wrapped ConfigParser API
Download

Figs Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Shrikant Sharat
  • Publisher web site:
  • http://figs.sharats.me

Figs Tags


Figs Description

Figs is a Python library for reading ini like configuration files easily. Figs leverages the ConfigParser module from python's standard libraries.I personally don't like the ConfigParser API very much, so I wrote this. The idea is that the developer should have little overhead in thinking when using this library, i.e., an intuitive API.UsageIf you are familiar with PyYaml or the standard library's json modules, the following should be quite familiar to you.To load a configuration, use the load/loads functions. The following return the same:>>> # Takes a filename>>> conf = figs.load('config.ini')>>> # or a file-like object>>> conf = figs.load(open('config.ini'))>>> # Takes a string to be parsed>>> conf = figs.loads('''\ answer = 42 is_active = yes status = expanding ''')And, to dump configuration:>>> # Takes a filename>>> figs.dump(conf, 'config.ini')>>> # or a file-like object>>> figs.dump(conf, open('config.ini'))>>> # Dump to string>>> figs.dumps(conf)answer = 42is_active = yesstatus = expanding>>> # You can also dump just a section>>> figs.dumps(conf.universe)answer = 42is_active = yesstatus = expandingThose are the only functions in the figs module that you should be concerned with.Once you have the config object, how'd you use it? Surprise surprise! Anyway you feel comfortable.DictificationI know, you just want a dict of properties from the config file and be done with it. Lets see if you can guess how this can be done?:>>> # Returns a dict like {'section-name': < Section object >}>>> dict(conf)>>> # Returns a dict like {'key': < TypeableStr object >}>>> dict(conf.universe)You should keep in mind that dict on does not automatically do a dict on its Section objects. The TypeableStr class is a subclass of unicode with a few methods added (as_bool, as_int and as_float).If you want a dict of dicts, though, you can get that too.:>>> figs.as_dict(conf)>>> # or when loading>>> conf = figs.load('config.ini', as_dict=True)The loads method also takes the as_dict argument. Note that the as_dict has to be a keyword argument.Accesses>>> conf.universe.answeru'42'>>> conf.universe.answer.as_int42>>> conf.universe.is_activeu'yes'>>> conf.universe.is_active.as_boolTrue>>> conf.universe.statusu'expanding'>>> conf.universeu'expanding'Similary to as_int as shown above, there are also as_bool (boolean conversion done similar to how ConfigParser.getboolean does) and as_float.Check for presence>>> 'universe' in confTrue>>> 'multiverse' in confFalse>>> 'answer' in conf.universeTrue>>> 'is_active' in conf.universeTrue>>> 'is-active' in conf.universeFalseModifying configsSet new options...:>>> conf.universe.is_active = False>>> conf.universe.planet_maker = 'Magrathea'>>> conf.universe = 'mice'>>> figs.dumps(conf)answer = 42is_active = falsestatus = expandingplanet_maker = Magratheaearth-owners = mice...on new sections:>>> conf.multiverse.is_active = True>>> figs.dumps(conf)answer = 42is_active = falsestatus = expandingis_active = trueDeletingThe API is very boring isn't it?:>>> del conf.universe.answer>>> del conf.multiverseNow what?Well, if you have a life, get on with it. Seriously, there's nothing else to reading config files here.Product's homepage


Figs Related Software