Allow '-' characters in section names.
[update-copyright.git] / update_copyright / project.py
index e9de6cc17fe958a656b307cf11c569d7787150bc..ac93c2ff069b20df7ce8c8823b73f844813418db 100644 (file)
@@ -84,54 +84,75 @@ class Project (object):
         self.with_files = False
         self._ignored_paths = None
         self._pyfile = None
+        self._encoding = None
+        self._width = 79
 
         # unlikely to occur in the wild :p
-        self._copyright_tag = '-xyz-COPY' + '-RIGHT-zyx-'
+        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():
+            clean_section = section.replace('-', '_')
+            try:
+                loader = getattr(self, '_load_{}_conf'.format(clean_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:
             if vcs == 'Git':
                 self._vcs = _GitBackend()
             elif vcs == 'Bazaar':
+                if _BazaarBackend is None:
+                    raise _bazaar_import_error
                 self._vcs = _BazaarBackend()
             elif vcs == 'Mercurial':
+                if _MercurialBackend is None:
+                    raise _mercurial_import_error
                 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
 
@@ -146,20 +167,24 @@ class Project (object):
         authors = self._vcs.authors()
         new_contents = u'{} was written by:\n{}\n'.format(
             self._name, u'\n'.join(authors))
-        _utils.set_contents('AUTHORS', new_contents, dry_run=dry_run)
+        _utils.set_contents(
+            'AUTHORS', new_contents, unicode=True, encoding=self._encoding,
+            dry_run=dry_run)
 
     def update_file(self, filename, dry_run=False):
         _LOG.info('update {}'.format(filename))
-        contents = _utils.get_contents(filename=filename)
+        contents = _utils.get_contents(
+            filename=filename, unicode=True, encoding=self._encoding)
         original_year = self._vcs.original_year(filename=filename)
         authors = self._vcs.authors(filename=filename)
         new_contents = _utils.update_copyright(
             contents=contents, original_year=original_year, authors=authors,
             text=self._copyright, info=self._info(), prefix='# ',
-            tag=self._copyright_tag)
+            width=self._width, tag=self._copyright_tag)
         _utils.set_contents(
             filename=filename, contents=new_contents,
-            original_contents=contents, dry_run=dry_run)
+            original_contents=contents, unicode=True, encoding=self._encoding,
+            dry_run=dry_run)
 
     def update_files(self, files=None, dry_run=False):
         if files is None or len(files) == 0:
@@ -181,37 +206,38 @@ class Project (object):
             _utils.copyright_string(
                 original_year=original_year, final_year=current_year,
                 authors=authors, text=self._copyright, info=self._info(),
-                prefix='# '),
-            '', 'import textwrap as _textwrap', '', '',
-            'LICENSE = """',
+                prefix=u'# ', width=self._width),
+            u'', u'import textwrap as _textwrap', u'', u'',
+            u'LICENSE = """',
             _utils.copyright_string(
                 original_year=original_year, final_year=current_year,
                 authors=authors, text=self._copyright, info=self._info(),
-                prefix=''),
-            '""".strip()',
-            '',
-            'def short_license(info, wrap=True, **kwargs):',
-            '    paragraphs = [',
+                prefix=u'', width=self._width),
+            u'""".strip()',
+            u'',
+            u'def short_license(info, wrap=True, **kwargs):',
+            u'    paragraphs = [',
             ]
         paragraphs = _utils.copyright_string(
             original_year=original_year, final_year=current_year,
             authors=authors, text=self._short_copyright, info=self._info(),
             author_format_fn=_utils.short_author_formatter, wrap=False,
-            ).split('\n\n')
+            ).split(u'\n\n')
         for p in paragraphs:
-            lines.append("        '{}' % info,".format(
-                    p.replace("'", r"\'")))
+            lines.append(u"        '{}' % info,".format(
+                    p.replace(u"'", ur"\'")))
         lines.extend([
-                '        ]',
-                '    if wrap:',
-                '        for i,p in enumerate(paragraphs):',
-                '            paragraphs[i] = _textwrap.fill(p, **kwargs)',
-                r"    return '\n\n'.join(paragraphs)",
-                '',  # for terminal endline
+                u'        ]',
+                u'    if wrap:',
+                u'        for i,p in enumerate(paragraphs):',
+                u'            paragraphs[i] = _textwrap.fill(p, **kwargs)',
+                ur"    return '\n\n'.join(paragraphs)",
+                u'',  # for terminal endline
                 ])
-        new_contents = '\n'.join(lines)
+        new_contents = u'\n'.join(lines)
         _utils.set_contents(
-            filename=self._pyfile, contents=new_contents, dry_run=dry_run)
+            filename=self._pyfile, contents=new_contents, unicode=True,
+            encoding=self._encoding, dry_run=dry_run)
 
     def _ignored_file(self, filename):
         """