Skip to content

Commit

Permalink
Simplify and fix reading config files
Browse files Browse the repository at this point in the history
  • Loading branch information
takluyver committed Mar 7, 2017
1 parent 08759ee commit 1a45917
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 56 deletions.
2 changes: 2 additions & 0 deletions doc/releasenotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions nsist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
29 changes: 12 additions & 17 deletions nsist/configreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 11 additions & 22 deletions nsist/tests/data_files/valid_config_value_newline.cfg
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
19 changes: 4 additions & 15 deletions nsist/tests/test_configuration_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit 1a45917

Please sign in to comment.