# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/.
# This file contains utility functions for reading .properties files
import codecs import re
import six
class DotProperties:
r"""A thin representation of a key=value .properties file."""
def __init__(self, file=None):
self._properties = {} if file:
self.update(file)
def update(self, file): """Updates properties from a file name or file-like object.
Ignores empty lines and comment lines."""
if isinstance(file, str):
f = codecs.open(file, "r", "utf-8") else:
f = file
for l in f.readlines():
line = l.strip() ifnot line or line.startswith("#"): continue
(k, v) = re.split(r"\s*=\s*", line, 1)
self._properties[k] = v
def get_list(self, prefix): """Turns {'list.0':'foo', 'list.1':'bar'} into ['foo', 'bar'].
Returns [] to indicate an empty or missing list."""
ifnot prefix.endswith("."):
prefix = prefix + "."
indexes = [] for k, v in six.iteritems(self._properties): ifnot k.startswith(prefix): continue
key = k[len(prefix) :] if"."in key: # We have something like list.sublist.0. continue
indexes.append(int(key)) return [self._properties[prefix + str(index)] for index in sorted(indexes)]
def get_dict(self, prefix, required_keys=[]): """Turns {'foo.title':'title', ...} into {'title':'title', ...}.
If ``|required_keys|`` is present, it must be an iterable of required key
names. If a required key isnot present, ValueError is thrown.
Returns {} to indicate an empty or missing dict."""
ifnot prefix.endswith("."):
prefix = prefix + "."
D = dict(
(k[len(prefix) :], v) for k, v in six.iteritems(self._properties) if k.startswith(prefix) and"."notin k[len(prefix) :]
)
for required_key in required_keys: if required_key notin D: raise ValueError("Required key %s not present" % required_key)
return D
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.24Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.