Skip to content

Commit 7ee616c

Browse files
committed
* Implemented checker
* Changed readme * added thirdparty notices
1 parent 8512830 commit 7ee616c

File tree

11 files changed

+236
-49
lines changed

11 files changed

+236
-49
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ about: Create a report to help us improve
55
---
66

77
<!-- Please fulfill the table below-->
8-
| VS Code Version | vscode-timing Version |
9-
| --------------- | --------------------- |
10-
| | |
8+
| Python Version | Pylint Version | pylintfileheader Version |
9+
| -------------- | -------------- | ------------------------ |
10+
| | | |
1111

1212
# Description
1313
<!-- Please add a brief description of the error you are receiving here.-->
1414

1515

1616
## Expected Behavior
17-
<!-- How should the extension act? -->
17+
<!-- How should the package act? -->
1818

1919

2020
## Actual Behavior
21-
<!-- How is the extension actually behaving? -->
21+
<!-- How is the package actually behaving? -->
2222

2323

2424
# Steps to Reproduce
@@ -29,6 +29,6 @@ about: Create a report to help us improve
2929

3030
# Settings
3131
<details><pre><code>
32-
<!-- Please add all "timing" settings of your settings.json here -->
32+
<!-- Please add all "pylint" settings of your .pylintrc here -->
3333

3434
</code></pre></details>

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: python
22
python:
3+
- "2.7"
4+
- "3.5"
35
- "3.6"
46

