File: entry.py

package info (click to toggle)
flask-autoindex 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 288 kB
  • ctags: 165
  • sloc: python: 748; makefile: 8
file content (332 lines) | stat: -rw-r--r-- 10,826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from past.builtins import cmp
from future import standard_library
standard_library.install_hooks()
# -*- coding: utf-8 -*-
from datetime import datetime
from fnmatch import fnmatch
from mimetypes import guess_type
import functools
import os
import re
from future.utils import with_metaclass
from future.moves.urllib.parse import urljoin
from flask import url_for, send_file
from werkzeug import cached_property


Default = None


is_same_path = lambda x, y: os.stat(x) == os.stat(y)


def _make_mimetype_matcher(mimetype):
    return lambda ent: fnmatch(guess_type(ent.name)[0] or '', mimetype)


def _make_args_for_entry(args, kwargs):
    if not args:
        raise TypeError('path is required, but not given')
    rootdir = autoindex = None
    args = list(args)
    try:
        path = kwargs.get('path', args.pop(0))
        rootdir = kwargs.get('rootdir', args.pop(0))
        autoindex = kwargs.get('autoindex', args.pop(0))
    except IndexError:
        pass
    return (path, rootdir, autoindex)


class _EntryMeta(type):
    """The meta class for :class:`Entry`."""

    def __call__(cls, *args, **kwargs):
        """If an instance already initialized, just returns."""
        ent = cls.__new__(cls, *args, **kwargs)
        try:
            ent.path
        except AttributeError:
            ent.__init__(*args, **kwargs)
        return ent


class Entry(with_metaclass(_EntryMeta, object)):
    """This class wraps file or directory. It is an abstract class, but it
    returns a derived instance. You can make an instance such as::

        directory = Entry('/home/someone/public_html')
        assert isinstance(foler, Directory)
        file = Entry('/home/someone/public_html/favicon.ico')
        assert isinstance(file, File)
    """

    HIDDEN = re.compile('^\.')

    def __new__(cls, *args, **kwargs):
        """Returns a file or directory instance."""
        path, rootdir, autoindex = _make_args_for_entry(args, kwargs)
        if rootdir:
            abspath = os.path.join(rootdir.abspath, path)
        else:
            abspath = os.path.abspath(path)
        if os.path.isdir(abspath):
            return Directory.__new__(Directory, path, rootdir, autoindex)
        elif os.path.isfile(abspath):
            return File.__new__(File, path, rootdir, autoindex)
        else:
            raise IOError('{0} does not exists.'.format(abspath))

    def __init__(self, path, rootdir=None, autoindex=None):
        """Initializes an entry instance."""
        self.rootdir = rootdir
        self.autoindex = autoindex
        try:
            rootpath = self.rootdir.abspath
            if not autoindex and self.rootdir:
                self.autoindex = self.rootdir.autoindex
        except AttributeError:
            rootpath = ''
        self.path = path
        self.abspath = os.path.join(rootpath, self.path)
        self.name = os.path.basename(self.abspath)
        self.hidden = bool(self.HIDDEN.match(self.name))
        if self.rootdir:
            self.rootdir._register_descendant(self)

    def is_root(self):
        """Returns ``True`` if it is a root directory."""
        return isinstance(self, RootDirectory)

    @property
    def parent(self):
        if self.is_root():
            return None
        elif is_same_path(os.path.dirname(self.abspath), self.rootdir.abspath):
            return self.rootdir
        return Entry(os.path.dirname(self.path), self.rootdir)

    @property
    def modified(self):
        """Returns modified time of this."""
        return datetime.fromtimestamp(os.path.getmtime(self.abspath))

    @classmethod
    def add_icon_rule(cls, icon, rule=None):
        """Adds a new icon rule globally."""
        cls.icon_map.append((icon, rule))

    @classmethod
    def add_icon_rule_by_name(cls, icon, name):
        """Adds a new icon rule by the name globally."""
        cls.add_icon_rule(icon, lambda ent: ent.name == name)

    @classmethod
    def add_icon_rule_by_class(cls, icon, _class):
        """Adds a new icon rule by the class globally."""
        cls.add_icon_rule(icon, lambda ent: isinstance(ent, _class))

    def guess_icon(self):
        """Guesses an icon from itself."""
        def get_icon_url():
            try:
                if self.autoindex:
                    icon_map = self.autoindex.icon_map + self.icon_map
                else:
                    icon_map = self.icon_map
                for icon, rule in icon_map:
                    if not rule and callable(icon):
                        matched = icon = icon(self)
                    else:
                        matched = rule(self)
                    if matched:
                        return icon
            except AttributeError:
                pass
            try:
                return self.default_icon
            except AttributeError:
                raise GuessError('There is no matched icon.')
        try:
            return urljoin(url_for('.silkicon', filename=''), get_icon_url())
        except (AttributeError, RuntimeError):
            return 'ERROR'
            return get_icon_url()


