user929404
user929404

Reputation: 2243

Parse Python Configuration File for Java Programs

I have not found anything of this sort on Google and would like to know if there is a quicker way of doing the following:

I need to parse build scripts for Java programs which are written in Python. More specifically, I want to parse the dictionaries which are hard-coded into these build scripts.

For example, these scripts contain entries like:

config = {}

config["Project"] = \
    {
        "Name"                          : "ProjName",
        "Version"                       : "v2",
        "MinimumPreviousVersion"        : "v1",
    }   

def actualCode ():
# Some code that actually compiles the relevant files

(The actual compiling is done via a call to another program, this script just sets the required options which I want to extract). For example, I want to extract, "Name"="ProjName" and so on.

I am aware of the ConfigParser library which is part of Python, but that was designed for .ini files and hence has problems (throws exception and crashes) with actual python code which may appear in the build scripts which I am talking about. So using this library would mean that I would first have to read the file in and remove lines of the file which ConfigParser would object to.

Is there a quicker way than reading the config file in as a normal file and parsing it? I am looking for libraries which can do this. I don't mind too much which languages this libraries is in.

Upvotes: 3

Views: 2240

Answers (3)

PawanMahalle
PawanMahalle

Reputation: 36

I was trying to solve the similar problem. I converted the directory into a JSON object so that I can query keys using JSON object in simplest way possible. This solution worked for multi-level key values pairs for me. I

Here is the algorithm.

  1. Locate the config["key_name"] using a regular expression from string or file. Use the following regular expression

    config(.*?)\\[(.*?)\\]

  2. Get the data within curly brackets into a string. Use some stack based code since there could be nested brackets of type {} or [] in complex directories.
  3. Replace the circular bracket, if any, "()" with square brackets "[]" and backslash "\" with blank character " " as follows

      expression.replace('(', '[')
      .replace(')', ']')
      .replace('\\', ' ')
    
  4. JSONObject json = (JSONObject) parser.parse(expression)

Here is your JSON object. You can use it the way you want.

Upvotes: 2

hmartos
hmartos

Reputation: 919

I know this is an old question, but I have found an incredibly useful config parser library for Java here.

It provides a simple function getValue("sectionName", "optionName") that allows you to get the value of an option inside a section.

[sectionName] optionName = optionValue

Upvotes: 0

fge
fge

Reputation: 121712

Try Parboiled. It is written in Java and you write your grammars in... Java too.

Use the stack to store elements etc; its parser class is generic and you can get the final result out of it.

Upvotes: 0

Related Questions