PropertiesFile
Class¶
-
class
javaproperties.
PropertiesFile
(mapping: Union[None, Mapping[str, str], Iterable[Tuple[str, str]]] = None, **kwargs: str)[source]¶ New in version 0.3.0.
A custom mapping class for reading from, editing, and writing to a
.properties
file while preserving comments & whitespace in the original input.A
PropertiesFile
instance can be constructed from another mapping and/or iterable of pairs, after which it will act like anOrderedDict
. Alternatively, an instance can be constructed from a file or string withPropertiesFile.load()
orPropertiesFile.loads()
, and the resulting instance will remember the formatting of its input and retain that formatting when written back to a file or string with thedump()
ordumps()
method. The formatting information attached to an instancepf
can be forgotten by constructing another mapping from it viadict(pf)
,OrderedDict(pf)
, or evenPropertiesFile(pf)
(Use thecopy()
method if you want to create anotherPropertiesFile
instance with the same data & formatting).When not reading or writing,
PropertiesFile
behaves like a normalMutableMapping
class (i.e., you can doprops[key] = value
and so forth), except that (a) likeOrderedDict
, key insertion order is remembered and is used when iterating & dumping (andreversed
is supported), and (b) likeProperties
, it may only be used to store strings and will raise aTypeError
if passed a non-string object as key or value.Two
PropertiesFile
instances compare equal iff both their key-value pairs and comment & whitespace lines are equal and in the same order. When comparing aPropertiesFile
to any other type of mapping, only the key-value pairs are considered, and order is ignored.PropertiesFile
currently only supports reading & writing the simple line-oriented format, not XML.-
copy
() → javaproperties.propfile.PropertiesFile[source]¶ Create a copy of the mapping, including formatting information
-
dump
(fp: TextIO, separator: str = '=', ensure_ascii: bool = True) → None[source]¶ Write the mapping to a file in simple line-oriented
.properties
format.If the instance was originally created from a file or string with
PropertiesFile.load()
orPropertiesFile.loads()
, then the output will include the comments and whitespace from the original input, and any keys that haven’t been deleted or reassigned will retain their original formatting and multiplicity. Key-value pairs that have been modified or added to the mapping will be reformatted withjoin_key_value()
using the given separator andensure_ascii
setting. All key-value pairs are output in the order they were defined, with new keys added to the end.Changed in version 0.8.0:
ensure_ascii
parameter addedNote
Serializing a
PropertiesFile
instance with thedump()
function instead will cause all formatting information to be ignored, asdump()
will treat the instance like a normal mapping.- Parameters
fp (TextIO) – A file-like object to write the mapping to. It must have been opened as a text file with a Latin-1-compatible encoding.
separator (str) – The string to use for separating new or modified keys & values. Only
" "
,"="
, and":"
(possibly with added whitespace) should ever be used as the separator.ensure_ascii (bool) – if true, all non-ASCII characters in new or modified key-value pairs will be replaced with
\uXXXX
escape sequences in the output; if false, non-ASCII characters will be passed through as-is
- Returns
-
dumps
(separator: str = '=', ensure_ascii: bool = True) → str[source]¶ Convert the mapping to a
str
in simple line-oriented.properties
format.If the instance was originally created from a file or string with
PropertiesFile.load()
orPropertiesFile.loads()
, then the output will include the comments and whitespace from the original input, and any keys that haven’t been deleted or reassigned will retain their original formatting and multiplicity. Key-value pairs that have been modified or added to the mapping will be reformatted withjoin_key_value()
using the given separator andensure_ascii
setting. All key-value pairs are output in the order they were defined, with new keys added to the end.Changed in version 0.8.0:
ensure_ascii
parameter addedNote
Serializing a
PropertiesFile
instance with thedumps()
function instead will cause all formatting information to be ignored, asdumps()
will treat the instance like a normal mapping.- Parameters
separator (str) – The string to use for separating new or modified keys & values. Only
" "
,"="
, and":"
(possibly with added whitespace) should ever be used as the separator.ensure_ascii (bool) – if true, all non-ASCII characters in new or modified key-value pairs will be replaced with
\uXXXX
escape sequences in the output; if false, non-ASCII characters will be passed through as-is
- Return type
-
property
header_comment
¶ New in version 0.7.0.
The concatenated values of all comments at the top of the file, up to (but not including) the first key-value pair or timestamp comment, whichever comes first. The comments are returned with comment markers and the whitespace leading up to them removed, with line endings changed to
\n
, and with the line ending on the final comment (if any) removed. Blank/all-whitespace lines among the comments are ignored.The header comment can be changed by assigning to this property. Assigning a string
s
causes everything before the first key-value pair or timestamp comment to be replaced by the output ofto_comment(s)
. AssigningNone
causes the header comment to be deleted (also achievable withdel pf.header_comment
).>>> pf = PropertiesFile.loads('''\ ... #This is a comment. ... ! This is also a comment. ... #Tue Feb 25 19:13:27 EST 2020 ... key = value ... zebra: apple ... ''') >>> pf.header_comment 'This is a comment.\n This is also a comment.' >>> pf.header_comment = 'New comment' >>> print(pf.dumps(), end='') #New comment #Tue Feb 25 19:13:27 EST 2020 key = value zebra: apple >>> del pf.header_comment >>> pf.header_comment is None True >>> print(pf.dumps(), end='') #Tue Feb 25 19:13:27 EST 2020 key = value zebra: apple
-
classmethod
load
(fp: IO) → javaproperties.propfile.PropertiesFile[source]¶ Parse the contents of the
readline
-supporting file-like objectfp
as a simple line-oriented.properties
file and return aPropertiesFile
instance.fp
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.Changed in version 0.5.0: Invalid
\uXXXX
escape sequences will now cause anInvalidUEscapeError
to be raised- Parameters
fp (IO) – the file from which to read the
.properties
document- Return type
- Raises
InvalidUEscapeError – if an invalid
\uXXXX
escape sequence occurs in the input
-
classmethod
loads
(s: AnyStr) → javaproperties.propfile.PropertiesFile[source]¶ Parse the contents of the string
s
as a simple line-oriented.properties
file and return aPropertiesFile
instance.s
may be either a text string or bytes string. If it is a bytes string, its contents are decoded as Latin-1.Changed in version 0.5.0: Invalid
\uXXXX
escape sequences will now cause anInvalidUEscapeError
to be raised- Parameters
s (Union[str,bytes]) – the string from which to read the
.properties
document- Return type
- Raises
InvalidUEscapeError – if an invalid
\uXXXX
escape sequence occurs in the input
-
property
timestamp
¶ New in version 0.7.0.
The value of the timestamp comment, with the comment marker, any whitespace leading up to it, and the trailing newline removed. The timestamp comment is the first comment that appears to be a valid timestamp as produced by Java 8’s
Date.toString()
and that does not come after any key-value pairs; if there is no such comment, the value of this property isNone
.The timestamp can be changed by assigning to this property. Assigning a string
s
replaces the timestamp comment with the output ofto_comment(s)
; no check is made as to whether the result is a valid timestamp comment. AssigningNone
orFalse
causes the timestamp comment to be deleted (also achievable withdel pf.timestamp
). Assigning any other valuex
replaces the timestamp comment with the output ofto_comment(java_timestamp(x))
.>>> pf = PropertiesFile.loads('''\ ... #This is a comment. ... #Tue Feb 25 19:13:27 EST 2020 ... key = value ... zebra: apple ... ''') >>> pf.timestamp 'Tue Feb 25 19:13:27 EST 2020' >>> pf.timestamp = 1234567890 >>> pf.timestamp 'Fri Feb 13 18:31:30 EST 2009' >>> print(pf.dumps(), end='') #This is a comment. #Fri Feb 13 18:31:30 EST 2009 key = value zebra: apple >>> del pf.timestamp >>> pf.timestamp is None True >>> print(pf.dumps(), end='') #This is a comment. key = value zebra: apple
-