annotate configmix/ini.py @ 20:9bdc4e421415

A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary. Applies also type conversions and configparser-style string interpolations.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Thu, 10 Mar 2016 13:06:31 +0100
parents 36fa039f7e11
children ce290b10dac5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
7
7c095c6223b8 Module comment for ini.py
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
2 r"""Read INI-style configuration files.
7c095c6223b8 Module comment for ini.py
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
3
7c095c6223b8 Module comment for ini.py
Franz Glasner <hg@dom66.de>
parents: 5
diff changeset
4 """
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
5
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
6 from __future__ import division, absolute_import, print_function
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
7
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
8 import sys
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
9 import os
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
10 import io
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
11 import locale
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
12 try:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
13 from configparser import SafeConfigParser, NoSectionError, NoOptionError
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
14 except ImportError:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
15 from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
20
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
16 try:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
17 from collections import OrderedDict as DictImpl
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
18 except ImportError:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
19 try:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
20 from ordereddict import OrderedDict as DictImpl
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
21 except ImportError:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
22 DictImpl = dict
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
23
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
24 from .compat import PY2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
25
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
26
20
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
27 __all__ = ["INIConfigParser", "NoSectionError", "NoOptionError",
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
28 "load"]
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
29
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
30
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
31 class INIConfigParser(SafeConfigParser):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
32
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
33 """A case sensitive config parser that returns all-unicode string
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
34 values.
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
35
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
36 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
37
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
38 def __init__(self, filename, executable=None, encoding=None):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
39 SafeConfigParser.__init__(self)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
40 if executable is None:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
41 executable = sys.argv[0]
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
42 if PY2:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
43 if isinstance(filename, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
44 filename = filename.decode(locale.getpreferredencoding())
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
45 if isinstance(executable, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
46 executable = executable.decode(locale.getpreferredencoding())
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
47 self.executable = os.path.normpath(os.path.abspath(executable))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
48 if encoding is None:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
49 encoding = locale.getpreferredencoding()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
50 self.encoding = encoding
14
36fa039f7e11 Formatting
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 7
diff changeset
51 with io.open(filename, mode="rt", encoding=self.encoding) as cf:
2
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
52 self.readfp(cf, filename)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
53
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
54 def optionxform(self, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
55 return option
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
56
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
57 def get_path_list(self, section, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
58 v = self.get(section, option)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
59 return v.split(os.pathsep)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
60
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
61 def read(self, filenames):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
62 raise NotImplementedError("use `readfp()' instead")
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
63
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
64 def readfp(self, fp, filename):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
65 if hasattr(self, "filename"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
66 raise RuntimeError("already initialized")
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
67 filename = os.path.normpath(os.path.abspath(filename))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
68 if PY2:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
69 if isinstance(filename, str):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
70 filename = filename.decode(locale.getpreferredencoding())
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
71 self.set(None, "self", filename)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
72 self.set(None, "here", os.path.dirname(filename))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
73 self.set(None, "root", os.path.dirname(self.executable))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
74 SafeConfigParser.readfp(self, fp, filename=filename)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
75 self.filename = filename
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
76 self.root = os.path.dirname(self.executable)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
77
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
78 def getx(self, section, option):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
79 """Extended get() with some automatic type conversion support.
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
80
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
81 Default: Fetch as string (like `get()`).
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
82
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
83 If annotated with ``:bool:`` fetch as bool, if annotated with
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
84 ``:int:`` fetch as int, if annotated with ``:float:`` fetch as
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
85 float.
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
86
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
87 """
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
88 v = self.get(section, option)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
89 if v.startswith(":bool:"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
90 v = v[6:].lower()
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
91 if v not in self._BOOL_CVT:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
92 raise ValueError("Not a boolean: %s" % (v, ))
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
93 return self._BOOL_CVT[v]
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
94 elif v.startswith(":int:"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
95 return int(v[5:], 0)
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
96 elif v.startswith(":float:"):
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
97 return float(v[7:])
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
98 else:
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
99 return v
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
100
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
101 _BOOL_CVT = {'1': True, 'yes': True, 'true': True, 'on': True,
9981a68040b6 An INI-style configuration file parser
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
diff changeset
102 '0': False, 'no': False, 'false': False, 'off': False}
20
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
103
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
104
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
105 def load(filename, extract=["config"]):
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
106 """Load a single INI file and read/interpolate the sections given in
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
107 `extract`.
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
108
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
109 Flattens the given sections into the resulting dictionary.
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
110
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
111 """
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
112 conf = DictImpl()
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
113 ini = INIConfigParser(filename)
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
114 for sect in extract:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
115 try:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
116 cfg = ini.options(sect)
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
117 except NoSectionError:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
118 pass
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
119 else:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
120 for option in cfg:
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
121 value = ini.getx(sect, option)
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
122 conf[option] = value
9bdc4e421415 A "load()" function for INI-style configuration files that extracts given sections into the resulting dictonary.
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 14
diff changeset
123 return conf