Skip to content

Commit

Permalink
Keep license title on own line in verbatim mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rdipardo committed Jun 12, 2020
1 parent 4b33437 commit 6efd4e6
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 96 deletions.
2 changes: 1 addition & 1 deletion plugin/cpywrite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _write_header(writer, curr_buffer):
to_trim = 0

for idx, _ in enumerate(curr_buffer):
# replace shebang lines and encoding directives, if any
# replace shebang lines and encoding declarations, if any
if curr_buffer[idx].startswith("#!", 0) or \
match(r"^#.+(coding).+$", curr_buffer[idx]):
to_trim += 1
Expand Down
6 changes: 3 additions & 3 deletions rplugin/pythonx/cpywrite/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _apply_format(text, pattern, copying, email):
"""
Wrap text if the raw header has no break after author's email, as
some license formats require, e.g the ECL, all of the GFDLs. Fix up
copyright format and insert punctuation.
copyright format and insert punctuation
"""
year = str(datetime.now())[:4]
text = re.sub(r'\)%s' % year,
Expand Down Expand Up @@ -199,7 +199,7 @@ def _get_language_meta(filename):

def _get_source_author():
"""
Retrieve author details from local git configuration.
Retrieve author details from local git configuration
"""
author = \
environ.get('USERNAME', 'unknown') \
Expand Down Expand Up @@ -317,7 +317,7 @@ def extensions():
'bash': '#!/usr/bin/env bash',
'perl': '#!/usr/bin/env perl',
'php': '<?php',
'python': '#!/usr/bin/env python%s\n# -*- coding: utf-8 -*-'
'python': '#!/usr/bin/env python%s\n# -*- coding: utf-8 -*-\n'
% sys.version_info[0],
'ruby': '# frozen_string_literal: true\n',
}
Expand Down
26 changes: 15 additions & 11 deletions rplugin/pythonx/cpywrite/spdx/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def header(self):
header = ''

if self.spdx_code:
# https://bugs.python.org/issue11033
# ElementTree still has this problem in python 2.7.13 and
# probably later versions, too
strict_ascii = lambda c: ord(c) > 0 and ord(c) < 128
xml_resource = 'https://raw.githubusercontent.com/' \
'spdx/license-list-XML/master/src/%s.xml'
resource = quote(xml_resource % self.spdx_code, safe='/:')
Expand All @@ -42,14 +46,10 @@ def header(self):
try:
response = requests.get(resource)

# https://bugs.python.org/issue11033
# ElementTree still has this problem in python 2.7.13 and
# probably later versions, too
strict_ascii = lambda c: ord(c) > 0 and ord(c) < 128
safe_xml = ''.join(list(filter(strict_ascii, response.text)))

if response.status_code == 200:
try:
safe_xml = \
''.join(list(filter(strict_ascii, response.text)))
license_data = parser.fromstring(safe_xml)
except (parser.ParseError, TypeError, IOError):
print("Got invalid licence data from %s." % (resource),
Expand All @@ -69,9 +69,8 @@ def header(self):
p_tag = ('{http://www.spdx.org/license}p')
alt_tag = ('{http://www.spdx.org/license}alt')
opt_tag = ('{http://www.spdx.org/license}optional')
std_header = license_data.findall(header_tag)

for node in std_header:
for node in license_data.findall(header_tag):
for child in node.iter():
content = ''

Expand Down Expand Up @@ -124,7 +123,7 @@ def header(self):
return header

def license_text(self):
"""Return the full text this License"""
"""Return the full text of this License"""
license_text = ''

if self.spdx_code:
Expand All @@ -146,8 +145,13 @@ def license_text(self):
requests.exceptions.ConnectionError):
print("Error requesting %s." % (resource), file=stderr)

# try to keep subclauses left-aligned
return sub(r'\n{2}\s*\-', '\n\n -', license_text).splitlines()
# try to keep sub-clauses left-aligned;
# always put copyright notice on a new line
return sub(r'\n{2}\s*\-',
'\n\n -',
sub(r'(?!$) ([cC]opyright(\s+\([cC]\))?\s+[\<\[\s][yearxYEARX]+)',
'\n\nCopyright (c) <year>',
license_text)).splitlines()

def __repr__(self):
"""Return a debug string representing this License"""
Expand Down
30 changes: 15 additions & 15 deletions test/vader/test_c_langs.vader
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,45 @@ Then(Assert that default license was set);

Execute(Prepend a standard license header to a C header file);
new! standard.H
b standard.H
norm LH
b standard.H
norm LH

Then(C header quotes the Apache-2.0 license);
AssertEqual ' * standard.h', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual ' * Licensed under the Apache License, Version 2.0 (the "License");', getline(6)
AssertEqual ' * http://www.apache.org/licenses/LICENSE-2.0', getline(10)

Execute(Prepend a header-less license to a C source file);
new! brief.c
b brief.c
b brief.c
CPYwrite LGPL-3.0-or-later

