Completed
Pull Request — master (#50)
by
unknown
01:21
created

_pre_process_line()   B

Complexity

Conditions 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 27
rs 8.0894
1
#
2
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato @ redhat.com>
3
# License: MIT
4
#
5
"""
6
Java properties file support.
7
8
.. versionadded:: 0.2
9
   Added native Java properties parser instead of a plugin utilizes
10
   pyjavaproperties module.
11
12
- Format to support: Java Properties file, e.g.
13
  http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html
14
- Requirements: None (built-in)
15
- Limitations: None
16
- Special options: None
17
"""
18
from __future__ import absolute_import
19
20
import logging
21
import re
22
23
import anyconfig.backend.base
24
import anyconfig.compat
25
26
27
LOGGER = logging.getLogger(__name__)
28
_COMMENT_MARKERS = ("#", "!")
29
30
31
def _parseline(line):
32
    """
33
    Parse a line of Java properties file.
34
35
    :param line:
36
        A string to parse, must not start with ' ', '#' or '!' (comment)
37
    :return: A tuple of (key, value), both key and value may be None
38
39
    >>> _parseline("aaa")
40
    (None, None)
41
    >>> _parseline("calendar.japanese.type: LocalGregorianCalendar")
42
    ('calendar.japanese.type', 'LocalGregorianCalendar')
43
    """
44
    match = re.match(r"^(\S+)(?:\s+)?(?<!\\)[:=](?:\s+)?(.+)", line)
45
    if not match:
46
        LOGGER.warning("Invalid line found: %s", line)
47
        return (None, None)
48
49
    return match.groups()
50
51
52
def _pre_process_line(line, comment_markers=_COMMENT_MARKERS):
53
    """
54
    Preprocess a line in properties; strip comments, etc.
55
56
    :param line:
57
        A string not starting w/ any white spaces and ending w/ line breaks.
58
        It may be empty. see also: :func:`load`.
59
    :param comment_markers: Comment markers, e.g. '#' (hash)
60
61
    >>> _pre_process_line('') is None
62
    True
63
    >>> s0 = "calendar.japanese.type: LocalGregorianCalendar"
64
    >>> _pre_process_line("# " + s0) is None
65
    True
66
    >>> _pre_process_line("! " + s0) is None
67
    True
68
    >>> _pre_process_line(s0 + "# comment")
69
    'calendar.japanese.type: LocalGregorianCalendar'
70
    """
71
    if not line:
72
        return None
73
74
    if any(c in line for c in comment_markers):
75
        if line.startswith(comment_markers):
76
            return None
77
78
    return line
79
80
81
def unescape(in_s):
82
    """
83
    :param in_s: Input string
84
    """
85
    return re.sub(r"\\(.)", r"\1", in_s)
86
87
88
def _escape_char(in_c):
89
    """
90
    Escape some special characters in java .properties files.
91
92
    :param in_c: Input character
93
94
    >>> "\\:" == _escape_char(':')
95
    True
96
    >>> "\\=" == _escape_char('=')
97
    True
98
    >>> _escape_char('a')
99
    'a'
100
    """
101
    return '\\' + in_c if in_c in (':', '=', '\\') else in_c
102
103
104
def escape(in_s):
105
    """
106
    :param in_s: Input string
107
    """
108
    return ''.join(_escape_char(c) for c in in_s)
109
110
111
def load(stream, to_container=dict, comment_markers=_COMMENT_MARKERS):
112
    """
113
    Load and parse Java properties file given as a fiel or file-like object
114
    `stream`.
115
116
    :param stream: A file or file like object of Java properties files
117
    :param to_container:
118
        Factory function to create a dict-like object to store properties
119
    :param comment_markers: Comment markers, e.g. '#' (hash)
120
    :return: Dict-like object holding properties
121
122
    >>> to_strm = anyconfig.compat.StringIO
123
    >>> s0 = "calendar.japanese.type: LocalGregorianCalendar"
124
    >>> load(to_strm(''))
125
    {}
126
    >>> load(to_strm("# " + s0))
127
    {}
128
    >>> load(to_strm("! " + s0))
129
    {}
130
    >>> load(to_strm("calendar.japanese.type:"))
131
    {}
132
    >>> load(to_strm(s0))
133
    {'calendar.japanese.type': 'LocalGregorianCalendar'}
134
    >>> load(to_strm(s0 + "# ..."))
135
    {'calendar.japanese.type': 'LocalGregorianCalendar'}
136
    >>> s1 = r"key=a\\:b"
137
    >>> load(to_strm(s1))
138
    {'key': 'a:b'}
139
    >>> s2 = '''application/postscript: \\
140
    ...         x=Postscript File;y=.eps,.ps
141
    ... '''
142
    >>> load(to_strm(s2))
143
    {'application/postscript': 'x=Postscript File;y=.eps,.ps'}
144
    """
145
    ret = to_container()
146
    prev = ""
147
148
    for line in stream.readlines():
149
        line = _pre_process_line(prev + line.strip().rstrip(),
150
                                 comment_markers)
151
        # I don't think later case may happen but just in case.
152
        if line is None or not line:
153
            continue
154
155
        if line.endswith("\\"):
156
            prev += line.rstrip(" \\")
157
            continue
158
159
        prev = ""  # re-initialize for later use.
160
161
        (key, val) = _parseline(line)
162
        if key is None:
163
            LOGGER.warning("Failed to parse the line: %s", line)
164
            continue
165
166
        ret[key] = unescape(val)
167
168
    return ret
169
170
171
class Parser(anyconfig.backend.base.FromStreamLoader,
172
             anyconfig.backend.base.ToStreamDumper):
173
    """
174
    Parser for Java properties files.
175
    """
176
    _type = "properties"
177
    _extensions = ["properties"]
178
179
    def load_from_stream(self, stream, to_container, **kwargs):
180
        """
181
        Load config from given file like object `stream`.
182
183
        :param stream: A file or file like object of Java properties files
184
        :param to_container: callble to make a container object
185
        :param kwargs: optional keyword parameters (ignored)
186
187
        :return: Dict-like object holding config parameters
188
        """
189
        return load(stream, to_container=to_container)
190
191
    def dump_to_stream(self, cnf, stream, **kwargs):
192
        """
193
        Dump config `cnf` to a file or file-like object `stream`.
194
195
        :param cnf: Java properties config data to dump
196
        :param stream: Java properties file or file like object
197
        :param kwargs: backend-specific optional keyword parameters :: dict
198
        """
199
        for key, val in anyconfig.compat.iteritems(cnf):
200
            stream.write("%s = %s\n" % (key, escape(val)))
201
202
# vim:sw=4:ts=4:et:
203