57
sudo: false
@@ -20,9 +22,11 @@ jobs:
2022
- python -m pylint ./*/*.py
2123
- stage: bundle
2224
script:
25+
- cp ThirdPartyNotices.txt ./pylintfileheader
2326
- python setup.py sdist bdist_wheel
2427
- stage: deploy
2528
before_deploy:
29+
- cp ThirdPartyNotices.txt ./pylintfileheader
2630
- python setup.py sdist bdist_wheel
2731
- twine upload dist/* -u HaaLeo -p ${PYPI_PASSWORD}
2832
script: skip

.vscode/settings.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
"cSpell.words": [
33
"pylint",
44
"Hanisch",
5-
"pylintfileheader"
6-
]
5+
"pylintfileheader",
6+
"pylint",
7+
"pylintrc",
8+
"pypi",
9+
"msgs",
10+
"rcfile"
11+
],
12+
"files.eol": "\n"
713
}

.vscode/tasks.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Bundle and Install",
6+
"type": "shell",
7+
"presentation": {
8+
"echo": true,
9+
"reveal": "silent",
10+
"focus": true,
11+
"panel": "shared",
12+
"showReuseMessage": true,
13+
"clear": false
14+
},
15+
"windows": {
16+
"command": "Remove-Item -Recurse -Force -ErrorAction Ignore dist; py -3 setup.py sdist bdist_wheel; Get-Childitem –Path ./dist/*whl | Select-Object -ExpandProperty Name -outvariable package; pip3 install ./dist/$package --user --upgrade"
17+
},
18+
"group": {
19+
"kind": "build",
20+
"isDefault": true
21+
}
22+
}
23+
]
24+
}

CHANGELOG.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Changelog
2-
All notable changes to the "pylint-file-header" pypi package will be documented in this file.
3-
This project follows [semantic versioning](https://semver.org/).
4-
5-
## Unreleased - v0.0.1
6-
* **Initial Release**
7-
* **Added** a feature that enables pylint to lint for a consistent file header
1+
# Changelog
2+
All notable changes to the "pylintfileheader" pypi package will be documented in this file.
3+
This project follows [semantic versioning](https://semver.org/).
4+
5+
## Unreleased - v0.0.1
6+
* **Initial Release**
7+
* **Added** a feature that enables pylint to lint for a consistent file header

LICENSE renamed to LICENSE.txt

File renamed without changes.

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,80 @@
11
# pylint-file-header
2+
3+
[![Pypi](https://img.shields.io/pypi/v/pylintfileheader.svg?style=flat-square)](https://pypi.python.org/pypi/pylintfileheader) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pylintfileheader.svg?style=flat-square)](https://pypi.python.org/pypi/pylintfileheader) [![Stars](https://img.shields.io/github/stars/HaaLeo/pylint-file-header.svg?label=Stars&logo=github&style=flat-square)](https://github.com/HaaLeo/pylint-file-header/stargazers)
4+
[![PyPI - License](https://img.shields.io/pypi/l/pylintfileheader.svg?style=flat-square)](https://pypi.python.org/pypi/pylintfileheader)
5+
[![Build Status](https://img.shields.io/travis/HaaLeo/pylint-file-header/master.svg?style=flat-square)](https://travis-ci.org/HaaLeo/pylint-file-header) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6+
[![Donate](https://img.shields.io/badge/-Donate-blue.svg?logo=paypal&style=flat-square)](https://www.paypal.me/LeoHanisch)
7+
28
Pylint plugin to enforce consistent file/module header
9+
10+
## Installation
11+
12+
You can install the package with `pip` from [pypi](https://pypi.org/project/pylintfileheader):
13+
14+
```
15+
pip install pylintfileheader
16+
```
17+
18+
## Configuration
19+
20+
Generate a `.pylintrc` file by executing `pylint --generate-rcfile`.
21+
Then add `pylintfileheader` to the plugins to load and set the `file-header` option to the [regular expression](https://docs.python.org/3.7/library/re.html#regular-expression-syntax) that the file header should match.
22+
When the `file-header` setting is omitted, pylint will pass.
23+
24+
## Example
25+
26+
### Setup
27+
28+
`.pylintrc`:
29+
30+
```pylintrc
31+
[MASTER]
32+
load-plugins=pylintfileheader
33+
34+
file-header=# -----------------------------------------------\n# Copyright (c) Leo Hanisch. All rights reserved.\n# -----------------------------------------------
35+
```
36+
37+
`valid_example.py`:
38+
39+
```python
40+
# -----------------------------------------------
41+
# Copyright (c) Leo Hanisch. All rights reserved.
42+
# -----------------------------------------------
43+
44+
print('I am a valid example')
45+
```
46+
47+
`invalid_example.py`:
48+
49+
```python
50+
print('I am an invalid example')
51+
```
52+
53+
### Evaluation
54+
55+
* `pylint valid_example.py` evaluates to:
56+
```
57+
Using config file /path/to/your/.pylintrc
58+
59+
------------------------------------
60+
Your code has been rated at 10.00/10
61+
```
62+
63+
* `pylint invalid_example.py` evaluates to:
64+
```
65+
Using config file /path/to/your/.pylintrc
66+
************* Module invalid_example
67+
C: 1, 0: File header should match regex "# -----------------------------------------------\n# Copyright (c) Leo Hanisch. All rights reserved.\n# -----------------------------------------------" (invalid-file-header)
68+
69+
-----------------------------------
70+
Your code has been rated at 8.57/10
71+
```
72+
73+
## Contribution
74+
75+
If you found a bug or are missing a feature do not hesitate to [file an issue](https://github.com/HaaLeo/pylint-file-header/issues/new/choose).
76+
Pull Requests are welcome!
77+
78+
## Support
79+
When you like this package make sure to [star the repository](https://github.com/HaaLeo/pylint-file-header/stargazers). I am always looking for new ideas and feedback.
80+
In addition, it is possible to [donate via paypal](https://www.paypal.me/LeoHanisch).

ThirdPartyNotices.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
2+
For Leo Hanisch pylintfileheader (pylint-file-header)
3+
4+
This project incorporates material from the project(s) listed below (collectively, “Third Party Code”).
5+
I am not the original author of the Third Party Code. The original copyright notice and license
6+
under which I received such Third Party Code are set out below. This Third Party Code is licensed
7+
to you under their original license terms set forth below. I reserve all other rights not expressly
8+
granted, whether by implication, estoppel or otherwise.
9+
10+
1. pylint-quotes version 0.1.9 (https://github.com/edaniszewski/pylint-quotes)
11+
12+
MIT License
13+
14+
Copyright (c) 2018 Erick Daniszewski
15+
16+
Permission is hereby granted, free of charge, to any person obtaining a copy
17+
of this software and associated documentation files (the "Software"), to deal
18+
in the Software without restriction, including without limitation the rights
19+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+
copies of the Software, and to permit persons to whom the Software is
21+
furnished to do so, subject to the following conditions:
22+
23+
The above copyright notice and this permission notice shall be included in all
24+
copies or substantial portions of the Software.
25+
26+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32+
SOFTWARE.

pylintfileheader/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
"""pylintfileheader module"""
3+
4+
from __future__ import absolute_import
5+
from pylintfileheader import plugin, file_header_checker # noqa: F401
6+
7+
8+
register = plugin.register # pylint: disable=invalid-name
Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,56 @@
1-
from pylint.checkers import BaseChecker
2-
from pylint.interfaces import IRawChecker
3-
4-
5-
class FileHeaderChecker(BaseChecker):
6-
__implements__ = IRawChecker
7-
8-
name = 'pylintfileheader'
9-
10-
msgs = {
11-
'C5001': (
12-
'Invalid file header %s, should be %s',
13-
'invalid-file-header',
14-
'Used when the file header does not match the '
15-
'value configured in the `file-header` option.'
16-
)
17-
}
18-
19-
options = (
20-
(
21-
'file-header',
22-
{
23-
'type': 'string',
24-
'default': None,
25-
'help': 'The file header that should be on top of a file.'
26-
}
27-
)
28-
)
29-
30-
def process_module(self, astroid):
31-
print(astroid)
1+
import re
2+
import sys
3+
4+
from pylint.checkers import BaseChecker
5+
from pylint.interfaces import IRawChecker
6+
7+
# pylint: disable=anomalous-backslash-in-string
8+
9+
10+
class FileHeaderChecker(BaseChecker):
11+
__implements__ = IRawChecker
12+
13+
name = 'pylintfileheader'
14+
15+
msgs = {
16+
'C5001': (
17+
'File header should match regex "%s"',
18+
'invalid-file-header',
19+
'Used when the file header does not match the '
20+
'regex configured in the `file-header` option.'
21+
)
22+
}
23+
24+
options = (
25+
(
26+
'file-header',
27+
{
28+
'default': None,
29+
'type': 'string',
30+
'metavar': '<REGEXP>',
31+
'help': 'The file header that should be on top of a file.',
32+
}
33+
),
34+
)
35+
36+
def process_module(self, node):
37+
"""Process the astroid node stream."""
38+
if self.config.file_header:
39+
if sys.version_info[0] < 3:
40+
pattern = re.compile(
41+
'\A' + self.config.file_header, re.LOCALE | re.MULTILINE)
42+
else:
43+
# The use of re.LOCALE is discouraged in python 3
44+
pattern = re.compile(
45+
'\A' + self.config.file_header, re.MULTILINE)
46+
47+
content = None
48+
with node.stream() as stream:
49+
# Explicit decoding required by python 3
50+
content = stream.read().decode('utf-8')
51+
52+
matches = pattern.findall(content)
53+
54+
if len(matches) != 1:
55+
self.add_message('invalid-file-header', 1,
56+
args=self.config.file_header)

0 commit comments

Comments
 (0)