Skip to content

Commit 8724905

Browse files
committed
Adding initial version
1 parent faa669c commit 8724905

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

jprops.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""java properties file parser"""
2+
3+
import re
4+
5+
def getJavaProperties(propfile):
6+
"""Read the key, element pairs from a java properties file
7+
8+
Follows the file format 'http://docs.oracle.com/javase/6/docs/api/
9+
java/util/Properties.html#load(java.io.Reader)' and tested against
10+
the Java 6 version of java.util.Properties
11+
12+
Args:
13+
propfile: a valid file object (as returned by open(filename))
14+
Returns:
15+
The property key and elements as a dict
16+
Raises:
17+
IOError: if file operation fails with I/O related reason
18+
Corresponds to java IOException in Properties.load()
19+
UnicodeDecodeError: if the property file has malformed \uxxxx encoding,
20+
Corresponds to java IllegalArgumentException in Properties.load()
21+
AttributeError: if invalid object was provided for file object
22+
Corresponds to java NullPointerException
23+
24+
Author: Kishan Thomas <kishan@hackorama.com>
25+
"""
26+
LINE_BREAKS = '\n\r\f' #end-of-line, carriage-return, form-feed
27+
ESC_DELIM = r'\\' # '\'
28+
ESCAPED_ESC_DELIM = r'\\\\' # '\\'
29+
COMMENT_LINE = re.compile('\s*[#!].*') # starts with #|! ignore white space
30+
MULTI_LINE = re.compile(r'.*[\\]\s*$') # ending with '\' ignore white space
31+
# non escaped =|:|' ', include surrounding non escaped white space
32+
SPLIT_DELIM = re.compile(r'(?<!\\)\s*(?<!\\)[=: ]\s*')
33+
# match escape characters '\', except escaped '\\' and unicode escape '\u'
34+
VALID_ESC_DELIM = r'(?<!\\)[\\](?!u)'
35+
DEFAULT_ELEMENT = ''
36+
37+
result = dict()
38+
natural_line = propfile.readline()
39+
while natural_line:
40+
# skip blank lines and comment lines, process only valid logical lines
41+
if natural_line.strip() and COMMENT_LINE.match(natural_line) is None:
42+
logical_line = natural_line.lstrip().rstrip(LINE_BREAKS)
43+
# remove multi line delim and append adjacent lines
44+
while MULTI_LINE.match(logical_line):
45+
logical_line = logical_line.rstrip()[:-1] + propfile.readline().lstrip().rstrip(LINE_BREAKS)
46+
pair = SPLIT_DELIM.split(logical_line, 1)
47+
if len(pair) == 1: pair.append(DEFAULT_ELEMENT)
48+
pair = [re.sub(VALID_ESC_DELIM, '', item) for item in pair]
49+
pair = [re.sub(ESCAPED_ESC_DELIM, ESC_DELIM, item) for item in pair]
50+
pair = [unicode(item, 'unicode_escape') for item in pair]
51+
result[pair[0]] = pair[1] # add key, element to result dict
52+
natural_line = propfile.readline()
53+
return result

0 commit comments

Comments
 (0)