Completed
Pull Request — master (#393)
by Jace
15:10 queued 05:12
created

_get_watermark()   A

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 20
rs 9.2
1 1
import logging
2
3 1
from flask import Blueprint, current_app, request, 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
    'watermark': fields.Str(missing=None),
25
}
26
27 1
28 1
@blueprint.route("/latest.jpg")
29 1
@blueprint.route("/latest<int:index>.jpg")
30 1
@flaskparser.use_kwargs({'filtered': fields.Bool(missing=True)})
31 1
def get_latest(index=1, filtered=True):
32 1
    cache = cache_filtered if filtered else cache_unfiltered
33
    kwargs = cache.get(index - 1)
34 1
35 1
    if kwargs:
36 1
        kwargs['preview'] = True
37 1
    else:
38
        kwargs['key'] = 'custom'
39 1
        kwargs['path'] = "your_meme/goes_here"
40
        kwargs['alt'] = "https://raw.githubusercontent.com/jacebrowning/memegen/master/memegen/static/images/missing.png"
41
42 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...
43 1
44
45 1
@blueprint.route("/<key>.jpg")
46 1
@flaskparser.use_kwargs(OPTIONS)
47
def get_without_text(key, **options):
48 1
    options.pop('preview')
49 1
    options.pop('share')
50
51 1
    template = current_app.template_service.find(key)
52
    text = domain.Text(template.default_path)
53
54 1
    return redirect(route('.get', key=key, path=text.path, **options))
55
56 1
57
@blueprint.route("/<key>.jpeg")
58
def get_without_text_jpeg(key):
59 1
    return redirect(route('.get_without_text', key=key))
60 1
61
62 1
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get')
63 1
@flaskparser.use_kwargs(OPTIONS)
64 1
def get_with_text(key, path, alt, font, watermark, preview, share, **size):
65 1
    options = dict(key=key, path=path, alt=alt, font=font, **size)
66
    if preview:
67
        options['preview'] = True
68 1
    if share:
69 1
        options['share'] = True
70
    options['watermark'] = watermark = _get_watermark(request, watermark)
71 1
72 1
    text = domain.Text(path)
73 1
    fontfile = current_app.font_service.find(font)
74 1
75
    template = current_app.template_service.find(key, allow_missing=True)
76 1
    if template.key != key:
77 1
        options['key'] = template.key
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 alt and template.path == template.get_path(alt):
81 1
        options.pop('alt')
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
    if font and not fontfile:
89
        options.pop('font')
90
        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...
91 1
92 1
    if watermark and watermark not in current_app.config['WATERMARK_OPTIONS']:
93 1
        options.pop('watermark')
94 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...
95
96 1
    image = current_app.image_service.create(
97
        template, text,
98
        style=alt, font=fontfile, size=size, watermark=watermark,
99 1
    )
100
101 1
    if not preview:
102
        cache_filtered.add(key=key, path=path, alt=alt, font=font)
103
        cache_unfiltered.add(key=key, path=path, alt=alt, font=font)
104 1
        track(image.text)
105
106
    return display(image.text, image.path, share=share)
107 1
108 1
109 1
@blueprint.route("/<key>/<path:path>.jpeg")
110 1
def get_with_text_jpeg(key, path):
111
    return redirect(route('.get', key=key, path=path))
112 1
113
114 1
@blueprint.route("/_<code>.jpg")
115
def get_encoded(code):
116
117
    key, path = current_app.link_service.decode(code)
118
    template = current_app.template_service.find(key)
119
    text = domain.Text(path)
120
    image = current_app.image_service.create(template, text)
121
122
    track(image.text)
123
124
    return display(image.text, image.path)
125
126
127
def _get_watermark(_request, watermark):
128
    if watermark:
129
        return watermark
130
131
    referer = _request.environ.get('HTTP_REFERER', "")
132
    for option in current_app.config['WATERMARK_OPTIONS']:
133
        if option in referer:
134
            return None
135
136
    return current_app.config['WATERMARK_OPTIONS'][0]
137