4

I'm trying to read a java multiline i18n properties file. Having lines like:

messages.welcome=Hello\
 World!
messages.bye=bye

Using this code:

import configobj
properties = configobj.ConfigObj(propertyFileName)

But with multilines values it fails.

Any suggestions?

quamrana
  • 37,849
  • 12
  • 53
  • 71
ghm1014
  • 675
  • 6
  • 12

5 Answers5

6

According to the ConfigObj documentation, configobj requires you to surround multiline values in triple quotes:

Values that contain line breaks (multi-line values) can be surrounded by triple quotes. These can also be used if a value contains both types of quotes. List members cannot be surrounded by triple quotes:

If modifying the properties file is out of the question, I suggest using configparser:

In config parsers, values can span multiple lines as long as they are indented more than the key that holds them. By default parsers also let empty lines to be parts of values.

Here's a quick proof of concept:

#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

try:
    import ConfigParser as configparser
except ImportError:
    import configparser

try:
    import StringIO
except ImportError:
    import io.StringIO as StringIO

test_ini = """
[some_section]
messages.welcome=Hello\
 World
messages.bye=bye
"""
config = configparser.ConfigParser()
config.readfp(StringIO.StringIO(test_ini))
print(config.items('some_section'))

Output:

[('messages.welcome', 'Hello World'), ('messages.bye', 'bye')]

Gregg
  • 3,236
  • 20
  • 15
1

Thanks for the answers, this is what I finally did:

  • Add the section to the fist line of the properties file
  • Remove empty lines
  • Parse with configparser
  • Remove first line (section added in first step)

This is a extract of the code:

#!/usr/bin/python
...
# Add the section
subprocess.Popen(['/bin/bash','-c','sed -i \'1i [default]\' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)
# Remove empty lines
subprocess.Popen(['/bin/bash','-c','sed -i \'s/^$/#/g' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)
# Get all i18n files
files=glob.glob(srcDirectory+"/"+baseFileName+"_*.properties")
config = ConfigParser.ConfigParser()
for propFile in files:
...
    config.read(propertyFileName)
    value=config.get('default',"someproperty")
...
# Remove section 
subprocess.Popen(['/bin/bash','-c','sed -i \'1d\' '+srcDirectory+"/*.properties"], stdout=subprocess.PIPE)

I still have troubles with those multilines that doesn't start with an empty space. I just fixed them manually, but a sed could do the trick.

ghm1014
  • 675
  • 6
  • 12
0

Format your properties file like this:

messages.welcome="""Hello
 World!"""
messages.bye=bye
Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • This is a Java properties file, I can't modify them. Even if I use temporary files, formatting will be tough. – ghm1014 May 13 '11 at 22:48
0

Give a try to ConfigParser

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
0

I don't understand anything in the Java broth, but a regex would help you, I hope:

import re

ch = '''messages.welcome=Hello
  World!  
messages.bye=bye'''

regx = re.compile('^(messages\.[^= \t]+)[ \t]*=[ \t]*(.+?)(?=^messages\.|\Z)',re.MULTILINE|re.DOTALL)

print regx.findall(ch)

result

[('messages.welcome', 'Hello\n  World!  \n'), ('messages.bye', 'bye')]
eyquem
  • 26,771
  • 7
  • 38
  • 46
  • Thanks for the response. But my problem is to get the assignation from a text file, the parsing is OK – ghm1014 May 13 '11 at 17:02
  • @ghm1014 Well, don't you know to read the content of a file and then to treat it in order to remove newlines and surrounding whitespaces, and other manipulations like that ? – eyquem May 13 '11 at 17:08
  • Sure I could do in the hard way, but too many special cases may appear. Taking in count these are translation files for 12 languages and thousands of messages. – ghm1014 May 13 '11 at 22:51