class File(Entry):
    """This class wraps a file."""

    EXTENSION = re.compile('\.([^.]+)$')

    default_icon = 'page_white.png'
    icon_map = []

    def __new__(cls, path, rootdir=None, autoindex=None):
        try:
            return rootdir._descendants[(path, autoindex)]
        except (AttributeError, KeyError):
            pass
        return object.__new__(cls)

    def __init__(self, path, rootdir=None, autoindex=None):
        super(File, self).__init__(path, rootdir, autoindex)
        try:
            self.ext = re.search(self.EXTENSION, self.name).group(1)
        except AttributeError:
            self.ext = None

    @cached_property
    def data(self):
        """Data of this file."""
        with open(self.abspath) as f:
            return ''.join(f.readlines())

    @cached_property
    def mimetype(self):
        """A mimetype of this file."""
        return guess_type(self.abspath)

    @cached_property
    def size(self):
        """A size of this file."""
        return os.path.getsize(self.abspath)

    @classmethod
    def add_icon_rule_by_ext(cls, icon, ext):
        """Adds a new icon rule by the file extension globally."""
        cls.add_icon_rule(icon, lambda ent: ent.ext == ext)

    @classmethod
    def add_icon_rule_by_mimetype(cls, icon, mimetype):
        """Adds a new icon rule by the mimetype globally."""
        cls.add_icon_rule(icon, _make_mimetype_matcher(mimetype))


class Directory(Entry):
    """This class wraps a directory."""

    default_icon = 'folder.png'
    icon_map = []

    def __new__(cls, *args, **kwargs):
        """If the path is same with root path, it returns a
        :class:`RootDirectory` object.
        """
        path, rootdir, autoindex = _make_args_for_entry(args, kwargs)
        if not rootdir:
            return RootDirectory(path, autoindex)
        try:
            return rootdir._descendants[(path, autoindex)]
        except KeyError:
            pass
        rootpath = rootdir.abspath
        if is_same_path(os.path.join(rootpath, path), rootpath):
            if not rootdir:
                rootdir = RootDirectory(rootpath, autoindex)
            return rootdir
        return object.__new__(cls)

    def explore(self, sort_by='name', order=1, show_hidden=False):
        """It is a generator. Each item is a child entry."""

        def compare(ent1, ent2):
            def asc():
                if sort_by != 'modified' and type(ent1) is not type(ent2):
                    return 1 if type(ent1) is File else -1
                else:
                    try:
                        return cmp(getattr(ent1, sort_by),
                                   getattr(ent2, sort_by))
                    except AttributeError:
                        return cmp(getattr(ent1, 'name'),
                                   getattr(ent2, 'name'))
            return asc() * order
        if not self.is_root():
            yield _ParentDirectory(self)
            rootdir = self.rootdir
        else:
            rootdir = self
        dirlist = os.listdir(self.abspath)
        entries = []
        for name in dirlist:
            try:
                entries.append(Entry(os.path.join(self.path, name), rootdir))
            except IOError:
                continue  # ignore stuff like broken links
        entries = sorted(entries, key=functools.cmp_to_key(compare))
        for ent in entries:
            if show_hidden or not ent.hidden:
                yield ent

    def get_child(self, childname):
        """Returns a child file or directory."""
        if childname in self:
            if self.path != '.':
                path = os.path.join(self.path, childname)
            else:
                path = childname
            return Entry(path, self.rootdir)
        else:
            raise IOError('{0} does not exist'.format(childname))

    def __contains__(self, path_or_entry):
        """Checks this directory has a file or directory.

            public_html = Directory('public_html')
            'favicon.ico' in public_html
            File('favicon.ico', public_html) in public_html
        """
        if isinstance(path_or_entry, Entry):
            path = os.path.relpath(path_or_entry.path, self.path)
            if os.path.pardir in path:
                return False
        else:
            path = path_or_entry
        return os.path.exists(os.path.join(self.abspath, path))


class RootDirectory(Directory):
    """This class wraps a root directory."""

    default_icon = 'server.png'
    icon_map = []
    _rootdirs = {}

    def __new__(cls, path, autoindex=None):
        try:
            return RootDirectory._rootdirs[(path, autoindex)]
        except KeyError:
            return object.__new__(cls)

    def __init__(self, path, autoindex=None):
        super(RootDirectory, self).__init__('.', autoindex=autoindex)
        self.abspath = os.path.abspath(path)
        self.rootdir = self
        self._descendants = {}
        RootDirectory._register_rootdir(self)

    @classmethod
    def _register_rootdir(cls, rootdir):
        cls._rootdirs[(rootdir.abspath, rootdir.autoindex)] = rootdir

    def _register_descendant(self, entry):
        self._descendants[(entry.path, entry.autoindex)] = entry


class _ParentDirectory(Directory):
    """This class wraps a parent directory."""

    default_icon = 'arrow_turn_up.png'
    icon_map = []

    def __new__(cls, *args, **kwargs):
        return object.__new__(cls)

    def __init__(self, child_directory):
        path = os.path.join(child_directory.path, '..')
        super(_ParentDirectory, self).__init__(path, child_directory.rootdir)


class GuessError(RuntimeError): pass
class MarkupError(RuntimeError): pass