pixelb / crudini

A utility for manipulating ini files
GNU General Public License v2.0
439 stars 63 forks source link

Turn crudini into a Python library #19

Open falfaro opened 10 years ago

falfaro commented 10 years ago

openstack-config seems to rely in crudini for manipulating configuration files. I'm working on a Python project for automation and troubleshooting in OpenStack and, as part of that project, I need to retrieve information from OpenStack configuration files. I think it makes sense to use crudini, as that's what it's used for installation and management and mentioned in OpenStack documents.

Problem is that crudini is a command-line tool and not a Python library. In order to retrieve data from OpenStack configuration files, I could spawn a subprocess calling /usr/bin/crudini but this is a very limited and weak interface mechanism (build a command-line, spawn crudini, parse standard output and exit code). I would prefer being able to "link" crudini as a library and, instead, using a Python native interface. For example, by calling a function or instantiating a class.

At the moment, the only functionality from crudini that I need is being able to retrieve key/value pairs (e.g. from section DEFAULT, retrieve the value for the report_interval key in file /etc/neutron/neutron.conf).

pixelb commented 10 years ago

While I would be very happy to accept patches for that if appropriate, I must emphasize that crudini's focus is command line interaction. What I suspect you might want is improved functionality from iniparse? I.E. that project could be updated to better support multiline options for example (see issue #10)

To explore what a crudini library might provide, currently from a high level cruini currently does:

  if writing:
    lock file
  do operation
  if writing:
    unlock file

So I suppose the context returned from a library would handle the locking.

import crudini
ini = crudini.parse (filename, shared=False)
ini.get(...)
init.set(...)
del ini # to release locks etc.

Note the "shared" param would probably need to be explicitly specified as that would support multiple subsequent operations. I.E. currently crudini auto selects the "shared" locking operation depending on the operation selected.

larsks commented 9 years ago

Or better yet:

import crudini
with crudini.parse (filename, shared=False) as ini:
   ini.get(...)
   ini.set(...)

That is, implement a context manager if you want to manage lock acquisition/release.