Skip to content

Commit 5a659b3

Browse files
author
maypatha
committed
Adds, pylint as static analysis checker
1 parent 25ff189 commit 5a659b3

File tree

15 files changed

+248
-54
lines changed

15 files changed

+248
-54
lines changed

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ env/
66

77
.env.example
88
.gitignore
9-
.isort.cfg

.isort.cfg

Lines changed: 0 additions & 9 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ script:
2323
- safety check --full-report --file requirements/prod.txt
2424
- safety check --full-report --file requirements/dev.txt
2525
- safety check --full-report --file requirements/test.txt
26-
- black --verbose --line-length 79 --target-version py37 *.py app
27-
- isort --verbose --force-alphabetical-sort-within-sections --case-sensitive --lines-after-imports 2 --lines-between-types 1 *.py app/*.py app/**/*.py
26+
- pylint manage.py app
2827
- python manage.py test

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
## TODO
1+
Flask-REST-Server-Template
2+
==========================
3+
4+
## Info
5+
6+
## Dependencies
7+
8+
## Setup
9+
10+
## Contact
11+
[onlinejudge95](mailto:onlinejudge95@gmail.com)

app/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""
22
Module to register our Blueprints
33
"""
4-
from .src.controller.ping import api as ping_ns
54
from flask import Blueprint
65
from flask_restplus import Api
76

7+
from .src.controller.ping import api as ping_ns
88

9-
blueprint = Blueprint("api", __name__)
9+
blueprint = Blueprint("api", __name__) # pylint: disable=invalid-name
1010

11+
# pylint: disable=invalid-name
1112
api = Api(
1213
blueprint,
1314
title="Flask-RESTplus API server",

app/src/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
"""
22
App factory for our server.
33
"""
4-
from .config import config_map
4+
import os
5+
import pathlib
6+
57
from dotenv import find_dotenv, load_dotenv
68
from flask import Flask
79

8-
import os
9-
import pathlib
10+
from .config import CFG_MAP
1011

1112

1213
load_dotenv(dotenv_path=str(pathlib.Path.cwd() / ".env"), verbose=True)
1314

1415

1516
def create_app(cfg):
17+
"""
18+
App factory to create app instance with provided environment settings.
19+
20+
The following creates an app instance for different environments
21+
>>> production_app = create_app("production")
22+
>>> development_app = create_app("development")
23+
>>> testing_app = create_app("testing")
24+
25+
:param: cfg
26+
Configuration to setup the corresponding environment.
27+
"""
1628
app = Flask(__name__)
17-
app.config.from_object(config_map[cfg])
29+
app.config.from_object(CFG_MAP[cfg])
1830

1931
return app

app/src/config.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,64 @@
11
"""
22
Configuration module for the app.
33
"""
4-
import os
54
import pathlib
65
import uuid
76

87

9-
class BaseConfig:
8+
class BaseConfig: # pylint: disable=too-few-public-methods
9+
"""
10+
Base configuration class to hold configuration settings as class variable.
11+
12+
NOTE:- Never instantiate this class directly, the proper usage is to inherit
13+
from this class.
14+
15+
>>> class CustomConfig(BaseConfig):
16+
... pass
17+
>>> app.config.from_object(CustomConfig())
18+
"""
1019
TESTING = False
1120
BASE_DIR = pathlib.Path.cwd()
1221
SECRET_KEY = uuid.uuid4().hex
1322

1423

15-
class DevelopmentConfig(BaseConfig):
16-
pass
24+
class DevelopmentConfig(BaseConfig): # pylint: disable=too-few-public-methods
25+
"""
26+
Configuration class to hold configuration settings as class variables for
27+
development environment
28+
29+
NOTE:- Proper usage is to instantiate this class directly.
30+
31+
>>> dev_config = DevelopmentConfig()
32+
"""
33+
JSONIFY_PRETTYPRINT_REGULAR = True
1734

1835

19-
class TestingConfig(BaseConfig):
36+
class TestingConfig(BaseConfig): # pylint: disable=too-few-public-methods
37+
"""
38+
Configuration class to hold configuration settings as class variables for
39+
testing environment
40+
41+
NOTE:- Proper usage is to instantiate this class directly.
42+
43+
>>> test_config = TestingConfig()
44+
"""
2045
TESTING = True
2146
JSONIFY_PRETTYPRINT_REGULAR = True
2247

2348

24-
class ProductionConfig(BaseConfig):
49+
class ProductionConfig(BaseConfig): # pylint: disable=too-few-public-methods
50+
"""
51+
Configuration class to hold configuration settings as class variables for
52+
production environment
53+
54+
NOTE:- Proper usage is to instantiate this class directly.
55+
56+
>>> prod_config = ProductionConfig()
57+
"""
2558
JSONIFY_PRETTYPRINT_REGULAR = True
2659

2760

28-
config_map = {
61+
CFG_MAP = {
2962
"development": "app.src.config.DevelopmentConfig",
3063
"testing": "app.src.config.TestingConfig",
3164
"production": "app.src.config.ProductionConfig",

app/src/controller/ping.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
"""
22
Module to provide ping controller functionality
33
"""
4+
from flask_restplus import Resource
5+
46
from ..service import ping as service
57
from ..utils import decorators, dto
6-
from flask_restplus import Resource
78

89

9-
api = dto.PingDto.api
10+
api = dto.PingDto.api # pylint: disable=invalid-name
1011

1112

1213
@api.route("/")
13-
class Ping(Resource):
14+
class PingAPI(Resource):
15+
"""
16+
Ping controller class.
17+
It handles the ping operation routing.
18+
This class subclasses flask_restplus.Resource, representing a
19+
resource for which all HTTP verbs are allowed as methods to operate on.
20+
21+
This class receives the request from the server and calls the ping service on it.
22+
Endpoints defined are
23+
24+
1. GET /ping/
25+
"""
1426
@decorators.validate_headers
1527
@api.doc("ping")
16-
def get(self):
28+
def get(self): # pylint: disable=no-self-use
1729
"""Ping response to find if the server is up"""
1830
return service.ping()

app/src/service/ping.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
"""
2-
Module to provide ping service fundtionality
2+
Module to provide ping service funcssstionality
33
"""
44

55

66
def ping():
7+
"""
8+
Ping service, it basically checks if the server is up or not.
9+
>>> obj = ping()
10+
>>> obj.response
11+
>>> {"status": "success", "message": "PONG"}
12+
>>> obj.status_code
13+
>>> 200
14+
"""
715
response_object = {"status": "success", "message": "PONG"}
8-
return (response_object, 200, {"Connection": "close"})
16+
return response_object, 200

app/src/utils/decorators.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
from .dto import PingDto
2-
from flask import request
3-
4-
import functools
1+
"""
2+
Module for implementing custom decorators to extend function behaviour.
53
4+
Usage:-
65
7-
api = PingDto.api
6+
@decorator
7+
def function(*args, **kwargs):
8+
pass
9+
"""
10+
import functools
811

912

1013
def validate_headers(func):
14+
"""
15+
Decorator to validate any header
16+
17+
Currently it is left blank, we can implement following checks.
18+
* Content-Type check
19+
* Authorization check etc.
20+
"""
1121
@functools.wraps(func)
1222
def check(*args, **kwargs):
13-
if request.headers.get("Content-Type") != "application/json":
14-
return api.abort(415)
1523
return func(*args, **kwargs)
1624

1725
return check

0 commit comments

Comments
 (0)