Completed
Push — master ( 874f22...ba55f4 )
by Jace
24s
created

get_with_text()   F

Complexity

Conditions 10

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 10.0045

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
c 1
b 0
f 0
dl 0
loc 38
ccs 27
cts 28
cp 0.9643
crap 10.0045
rs 3.1304

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_filtered = Cache()
15 1
cache_unfiltered = Cache(filtered=False)
16
17 1
OPTIONS = {
18
    'alt': fields.Str(missing=None),
19
    'font': fields.Str(missing=None),
20
    'preview': fields.Bool(missing=False),
21
    'share': fields.Bool(missing=False),
22
    'width': fields.Int(missing=None),
23
    'height': fields.Int(missing=None),
24
}
25
26
27 1
@blueprint.route("/latest.jpg")
28 1
@blueprint.route("/latest<int:index>.jpg")
29 1
@flaskparser.use_kwargs({'filtered': fields.Bool(missing=True)})
30 1
def get_latest(index=1, filtered=True):
31 1
    cache = cache_filtered if filtered else cache_unfiltered
32 1
    kwargs = cache.get(index - 1)
33
34 1
    if not kwargs:
35 1
        kwargs['key'] = 'custom'
36 1
        kwargs['path'] = "your-meme/goes-here"
37 1
        kwargs['alt'] = "https://raw.githubusercontent.com/jacebrowning/memegen/master/memegen/static/images/missing.png"
38
39 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...
40
41
42 1
@blueprint.route("/<key>.jpg")
43 1
@flaskparser.use_kwargs(OPTIONS)
44
def get_without_text(key, **options):
45 1
    options.pop('preview')
46 1
    options.pop('share')
47
48 1
    template = app.template_service.find(key)
49 1
    text = domain.Text(template.default_path)
50
51 1
    return redirect(route('.get', key=key, path=text.path, **options))
52
53
54 1
@blueprint.route("/<key>.jpeg")
55
def get_without_text_jpeg(key):
56 1
    return redirect(route('.get_without_text', key=key))
57
58
59 1
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get')
60 1
@flaskparser.use_kwargs(OPTIONS)
61
def get_with_text(key, path, alt, font, preview, share, **size):
62 1
    options = dict(key=key, path=path, alt=alt, font=font, **size)
63 1
    if preview:
64 1
        options['preview'] = 'true'
65 1
    if share:
66
        options['share'] = 'true'
67
68 1
    text = domain.Text(path)
69 1
    fontfile = app.font_service.find(font)
70
71 1
    template = app.template_service.find(key, allow_missing=True)
72 1
    if template.key != key:
73 1
        options['key'] = template.key
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 alt and template.path == template.get_path(alt):
77 1
        options.pop('alt')
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 font and not fontfile:
81 1
        options.pop('font')
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
    if path != text.path:
85 1
        options['path'] = text.path
86 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...
87
88 1
    image = app.image_service.create(template, text,
89
                                     style=alt, font=fontfile, size=size)
90
91 1
    if not preview:
92 1
        cache_filtered.add(key=key, path=path, alt=alt, font=font)
93 1
        cache_unfiltered.add(key=key, path=path, alt=alt, font=font)
94 1
        track(image.text)
95
96 1
    return display(image.text, image.path, share=share)
97
98
99 1
@blueprint.route("/<key>/<path:path>.jpeg")
100
def get_with_text_jpeg(key, path):
101 1
    return redirect(route('.get', key=key, path=path))
102
103
104 1
@blueprint.route("/_<code>.jpg")
105
def get_encoded(code):
106
107 1
    key, path = app.link_service.decode(code)
108 1
    template = app.template_service.find(key)
109 1
    text = domain.Text(path)
110 1
    image = app.image_service.create(template, text)
111
112 1
    track(image.text)
113
114
    return display(image.text, image.path)
115