project: Convert parser access to use the mapping protocol
authorW. Trevor King <wking@tremily.us>
Sat, 28 Mar 2015 19:44:11 +0000 (12:44 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 28 Mar 2015 19:44:11 +0000 (12:44 -0700)
Use the more dict-like access [1] instead of the API that's specific
to ConfigParser.  This is likely to be more familiar to future
developers, and reduces the cognitive load of dealing with another
API.

The only place I couldn't drop the old API was in load_config, where I
kept parser.sections() [2] because using parser.keys() was giving me:

  AttributeError: 'Project' object has no attribute '_load_DEFAULT_conf'

The:

  try:
      section = parser['...']
  except KeyError:
      section = {}

blocks are also more awkward than a dicts:

  section = parser.get('...', {})

but we can't use that API because it's clobbered by
ConfigParser.get(section, option, ...) [3].  Maybe someday they'll
support:

  section = parser.get('...', default={})

(or with 'fallback' instead of 'default').

[1]: https://docs.python.org/3/library/configparser.html#mapping-protocol-access
[2]: https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.sections
[3]: https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.get

update_copyright/project.py

index 429a34f2d3e12352f26200a1c20399e78a87a7aa..e04364fcc05c2720504f5e322744a876d3e47867 100644 (file)
@@ -68,14 +68,12 @@ class Project (object):
 
     def _load_project_conf(self, parser):
         try:
-            self._name = parser.get('project', 'name')
-        except _configparser.NoOptionError:
-            pass
-        try:
-            vcs = parser.get('project', 'vcs')
-        except _configparser.NoOptionError:
-            pass
-        else:
+            project = parser['project']
+        except KeyError:
+            project = {}
+        self._name = project.get('name')
+        vcs = project.get('vcs')
+        if vcs:
             kwargs = {
                 'root': self._root,
                 'author_hacks': self._author_hacks,
@@ -93,45 +91,35 @@ class Project (object):
 
     def _load_copyright_conf(self, parser):
         try:
-            self._copyright = self._split_paragraphs(
-                parser.get('copyright', 'long'))
-        except _configparser.NoOptionError:
-            pass
-        try:
-            self._short_copyright = self._split_paragraphs(
-                parser.get('copyright', 'short'))
-        except _configparser.NoOptionError:
-            pass
+            copyright = parser['copyright']
+        except KeyError:
+            copyright = {}
+        if 'long' in copyright:
+            self._copyright = self._split_paragraphs(copyright['long'])
+        if 'short' in copyright:
+            self._short_copyright = self._split_paragraphs(copyright['short'])
 
     def _split_paragraphs(self, text):
         return [p.strip() for p in text.split('\n\n')]
 
     def _load_files_conf(self, parser):
-        try:
-            self.with_authors = parser.getboolean('files', 'authors')
-        except _configparser.NoOptionError:
-            pass
-        try:
-            self.with_files = parser.getboolean('files', 'files')
-        except _configparser.NoOptionError:
-            pass
-        try:
-            ignored = parser.get('files', 'ignored')
-        except _configparser.NoOptionError:
-            pass
-        else:
+        files = parser['files']
+        self.with_authors = files.getboolean('authors')
+        self.with_files = files.getboolean('files')
+        ignored = files.get('ignored')
+        if ignored:
             self._ignored_paths = [pth.strip() for pth in ignored.split('|')]
-        try:
-            pyfile = parser.get('files', 'pyfile')
-        except _configparser.NoOptionError:
-            pass
-        else:
+        pyfile = files.get('pyfile')
+        if pyfile:
             self._pyfile = _os_path.join(self._root, pyfile)
 
     def _load_author_hacks_conf(self, parser):
+        try:
+            section = parser['author-hacks']
+        except KeyError:
+            section = {}
         author_hacks = {}
-        for path in parser.options('author-hacks'):
-            authors = parser.get('author-hacks', path)
+        for path, authors in section.items():
             author_hacks[tuple(path.split('/'))] = set(
                 a.strip() for a in authors.split('|'))
         self._author_hacks = author_hacks
@@ -139,18 +127,24 @@ class Project (object):
             self._vcs._author_hacks = self._author_hacks
 
     def _load_year_hacks_conf(self, parser):
+        try:
+            section = parser['year-hacks']
+        except KeyError:
+            section = {}
         year_hacks = {}
-        for path in parser.options('year-hacks'):
-            year = parser.get('year-hacks', path)
+        for path, year in section.items():
             year_hacks[tuple(path.split('/'))] = int(year)
         self._year_hacks = year_hacks
         if self._vcs is not None:
             self._vcs._year_hacks = self._year_hacks
 
     def _load_aliases_conf(self, parser):
+        try:
+            section = parser['aliases']
+        except KeyError:
+            section = {}
         aliases = {}
-        for author in parser.options('aliases'):
-            _aliases = parser.get('aliases', author)
+        for author, _aliases in section.items():
             aliases[author] = set(
                 a.strip() for a in _aliases.split('|'))
         self._aliases = aliases