GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ImagineFilesystemAdapter   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 136
rs 10
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 8 1
B create_cached_item() 0 24 3
A check_cached_item() 0 16 2
A remove_cached_item() 0 16 2
A get_cached_item() 0 20 3
B get_item() 0 26 4
A make_dirs() 0 13 3
1
"""
2
This module implement a filesystem storage adapter.
3
"""
4
from __future__ import unicode_literals
5
import errno
6
import logging
7
import os
8
from flask import current_app
9
from .interface import ImagineAdapterInterface
10
from PIL import Image
11
12
LOGGER = logging.getLogger(__name__)
13
14
15
class ImagineFilesystemAdapter(ImagineAdapterInterface):
16
    """
17
    Filesystem storage adapter
18
    """
19
    source_folder = None
20
    cache_folder = None
21
22
    def __init__(self, **kwargs):
23
        """
24
        Init _adapter
25
        :param kwargs: parameters
26
        :return:
27
        """
28
        self.source_folder = kwargs.get('source_folder', '').strip('/')
29
        self.cache_folder = kwargs.get('cache_folder', 'cache').strip('/')
30
31
    def get_item(self, path):
32
        """
33
        Get resource item
34
        :param path: string
35
        :return: PIL.Image
36
        """
37
        if self.source_folder:
38
            item_path = '%s/%s/%s' % (
39
                    current_app.static_folder,
40
                    self.source_folder,
41
                    path.strip('/')
42
                )
43
        else:
44
            item_path = '%s/%s' % (
45
                    current_app.static_folder,
46
                    path.strip('/')
47
                )
48
49
        if os.path.isfile(item_path):
50
            try:
51
                return Image.open(item_path)
52
            except IOError as err:
53
                LOGGER.warning('File not found on path "%s" with error: %s' % (item_path, str(err)))
54
                return False
55
        else:
56
            return False
57
58
    def create_cached_item(self, path, content):
59
        """
60
        Create cached resource item
61
        :param path: str
62
        :param content: Image
63
        :return: str
64
        """
65
        if isinstance(content, Image.Image):
66
            item_path = '%s/%s/%s' % (
67
                current_app.static_folder,
68
                self.cache_folder,
69
                path.strip('/')
70
            )
71
            self.make_dirs(item_path)
72
73
            content.save(item_path)
74
75
            if os.path.isfile(item_path):
76
                return '%s/%s/%s' % (current_app.static_url_path, self.cache_folder, path.strip('/'))
77
            else:  # pragma: no cover
78
                LOGGER.warning('File is not created on path: %s' % item_path)
79
                return False
80
        else:
81
            return False
82
83
    def get_cached_item(self, path):
84
        """
85
        Get cached resource item
86
        :param path: str
87
        :return: PIL.Image
88
        """
89
        item_path = '%s/%s/%s' % (
90
                current_app.static_folder,
91
                self.cache_folder,
92
                path.strip('/')
93
            )
94
95
        if os.path.isfile(item_path):
96
            try:
97
                return Image.open(item_path)
98
            except IOError as err:  # pragma: no cover
99
                LOGGER.warning('Cached file not found on path "%s" with error: %s' % (item_path, str(err)))
100
                return False
101
        else:
102
            return False
103
104
    def check_cached_item(self, path):
105
        """
106
        Check for cached resource item exists
107
        :param path: str
108
        :return: bool
109
        """
110
        item_path = '%s/%s/%s' % (
111
                current_app.static_folder,
112
                self.cache_folder,
113
                path.strip('/')
114
            )
115
116
        if os.path.isfile(item_path):
117
            return '%s/%s/%s' % (current_app.static_url_path, self.cache_folder, path.strip('/'))
118
        else:
119
            return False
120
121
    def remove_cached_item(self, path):
122
        """
123
        Remove cached resource item
124
        :param path: str
125
        :return: bool
126
        """
127
        item_path = '%s/%s/%s' % (
128
                current_app.static_folder,
129
                self.cache_folder,
130
                path.strip('/')
131
            )
132
133
        if os.path.isfile(item_path):
134
            os.remove(item_path)
135
136
        return True
137
138
    @staticmethod
139
    def make_dirs(path):
140
        """
141
        Create directories if not exist
142
        :param path: string
143
        :return:
144
        """
145
        try:
146
            os.makedirs(os.path.dirname(path))
147
        except OSError as err:
148
            if err.errno != errno.EEXIST:
149
                LOGGER.error('Failed to create directory %s with error: %s' % (path, str(err)))
150
                raise
151