Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn crudini into a Python library #19

Open
falfaro opened this issue Oct 29, 2014 · 2 comments
Open

Turn crudini into a Python library #19

falfaro opened this issue Oct 29, 2014 · 2 comments

Comments

@falfaro
Copy link

falfaro commented Oct 29, 2014

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
Copy link
Owner

pixelb commented Oct 29, 2014

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
Copy link

larsks commented Oct 6, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants