anyconfig.backend.properties
¶
Java properties backend:
Format to support: Java Properties file, e.g. http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html
Requirements: None (built-in)
Development Status :: 4 - Beta
Limitations:
Key and value separator of white spaces is not supported
Keys contain escaped white spaces is not supported
Special options: None
Changelog:
-
Changed in version 0.7.0:
Fix handling of empty values, pointed by @ajays20078
Fix handling of values contain strings start with ‘#’ or ‘!’ by @ajays20078
-
New in version 0.2:
Added native Java properties parser instead of a plugin utilizes pyjavaproperties module.
-
anyconfig.backend.properties.
_parseline
(line)¶ Parse a line of Java properties file.
- Parameters
line – A string to parse, must not start with ‘ ‘, ‘#’ or ‘!’ (comment)
- Returns
A tuple of (key, value), both key and value may be None
>>> _parseline(" ") (None, '') >>> _parseline("aaa:") ('aaa', '') >>> _parseline(" aaa:") ('aaa', '') >>> _parseline("aaa") ('aaa', '') >>> _parseline("url = http://localhost") ('url', 'http://localhost') >>> _parseline("calendar.japanese.type: LocalGregorianCalendar") ('calendar.japanese.type', 'LocalGregorianCalendar')
-
anyconfig.backend.properties.
_pre_process_line
(line, comment_markers=('#', '!'))¶ Preprocess a line in properties; strip comments, etc.
- Parameters
line – A string not starting w/ any white spaces and ending w/ line breaks. It may be empty. see also:
load()
.comment_markers – Comment markers, e.g. ‘#’ (hash)
>>> _pre_process_line('') is None True >>> s0 = "calendar.japanese.type: LocalGregorianCalendar" >>> _pre_process_line("# " + s0) is None True >>> _pre_process_line("! " + s0) is None True >>> _pre_process_line(s0 + "# comment") 'calendar.japanese.type: LocalGregorianCalendar# comment'
-
anyconfig.backend.properties.
unescape
(in_s)¶ - Parameters
in_s – Input string
-
anyconfig.backend.properties.
_escape_char
(in_c)¶ Escape some special characters in java .properties files.
- Parameters
in_c – Input character
>>> "\:" == _escape_char(':') True >>> "\=" == _escape_char('=') True >>> _escape_char('a') 'a'
-
anyconfig.backend.properties.
escape
(in_s)¶ - Parameters
in_s – Input string
-
anyconfig.backend.properties.
load
(stream, container=<class 'dict'>, comment_markers=('#', '!'))¶ Load and parse Java properties file given as a fiel or file-like object stream.
- Parameters
stream – A file or file like object of Java properties files
container – Factory function to create a dict-like object to store properties
comment_markers – Comment markers, e.g. ‘#’ (hash)
- Returns
Dict-like object holding properties
>>> to_strm = anyconfig.compat.StringIO >>> s0 = "calendar.japanese.type: LocalGregorianCalendar" >>> load(to_strm('')) {} >>> load(to_strm("# " + s0)) {} >>> load(to_strm("! " + s0)) {} >>> load(to_strm("calendar.japanese.type:")) {'calendar.japanese.type': ''} >>> load(to_strm(s0)) {'calendar.japanese.type': 'LocalGregorianCalendar'} >>> load(to_strm(s0 + "# ...")) {'calendar.japanese.type': 'LocalGregorianCalendar# ...'} >>> s1 = r"key=a\:b" >>> load(to_strm(s1)) {'key': 'a:b'} >>> s2 = '''application/postscript: \ ... x=Postscript File;y=.eps,.ps ... ''' >>> load(to_strm(s2)) {'application/postscript': 'x=Postscript File;y=.eps,.ps'}
-
class
anyconfig.backend.properties.
Parser
¶ Bases:
anyconfig.backend.base.StreamParser
Parser for Java properties files.
-
_type
= 'properties'¶
-
_extensions
= ['properties']¶
-
_ordered
= True¶
-
_dict_opts
= ['ac_dict']¶
-
load_from_stream
(stream, container, **kwargs)¶ Load config from given file like object stream.
- Parameters
stream – A file or file like object of Java properties files
container – callble to make a container object
kwargs – optional keyword parameters (ignored)
- Returns
Dict-like object holding config parameters
-
dump_to_stream
(cnf, stream, **kwargs)¶ Dump config cnf to a file or file-like object stream.
- Parameters
cnf – Java properties config data to dump
stream – Java properties file or file like object
kwargs – backend-specific optional keyword parameters :: dict
-
__module__
= 'anyconfig.backend.properties'¶
-