Properties
Class¶
-
class
javaproperties.
Properties
(data=None, defaults=None)[source]¶ A port of Java 8’s
java.net.Properties
that tries to match its behavior as much as is Pythonically possible.Properties
behaves like a normalMutableMapping
class (i.e., you can doprops[key] = value
and so forth), except that it may only be used to store strings (str
andunicode
in Python 2; juststr
in Python 3). Attempts to use a non-string object as a key or value will produce aTypeError
.Parameters: - data (mapping or
None
) – A mapping or iterable of(key, value)
pairs with which to initialize theProperties
object. All keys and values indata
must be text strings. - defaults (
Properties
orNone
) – a set of default properties that will be used as fallback forgetProperty
-
defaults
= None¶ A
Properties
subobject used as fallback forgetProperty
. OnlygetProperty
,propertyNames
, andstringPropertyNames
use this attribute; all other methods (including the standard mapping methods) ignore it.
-
getProperty
(key, defaultValue=None)[source]¶ Fetch the value associated with the key
key
in theProperties
object. If the key is not present,defaults
is checked, and then itsdefaults
, etc., until either a value forkey
is found or the nextdefaults
isNone
, in which casedefaultValue
is returned.Parameters: - key (text string) – the key to look up the value of
- defaultValue – the value to return if
key
is not found in theProperties
object
Return type: text string (if
key
was found)Raises: TypeError – if
key
is not a string
-
load
(inStream)[source]¶ Update the
Properties
object with the entries in a.properties
file or file-like object.inStream
may be either a text or binary filehandle, with or without universal newlines enabled. If it is a binary filehandle, its contents are decoded as Latin-1.Parameters: inStream (file-like object) – the file from which to read the .properties
documentReturns: None
-
loadFromXML
(inStream)[source]¶ Update the
Properties
object with the entries in the XML properties fileinStream
.Beyond basic XML well-formedness,
loadFromXML
only checks that the root element is namedproperties
and that all of itsentry
children havekey
attributes; no further validation is performed.Note
This uses
xml.etree.ElementTree
for parsing, which does not have decent support forunicode
input in Python 2. Files containing non-ASCII characters need to be opened in binary mode in Python 2, while Python 3 accepts both binary and text input.Parameters: inStream (file-like object) – the file from which to read the XML properties document Returns: None
Raises: ValueError – if the root of the XML tree is not a <properties>
tag or an<entry>
element is missing akey
attribute
-
propertyNames
()[source]¶ Returns a generator of all distinct keys in the
Properties
object and itsdefaults
(and itsdefaults
’sdefaults
, etc.) in unspecified orderReturn type: generator of text strings
-
store
(out, comments=None)[source]¶ Write the
Properties
object’s entries (in unspecified order) in.properties
format toout
, including the current timestamp.Parameters: Returns:
-
storeToXML
(out, comment=None, encoding='UTF-8')[source]¶ Write the
Properties
object’s entries (in unspecified order) in XML properties format toout
.Parameters: - out (binary file-like object) – a file-like object to write the properties to
- comment (text string or
None
) – if non-None
,comment
will be output as a<comment>
element before the<entry>
elements - encoding (string) – the name of the encoding to use for the XML document (also included in the XML declaration)
Returns:
- data (mapping or