From 7661c23ef84f82e9bead056222da161c00938ffb Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 16 Feb 2012 14:13:01 -0500 Subject: [PATCH] Make config file parsing more modular. --- update_copyright/project.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/update_copyright/project.py b/update_copyright/project.py index 4684d38..2402700 100644 --- a/update_copyright/project.py +++ b/update_copyright/project.py @@ -91,14 +91,23 @@ class Project (object): self._copyright_tag = u'-xyz-COPY' + u'-RIGHT-zyx-' def load_config(self, stream): - p = _configparser.RawConfigParser() - p.readfp(stream) + parser = _configparser.RawConfigParser() + parser.readfp(stream) + for section in parser.sections(): + try: + loader = getattr(self, '_load_{}_conf'.format(section)) + except AttributeError, e: + _LOG.error('invalid {} section'.format(section)) + raise + loader(parser=parser) + + def _load_project_conf(self, parser): try: - self._name = p.get('project', 'name') + self._name = parser.get('project', 'name') except _configparser.NoOptionError: pass try: - vcs = p.get('project', 'vcs') + vcs = parser.get('project', 'vcs') except _configparser.NoOptionError: pass else: @@ -114,30 +123,35 @@ class Project (object): self._vcs = _MercurialBackend() else: raise NotImplementedError('vcs: {}'.format(vcs)) + + def _load_copyright_conf(self, parser): try: - self._copyright = p.get('copyright', 'long').splitlines() + self._copyright = parser.get('copyright', 'long').splitlines() except _configparser.NoOptionError: pass try: - self._short_copyright = p.get('copyright', 'short').splitlines() + self._short_copyright = parser.get( + 'copyright', 'short').splitlines() except _configparser.NoOptionError: pass + + def _load_files_conf(self, parser): try: - self.with_authors = p.get('files', 'authors') + self.with_authors = parser.get('files', 'authors') except _configparser.NoOptionError: pass try: - self.with_files = p.get('files', 'files') + self.with_files = parser.get('files', 'files') except _configparser.NoOptionError: pass try: - ignored = p.get('files', 'ignored') + ignored = parser.get('files', 'ignored') except _configparser.NoOptionError: pass else: self._ignored_paths = [pth.strip() for pth in ignored.split(',')] try: - self._pyfile = p.get('files', 'pyfile') + self._pyfile = parser.get('files', 'pyfile') except _configparser.NoOptionError: pass -- 2.26.2