Example #1
0
    def test_compat(self):
        s = dedent("""
            [sec1]
            a=1


            [sec2]

            b=2

            c=3


            """)
        cfg = ConfigParser()
        cfg.readfp(StringIO(s))
        tidy(cfg)
        self.assertEqual(str(cfg.data), dedent("""\
            [sec1]
            a=1

            [sec2]
            b=2

            c=3
            """))
Example #2
0
 def test_last_line(self):
     self.cfg.newsection.newproperty = "Ok"
     self.assertEqual(str(self.cfg), dedent("""\
         [newsection]
         newproperty = Ok"""))
     tidy(self.cfg)
     self.assertEqual(str(self.cfg), dedent("""\
         [newsection]
         newproperty = Ok
         """))
Example #3
0
    def test_first_line(self):
        s = dedent("""\
 
                [newsection]
                newproperty = Ok
                """)
        self.cfg._readfp(StringIO(s))
        tidy(self.cfg)
        self.assertEqual(str(self.cfg), dedent("""\
                [newsection]
                newproperty = Ok
                """))
Example #4
0
 def test_last_line(self):
     self.cfg.newsection.newproperty = "Ok"
     self.assertEqual(
         str(self.cfg),
         dedent("""\
         [newsection]
         newproperty = Ok"""))
     tidy(self.cfg)
     self.assertEqual(
         str(self.cfg),
         dedent("""\
         [newsection]
         newproperty = Ok
         """))
Example #5
0
    def save(self):
        """Save the current state of the INI file."""

        # Tidy it so when sections get added and removed and whatnot it looks
        # generally decent (no multiple blank lines, EOL at EOF)
        iniparse.tidy(self.ini)

        # Make sure the directory exists
        inidir = dirname(self.path_abs())
        if not exists(inidir):
            makedirs(inidir)

        # Now write it
        iniw = open(self.path_abs(), 'w')
        iniw.write(unicode(self.ini))
        iniw.close()
Example #6
0
 def test_fuzz(self):
     random.seed(42)
     try:
         num_iter = int(os.environ['INIPARSE_FUZZ_ITERATIONS'])
     except (KeyError, ValueError):
         num_iter = 100
     for fuzz_iter in range(num_iter):
         try:
             # parse random file with errors disabled
             s = random_ini_file()
             c = ini.INIConfig(parse_exc=False)
             c._readfp(StringIO(s))
             # check that file is preserved, except for
             # commenting out erroneous lines
             l1 = s.split('\n')
             l2 = str(c).split('\n')
             self.assertEqual(len(l1), len(l2))
             good_lines = []
             for i in range(len(l1)):
                 try:
                     self.assertEqual(l1[i], l2[i])
                     good_lines.append(l1[i])
                 except AssertionError:
                     self.assertEqual('#'+l1[i], l2[i])
             # parse the good subset of the file
             # using ConfigParser
             s = '\n'.join(good_lines)
             cc = compat.RawConfigParser()
             cc.readfp(StringIO(s))
             cc_py = configparser.RawConfigParser()
             cc_py.read_file(StringIO(s))
             # compare the two configparsers
             self.assertEqualConfig(cc_py, cc)
             # check that tidy does not change semantics
             tidy(cc)
             cc_tidy = configparser.RawConfigParser()
             cc_tidy.readfp(StringIO(str(cc.data)))
             self.assertEqualConfig(cc_py, cc_tidy)
         except AssertionError:
             fname = 'fuzz-test-iter-%d.ini' % fuzz_iter
             print('Fuzz test failed at iteration', fuzz_iter)
             print('Writing out failing INI file as', fname)
             f = open(fname, 'w')
             f.write(s)
             f.close()
             raise
 def test_fuzz(self):
     random.seed(42)
     try:
         num_iter = int(os.environ['INIPARSE_FUZZ_ITERATIONS'])
     except (KeyError, ValueError):
         num_iter = 100
     for fuzz_iter in range(num_iter):
         try:
             # parse random file with errors disabled
             s = random_ini_file()
             c = ini.INIConfig(parse_exc=False)
             c._readfp(StringIO(s))
             # check that file is preserved, except for
             # commenting out erroneous lines
             l1 = s.split('\n')
             l2 = str(c).split('\n')
             self.assertEqual(len(l1), len(l2))
             good_lines = []
             for i in range(len(l1)):
                 try:
                     self.assertEqual(l1[i], l2[i])
                     good_lines.append(l1[i])
                 except AssertionError:
                     self.assertEqual('#'+l1[i], l2[i])
             # parse the good subset of the file
             # using ConfigParser
             s = '\n'.join(good_lines)
             cc = compat.RawConfigParser()
             cc.readfp(StringIO(s))
             cc_py = configparser.RawConfigParser()
             cc_py.readfp(StringIO(s))
             # compare the two configparsers
             self.assertEqualConfig(cc_py, cc)
             # check that tidy does not change semantics
             tidy(cc)
             cc_tidy = configparser.RawConfigParser()
             cc_tidy.readfp(StringIO(str(cc.data)))
             self.assertEqualConfig(cc_py, cc_tidy)
         except AssertionError:
             fname = 'fuzz-test-iter-%d.ini' % fuzz_iter
             print('Fuzz test failed at iteration', fuzz_iter)
             print('Writing out failing INI file as', fname)
             f = open(fname, 'w')
             f.write(s)
             f.close()
             raise
Example #8
0
if opt.instance_var:
    buildout.set('buildout', 'var-dir', opt.instance_var)
if opt.backup_dir:
    buildout.set('buildout', 'backups-dir', opt.backup_dir)

