简体   繁体   中英

Parsing Custom Config File

Hello we have some custom application that has a config file that looks like this:

application{
    rules{
        a{
             role admin
             permission write
         }
    }
    config{
        settings{
            file name.txt
            parameters{
                id 1234
                hash-size 4096
            }
        }
    }
}

I am just trying to figure out a way to parse through this and only get the config section so everything else isn't needed. I can't figure out how to even start parsing a file like this. Any help would be very much appreciated.

For "quick-and-dirty" solution you can transform the config file to something similar to XML and use BeautifulSoup to parse it. But if the syntax of config file is complex enough, I would use some other solution (eg PyParsing like in comments):

data = '''application{
    rules{
        a{
             role admin
             permission write
         }
    }
    config{
        settings{
            file name.txt
            parameters{
                id 1234
                hash-size 4096
            }
        }
    }
}'''

from bs4 import BeautifulSoup
import re

data = re.sub(r'([a-z]+)\{', r'<t data="\1">', data)
data = re.sub(r'\}', r'</t>', data)
data = re.sub(r'^[^\w]+([^\s]+)\s([a-z\d\-\.]+)\n', r'<parameter name="\1">\2</parameter>', data, flags=re.M)

soup = BeautifulSoup(data, 'lxml')

settings = soup.select_one('t[data="config"]').select_one('t[data="settings"]')
print('File: {}'.format(settings.parameter.text))
print('Parameters:')
for p in settings.select('t[data="parameters"] > parameter'):
    print('name={} value={}'.format(p['name'], p.text))

Prints:

File: name.txt
Parameters:
name=id value=1234
name=hash-size value=4096

Here is the way to construct a json string.

data = '''application{
    rules{
        a{
             role admin
             permission write
         }
    }
    config{
        settings{
            file name.txt
            parameters{
                id 1234
                hash-size 4096
            }
        }
    }
}'''


def to_dict(data):
    data_list = ['{']

    for line in data.splitlines():
        line = line.strip()

        if line.endswith('{'):
            data_list.append('"' + line.replace('{', '": {'))
        elif line == '}':
            data_list.append(line + ',')
        elif ' ' in line:
            tokens = line.split(' ')
            data_list.append('"%s": "%s",' % (tokens[0], tokens[1]))

    data_list.append('}')

    import json

    data_str = ''.join(data_list).replace('",}', '"}')

    while '},}' in data_str:
        data_str = data_str.replace('},}', '}}')

    return json.loads(data_str)


print(to_dict(data))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM