Attempt to load a system-wide configuration file, whose settings will be overwritten by a user preferences files.
Missing files are silently ignored.
from ihih import IHIH
conf = IHIH(
(
'/etc/example.conf',
os.path.join(os.path.expanduser('~'), '.example.conf')
),
debug='1'
)
if conf.get_float('debug', errors='ignore'):
print 'i am running in debug mode'
Assuming conf is a IHIH instance.
# reload on SIGHUP
import signal
signal.signal(signal.SIGHUP, lambda s, f: conf.reload())
By default, IHIH parse files using the following rules:
By default, IHIHI parse files accordingly the following rules:
Which mean that it could parse, to a certain extent (see Single-line only), subset of:
That could be convenient if you have to share a configuration file between scripts, given you pay attention to respect both formats.
Parsing a shell script:
# as in shell
FOO="bar"
FOOBAR=foo-$FOO # resolve as: foo-bar
FOOBAR="foo-$FOO" # resolve as: foo-bar
FOOBAR='foo-$FOO' # resolve as: foo-$FOO
BAR=${FOO} # resolve as: bar
ABC="a" 'b' c # resolve as: a b c
C=hello # world # resolve as: hello
D=hello \# world # resolve as: hello # world
# different
DATE=$(date) # resolve as: $(date)
Parsing a main.cf:
smtpd_banner = $myhostname ESMTP
myhostname = foo.example.net
Parsing some Python:
# same
a = 'AA'
b = "BB"
# notably different
c = 'A' "B" # resolve as: A B
d = c # resolve as: c
Parsing an INI file:
; section is ignored
[uwsgi]
http-socket = :9090
processes = 4
; different, resolve as: localhost:9000
URL = localhost${http-socket}