Completed
Pull Request — master (#396)
by Jace
07:56
created

_get_watermark()   D

Complexity

Conditions 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8.0551

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 8
c 5
b 0
f 0
dl 0
loc 25
rs 4
ccs 19
cts 21
cp 0.9048
crap 8.0551
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
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 1
    kwargs = cache.get(index - 1)
34
35 1
    if kwargs:
36 1
        kwargs['preview'] = True
37
    else:
38 1
        kwargs['key'] = 'custom'
39 1
        kwargs['path'] = "your_meme/goes_here"
40 1
        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
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 1
    text = domain.Text(template.default_path)
53
54 1
    return redirect(route('.get', key=key, path=text.path, **options))
55
56
57 1
@blueprint.route("/<key>.jpeg")
58
def get_without_text_jpeg(key):
59 1
    return redirect(route('.get_without_text', key=key))
60
61
62 1
@blueprint.route("/<key>/<path:path>.jpg", endpoint='get')
63 1
@flaskparser.use_kwargs(OPTIONS)
64
def get_with_text(key, path, alt, font, watermark, preview, share, **size):
65 1
    options = dict(key=key, path=path,
66
                   alt=alt, font=font, watermark=watermark, **size)
67 1
    if preview:
68 1
        options['preview'] = True
69 1
    if share:
70
        options['share'] = True
71
72 1
    text = domain.Text(path)
73 1
    fontfile = current_app.font_service.find(font)
74
75 1
    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 1
        options.pop('font')
90 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...
91
92 1
    watermark, valid = _get_watermark(request, watermark)
93 1
    if not valid:
94 1
        options.pop('watermark')
95 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...
96
97 1
    image = current_app.image_service.create(
98
        template, text,
99
        style=alt, font=fontfile, size=size, watermark=watermark,
100
    )
101
102 1
    if not preview:
103 1
        cache_filtered.add(key=key, path=path, alt=alt, font=font)
104 1
        cache_unfiltered.add(key=key, path=path, alt=alt, font=font)
105 1
        track(image.text)
106
107 1
    return display(image.text, image.path, share=share)
108
109
110 1
@blueprint.route("/<key>/<path:path>.jpeg")
111
def get_with_text_jpeg(key, path):
112 1
    return redirect(route('.get', key=key, path=path))
113
114
115 1
@blueprint.route("/_<code>.jpg")
116
def get_encoded(code):
117
118 1
    key, path = current_app.link_service.decode(code)
119 1
    template = current_app.template_service.find(key)
120 1
    text = domain.Text(path)
121 1
    image = current_app.image_service.create(template, text)
122
123 1
    track(image.text)
124
125 1
    return display(image.text, image.path)
126
127
128 1
def _get_watermark(_request, watermark):
129 1
    referrer = _request.environ.get('HTTP_REFERER', "")
130 1
    agent = _request.environ.get('HTTP_USER_AGENT', "")
131 1
    log.debug("Referrer: %r, Agent: %r", referrer, agent)
132
133 1
    if watermark == 'none':
134 1
        for option in current_app.config['WATERMARK_OPTIONS']:
135 1
            for identity in (referrer, agent):
136 1
                if option in identity:
137 1
                    log.debug("Watermark disabled (%r in %r)", option, identity)
138 1
                    return None, True
139
        log.warning("Request does not support unmarked images")
140
        return None, False
141
142 1
    if watermark and watermark not in current_app.config['WATERMARK_OPTIONS']:
143 1
        log.warning("Unsupported custom watermark: %r", watermark)
144 1
        return watermark, False
145
146 1
    if watermark:
147 1
        log.debug("Using custom watermark: %r", watermark)
148 1
        return watermark, True
149
150 1
    default = current_app.config['WATERMARK_OPTIONS'][0]
151 1
    log.debug("Using default watermark: %r", default)
152
    return default, True
153