Completed
Push — master ( b13734...b2c969 )
by Felipe A.
47s
created

browsepy.WidgetBase.for_file()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
2
from markupsafe import Markup
3
from flask import url_for
4
5
class WidgetBase(object):
6
    place = None
7
    def __init__(self, *args, **kwargs):
8
        self.args = args
9
        self.kwargs = kwargs
10
11
    def for_file(self, file):
12
        return self
13
14
    @classmethod
15
    def from_file(cls, file):
16
        if not hasattr(cls, '__empty__'):
17
            cls.__empty__ = cls()
18
        return cls.__empty__.for_file(file)
19
20
21
class LinkWidget(WidgetBase):
22
    place = 'link'
23
24
    def __init__(self, text=None, css=None, icon=None):
25
        self.text = text
26
        self.css = css
27
        self.icon = icon
28
        super(LinkWidget, self).__init__()
29
30
    def for_file(self, file):
31
        if None in (self.text, self.icon):
32
            return self.__class__(
33
                file.name if self.text is None else self.text,
34
                self.css,
35
                ('dir-icon' if file.is_directory else 'file-icon') if self.icon is None else self.icon,
36
            )
37
        return self
38
39
40
class ButtonWidget(WidgetBase):
41
    place = 'button'
42
    def __init__(self, html='', text='', css=''):
43
        self.content = Markup(html) if html else text
44
        self.css = css
45
        super(ButtonWidget, self).__init__()
46
47
48
class StyleWidget(WidgetBase):
49
    place = 'style'
50
51
    @property
52
    def href(self):
53
        return url_for(*self.args, **self.kwargs)
54
55
56
class JavascriptWidget(WidgetBase):
57
    place = 'javascript'
58
59
    @property
60
    def src(self):
61
        return url_for(*self.args, **self.kwargs)