2

I'm using ConfigParser for a project with a configuration file that is getting to large. I plan to split it but maintain a central config file that points to the others.

I haven't seen this in the documentation, but can ConfigParser handle a hierarchical configuration file structure? Can I somehow point it from one config file to another automatically?

Of course I could do it manually or better yet, create a module that handles this using ConfigParser as a low-level tool, but I'm sure I'm not the first to tackle thisproblem - do you know of a different package that handles this? or perhaps a different approach altogether?

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

2 Answers2

2

You can use a Python module as configuration file, say config.py so you just have to import it :

import config
dugres
  • 12,613
  • 8
  • 46
  • 51
  • I've posted a [follow up question](http://stackoverflow.com/questions/8067651/configparser-vs-import-config) – Jonathan Livni Nov 09 '11 at 15:53
  • 1
    This is only a good idea *if you can completely trust your users*. Otherwise, what's to stop them from calling arbitrary functions from within the config.py file? Functions such as `unlink`. – Nathan Nov 09 '11 at 15:56
1

XML has XInclude support. If you use lxml when parsing an XML file, it will go off and find the configuration files that you've included through <xinclude> statements

Nathan
  • 4,777
  • 1
  • 28
  • 35
  • That's exactly the functionality I'm looking for, however I find `XML` to be tedious. Maybe `json` would be a better fit. – Jonathan Livni Nov 11 '11 at 10:46
  • I agree XML is tedious. Fortunately, lxml supports various features that make it *less* so. The two main ones are `xpath` and `ElementMaker`. xpath let's you use search strings, pretty much like directory names, to find elements in the XML tree. That makes reading in a file *much* simpler. `ElementMaker` simplifies the creation of XML. Create `E`, an instance of `ElementMaker`, and you can create new XML elements simply. For example, create `100` as `node = E.val(100)`. or `yup` as `E.my_node('yup')`. – Nathan Nov 11 '11 at 14:32
  • `lxml` is great and I use it every time I parse `XML` files, however my users will be updating the config frequently using a text editor, and I was thinking of them... – Jonathan Livni Nov 12 '11 at 14:44
  • Yeah, xml, whilst "readable", isn't really all that readable. – Nathan Nov 12 '11 at 18:29