Bläddra i källkod

Simplify and fix reading config files

Thomas Kluyver 8 år sedan
förälder
incheckning
1a45917c55

+ 2 - 0
doc/releasenotes.rst

@@ -10,6 +10,8 @@ Version 1.10
 * The directory containing ``python.exe`` is now added to the ``%PATH%``
   environment variable when your application runs. This fixes a DLL loading
   issue for PyQt5 if you use bundled Python.
+* Lists in the config file, such as ``packages`` and ``pypi_wheels`` can now
+  begin on the line after the key.
 * When installing a 64-bit application, the uninstall registry keys are now
   added to the 64-bit view of the registry.
 * Fixed an error when using wheels which install files into the same package,

+ 0 - 2
nsist/__init__.py

@@ -525,8 +525,6 @@ def main(argv=None):
     from . import configreader
     try:
         cfg = configreader.read_and_validate(config_file)
-        shortcuts = configreader.read_shortcuts_config(cfg)
-        commands = configreader.read_commands_config(cfg)
     except configreader.InvalidConfig as e:
         logger.error('Error parsing configuration file:')
         logger.error(str(e))

+ 12 - 17
nsist/configreader.py

@@ -209,29 +209,24 @@ def get_installer_builder_args(config):
                     DEFAULT_BUILD_DIR,
                     DEFAULT_ICON,
                     DEFAULT_PY_VERSION)
-    def get_boolean(s):
-        if s.lower() in ('1', 'yes', 'true', 'on'):
-            return True
-        if s.lower() in ('0', 'no', 'false', 'off'):
-            return False
-        raise ValueError('ValueError: Not a boolean: {}'.format(s))
 
     appcfg = config['Application']
     args = {}
-    args['appname'] = appcfg['name'].strip()
-    args['version'] = appcfg['version'].strip()
-    args['publisher'] = appcfg['publisher'].strip() if 'publisher' in appcfg else None
-    args['icon'] = appcfg.get('icon', DEFAULT_ICON).strip()
+    args['appname'] = appcfg['name']
+    args['version'] = appcfg['version']
+    args['shortcuts'] = read_shortcuts_config(config)
+    args['commands'] = read_commands_config(config)
+    args['publisher'] = appcfg.get('publisher', None)
+    args['icon'] = appcfg.get('icon', DEFAULT_ICON)
     args['packages'] = config.get('Include', 'packages', fallback='').strip().splitlines()
     args['pypi_wheel_reqs'] = config.get('Include', 'pypi_wheels', fallback='').strip().splitlines()
     args['extra_files'] = read_extra_files(config)
-    args['py_version'] = config.get('Python', 'version', fallback=DEFAULT_PY_VERSION).strip()
+    args['py_version'] = config.get('Python', 'version', fallback=DEFAULT_PY_VERSION)
     args['py_bitness'] = config.getint('Python', 'bitness', fallback=DEFAULT_BITNESS)
-    args['py_format'] = config.get('Python', 'format').strip() if 'format' in config['Python'] else None
-    inc_msvcrt = config.get('Python', 'include_msvcrt', fallback='True')
-    args['inc_msvcrt'] = get_boolean(inc_msvcrt.strip())
-    args['build_dir'] = config.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR).strip()
-    args['installer_name'] = config.get('Build', 'installer_name') if 'installer_name' in config['Build'] else None
-    args['nsi_template'] = config.get('Build', 'nsi_template').strip() if 'nsi_template' in config['Build'] else None
+    args['py_format'] = config.get('Python', 'format', fallback=None)
+    args['inc_msvcrt'] = config.getboolean('Python', 'include_msvcrt', fallback=True)
+    args['build_dir'] = config.get('Build', 'directory', fallback=DEFAULT_BUILD_DIR)
+    args['installer_name'] = config.get('Build', 'installer_name', fallback=None)
+    args['nsi_template'] = config.get('Build', 'nsi_template', fallback=None)
     args['exclude'] = config.get('Include', 'exclude', fallback='').strip().splitlines()
     return args

+ 11 - 22
nsist/tests/data_files/valid_config_value_newline.cfg

@@ -1,30 +1,19 @@
 [Application]
-name=
-    My App
-version=
-    1.0
-publisher=
-    Test
-entry_point=
-    myapp:main
-icon=
-    myapp.ico
+name = My App
+version = 1.0
+publisher = Test
+entry_point = myapp:main
+icon = myapp.ico
 
 [Python]
-version=
-    3.6.0
-bitness=
-    64
-format=
-    bundled
-include_msvcrt =
-    True
+version = 3.6.0
+bitness = 64
+format = bundled
+include_msvcrt = True
 
 [Build]
-directory=
-    build/
-nsi_template=
-    template.nsi
+directory = build/
+nsi_template= template.nsi
 
 [Include]
 packages =

+ 4 - 15
nsist/tests/test_configuration_validator.py

@@ -12,6 +12,8 @@ def test_valid_config():
     configfile = os.path.join(DATA_FILES, 'valid_config.cfg')
     config = configreader.read_and_validate(configfile)
     assert config.has_section('Application')
+    args = configreader.get_installer_builder_args(config)
+    assert args['py_version'] == '3.4.0'
 
 def test_valid_config_with_shortcut():
     configfile = os.path.join(DATA_FILES, 'valid_config_with_shortcut.cfg')
@@ -24,21 +26,6 @@ def test_valid_config_with_values_starting_on_new_line():
     assert len(config.sections()) == len(sections)
     for section in sections:
         assert section in config
-        assert config.has_section(section)
-
-    assert config.get('Application', 'name') == '\nMy App'
-    assert config.get('Application', 'version') == '\n1.0'
-    assert config.get('Application', 'publisher') == '\nTest'
-    assert config.get('Application', 'entry_point') == '\nmyapp:main'
-    assert config.get('Application', 'icon') == '\nmyapp.ico'
-
-    assert config.get('Python', 'version') == '\n3.6.0'
-    assert config.get('Python', 'bitness') == '\n64'
-    assert config.get('Python', 'format') == '\nbundled'
-    assert config.get('Python', 'include_msvcrt') == '\nTrue'
-
-    assert config.get('Build', 'directory') == '\nbuild/'
-    assert config.get('Build', 'nsi_template') == '\ntemplate.nsi'
 
     assert config.get('Include', 'packages') == '\nrequests\nbs4'
     assert config.get('Include', 'pypi_wheels') == '\nhtml5lib'
@@ -48,6 +35,8 @@ def test_valid_config_with_values_starting_on_new_line():
     args = configreader.get_installer_builder_args(config)
     assert args['appname'] == 'My App'
     assert args['version'] == '1.0'
+    assert args['shortcuts']['My App']['entry_point'] == 'myapp:main'
+    assert args['commands'] == {}
     assert args['publisher'] == 'Test'
     # assert args['entry_point'] == '\nmyapp:main'
     assert args['icon'] == 'myapp.ico'