-# Copyright (C) 2012 W. Trevor King
+# Copyright (C) 2012-2014 W. Trevor King <wking@tremily.us>
#
# This file is part of update-copyright.
#
-# update-copyright is free software: you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
+# update-copyright is free software: you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
#
-# update-copyright is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
+# update-copyright is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+# more details.
#
-# You should have received a copy of the GNU General Public License
-# along with update-copyright. If not, see
-# <http://www.gnu.org/licenses/>.
+# You should have received a copy of the GNU General Public License along with
+# update-copyright. If not, see <http://www.gnu.org/licenses/>.
import codecs as _codecs
import difflib as _difflib
import os.path as _os_path
import sys as _sys
import textwrap as _textwrap
-import time as _time
from . import LOG as _LOG
def long_author_formatter(copyright_year_string, authors):
"""
- >>> print '\\n'.join(long_author_formatter(
+ >>> print('\\n'.join(long_author_formatter(
... copyright_year_string='Copyright (C) 1990-2010',
- ... authors=['Jack', 'Jill', 'John']))
+ ... authors=['Jack', 'Jill', 'John'])))
Copyright (C) 1990-2010 Jack
Jill
John
"""
- lines = ['%s %s' % (copyright_year_string, authors[0])]
+ lines = ['{} {}'.format(copyright_year_string, authors[0])]
for author in authors[1:]:
lines.append(' '*(len(copyright_year_string)+1) + author)
return lines
def short_author_formatter(copyright_year_string, authors):
"""
- >>> print '\\n'.join(short_author_formatter(
+ >>> print('\\n'.join(short_author_formatter(
... copyright_year_string='Copyright (C) 1990-2010',
- ... authors=['Jack', 'Jill', 'John']*5))
+ ... authors=['Jack', 'Jill', 'John']*5)))
Copyright (C) 1990-2010 Jack, Jill, John, Jack, Jill, John, Jack, Jill, John, Jack, Jill, John, Jack, Jill, John
"""
- blurb = '%s %s' % (copyright_year_string, ', '.join(authors))
+ blurb = '{} {}'.format(copyright_year_string, ', '.join(authors))
return [blurb]
-def copyright_string(original_year, final_year, authors, text, info={},
+def copyright_string(years, authors, text, info={},
author_format_fn=long_author_formatter,
formatter_kwargs={}, prefix=('', '', None), wrap=True,
**wrap_kwargs):
"""
- >>> print(copyright_string(original_year=2005, final_year=2005,
+ >>> print(copyright_string(years=[2005],
... authors=['A <a@a.com>', 'B <b@b.edu>'],
... text=['BLURB',], prefix=('# ', '# ', None),
... )) # doctest: +REPORT_UDIFF
# B <b@b.edu>
#
# BLURB
- >>> print(copyright_string(original_year=2005, final_year=2009,
+ >>> print(copyright_string(years=[2005, 2009],
... authors=['A <a@a.com>', 'B <b@b.edu>'],
... text=['BLURB',], prefix=('/* ', ' * ', ' */'),
... )) # doctest: +REPORT_UDIFF
*
* BLURB
*/
- >>> print(copyright_string(original_year=2005, final_year=2009,
+ >>> print(copyright_string(years=[2005, 2009],
... authors=['A <a@a.com>', 'B <b@b.edu>'],
... text=['BLURB',]
... )) # doctest: +REPORT_UDIFF
B <b@b.edu>
<BLANKLINE>
BLURB
- >>> print(copyright_string(original_year=2005, final_year=2005,
+ >>> print(copyright_string(years=[2005],
... authors=['A <a@a.com>', 'B <b@b.edu>'],
- ... text=['This file is part of %(program)s.',],
+ ... text=['This file is part of {program}.',],
... author_format_fn=short_author_formatter,
... info={'program':'update-copyright'},
... width=25,
<BLANKLINE>
This file is part of
update-copyright.
- >>> print(copyright_string(original_year=2005, final_year=2005,
+ >>> print(copyright_string(years=[2005],
... authors=['A <a@a.com>', 'B <b@b.edu>'],
- ... text=[('This file is part of %(program)s. '*3
+ ... text=[('This file is part of {program}. '*3
... ).strip(),],
... info={'program':'update-copyright'},
... author_format_fn=short_author_formatter,
if key not in wrap_kwargs:
wrap_kwargs[key] = prefix[1]
- if original_year == final_year:
- date_range = '%s' % original_year
+ if not years:
+ raise ValueError('empty years argument: {!r}'.format(years))
+ elif len(years) == 1:
+ date_range = str(years[0])
else:
- date_range = '%s-%s' % (original_year, final_year)
- copyright_year_string = 'Copyright (C) %s' % date_range
+ original_year = min(years)
+ final_year = max(years)
+ date_range = '{}-{}'.format(original_year, final_year)
+ copyright_year_string = 'Copyright (C) {}'.format(date_range)
lines = author_format_fn(copyright_year_string, authors,
**formatter_kwargs)
for i,paragraph in enumerate(text):
try:
- text[i] = paragraph % info
- except ValueError, e:
+ text[i] = paragraph.format(**info)
+ except ValueError as e:
_LOG.error(
"{}: can't format {} with {}".format(e, paragraph, info))
raise
- except TypeError, e:
+ except TypeError as e:
_LOG.error(
('{}: copright text must be a list of paragraph strings, '
'not {}').format(e, repr(text)))
... (copyright ends)
... bla bla bla
... '''
- >>> print tag_copyright(contents, tag='-xyz-CR-zyx-')
+ >>> print(tag_copyright(contents, tag='-xyz-CR-zyx-'))
Some file
bla bla
-xyz-CR-zyx-
... (copyright ends)
... bla bla bla
... '''
- >>> print tag_copyright(
- ... contents, prefix=('/* ', ' * ', ' */'), tag='-xyz-CR-zyx-')
+ >>> print(tag_copyright(
+ ... contents, prefix=('/* ', ' * ', ' */'), tag='-xyz-CR-zyx-'))
Some file
bla bla
-xyz-CR-zyx-
... (copyright ends)
... bla bla bla
... '''
- >>> print update_copyright(
- ... contents, original_year=2008, authors=['Jack', 'Jill'],
+ >>> print(update_copyright(
+ ... contents, years=[2008], authors=['Jack', 'Jill'],
... text=['BLURB',], prefix=('# ', '# ', None), tag='--tag--'
- ... ) # doctest: +ELLIPSIS, +REPORT_UDIFF
+ ... )) # doctest: +ELLIPSIS, +REPORT_UDIFF
Some file
bla bla
- # Copyright (C) 2008-... Jack
- # Jill
+ # Copyright (C) 2008 Jack
+ # Jill
#
# BLURB
(copyright ends)
bla bla bla
<BLANKLINE>
"""
- current_year = _time.gmtime()[0]
- string = copyright_string(final_year=current_year, prefix=prefix, **kwargs)
+ string = copyright_string(prefix=prefix, **kwargs)
contents = tag_copyright(contents=contents, prefix=prefix, tag=tag)
return contents.replace(tag, string)
_LOG.info('creating {}'.format(filename))
else:
_LOG.info('updating {}'.format(filename))
- _LOG.debug(u'\n'.join(
+ _LOG.debug('\n'.join(
_difflib.unified_diff(
original_contents.splitlines(), contents.splitlines(),
fromfile=_os_path.normpath(