Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for 0.3.0 release #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
v0.3.0 - 2019-06-XX
-------------------

* Remove flake8 v2 compatibility
* Remove `--copyright-check` boolean argument, now always check
* Change plugin registration so `select` does not need modifying
* Update message to clarify message may be missing or malformed
* Default min file size to 3 so empty or newline-only files pass

v0.2.2 - 2018-10-10
-------------------

Expand Down
8 changes: 1 addition & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@ Install with pip::

pip install flake8-copyright

Then, activate copyright checks in your flake8 configuration with::

copyright-check = True
# C errors are not selected by default, so add them to your selection
select = E,F,W,C


Further options
---------------

copyright-min-file-size
Minimum number of characters in a file before requiring a copyright notice. This is to avoid
forcing yourself to add copyright notices to very small or empty files. Default: ``0``.
forcing yourself to add copyright notices to very small or empty files. Default: ``3``.

copyright-author
Checks for a specific author in the copyright notice.
Expand Down
41 changes: 8 additions & 33 deletions flake8_copyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import re
import optparse

__version__ = '0.2.2'


# Temporary shim for flake8 2.x --> 3.x transition
# http://flake8.pycqa.org/en/latest/plugin-development/cross-compatibility.html#option-handling-on-flake8-2-and-3
def register_opt(parser, *args, **kwargs):
try:
# Flake8 3.x registration
parser.add_option(*args, **kwargs)
except (optparse.OptionError, TypeError):
# Flake8 2.x registration
parse_from_config = kwargs.pop('parse_from_config', False)
kwargs.pop('comma_separated_list', False)
kwargs.pop('normalize_paths', False)
parser.add_option(*args, **kwargs)
if parse_from_config:
parser.config_options.append(args[-1].lstrip('-'))
__version__ = '0.3.0'


class CopyrightChecker(object):
Expand All @@ -49,22 +31,18 @@ def __init__(self, tree, filename):

@classmethod
def add_options(cls, parser):
register_opt(
parser, '--copyright-check', action='store_true', parse_from_config=True,
help="Checks for copyright notices in every file."
)
register_opt(
parser, '--copyright-min-file-size', default=0, action='store', type='int',
parser.add_option(
'--copyright-min-file-size', default=3, action='store', type='int',
parse_from_config=True,
help="Minimum number of characters in a file before requiring a copyright notice."
)
register_opt(
parser, '--copyright-author', default='', action='store',
parser.add_option(
'--copyright-author', default='', action='store',
parse_from_config=True,
help="Checks for a specific author in the copyright notice."
)
register_opt(
parser, '--copyright-regexp',
parser.add_option(
'--copyright-regexp',
default=r"Copyright\s+(\(C\)\s+)?\d{4}([-,]\d{4})*\s+%(author)s",
action='store',
parse_from_config=True,
Expand All @@ -73,14 +51,11 @@ def add_options(cls, parser):

@classmethod
def parse_options(cls, options):
cls.copyright_check = options.copyright_check
cls.copyright_min_file_size = options.copyright_min_file_size
cls.copyright_author = options.copyright_author
cls.copyright_regexp = options.copyright_regexp

def run(self):
if not self.copyright_check:
return
toread = max(1024, self.copyright_min_file_size)
top_of_file = open(self.filename).read(toread)
if len(top_of_file) < self.copyright_min_file_size:
Expand All @@ -89,4 +64,4 @@ def run(self):
author = self.copyright_author if self.copyright_author else r".*"
re_copyright = re.compile(self.copyright_regexp % {'author': author}, re.IGNORECASE)
if not re_copyright.search(top_of_file):
yield 1, 1, "C801 Copyright notice not present.", type(self)
yield 1, 1, "C801 Copyright notice missing or malformed.", type(self)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ def get_long_description():
'setuptools',
],
entry_points={
'flake8.extension': ['flake8_copyright = flake8_copyright:CopyrightChecker'],
'flake8.extension': ['C801 = flake8_copyright:CopyrightChecker'],
},
)