ssato / python-anyconfig

Python library provides common APIs to load and dump configuration files in various formats
MIT License
278 stars 31 forks source link

Java properties and escaping #31

Closed HPotter closed 9 years ago

HPotter commented 9 years ago

Hello!

[sorry for my bad english]

I'm going to use python-anyconfig for parsing teamcity properties file

It has, e.g., line:

agent.work.dir=D\:\\BuildAgent\\work

I wrote sample Java script:

import java.util.Enumeration;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;

public class props
{
        public static void main(String[] args) throws IOException {
                InputStream input = new FileInputStream("test.properties");

                Properties properties = new Properties();
                properties.load(input);

                Enumeration<?> e = properties.propertyNames();
                while (e.hasMoreElements()) {
                        String key = (String) e.nextElement();
                        String value = properties.getProperty(key);
                        System.out.println(key + " -> " + value);
                }

                input.close();
        }
}

, and it outputs

agent.work.dir -> D:\BuildAgent\work

But anyconfig script

for x, y in anyconfig.load('test.properties').iteritems(): print '{} -> {}'.format(x, y)

outputs

agent.work.dir -> D\:\\BuildAgent\\work
HPotter commented 9 years ago

Oh, sorry, I missed the fact that you're using pyjavaproperties. So it looks like it isn't a bug of anyconfig, but a bug of pyjavaproperties

ssato commented 9 years ago

Thanks for your report!

Since 0.2.0, python-anyconfig supports properties files w/o pyjavaproperties. So if any issues exist when parsing .properties files, it's possible bug in python-anyconfig itself, not pyjavaproperties.

Anyway, it looks worked as far as I tested like the followings. Could you please let me know the details of you problem?

In [1]: import anyconfig

In [2]: open("/tmp/t.properties", 'w').write("""
...: agent.work.dir=D\:\\BuildAgent\\work
...: """)

In [3]: cat /tmp/t.properties

agent.work.dir=D\:\BuildAgent\work

In [4]: f="/tmp/t.properties"

In [5]: d = anyconfig.load(f)

In [6]: d
Out[6]: {'agent.work.dir': 'D\\:\\BuildAgent\\work'}

In [7]: d["agent.work.dir"]
Out[7]: 'D\\:\\BuildAgent\\work'

In [8]: print d["agent.work.dir"]
D\:\BuildAgent\work

In [9]:
HPotter commented 9 years ago

I've tried your code sample, and I've got same results. But my Java "test" code for file /tmp/t.properties prints this:

agent.work.dir -> D:BuildAgentwork

I suggest that your test's step №2 probably must be like this:

In [2]: open("/tmp/t.properties", 'w').write(r"""
   ...: agent.work.dir=D\:\\BuildAgent\\work
   ...: """)

(note the raw string)

UPD my version is 0.3.0, but I have pyjavaproperties installed in the same virtualenv, does it matters? UPD2 uninstalled pyjavaproperties, got same results

ssato commented 9 years ago

oh, I see. The value parsed from .properties files must be un-escaped. Wait for a while, please. I try to fix it until the next weekend if I have some time.

ssato commented 9 years ago

Excuse me to very late response. I could spend time to look into this and pushed possible fixes a minute ago. Could you please take a look at it ? I think I could fix it but I want to make sure that.

ssato commented 9 years ago

Here is a test session log:

In [1]: import sys,os

In [2]: sys.path.insert(0, os.curdir)

In [3]: import anyconfig

In [4]: fn = "/tmp/test.properties"

In [5]: open(fn, 'w').write(r"""
   ...: agent.work.dir=D\:\\BuildAgent\\work
      ...: """)

In [6]: cat /tmp/test.properties

agent.work.dir=D\:\\BuildAgent\\work

In [7]: d = anyconfig.load(fn)

In [8]: d
Out[8]: {'agent.work.dir': 'D:\\BuildAgent\\work'}

In [9]: d["agent.work.dir"]
Out[9]: 'D:\\BuildAgent\\work'

In [10]: print(d["agent.work.dir"])
D:\BuildAgent\work

In [11]: print(anyconfig.dumps(d, ac_parser="properties"))                                                
agent.work.dir = D\:\\BuildAgent\\work                                                                    

In [12]:
HPotter commented 9 years ago

Oh, sorry, I've missed a letter from github with your comments.

It looks like the problem is resolved - all my test files are parsed correctly. I'm looking forward to get the latest module version from pypi.

Thank you very much for your help!

ssato commented 9 years ago

Thanks for your confirmation! Now I've started for the next release.