Completed
Push — master ( 2a5ab9...12cd9a )
by Jace
14s
created

get_with_text()   F

Complexity

Conditions 10

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10.005

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 10
c 2
b 0
f 0
dl 0
loc 37
rs 3.1304
ccs 26
cts 27
cp 0.963
crap 10.005

How to fix   Complexity   

Complexity

Complex classes like get_with_text() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1 1
import logging
2
3 1
from flask import Blueprint, current_app as app, redirect
0 ignored issues
show
Configuration introduced by
The import flask could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
4 1
from webargs import fields, flaskparser
0 ignored issues
show
Configuration introduced by
The import webargs could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
5
6 1
from .. import domain
7
8 1
from ._cache import Cache
9 1
from ._utils import route, track, display
10
11
12 1
blueprint = Blueprint('image', __name__)
13 1
log = logging.getLogger(__name__)
14 1
cache = Cache()
15
16 1
OPTIONS = {
17
    'alt': fields.Str(missing=None),
18
    'font': fields.Str(missing=None),
19
    'preview': fields.Bool(missing=False),
20
    'share': fields.Bool(missing=False)
21
}
22
23
24 1
@blueprint.route("/latest.jpg")
25 1
@blueprint.route("/latest<int:index>.jpg")
26 1
def get_latest(index=1):
27 1
    kwargs = cache.get(index - 1)
28
29 1
    if not kwargs:
30 1
        kwargs['key'] = 'custom'
31 1
        kwargs['path'] = "your-meme/goes-here"
32 1
        kwargs['alt'] = "https://raw.githubusercontent.com/jacebrowning/memegen/master/memegen/static/images/missing.png"
33
34 1
    return redirect(route('.get', _external=True, **kwargs))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
35
36
37 1
@blueprint.route("/<key>.jpg")
38 1
@flaskparser.use_kwargs(OPTIONS)
39
def get_without_text(key, **options):
40 1
    options.pop('preview')
41 1
    options.pop('share')
42
43 1
    template = app.template_service.find(key)
44 1
    text = domain.Text(template.default_path)
45
46 1
    return redirect(route('.get', key=key, path=text.path, **options))
47
48
49 1
@blueprint.route("/<key>.jpeg")
50
def get_without_text_jpeg(key):
51 1
    return redirect(route('.get_without_text', key=key))
52
53
54 1
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get')
55 1
@flaskparser.use_kwargs(OPTIONS)
56
def get_with_text(key, path, alt, font, preview, share):
57 1
    options = dict(key=key, path=path, alt=alt, font=font)
58 1
    if preview:
59 1
        options['preview'] = 'true'
60
61 1
    if share:
62
        options['share'] = 'true'
63
64 1
    text = domain.Text(path)
65 1
    fontfile = app.font_service.find(font)
66
67 1
    template = app.template_service.find(key, allow_missing=True)
68 1
    if template.key != key:
69 1
        options['key'] = template.key
70 1
        return redirect(route('.get', **options))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
71
72 1
    if alt and template.path == template.get_path(alt):
73 1
        options.pop('alt')
74 1
        return redirect(route('.get', **options))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
75
76 1
    if font and not fontfile:
77 1
        options.pop('font')
78 1
        return redirect(route('.get', **options))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
79
80 1
    if path != text.path:
81 1
        options['path'] = text.path
82 1
        return redirect(route('.get', **options))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
83
84 1
    image = app.image_service.create(template, text, style=alt, font=fontfile)
85
86 1
    if not preview:
87 1
        cache.add(key=key, path=path, style=alt, font=font)
88 1
        track(image.text)
89
90 1
    return display(image.text, image.path, share=share)
91
92
93 1
@blueprint.route("/<key>/<path:path>.jpeg")
94
def get_with_text_jpeg(key, path):
95 1
    return redirect(route('.get', key=key, path=path))
96
97
98 1
@blueprint.route("/_<code>.jpg")
99
def get_encoded(code):
100
101 1
    key, path = app.link_service.decode(code)
102 1
    template = app.template_service.find(key)
103 1
    text = domain.Text(path)
104 1
    image = app.image_service.create(template, text)
105
106 1
    track(image.text)
107
108
    return display(image.text, image.path)
109