Completed
Push — dev-4.1-unstable ( 90aa3b...e0128e )
by Felipe A.
59s
created

StyleWidget   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

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

1 Method

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