Completed
Push — dev-4.1 ( dab2a4...5e66f2 )
by Felipe A.
01:11
created

HeadButtonWidget   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 7
rs 10
c 1
b 0
f 1
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 4 2
1
2
from markupsafe import Markup
3
from flask import url_for
4
5
6
class WidgetBase(object):
7
    place = None
8
9
    def __init__(self, *args, **kwargs):
10
        self.args = args
11
        self.kwargs = kwargs
12
13
    def for_file(self, file):
14
        return self
15
16
    @classmethod
17
    def from_file(cls, file):
18
        if not hasattr(cls, '__empty__'):
19
            cls.__empty__ = cls()
20
        return cls.__empty__.for_file(file)
21
22
23
class LinkWidget(WidgetBase):
24
    place = 'link'
25
26
    def __init__(self, text=None, css=None, icon=None):
27
        self.text = text
28
        self.css = css
29
        self.icon = icon
30
        super(LinkWidget, self).__init__()
31
32
    def for_file(self, file):
33
        if None in (self.text, self.icon):
34
            icon = self.icon
35
            if icon is None:
36
                icon = 'dir-icon' if file.is_directory else 'file-icon'
37
            return self.__class__(
38
                file.name if self.text is None else self.text,
39
                self.css,
40
                icon,
41
            )
42
        return self
43
44
45
class ButtonWidget(WidgetBase):
46
    place = 'button'
47
48
    def __init__(self, html='', text='', css=''):
49
        self.content = Markup(html) if html else text
50
        self.css = css
51
        super(ButtonWidget, self).__init__()
52
53
54
class HeadButtonWidget(WidgetBase):
55
    place = 'head-button'
56
57
    def __init__(self, html='', text='', css=''):
58
        self.content = Markup(html) if html else text
59
        self.css = css
60
        super(HeadButtonWidget, self).__init__()
61
62
63
class StyleWidget(WidgetBase):
64
    place = 'style'
65
66
    @property
67
    def href(self):
68
        return url_for(*self.args, **self.kwargs)
69
70
71
class JavascriptWidget(WidgetBase):
72
    place = 'javascript'
73
74
    @property
75
    def src(self):
76
        return url_for(*self.args, **self.kwargs)
77