Then(C header has brief license mention);
AssertEqual ' * brief.c', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual ' * Licensed under the terms of the LGPL-3.0-or-later license', getline(6)
AssertEqual ' */', getline(7)

Execute(Prepend a standard license header to a C++ header file);
new! standard.hpp
b standard.hpp
CPYwrite BitTorrent-1.1
b standard.hpp
CPYwrite BitTorrent-1.1

Then(C++ source file quotes the BitTorrent-1.1 license);
AssertEqual ' * standard.hpp', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual ' * The contents of this file are subject to the BitTorrent Open Source License Version 1.1 (the', getline(6)
AssertEqual ' */', getline(13)

Execute(Prepend a header-less license to a C++ source file);
new! brief.cxx
b brief.cxx
b brief.cxx
CPYwrite OpenSSL

Then(C++ source file has brief license mention);
AssertEqual ' * brief.cxx', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual ' * Licensed under the terms of the OpenSSL license', getline(6)
AssertEqual ' */', getline(7)

Expand All @@ -66,12 +66,12 @@ Then(Assert that verbatim mode is now on);
" Tc #5
Execute(Prepend a license verbatim to a Rust file);
new! verbatim.rs
b verbatim.rs
b verbatim.rs
CPYwrite BSD-3-Clause

Then(Rust file header quotes the entire BSD-3-Clause license);
AssertEqual '// verbatim.rs', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '// 1. Redistributions of source code must retain the above copyright notice,', getline(9)
AssertEqual '// 2. Redistributions in binary form must reproduce the above copyright notice,', getline(12)
AssertEqual '// 3. Neither the name of the copyright holder nor the names of its contributors', getline(16)
Expand All @@ -80,15 +80,15 @@ Then(Rust file header quotes the entire BSD-3-Clause license);

Execute(Prepend a license verbatim to a Go file);
new! verbatim.go
b verbatim.go
b verbatim.go
CPYwrite BSD-1-Clause

Then(Go file header quotes the entire BSD-1-Clause license);
AssertEqual '// verbatim.go', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '// 1. Redistributions of source code must retain the above copyright notice,', getline(9)
AssertEqual '// THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. "AS IS" AND ANY', getline(12)
AssertEqual '// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.', getline(21)
AssertEqual '// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.', getline(21)
AssertEqual 2, len(getline(22)), 'Allow no trailing spaces'

Execute(Turn off verbatim mode);
Expand Down
80 changes: 40 additions & 40 deletions test/vader/test_scripting_langs.vader
Original file line number Diff line number Diff line change
Expand Up @@ -15,151 +15,151 @@ Then(Assert that default license was set);
" Tc #1
Execute(Prepend the default license header to a shell script);
new! default_header
b default_header
b default_header
norm LH

Then(Shell script header quotes the GPL-3.0+);
AssertEqual '#!/usr/bin/env bash', getline(1)
AssertEqual 1, len(getline(2)), 'Allow no trailing spaces'
AssertEqual 1, len(getline(2)), 'Allow no trailing spaces'
AssertEqual '# default_header', getline(3)
AssertEqual '# modify it under the terms of the GNU General Public License', getline(8)
AssertEqual '# 3 of the License, or (at your option) any later version.', getline(10)

" Tc #2
Execute(Prepend a header-less license to a shell script);
new! brief_header
b brief_header
b brief_header
CPYwrite Vim

Then(Shell script header has brief license mention);
AssertEqual '#!/usr/bin/env bash', getline(1)
AssertEqual 1, len(getline(2)), 'Allow no trailing spaces'
AssertEqual 1, len(getline(2)), 'Allow no trailing spaces'
AssertEqual '# brief_header', getline(3)
AssertEqual '# Licensed under the terms of the Vim license', getline(7)

" Tc #3
Execute(Prepend the default license header to a Python file);
new! default_header.py
b default_header.py
b default_header.py
CPYwrite

Then(Python file header quotes the GPL-3.0+);
AssertEqual '# -*- coding: utf-8 -*-', getline(2)
AssertEqual 1, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '# default_header.py', getline(4)
AssertEqual '# modify it under the terms of the GNU General Public License', getline(9)
AssertEqual '# 3 of the License, or (at your option) any later version.', getline(11)
AssertEqual 1, len(getline(4)), 'Allow no trailing spaces'
AssertEqual '# default_header.py', getline(5)
AssertEqual '# modify it under the terms of the GNU General Public License', getline(10)
AssertEqual '# 3 of the License, or (at your option) any later version.', getline(12)

" Tc #4
Execute(Prepend a standard header to a Python file);
new! standard.py
b standard.py
b standard.py
CPYwrite AFL-1.1

Then(Python file header quotes the AFL-1.1);
AssertEqual '# -*- coding: utf-8 -*-', getline(2)
AssertEqual 1, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '# standard.py', getline(4)
AssertEqual '# Licensed under the Academic Free License version 1.1.', getline(8)
AssertEqual 1, len(getline(4)), 'Allow no trailing spaces'
AssertEqual '# standard.py', getline(5)
AssertEqual '# Licensed under the Academic Free License version 1.1.', getline(9)

