Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
ConfigParser Class Reference

Public Member Functions

def __init__
 
def defaults
 
def sections
 
def add_section
 
def has_section
 
def options
 
def read
 
def readfp
 
def get
 
def getint
 
def getfloat
 
def getboolean
 
def optionxform
 
def has_option
 
def set
 
def write
 
def remove_option
 
def remove_section
 

Static Public Attributes

tuple SECTCRE
 
tuple OPTCRE
 

Detailed Description

Definition at line 172 of file ConfigParser.py.

Constructor & Destructor Documentation

def __init__ (   self,
  defaults = None 
)

Definition at line 173 of file ConfigParser.py.

References ConfigParser.__defaults, and ConfigParser.__sections.

174  def __init__(self, defaults=None):
175  self.__sections = {}
176  if defaults is None:
177  self.__defaults = {}
178  else:
179  self.__defaults = defaults

Member Function Documentation

def add_section (   self,
  section 
)
Create a new section in the configuration.

Raise DuplicateSectionError if a section by the specified name
already exists.

Definition at line 188 of file ConfigParser.py.

References ConfigParser.__sections.

189  def add_section(self, section):
190  """Create a new section in the configuration.
191 
192  Raise DuplicateSectionError if a section by the specified name
193  already exists.
194  """
195  if self.__sections.has_key(section):
196  raise DuplicateSectionError(section)
197  self.__sections[section] = {}
def defaults (   self)

Definition at line 180 of file ConfigParser.py.

References ConfigParser.__defaults.

181  def defaults(self):
182  return self.__defaults
def get (   self,
  section,
  option,
  raw = 0,
  vars = None 
)
Get an option value for a given section.

All % interpolations are expanded in the return values, based on the
defaults passed into the constructor, unless the optional argument
`raw' is true.  Additional substitutions may be provided using the
`vars' argument, which must be a dictionary whose contents overrides
any pre-existing defaults.

The section DEFAULT is special.

Definition at line 252 of file ConfigParser.py.

References ConfigParser.__sections, ConfigParser.get(), Message.get, and ConfigParser.optionxform().

253  def get(self, section, option, raw=0, vars=None):
254  """Get an option value for a given section.
255 
256  All % interpolations are expanded in the return values, based on the
257  defaults passed into the constructor, unless the optional argument
258  `raw' is true. Additional substitutions may be provided using the
259  `vars' argument, which must be a dictionary whose contents overrides
260  any pre-existing defaults.
261 
262  The section DEFAULT is special.
263  """
264  try:
265  sectdict = self.__sections[section].copy()
266  except KeyError:
267  if section == DEFAULTSECT:
268  sectdict = {}
269  else:
270  raise NoSectionError(section)
271  d = self.__defaults.copy()
272  d.update(sectdict)
273  # Update with the entry specific variables
274  if vars:
275  d.update(vars)
276  option = self.optionxform(option)
277  try:
278  rawval = d[option]
279  except KeyError:
280  raise NoOptionError(option, section)
281 
282  if raw:
283  return rawval
284 
285  # do the string interpolation
286  value = rawval # Make it a pretty variable name
287  depth = 0
288  while depth < 10: # Loop through this until it's done
289  depth = depth + 1
290  if value.find("%(") >= 0:
291  try:
292  value = value % d
293  except KeyError, key:
294  raise InterpolationError(key, option, section, rawval)
295  else:
296  break
297  if value.find("%(") >= 0:
298  raise InterpolationDepthError(option, section, rawval)
299  return value
def getboolean (   self,
  section,
  option 
)

Definition at line 309 of file ConfigParser.py.

References ConfigParser.get(), and Message.get.

310  def getboolean(self, section, option):
311  states = {'1': 1, 'yes': 1, 'true': 1, 'on': 1,
312  '0': 0, 'no': 0, 'false': 0, 'off': 0}
313  v = self.get(section, option)
314  if not states.has_key(v.lower()):
315  raise ValueError, 'Not a boolean: %s' % v
316  return states[v.lower()]
def getfloat (   self,
  section,
  option 
)

Definition at line 306 of file ConfigParser.py.

References ConfigParser.__get().

307  def getfloat(self, section, option):
308  return self.__get(section, string.atof, option)
def getint (   self,
  section,
  option 
)

Definition at line 303 of file ConfigParser.py.

References ConfigParser.__get().

304  def getint(self, section, option):
305  return self.__get(section, string.atoi, option)
def has_option (   self,
  section,
  option 
)
Check for the existence of a given option in a given section.

Definition at line 320 of file ConfigParser.py.

References ConfigParser.__sections, dumbdbm.has_key(), ConfigParser.has_section(), and ConfigParser.optionxform().

321  def has_option(self, section, option):
322  """Check for the existence of a given option in a given section."""
323  if not section or section == "DEFAULT":
324  return self.__defaults.has_key(option)
325  elif not self.has_section(section):
326  return 0
327  else:
328  option = self.optionxform(option)
329  return self.__sections[section].has_key(option)
def has_section (   self,
  section 
)
Indicate whether the named section is present in the configuration.

The DEFAULT section is not acknowledged.

Definition at line 198 of file ConfigParser.py.

References ConfigParser.sections().

199  def has_section(self, section):
200  """Indicate whether the named section is present in the configuration.
201 
202  The DEFAULT section is not acknowledged.
203  """
204  return section in self.sections()
def options (   self,
  section 
)
Return a list of option names for the given section name.

Definition at line 205 of file ConfigParser.py.