# remove unneeded sections
if opt.itype == 'standalone':
    buildout.remove_section('zeoserver')
else:
    buildout.remove_section('instance')

# Insert variable number of zeo client specs. This doesn't fit the iniparse
# model because the clients need to be inserted at particular
# points without fouling comments or section order.
iniparse.tidy(buildout)
buildout = str(buildout.data)
if opt.itype == 'standalone':
    # remove extra clients marker
    buildout = buildout.replace(ADD_CLIENTS_MARKER, '')
else:
    client_parts = ''
    client_addresses = ''
    for client in range(1, CLIENTS + 1):
        options = {
            'client_num': client,
            'client_port': BASE_ADDRESS + client - 1,
        }
        client_parts = "%s%s" % (client_parts, CLIENT_TEMPLATE % options)
    buildout = buildout.replace(ADD_CLIENTS_MARKER, client_parts)
+from six import StringIO
+from six.moves import configparser
 from iniparse import compat, ini, tidy
 
 # TODO:
@@ -96,24 +97,25 @@ class test_fuzz(unittest.TestCase):
                 s = '\n'.join(good_lines)
                 cc = compat.RawConfigParser()
                 cc.readfp(StringIO(s))
-                cc_py = ConfigParser.RawConfigParser()
+                cc_py = configparser.RawConfigParser()
                 cc_py.readfp(StringIO(s))
                 # compare the two configparsers
                 self.assertEqualConfig(cc_py, cc)
                 # check that tidy does not change semantics
                 tidy(cc)
-                cc_tidy = ConfigParser.RawConfigParser()
+                cc_tidy = configparser.RawConfigParser()
                 cc_tidy.readfp(StringIO(str(cc.data)))
                 self.assertEqualConfig(cc_py, cc_tidy)
             except AssertionError:
                 fname = 'fuzz-test-iter-%d.ini' % fuzz_iter
-                print 'Fuzz test failed at iteration', fuzz_iter
-                print 'Writing out failing INI file as', fname
+                print('Fuzz test failed at iteration', fuzz_iter)
+                print('Writing out failing INI file as', fname)
                 f = open(fname, 'w')
                 f.write(s)
                 f.close()
                 raise
 
# remove unneeded sections
if opt.itype == 'standalone':
    buildout.remove_section('zeoserver')
else:
    buildout.remove_section('instance')

# Windows cleanup
if os.name == 'nt':
    buildout.remove_option('buildout', 'extensions')
    eggs = buildout.get('buildout', 'eggs')
    buildout.set('buildout', 'eggs', eggs + '\npywin32\nnt_svcutils')

# Insert variable number of zeo client specs. This doesn't fit the iniparse
# model because the clients need to be inserted at particular
# points without fouling comments or section order.
iniparse.tidy(buildout)
buildout = str(buildout.data)
if opt.itype == 'standalone':
    # remove extra clients marker
    buildout = buildout.replace(ADD_CLIENTS_MARKER, '')
else:
    client_parts = ''
    client_addresses = ''
    for client in range(1, CLIENTS + 1):
        options = {
            'client_num': client,
            'client_port': BASE_ADDRESS + client - 1,
        }
        client_parts = "%s%s" % (client_parts, CLIENT_TEMPLATE % options)
    buildout = buildout.replace(ADD_CLIENTS_MARKER, client_parts)
Example #11
0
def save():
    """Save the user's settings."""
    tidy(settings)
    with open(settings_path('settings.ini'), 'w') as outfile:
        outfile.write(unicode(settings))
Example #12
0
    def test_remove_newlines(self):
        s = dedent("""\


                [newsection]
                newproperty = Ok
               



                [newsection2]

                newproperty2 = Ok


                newproperty3 = yup
               
               
                [newsection4]


                # remove blank lines, but leave continuation lines unharmed

                a = 1

                b = l1
                 l2

                
                # asdf
                 l5

                c = 2
               
               
                """)
        self.cfg._readfp(StringIO(s))
        tidy(self.cfg)
        self.assertEqual(
            str(self.cfg),
            dedent("""\
                [newsection]
                newproperty = Ok

                [newsection2]
                newproperty2 = Ok

                newproperty3 = yup

                [newsection4]
                # remove blank lines, but leave continuation lines unharmed

                a = 1

                b = l1
                 l2


                # asdf
                 l5

                c = 2
                """))
Example #13
0
 def test_empty_file(self):
     self.assertEqual(str(self.cfg), '')
     tidy(self.cfg)
     self.assertEqual(str(self.cfg), '')
Example #14
0
    def test_remove_newlines(self):
        s = dedent("""\


                [newsection]
                newproperty = Ok
               



                [newsection2]

                newproperty2 = Ok


                newproperty3 = yup
               
               
                [newsection4]


                # remove blank lines, but leave continuation lines unharmed

                a = 1

                b = l1
                 l2

                
                # asdf
                 l5

                c = 2
               
               
                """)
        self.cfg._readfp(StringIO(s))
        tidy(self.cfg)
        self.assertEqual(str(self.cfg), dedent("""\
                [newsection]
                newproperty = Ok

                [newsection2]
                newproperty2 = Ok

                newproperty3 = yup

                [newsection4]
                # remove blank lines, but leave continuation lines unharmed

                a = 1

                b = l1
                 l2


                # asdf
                 l5

                c = 2
                """))
Example #15
0
 def test_empty_file(self):
     self.assertEqual(str(self.cfg), '')
     tidy(self.cfg)
     self.assertEqual(str(self.cfg), '')