Browse Source

simplified config reader, moved import of configreader into main

twall 11 years ago
parent
commit
3228d9c1de
3 changed files with 63 additions and 76 deletions
  1. 1 1
      nsist/__init__.py
  2. 38 75
      nsist/configreader.py
  3. 24 0
      nsist/tests/data_files/valid_config_with_shortcut.cfg

+ 1 - 1
nsist/__init__.py

@@ -23,7 +23,6 @@ else:
 
 from .copymodules import copy_modules
 from .nsiswriter import NSISFileWriter
-from . import configreader
 
 pjoin = os.path.join
 logger = logging.getLogger(__name__)
@@ -271,6 +270,7 @@ def main(argv=None):
         os.chdir(dirname)
     
     try:
+        from . import configreader
         cfg = configreader.read_and_validate(config_file)
     except configreader.InvalidConfig as e:
         logger.error('Error parsing configuration file:')

+ 38 - 75
nsist/configreader.py

@@ -3,20 +3,12 @@
 import configparser
 
 class SectionValidator(object):
-    def __init__(self, name, subsections, identification_func=None):
+    def __init__(self, subsections):
         """
-        name
-            a descriptive name of the section
         subsections
             list of tuples containing the names and whether the
             subsection is mandatory
-        identy_func
-            returns True if this is the correct Validator for the
-            section name. If it is left out the descriptive name will be
-            used to identify the section
         """
-        self.name = name
-        self.identification_func = identification_func
         self.subsections = subsections
     
     def check(self, config, section_name):
@@ -26,20 +18,8 @@ class SectionValidator(object):
         
         raises InvalidConfig if something inside the section is wrong
         """
-        was_identified = False        
-        if self._identify(section_name):
-            was_identified = True
-            self._check_mandatory_fields(section_name, config[section_name])
-            self._check_invalid_subsections(section_name, config[section_name])
-        return was_identified
-
-    def _identify(self, section_name):
-        # if no identification function is set, just compare the section
-        # name with the name of the validator
-        if self.identification_func is None:
-            return section_name == self.name
-        else:
-            return self.identification_func(section_name)
+        self._check_mandatory_fields(section_name, config[section_name])
+        self._check_invalid_subsections(section_name, config[section_name])
 
     def _check_mandatory_fields(self, section_name, subsection):
         for subsection_name, mandatory in self.subsections:
@@ -54,7 +34,6 @@ class SectionValidator(object):
                     raise InvalidConfig(err_msg)
     
     def _check_invalid_subsections(self, section_name, section):
-        # check subsection names
         for subsection in section:
             subsection_name = str(subsection)
             valid_subsection_names = [s[0] for s in self.subsections]
@@ -70,46 +49,35 @@ class SectionValidator(object):
 # contains all configuration sections and subsections
 # the subsections are a tuple with their name and a boolean, which
 # tells us whether the option is mandatory
-CONFIG_VALIDATORS = [
-    SectionValidator('Application',
-        [
-            ('name', True),
-            ('version', True),
-            ('entry_point', True),
-            ('script', False),
-            ('icon', False),
-            ('console', False),
-        ],
-    ),
-    SectionValidator('Build',
-        [
-            ('directory', False),
-            ('installer_name', False),
-            ('nsi_template', False),
-        ]
-    ),
-    SectionValidator('Include',
-        [
-            ('packages', False),
-            ('files', False),
-        ]
-    ),
-    SectionValidator('Python',
-        [
-            ('version', True),
-            ('bitness', False),
-        ]
-    ),
-    SectionValidator('Shortcut ... ',
-        [
-            ('entry_point', True),
-            ('script', False),
-            ('icon', False),
-            ('console', False),
-        ],
-        lambda s: s.startswith('Shortcut ')
-    ),
-]
+CONFIG_VALIDATORS = {
+    'Application': SectionValidator([
+        ('name', True),
+        ('version', True),
+        ('entry_point', True),
+        ('script', False),
+        ('icon', False),
+        ('console', False),
+    ]),
+    'Build': SectionValidator([
+        ('directory', False),
+        ('installer_name', False),
+        ('nsi_template', False),
+    ]),
+    'Include': SectionValidator([
+        ('packages', False),
+        ('files', False),
+    ]),
+    'Python': SectionValidator([
+        ('version', True),
+        ('bitness', False),
+    ]),
+    'Shortcut': SectionValidator([
+        ('entry_point', True),
+        ('script', False),
+        ('icon', False),
+        ('console', False),
+    ]),
+}
 
 class InvalidConfig(ValueError):
     pass
@@ -118,17 +86,12 @@ def read_and_validate(config_file):
     config = configparser.ConfigParser()
     config.read(config_file)
     for section in config.sections():
-        # each section *must* be identified by at least one validator
-        # otherwise it is an invalid section (e.g. has an invalid name)
-        was_identified = False
-        for validator in CONFIG_VALIDATORS:
-            was_identified = validator.check(config, section)
-            # if we already found the correct validator and it did not
-            # raise InvalidConfig, we can skip the other validators
-            if was_identified:
-                break
-        if not was_identified:
-            valid_section_names = [v.name for v in CONFIG_VALIDATORS]
+        if section in CONFIG_VALIDATORS:
+            CONFIG_VALIDATORS[section].check(config, section)
+        elif section.startswith('Shortcut '):
+            CONFIG_VALIDATORS['Shortcut'].check(config, section)
+        else:
+            valid_section_names = CONFIG_VALIDATORS.keys()
             err_msg = ("{0} is not a valid section header. Must "
                        "be one of these: {1}").format(
                        section,

+ 24 - 0
nsist/tests/data_files/valid_config_with_shortcut.cfg

@@ -0,0 +1,24 @@
+[Application]
+name=My App
+version=1.0
+# How to launch the app - this calls the 'main' function from the 'myapp' package:
+entry_point=myapp:main
+icon=myapp.ico
+
+[Python]
+version=3.4.0
+
+[Include]
+# Importable packages that your application requires, one per line
+packages = requests
+     bs4
+     html5lib
+
+# Other files and folders that should be installed
+files = LICENSE
+    data_files/
+    
+[Shortcut IPython Notebook]
+entry_point=IPython.html.notebookapp:launch_new_instance
+icon=scripts/ipython_nb.ico
+console=true