Przeglądaj źródła

Move read_shortcut_config function into configreader module

Thomas Kluyver 11 lat temu
rodzic
commit
9e813fb50a
2 zmienionych plików z 37 dodań i 27 usunięć
  1. 2 27
      nsist/__init__.py
  2. 35 0
      nsist/configreader.py

+ 2 - 27
nsist/__init__.py

@@ -244,32 +244,6 @@ class InstallerBuilder(object):
         if not exitcode:
         if not exitcode:
             logger.info('Installer written to %s', pjoin(self.build_dir, self.installer_name))
             logger.info('Installer written to %s', pjoin(self.build_dir, self.installer_name))
 
 
-def read_shortcuts_config(cfg):
-    
-    shortcuts = {}
-    def _check_shortcut(name, sc, section):
-        if ('entry_point' not in sc) and ('script' not in sc):
-            raise ValueError('Section {} has neither entry_point nor script.'.format(section))
-        elif ('entry_point' in sc) and ('script' in sc):
-            raise ValueError('Section {} has both entry_point and script.'.format(section))
-            
-        # Copy to a regular dict so it can hold a boolean value
-        sc2 = dict(sc)
-        if 'icon' not in sc2:
-            sc2['icon'] = DEFAULT_ICON        
-        sc2['console'] = sc.getboolean('console', fallback=False)
-        shortcuts[name] = sc2
-    
-    for section in cfg.sections():
-        if section.startswith("Shortcut "):
-            name = section[len("Shortcut "):]
-            _check_shortcut(name, cfg[section], section)
-    
-    appcfg = cfg['Application']
-    _check_shortcut(appcfg['name'], appcfg, 'Application')
-    
-    return shortcuts
-
 def main(argv=None):
 def main(argv=None):
     """Make an installer from the command line.
     """Make an installer from the command line.
     
     
@@ -291,6 +265,7 @@ def main(argv=None):
     try:
     try:
         from . import configreader
         from . import configreader
         cfg = configreader.read_and_validate(config_file)
         cfg = configreader.read_and_validate(config_file)
+        shortcuts = configreader.read_shortcuts_config(cfg)
     except configreader.InvalidConfig as e:
     except configreader.InvalidConfig as e:
         logger.error('Error parsing configuration file:')
         logger.error('Error parsing configuration file:')
         logger.error(str(e))
         logger.error(str(e))
@@ -300,7 +275,7 @@ def main(argv=None):
         appname = appcfg['name'],
         appname = appcfg['name'],
         version = appcfg['version'],
         version = appcfg['version'],
         icon = appcfg.get('icon', DEFAULT_ICON),
         icon = appcfg.get('icon', DEFAULT_ICON),
-        shortcuts = read_shortcuts_config(cfg),
+        shortcuts = shortcuts,
         packages = cfg.get('Include', 'packages', fallback='').splitlines(),
         packages = cfg.get('Include', 'packages', fallback='').splitlines(),
         extra_files = cfg.get('Include', 'files', fallback='').splitlines(),
         extra_files = cfg.get('Include', 'files', fallback='').splitlines(),
         py_version = cfg.get('Python', 'version', fallback=DEFAULT_PY_VERSION),
         py_version = cfg.get('Python', 'version', fallback=DEFAULT_PY_VERSION),

+ 35 - 0
nsist/configreader.py

@@ -98,3 +98,38 @@ def read_and_validate(config_file):
                        ', '.join(['"%s"' % n for n in valid_section_names]))
                        ', '.join(['"%s"' % n for n in valid_section_names]))
             raise InvalidConfig(err_msg)    
             raise InvalidConfig(err_msg)    
     return config
     return config
+
+def read_shortcuts_config(cfg):
+    """Read and verify the shortcut definitions from the config file.
+    
+    There is one shortcut per 'Shortcut <name>' section, and one for the
+    Application section.
+    
+    Returns a list of dictionaries with the fields from the shortcut sections.
+    The optional 'icon' and 'console' fields will be filled with their
+    default values if not supplied.
+    """
+    shortcuts = {}
+    def _check_shortcut(name, sc, section):
+        if ('entry_point' not in sc) and ('script' not in sc):
+            raise InvalidConfig('Section {} has neither entry_point nor script.'.format(section))
+        elif ('entry_point' in sc) and ('script' in sc):
+            raise InvalidConfig('Section {} has both entry_point and script.'.format(section))
+
+        # Copy to a regular dict so it can hold a boolean value
+        sc2 = dict(sc)
+        if 'icon' not in sc2:
+            from . import DEFAULT_ICON
+            sc2['icon'] = DEFAULT_ICON
+        sc2['console'] = sc.getboolean('console', fallback=False)
+        shortcuts[name] = sc2
+
+    for section in cfg.sections():
+        if section.startswith("Shortcut "):
+            name = section[len("Shortcut "):]
+            _check_shortcut(name, cfg[section], section)
+
+    appcfg = cfg['Application']
+    _check_shortcut(appcfg['name'], appcfg, 'Application')
+
+    return shortcuts