You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigParser.py 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. """Configuration file parser.
  2. A setup file consists of sections, lead by a "[section]" header,
  3. and followed by "name: value" entries, with continuations and such in
  4. the style of RFC 822.
  5. The option values can contain format strings which refer to other values in
  6. the same section, or values in a special [DEFAULT] section.
  7. For example:
  8. something: %(dir)s/whatever
  9. would resolve the "%(dir)s" to the value of dir. All reference
  10. expansions are done late, on demand.
  11. Intrinsic defaults can be specified by passing them into the
  12. ConfigParser constructor as a dictionary.
  13. class:
  14. ConfigParser -- responsible for for parsing a list of
  15. configuration files, and managing the parsed database.
  16. methods:
  17. __init__(defaults=None)
  18. create the parser and specify a dictionary of intrinsic defaults. The
  19. keys must be strings, the values must be appropriate for %()s string
  20. interpolation. Note that `__name__' is always an intrinsic default;
  21. it's value is the section's name.
  22. sections()
  23. return all the configuration section names, sans DEFAULT
  24. has_section(section)
  25. return whether the given section exists
  26. has_option(section, option)
  27. return whether the given option exists in the given section
  28. options(section)
  29. return list of configuration options for the named section
  30. has_option(section, option)
  31. return whether the given section has the given option
  32. read(filenames)
  33. read and parse the list of named configuration files, given by
  34. name. A single filename is also allowed. Non-existing files
  35. are ignored.
  36. readfp(fp, filename=None)
  37. read and parse one configuration file, given as a file object.
  38. The filename defaults to fp.name; it is only used in error
  39. messages (if fp has no `name' attribute, the string `<???>' is used).
  40. get(section, option, raw=0, vars=None)
  41. return a string value for the named option. All % interpolations are
  42. expanded in the return values, based on the defaults passed into the
  43. constructor and the DEFAULT section. Additional substitutions may be
  44. provided using the `vars' argument, which must be a dictionary whose
  45. contents override any pre-existing defaults.
  46. getint(section, options)
  47. like get(), but convert value to an integer
  48. getfloat(section, options)
  49. like get(), but convert value to a float
  50. getboolean(section, options)
  51. like get(), but convert value to a boolean (currently defined as 0 or
  52. 1, only)
  53. remove_section(section)
  54. remove the given file section and all its options
  55. remove_option(section, option)
  56. remove the given option from the given section
  57. set(section, option, value)
  58. set the given option
  59. write(fp)
  60. write the configuration state in .ini format
  61. """
  62. import sys
  63. import string
  64. import re
  65. __all__ = ["NoSectionError","DuplicateSectionError","NoOptionError",
  66. "InterpolationError","InterpolationDepthError","ParsingError",
  67. "MissingSectionHeaderError","ConfigParser",
  68. "MAX_INTERPOLATION_DEPTH"]
  69. DEFAULTSECT = "DEFAULT"
  70. MAX_INTERPOLATION_DEPTH = 10
  71. # exception classes
  72. class Error(Exception):
  73. def __init__(self, msg=''):
  74. self._msg = msg
  75. Exception.__init__(self, msg)
  76. def __repr__(self):
  77. return self._msg
  78. __str__ = __repr__
  79. class NoSectionError(Error):
  80. def __init__(self, section):
  81. Error.__init__(self, 'No section: %s' % section)
  82. self.section = section
  83. class DuplicateSectionError(Error):
  84. def __init__(self, section):
  85. Error.__init__(self, "Section %s already exists" % section)
  86. self.section = section
  87. class NoOptionError(Error):
  88. def __init__(self, option, section):
  89. Error.__init__(self, "No option `%s' in section: %s" %
  90. (option, section))
  91. self.option = option
  92. self.section = section
  93. class InterpolationError(Error):
  94. def __init__(self, reference, option, section, rawval):
  95. Error.__init__(self,
  96. "Bad value substitution:\n"
  97. "\tsection: [%s]\n"
  98. "\toption : %s\n"
  99. "\tkey : %s\n"
  100. "\trawval : %s\n"
  101. % (section, option, reference, rawval))
  102. self.reference = reference
  103. self.option = option
  104. self.section = section
  105. class InterpolationDepthError(Error):
  106. def __init__(self, option, section, rawval):
  107. Error.__init__(self,
  108. "Value interpolation too deeply recursive:\n"
  109. "\tsection: [%s]\n"
  110. "\toption : %s\n"
  111. "\trawval : %s\n"
  112. % (section, option, rawval))
  113. self.option = option
  114. self.section = section
  115. class ParsingError(Error):
  116. def __init__(self, filename):
  117. Error.__init__(self, 'File contains parsing errors: %s' % filename)
  118. self.filename = filename
  119. self.errors = []
  120. def append(self, lineno, line):
  121. self.errors.append((lineno, line))
  122. self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
  123. class MissingSectionHeaderError(ParsingError):
  124. def __init__(self, filename, lineno, line):
  125. Error.__init__(
  126. self,
  127. 'File contains no section headers.\nfile: %s, line: %d\n%s' %
  128. (filename, lineno, line))
  129. self.filename = filename
  130. self.lineno = lineno
  131. self.line = line
  132. class ConfigParser:
  133. def __init__(self, defaults=None):
  134. self.__sections = {}
  135. if defaults is None:
  136. self.__defaults = {}
  137. else:
  138. self.__defaults = defaults
  139. def defaults(self):
  140. return self.__defaults
  141. def sections(self):
  142. """Return a list of section names, excluding [DEFAULT]"""
  143. # self.__sections will never have [DEFAULT] in it
  144. return self.__sections.keys()
  145. def add_section(self, section):
  146. """Create a new section in the configuration.
  147. Raise DuplicateSectionError if a section by the specified name
  148. already exists.
  149. """
  150. if self.__sections.has_key(section):
  151. raise DuplicateSectionError(section)
  152. self.__sections[section] = {}
  153. def has_section(self, section):
  154. """Indicate whether the named section is present in the configuration.
  155. The DEFAULT section is not acknowledged.
  156. """
  157. return section in self.sections()
  158. def options(self, section):
  159. """Return a list of option names for the given section name."""
  160. try:
  161. opts = self.__sections[section].copy()
  162. except KeyError:
  163. raise NoSectionError(section)
  164. opts.update(self.__defaults)
  165. if opts.has_key('__name__'):
  166. del opts['__name__']
  167. return opts.keys()
  168. def has_option(self, section, option):
  169. """Return whether the given section has the given option."""
  170. return option in self.options(section)
  171. def read(self, filenames):
  172. """Read and parse a filename or a list of filenames.
  173. Files that cannot be opened are silently ignored; this is
  174. designed so that you can specify a list of potential
  175. configuration file locations (e.g. current directory, user's
  176. home directory, systemwide directory), and all existing
  177. configuration files in the list will be read. A single
  178. filename may also be given.
  179. """
  180. if type(filenames) in [type(''), type(u'')]:
  181. filenames = [filenames]
  182. for filename in filenames:
  183. try:
  184. fp = open(filename)
  185. except IOError:
  186. continue
  187. self.__read(fp, filename)
  188. fp.close()
  189. def readfp(self, fp, filename=None):
  190. """Like read() but the argument must be a file-like object.
  191. The `fp' argument must have a `readline' method. Optional
  192. second argument is the `filename', which if not given, is
  193. taken from fp.name. If fp has no `name' attribute, `<???>' is
  194. used.
  195. """
  196. if filename is None:
  197. try:
  198. filename = fp.name
  199. except AttributeError:
  200. filename = '<???>'
  201. self.__read(fp, filename)
  202. def get(self, section, option, raw=0, vars=None):
  203. """Get an option value for a given section.
  204. All % interpolations are expanded in the return values, based on the
  205. defaults passed into the constructor, unless the optional argument
  206. `raw' is true. Additional substitutions may be provided using the
  207. `vars' argument, which must be a dictionary whose contents overrides
  208. any pre-existing defaults.
  209. The section DEFAULT is special.
  210. """
  211. try:
  212. sectdict = self.__sections[section].copy()
  213. except KeyError:
  214. if section == DEFAULTSECT:
  215. sectdict = {}
  216. else:
  217. raise NoSectionError(section)
  218. d = self.__defaults.copy()
  219. d.update(sectdict)
  220. # Update with the entry specific variables
  221. if vars:
  222. d.update(vars)
  223. option = self.optionxform(option)
  224. try:
  225. rawval = d[option]
  226. except KeyError:
  227. raise NoOptionError(option, section)
  228. if raw:
  229. return rawval
  230. # do the string interpolation
  231. value = rawval # Make it a pretty variable name
  232. depth = 0
  233. while depth < 10: # Loop through this until it's done
  234. depth = depth + 1
  235. if value.find("%(") >= 0:
  236. try:
  237. value = value % d
  238. except KeyError, key:
  239. raise InterpolationError(key, option, section, rawval)
  240. else:
  241. break
  242. if value.find("%(") >= 0:
  243. raise InterpolationDepthError(option, section, rawval)
  244. return value
  245. def __get(self, section, conv, option):
  246. return conv(self.get(section, option))
  247. def getint(self, section, option):
  248. return self.__get(section, string.atoi, option)
  249. def getfloat(self, section, option):
  250. return self.__get(section, string.atof, option)
  251. def getboolean(self, section, option):
  252. v = self.get(section, option)
  253. val = int(v)
  254. if val not in (0, 1):
  255. raise ValueError, 'Not a boolean: %s' % v
  256. return val
  257. def optionxform(self, optionstr):
  258. return optionstr.lower()
  259. def has_option(self, section, option):
  260. """Check for the existence of a given option in a given section."""
  261. if not section or section == "DEFAULT":
  262. return self.__defaults.has_key(option)
  263. elif not self.has_section(section):
  264. return 0
  265. else:
  266. option = self.optionxform(option)
  267. return self.__sections[section].has_key(option)
  268. def set(self, section, option, value):
  269. """Set an option."""
  270. if not section or section == "DEFAULT":
  271. sectdict = self.__defaults
  272. else:
  273. try:
  274. sectdict = self.__sections[section]
  275. except KeyError:
  276. raise NoSectionError(section)
  277. option = self.optionxform(option)
  278. sectdict[option] = value
  279. def write(self, fp):
  280. """Write an .ini-format representation of the configuration state."""
  281. if self.__defaults:
  282. fp.write("[DEFAULT]\n")
  283. for (key, value) in self.__defaults.items():
  284. fp.write("%s = %s\n" % (key, value))
  285. fp.write("\n")
  286. for section in self.sections():
  287. fp.write("[" + section + "]\n")
  288. sectdict = self.__sections[section]
  289. for (key, value) in sectdict.items():
  290. if key == "__name__":
  291. continue
  292. fp.write("%s = %s\n" % (key, value))
  293. fp.write("\n")
  294. def remove_option(self, section, option):
  295. """Remove an option."""
  296. if not section or section == "DEFAULT":
  297. sectdict = self.__defaults
  298. else:
  299. try:
  300. sectdict = self.__sections[section]
  301. except KeyError:
  302. raise NoSectionError(section)
  303. option = self.optionxform(option)
  304. existed = sectdict.has_key(option)
  305. if existed:
  306. del sectdict[option]
  307. return existed
  308. def remove_section(self, section):
  309. """Remove a file section."""
  310. if self.__sections.has_key(section):
  311. del self.__sections[section]
  312. return 1
  313. else:
  314. return 0
  315. #
  316. # Regular expressions for parsing section headers and options. Note a
  317. # slight semantic change from the previous version, because of the use
  318. # of \w, _ is allowed in section header names.
  319. SECTCRE = re.compile(
  320. r'\[' # [
  321. r'(?P<header>[^]]+)' # very permissive!
  322. r'\]' # ]
  323. )
  324. OPTCRE = re.compile(
  325. r'(?P<option>[]\-[\w_.*,(){}]+)' # a lot of stuff found by IvL
  326. r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
  327. # followed by separator
  328. # (either : or =), followed
  329. # by any # space/tab
  330. r'(?P<value>.*)$' # everything up to eol
  331. )
  332. def __read(self, fp, fpname):
  333. """Parse a sectioned setup file.
  334. The sections in setup file contains a title line at the top,
  335. indicated by a name in square brackets (`[]'), plus key/value
  336. options lines, indicated by `name: value' format lines.
  337. Continuation are represented by an embedded newline then
  338. leading whitespace. Blank lines, lines beginning with a '#',
  339. and just about everything else is ignored.
  340. """
  341. cursect = None # None, or a dictionary
  342. optname = None
  343. lineno = 0
  344. e = None # None, or an exception
  345. while 1:
  346. line = fp.readline()
  347. if not line:
  348. break
  349. lineno = lineno + 1
  350. # comment or blank line?
  351. if line.strip() == '' or line[0] in '#;':
  352. continue
  353. if line.split()[0].lower() == 'rem' \
  354. and line[0] in "rR": # no leading whitespace
  355. continue
  356. # continuation line?
  357. if line[0] in ' \t' and cursect is not None and optname:
  358. value = line.strip()
  359. if value:
  360. k = self.optionxform(optname)
  361. cursect[k] = "%s\n%s" % (cursect[k], value)
  362. # a section header or option header?
  363. else:
  364. # is it a section header?
  365. mo = self.SECTCRE.match(line)
  366. if mo:
  367. sectname = mo.group('header')
  368. if self.__sections.has_key(sectname):
  369. cursect = self.__sections[sectname]
  370. elif sectname == DEFAULTSECT:
  371. cursect = self.__defaults
  372. else:
  373. cursect = {'__name__': sectname}
  374. self.__sections[sectname] = cursect
  375. # So sections can't start with a continuation line
  376. optname = None
  377. # no section header in the file?
  378. elif cursect is None:
  379. raise MissingSectionHeaderError(fpname, lineno, `line`)
  380. # an option line?
  381. else:
  382. mo = self.OPTCRE.match(line)
  383. if mo:
  384. optname, vi, optval = mo.group('option', 'vi', 'value')
  385. if vi in ('=', ':') and ';' in optval:
  386. # ';' is a comment delimiter only if it follows
  387. # a spacing character
  388. pos = optval.find(';')
  389. if pos and optval[pos-1] in string.whitespace:
  390. optval = optval[:pos]
  391. optval = optval.strip()
  392. # allow empty values
  393. if optval == '""':
  394. optval = ''
  395. cursect[self.optionxform(optname)] = optval
  396. else:
  397. # a non-fatal parsing error occurred. set up the
  398. # exception but keep going. the exception will be
  399. # raised at the end of the file and will contain a
  400. # list of all bogus lines
  401. if not e:
  402. e = ParsingError(fpname)
  403. e.append(lineno, `line`)
  404. # if any parsing errors occurred, raise an exception
  405. if e:
  406. raise e