home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_CFGPARSER.PY < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.3 KB  |  142 lines

  1. import ConfigParser
  2. import StringIO
  3.  
  4. def basic(src):
  5.     print
  6.     print "Testing basic accessors..."
  7.     cf = ConfigParser.ConfigParser()
  8.     sio = StringIO.StringIO(src)
  9.     cf.readfp(sio)
  10.     L = cf.sections()
  11.     L.sort()
  12.     print L
  13.     for s in L:
  14.         print "%s: %s" % (s, cf.options(s))
  15.  
  16.     # The use of spaces in the section names serves as a regression test for
  17.     # SourceForge bug #115357.
  18.     # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
  19.     print `cf.get('Foo Bar', 'foo', raw=1)`
  20.     print `cf.get('Spacey Bar', 'foo', raw=1)`
  21.     print `cf.get('Commented Bar', 'foo', raw=1)`
  22.  
  23.     if '__name__' in cf.options("Foo Bar"):
  24.         print '__name__ "option" should not be exposed by the API!'
  25.     else:
  26.         print '__name__ "option" properly hidden by the API.'
  27.  
  28. def interpolation(src):
  29.     print
  30.     print "Testing value interpolation..."
  31.     cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
  32.     sio = StringIO.StringIO(src)
  33.     cf.readfp(sio)
  34.     print `cf.get("Foo", "getname")`
  35.     print `cf.get("Foo", "bar")`
  36.     print `cf.get("Foo", "bar9")`
  37.     print `cf.get("Foo", "bar10")`
  38.     expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
  39.  
  40. def parse_errors():
  41.     print
  42.     print "Testing for parsing errors..."
  43.     expect_parse_error(ConfigParser.ParsingError,
  44.                        """[Foo]\n  extra-spaces: splat\n""")
  45.     expect_parse_error(ConfigParser.ParsingError,
  46.                        """[Foo]\n  extra-spaces= splat\n""")
  47.     expect_parse_error(ConfigParser.ParsingError,
  48.                        """[Foo]\noption-without-value\n""")
  49.     expect_parse_error(ConfigParser.ParsingError,
  50.                        """[Foo]\n:value-without-option-name\n""")
  51.     expect_parse_error(ConfigParser.ParsingError,
  52.                        """[Foo]\n=value-without-option-name\n""")
  53.     expect_parse_error(ConfigParser.MissingSectionHeaderError,
  54.                        """No Section!\n""")
  55.  
  56. def query_errors():
  57.     print
  58.     print "Testing query interface..."
  59.     cf = ConfigParser.ConfigParser()
  60.     print cf.sections()
  61.     print "Has section 'Foo'?", cf.has_section("Foo")
  62.     try:
  63.         cf.options("Foo")
  64.     except ConfigParser.NoSectionError, e:
  65.         print "Caught expected NoSectionError:", e
  66.     else:
  67.         print "Failed to catch expected NoSectionError from options()"
  68.     try:
  69.         cf.set("foo", "bar", "value")
  70.     except ConfigParser.NoSectionError, e:
  71.         print "Caught expected NoSectionError:", e
  72.     else:
  73.         print "Failed to catch expected NoSectionError from set()"
  74.     expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
  75.     cf.add_section("foo")
  76.     expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
  77.  
  78. def weird_errors():
  79.     print
  80.     print "Testing miscellaneous error conditions..."
  81.     cf = ConfigParser.ConfigParser()
  82.     cf.add_section("Foo")
  83.     try:
  84.         cf.add_section("Foo")
  85.     except ConfigParser.DuplicateSectionError, e:
  86.         print "Caught expected DuplicateSectionError:", e
  87.     else:
  88.         print "Failed to catch expected DuplicateSectionError"
  89.  
  90. def expect_get_error(cf, exctype, section, option, raw=0):
  91.     try:
  92.         cf.get(section, option, raw=raw)
  93.     except exctype, e:
  94.         print "Caught expected", exctype.__name__, ":"
  95.         print e
  96.     else:
  97.         print "Failed to catch expected", exctype.__name__
  98.  
  99. def expect_parse_error(exctype, src):
  100.     cf = ConfigParser.ConfigParser()
  101.     sio = StringIO.StringIO(src)
  102.     try:
  103.         cf.readfp(sio)
  104.     except exctype, e:
  105.         print "Caught expected exception:", e
  106.     else:
  107.         print "Failed to catch expected", exctype.__name__
  108.  
  109. basic(r"""
  110. [Foo Bar]
  111. foo=bar
  112. [Spacey Bar]
  113. foo = bar 
  114. [Commented Bar]
  115. foo: bar ; comment
  116. """)
  117. interpolation(r"""
  118. [Foo]
  119. bar=something %(with1)s interpolation (1 step)
  120. bar9=something %(with9)s lots of interpolation (9 steps)
  121. bar10=something %(with10)s lots of interpolation (10 steps)
  122. bar11=something %(with11)s lots of interpolation (11 steps)
  123. with11=%(with10)s
  124. with10=%(with9)s
  125. with9=%(with8)s
  126. with8=%(with7)s
  127. with7=%(with6)s
  128. with6=%(with5)s
  129. with5=%(with4)s
  130. with4=%(with3)s
  131. with3=%(with2)s
  132. with2=%(with1)s
  133. with1=with
  134.  
  135. [Mutual Recursion]
  136. foo=%(bar)s
  137. bar=%(foo)s
  138. """)
  139. parse_errors()
  140. query_errors()
  141. weird_errors()
  142.