References ConfigParser.__defaults, and ConfigParser.__sections.

206  def options(self, section):
207  """Return a list of option names for the given section name."""
208  try:
209  opts = self.__sections[section].copy()
210  except KeyError:
211  raise NoSectionError(section)
212  opts.update(self.__defaults)
213  if opts.has_key('__name__'):
214  del opts['__name__']
215  return opts.keys()
def optionxform (   self,
  optionstr 
)

Definition at line 317 of file ConfigParser.py.

318  def optionxform(self, optionstr):
319  return optionstr.lower()
def read (   self,
  filenames 
)
Read and parse a filename or a list of filenames.

Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
configuration file locations (e.g. current directory, user's
home directory, systemwide directory), and all existing
configuration files in the list will be read.  A single
filename may also be given.

Definition at line 216 of file ConfigParser.py.

References ConfigParser.__read(), and aifc.open().

217  def read(self, filenames):
218  """Read and parse a filename or a list of filenames.
219 
220  Files that cannot be opened are silently ignored; this is
221  designed so that you can specify a list of potential
222  configuration file locations (e.g. current directory, user's
223  home directory, systemwide directory), and all existing
224  configuration files in the list will be read. A single
225  filename may also be given.
226  """
227  if type(filenames) in types.StringTypes:
228  filenames = [filenames]
229  for filename in filenames:
230  try:
231  fp = open(filename)
232  except IOError:
233  continue
234  self.__read(fp, filename)
235  fp.close()
def readfp (   self,
  fp,
  filename = None 
)
Like read() but the argument must be a file-like object.

The `fp' argument must have a `readline' method.  Optional
second argument is the `filename', which if not given, is
taken from fp.name.  If fp has no `name' attribute, `<???>' is
used.

Definition at line 236 of file ConfigParser.py.

References ConfigParser.__read().

237  def readfp(self, fp, filename=None):
238  """Like read() but the argument must be a file-like object.
239 
240  The `fp' argument must have a `readline' method. Optional
241  second argument is the `filename', which if not given, is
242  taken from fp.name. If fp has no `name' attribute, `<???>' is
243  used.
244 
245  """
246  if filename is None:
247  try:
248  filename = fp.name
249  except AttributeError:
250  filename = '<???>'
251  self.__read(fp, filename)
def remove_option (   self,
  section,
  option 
)
Remove an option.

Definition at line 358 of file ConfigParser.py.

References ConfigParser.__defaults, ConfigParser.__sections, and ConfigParser.optionxform().

359  def remove_option(self, section, option):
360  """Remove an option."""
361  if not section or section == "DEFAULT":
362  sectdict = self.__defaults
363  else:
364  try:
365  sectdict = self.__sections[section]
366  except KeyError:
367  raise NoSectionError(section)
368  option = self.optionxform(option)
369  existed = sectdict.has_key(option)
370  if existed:
371  del sectdict[option]
372  return existed
def remove_section (   self,
  section 
)
Remove a file section.

Definition at line 373 of file ConfigParser.py.

References ConfigParser.__sections.

374  def remove_section(self, section):
375  """Remove a file section."""
376  if self.__sections.has_key(section):
377  del self.__sections[section]
378  return 1
379  else:
380  return 0
def sections (   self)
Return a list of section names, excluding [DEFAULT]

Definition at line 183 of file ConfigParser.py.

184  def sections(self):
185  """Return a list of section names, excluding [DEFAULT]"""
186  # self.__sections will never have [DEFAULT] in it
187  return self.__sections.keys()
def set (   self,
  section,
  option,
  value 
)
Set an option.

Definition at line 330 of file ConfigParser.py.

References ConfigParser.__defaults, ConfigParser.__sections, and ConfigParser.optionxform().

331  def set(self, section, option, value):
332  """Set an option."""
333  if not section or section == "DEFAULT":
334  sectdict = self.__defaults
335  else:
336  try:
337  sectdict = self.__sections[section]
338  except KeyError:
339  raise NoSectionError(section)
340  option = self.optionxform(option)
341  sectdict[option] = value
def write (   self,
  fp 
)
Write an .ini-format representation of the configuration state.

Definition at line 342 of file ConfigParser.py.

References ConfigParser.__defaults, ConfigParser.__sections, pydoc.replace(), ConfigParser.sections(), and locale.str().

343  def write(self, fp):
344  """Write an .ini-format representation of the configuration state."""
345  if self.__defaults:
346  fp.write("[DEFAULT]\n")
347  for (key, value) in self.__defaults.items():
348  fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
349  fp.write("\n")
350  for section in self.sections():
351  fp.write("[" + section + "]\n")
352  sectdict = self.__sections[section]
353  for (key, value) in sectdict.items():
354  if key == "__name__":
355  continue
356  fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
357  fp.write("\n")

Field Documentation

tuple OPTCRE
static
Initial value:
1 = re.compile(
2  r'(?P<option>[]\-[\w_.*,(){}]+)' # a lot of stuff found by IvL
3  r'[ \t]*(?P<vi>[:=])[ \t]*' # any number of space/tab,
4  # followed by separator
5  # (either : or =), followed
6  # by any # space/tab
7  r'(?P<value>.*)$' # everything up to eol
8  )

Definition at line 390 of file ConfigParser.py.

tuple SECTCRE
static
Initial value:
1 = re.compile(
2  r'\[' # [
3  r'(?P<header>[^]]+)' # very permissive!
4  r'\]' # ]
5  )

Definition at line 385 of file ConfigParser.py.


The documentation for this class was generated from the following file: