File: PageIcon.py

package info (click to toggle)
cherokee 0.7.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,808 kB
  • ctags: 6,577
  • sloc: ansic: 45,071; python: 9,628; sh: 9,468; makefile: 1,639; xml: 61; perl: 32
file content (203 lines) | stat: -rw-r--r-- 6,757 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
import os.path

from Page import *
from Form import *
from Table import *
from Entry import *
from configured import *

class PageIcon (PageMenu, FormHelper):
    def __init__ (self, cfg):
        PageMenu.__init__ (self, 'icon', cfg)
        FormHelper.__init__ (self, 'icon', cfg)

    def _op_render (self):
        content = self._render_icon_list()

        self.AddMacroContent ('title', 'Icon configuration')
        self.AddMacroContent ('content', content)

        return Page.Render(self)

    def _op_handler (self, uri, post):
        if uri.startswith('/update'):
            return self._op_apply_changes (post)
        elif uri.startswith('/add_file'):
            return self._op_add_file (post)
        elif uri.startswith('/add_suffix'):
            return self._op_add_suffix (post)
        raise 'Unknown method'

    def _op_add_file (self, post):
        match = post.get_val('file_new_match')
        file  = post.get_val('file_new_file')

        # Add it to the config
        self._cfg["icons!file!%s" % (file)] = match
        # Return the URL
        return "/%s" % (self._id)

    def _op_add_suffix (self, post):
        exts = post['suffix_new_exts'][0]
        file = post['suffix_new_file'][0]

        # Add it to the config
        self._cfg["icons!suffix!%s" % (file)] = exts
        # Return the URL
        return "/%s" % (self._id)

    def _get_options_icons (self, cfg_key, file_filter=None, selected=None):
        file_options = []

        # No icon
        if file_filter and not file_filter(''):
            file_options.append (('', 'None'))

        # Build icons list
        for file in os.listdir (CHEROKEE_ICONSDIR):
            if file_filter:
                ignore = file_filter (file)
                if ignore: continue

            f = file.lower()
            if (f.endswith(".jpg") or
                f.endswith(".png") or
                f.endswith(".gif") or
                f.endswith(".svg")):
                file_options.append((file, file))

        # Check selected
        if not selected:
            selected = self._cfg.get_val(cfg_key)
            
        # Build the options
        if selected:
            options = EntryOptions (cfg_key, file_options, 
                                    onChange='return option_icons_update(\'%s\');'%(cfg_key), 
                                    selected=selected)
        else:
            options = EntryOptions (cfg_key, file_options,
                                    onChange='return option_icons_update(\'%s\');'%(cfg_key))

        # Get the image
        image = self._get_img_from_icon (selected, cfg_key)
        return options, image

    def _get_img_from_icon (self, icon_name, cfg_key):
        if not icon_name:
            return '<div id="image_%s"></div>'%(cfg_key)

        local_file  = os.path.join (CHEROKEE_ICONSDIR, icon_name)
        if not os.path.exists (local_file):
            comment = "<!-- Couldn't find %s -->" % (icon_name)
            return '<div id="image_%s">%s</div>' % (cfg_key, comment)

        remote_file = os.path.join ('/icons_local', icon_name)
        return '<div id="image_%s"><img src="%s" /></div>' % (cfg_key, remote_file)

    def _render_icon_list (self):        
        # Include its JavaScript file
        #
        txt  = '<script src="/static/js/icons.js" type="text/javascript"></script>'
        txt += "<h1>Icons configuration</h1>"

        tabs = []
        
        # Suffixes
        #
        icons = self._cfg['icons!suffix']

        tmp = ''
        if icons and icons.has_child():
            tmp += "<h3>Extension list</h3>"

            table = Table(4, 1)
            table += ('', 'File', 'Extensions', '')

            for icon in icons:
                cfg_key  = 'icons!suffix!%s' % (icon)
                im = self._get_img_from_icon (icon, cfg_key)

                entry  = self.InstanceEntry (cfg_key, 'text', size=45)
                js     = "post_del_key('/icons/update', '%s');" % (cfg_key)
                link_del = self.InstanceImage ("bin.png", "Delete", border="0", onClick=js)
                table += (im, icon, entry, link_del)

            tmp += self.Indent(table)

        # New suffix
        fo1 = Form ("/%s/add_suffix" % (self._id), add_submit=False)
        op1, im1 = self._get_options_icons ('suffix_new_file', 
                                            self._filter_icons_in_suffixes)
        en2 = self.InstanceEntry('suffix_new_exts', 'text')
        ta1 = Table (4,1)
        ta1 += ('', 'Icon', 'Extensions', '')
        ta1 += (im1, op1, en2, SUBMIT_ADD)
        tmp += "<h3>Add suffix</h3>"
        tmp += fo1.Render(self.Indent(ta1))

        tabs += [('Extensions', tmp)]

        # Special icons
        #
        table = Table(3)
        icons = self._cfg['icons']

        op_def, im_def = self._get_options_icons('icons!default')
        op_dir, im_dir = self._get_options_icons('icons!directory')
        op_par, im_par = self._get_options_icons('icons!parent_directory')

        table += (im_def, 'Default',      op_def)
        table += (im_dir, 'Directory',    op_dir)
        table += (im_par, 'Go to Parent', op_par)

        tabs += [('Special Icons', str(table))]
        
        # Files
        #
        icons = self._cfg['icons!file']

        tmp = ''
        if icons and icons.has_child():
            tmp += "<h3>File list</h3>"

            table = Table(4, 1)
            table += ('', 'Match', 'File')

            for icon_name in icons:
                cfg_key = 'icons!file!%s' % (icon_name)
                match   = self._cfg.get_val(cfg_key)
                op, im  = self._get_options_icons (cfg_key, selected=icon_name)
                js = "post_del_key('/icons/update', '%s');" % (cfg_key)
                link_del = self.InstanceImage ("bin.png", "Delete", border="0", onClick=js)
                table += (im, match, op, link_del)

            tmp += self.Indent(table)

        # New file
        fo1 = Form ("/%s/add_file" % (self._id), add_submit=False)
        op1, im1 = self._get_options_icons ('file_new_file')
        en1 = self.InstanceEntry('file_new_match', 'text')
        ta1 = Table (4,1)
        ta1 += ('', 'File', 'Icon', '')
        ta1 += (im1, en1, op1, SUBMIT_ADD)
        tmp += "<h3>Add file</h3>"
        tmp += fo1.Render(self.Indent(ta1))

        tabs += [('Files', tmp)]


        txt += self.InstanceTab (tabs)
        return txt

    def _op_apply_changes (self, post):
        self.ApplyChanges ([], post)
        return "/%s" % (self._id)

    def _filter_icons_in_suffixes (self, file):
        cfg = self._cfg['icons!suffix']
        if not cfg:
            return False
        if not file:
            return True
        return file in cfg.keys()