" Tc #5
Execute(Prepend the default license header to a Ruby file);
new! default_header.rb
b default_header.rb
silent exe "norm \<Plug>(cpywrite)"
b default_header.rb
silent exe "norm \<Plug>(cpywrite)"

Then(Ruby file header quotes the GPL-3.0+);
AssertEqual '# frozen_string_literal: true', getline(1)
AssertEqual 0, len(getline(2)), 'One blank line after frozen_string_literal directive'
AssertEqual 0, len(getline(2)), 'One blank line after frozen_string_literal directive'
AssertEqual '# default_header.rb', getline(4)
AssertEqual '# modify it under the terms of the GNU General Public License', getline(9)
AssertEqual '# 3 of the License, or (at your option) any later version.', getline(11)

" Tc #6
Execute(Prepend a header-less license to a Ruby file);
new! brief_header.rb
b brief_header.rb
b brief_header.rb
CPYwrite MIT

Then(Ruby file header has brief license mention);
AssertEqual '# frozen_string_literal: true', getline(1)
AssertEqual 0, len(getline(2)), 'One blank line after frozen_string_literal directive'
AssertEqual 0, len(getline(2)), 'One blank line after frozen_string_literal directive'
AssertEqual '# brief_header.rb', getline(4)
AssertEqual '# Licensed under the terms of the MIT license', getline(8)

" Tc #7
Execute(Prepend the default license header to a PHP file);
new! default_header.php
b default_header.php
CPYwrite
b default_header.php
CPYwrite

Then(PHP file quotes the GPL-3.0+);
AssertEqual '<?php', getline(1)
AssertEqual ' * default_header.php', getline(3)
AssertEqual 2, len(getline(4)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(4)), 'Allow no trailing spaces'
AssertEqual ' * modify it under the terms of the GNU General Public License', getline(8)
AssertEqual ' * 3 of the License, or (at your option) any later version.', getline(10)
AssertEqual '?>', getline(22), 'Ensure closing PHP tag'
AssertEqual '?>', getline(22), 'Ensure closing PHP tag'

" Tc #8
Execute(Prepend a header-less license to a PHP file);
new! brief_header.phtml
b brief_header.phtml
b brief_header.phtml
CPYwrite Zend-2.0

Then(PHP file has brief license mention);
AssertEqual '<?php', getline(1)
AssertEqual ' * brief_header.phtml', getline(3)
AssertEqual 2, len(getline(4)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(4)), 'Allow no trailing spaces'
AssertEqual ' * Licensed under the terms of the Zend-2.0 license', getline(7)
AssertEqual '?>', getline(10), 'Ensure closing PHP tag'
AssertEqual '?>', getline(10), 'Ensure closing PHP tag'

" Tc #9
Execute(Prepend the default license header to an SQL script);
new! default_header.sql
b default_header.sql
b default_header.sql
silent exe "norm \<Plug>(cpywrite)"

Then(SQL script quotes the GPL-3.0+);
AssertEqual '-- default_header.sql', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '-- modify it under the terms of the GNU General Public License', getline(7)
AssertEqual '-- 3 of the License, or (at your option) any later version.', getline(9)
AssertEqual '--', getline(19)
AssertEqual '--', getline(19)

" Tc #10
Execute(Prepend a header-less license to an SQL script);
new! brief.sql
b brief.sql
CPYwrite bzip2-1.0.5
b brief.sql
CPYwrite bzip2-1.0.5

Then(SQL script has brief license mention);
AssertEqual '-- brief.sql', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '-- Licensed under the terms of the bzip2-1.0.5 license', getline(6)
AssertEqual 2, len(getline(7)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(7)), 'Allow no trailing spaces'

" Tc #11
Execute(Prepend the default license header to a Vim script);
new! standard.vim
b standard.vim
norm LH
b standard.vim
norm LH

Then(Vim script quotes the GPL-3.0+);
AssertEqual '"" standard.vim', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '"" modify it under the terms of the GNU General Public License', getline(7)
AssertEqual '"" 3 of the License, or (at your option) any later version.', getline(9)
AssertEqual '""', getline(19)
AssertEqual '""', getline(19)

" Tc #12
Execute(Prepend a header-less license to a Vim script);
new! brief.vim
b brief.vim
CPYwrite Vim
b brief.vim
CPYwrite Vim

Then(Vim script has brief license mention);
AssertEqual '"" brief.vim', getline(2)
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(3)), 'Allow no trailing spaces'
AssertEqual '"" Licensed under the terms of the Vim license', getline(6)
AssertEqual 2, len(getline(7)), 'Allow no trailing spaces'
AssertEqual 2, len(getline(7)), 'Allow no trailing spaces'
Loading

0 comments on commit 6efd4e6

Please sign in to comment.