Tblue / python-jproperties

Java Property file parser and writer for Python
https://pypi.org/project/jproperties/
Other
30 stars 13 forks source link

Multiline comments are being converted in single line comment #12

Open manoj-aps opened 4 years ago

manoj-aps commented 4 years ago

When using the jproperties utility to update the existing properties file. All of the comments in properties files are converting from multiline comments to a single line comment.

Input File:

# logs XML being signed or verified if set to DEBUG log4j.category.XMLTooling.Signature.Debugger=INFO, sig_log log4j.additivity.XMLTooling.Signature.Debugger=false log4j.ownAppenders.XMLTooling.Signature.Debugger=true

# the tran log blocks the "default" appender(s) at runtime # Level should be left at INFO for this category log4j.category.Shibboleth-TRANSACTION=INFO, tran_log log4j.additivity.Shibboleth-TRANSACTION=false log4j.ownAppenders.Shibboleth-TRANSACTION=true

# uncomment to suppress particular event types #log4j.category.Shibboleth-TRANSACTION.AuthnRequest=WARN #log4j.category.Shibboleth-TRANSACTION.Login=WARN #log4j.category.Shibboleth-TRANSACTION.Logout=WARN

# define the appenders

log4j.appender.shibd_log=org.apache.log4j.RollingFileAppender log4j.appender.shibd_log.fileName=/var/log/shibboleth/shibd.log

and the Output file is:

log4j.category.XMLTooling.Signature.Debugger=INFO, sig_log log4j.additivity.XMLTooling.Signature.Debugger=false #: _doc=the tran log blocks the "default" appender(s) at runtime\nLevel should be left at INFO for this category\n log4j.ownAppenders.XMLTooling.Signature.Debugger=true log4j.category.Shibboleth-TRANSACTION=INFO, tran_log log4j.additivity.Shibboleth-TRANSACTION=false #: _doc=uncomment to suppress particular event types\nlog4j.category.Shibboleth-TRANSACTION.AuthnRequest\=WARN\nlog4j.category.Shibboleth-TRANSACTION.Login\=WARN\nlog4j.category.Shibboleth-TRANSACTION.Logout\=WARN\ndefine the appenders\n log4j.ownAppenders.Shibboleth-TRANSACTION=true log4j.appender.shibd_log=org.apache.log4j.RollingFileAppender log4j.appender.shibd_log.fileName=/var/log/shibboleth/shibd.log

In output file following things are being messed up

  1. The order of the properties in the file is being shuffled to random.
  2. Multiline comments are converted into single line comments which are completely different from original file. The multiline comments must remain the same.

Is there any way to make sure the resolution of above points?

Tblue commented 3 years ago

Sorry for the (very) late reply.

  1. The order of the properties in the file is being shuffled to random.

Okay, the comments are messed up, but the order of the properties themselves in your example is the same. Or am I missing something?

  1. Multiline comments are converted into single line comments which are completely different from original file. The multiline comments must remain the same.

Indeed. This is because we currently don't really support roundtripping comments. Comments are instead treated as containers for metadata: They can contain either metadata key-value pairs for a property (#: comment lines preceding a property), or are treated as _doc metadata (any normal comment lines after a property are stored into the _doc metadata of the property). When writing files, metadata is always output in the form of #: comment lines, and the _doc metadata comment lines are not re-constructed to match the _doc comment lines in the input file (instead, the generated #: lines directly contain the _doc metadata as a key-value pair -- the original _doc comment lines are lost).

So: No, currently it's not possible to preserve comments exactly. A patch to ensure that comments in the output file match those in the input file as closely as possible is very much welcome.


One rather ugly workaround could be this:

#:1:  logs XML being signed or verified if set to DEBUG
log4j.category.XMLTooling.Signature.Debugger=INFO, sig_log
log4j.additivity.XMLTooling.Signature.Debugger=false
log4j.ownAppenders.XMLTooling.Signature.Debugger=true
#:1:
#:2:  the tran log blocks the "default" appender(s) at runtime
#:3:  Level should be left at INFO for this category
log4j.category.Shibboleth-TRANSACTION=INFO, tran_log
log4j.additivity.Shibboleth-TRANSACTION=false
log4j.ownAppenders.Shibboleth-TRANSACTION=true
#:1:
#:2:  uncomment to suppress particular event types
#:3: log4j.category.Shibboleth-TRANSACTION.AuthnRequest=WARN
#:4: log4j.category.Shibboleth-TRANSACTION.Login=WARN
#:5: log4j.category.Shibboleth-TRANSACTION.Logout=WARN
#:6:
#:7:  define the appenders
log4j.appender.shibd_log=org.apache.log4j.RollingFileAppender
log4j.appender.shibd_log.fileName=/var/log/shibboleth/shibd.log

This actually attaches each comment line as metadata to the first property after the comment, with keys like 1 and 2 (ascending ASCII characters because metadata comments are sorted in ascending order by metadata key on output). Then, when you pass strip_meta=False to the store() method, the output should more or less resemble the input: It won't be ideal, but at least the comments are not lost.