Quelques correctifs au passage en Django 1.1…
[progfou.git] / partageweb / index.py
CommitLineData
4dabedd6
P
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""
4Index automatique des dossiers et sous-dossiers et liens sur les fichiers.
5(Page de téléchargement pour les documents de la CRE en Asie-Pacifique.)
6
7Copyright : Agence universitaire de la Francophonie
8Licence : GNU General Public Licence, version 2
9Auteur : Jean Christophe André
10Date de création : 25 mars 2009
11"""
12
13import os
1890e605 14import re
4dabedd6
P
15import mod_python
16import urllib
17
18HTML = u"""
19<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
20<html>
21<head>
22 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
23 <title>CRE</title>
24 <style type="text/css">
1890e605 25body { background: url('Bamboo2.png') #80FF80; }
4dabedd6
P
26a { color: black; text-decoration: none; }
27a:hover { color: black; text-decoration: underline; }
28a:visited { color: black; text-decoration: underline; }
29li { padding: 3pt; list-style: none; font-weight: bold; font-family: sans; }
30li em { color: black; font-size: 70%%; }
31.hidden { display: none; }
32.visible { display: inline; }
33 </style>
34 <script type="text/javascript">
35function sw(id) {
36 elt = document.getElementById(id)
37 if (elt.className == 'hidden') {
38 elt.className = 'visible';
39 } else {
40 elt.className = 'hidden';
41 }
42}
43 </script>
44</head>
45<body>
46%(content)s
47</body>
48</html>
49"""
50
51id_number = 0
52
53def human_size(size):
54 if size < 1024: return "%s octets" % size
55 size /= 1024
56 if size < 1024: return "%s Kio" % size
57 size /= 1024
58 if size < 1024: return "%s Mio" % size
59 size /= 1024
60 if size < 1024: return "%s Gio" % size
61 size /= 1024
62 return "%s Tio" % size
63
1890e605
P
64def my_cmp(name1, name2):
65 m1 = re.match('[0-9]+', name1)
66 m2 = re.match('[0-9]+', name2)
67 if not m1 or not m2:
68 return cmp(name1, name2)
69 n1 = int(m1.group())
70 n2 = int(m2.group())
71 if n1 == n2:
72 return cmp(name1[m1.end():], name2[m2.end():])
73 return n1 - n2
74
4dabedd6
P
75def my_listdir(dir):
76 path = os.path.join(os.environ["PWD"], dir)
77 names = os.listdir(path)
78 dirs = [x for x in names if os.path.isdir(os.path.join(path, x))]
1890e605 79 dirs.sort(cmp=my_cmp)
4dabedd6 80 files = [x for x in names if os.path.isfile(os.path.join(path, x))]
1890e605 81 files.sort(cmp=my_cmp)
4dabedd6
P
82 return dirs, files
83
84def my_getsize(file):
85 path = os.path.join(os.environ["PWD"], file)
86 return os.path.getsize(path)
87
88def dir_content(root, level=0):
89 global id_number
90 space = u" " * level
91 dirs, files = my_listdir(root)
92 content = space + u"""<div id="l%s" class="%s"><ul>\n""" \
93 % (id_number, level == 0 and "visible" or "hidden")
94 id_number += 1
1890e605 95 for dir in dirs:
4dabedd6
P
96 content += space + u"""<li>"""
97 content += u"""<a href="javascript:void(0)" onclick="sw('l%s')">%s</a>""" \
71b028d0 98 % (id_number, dir.decode('utf-8').replace('_',' '))
4dabedd6
P
99 content += u"""</li>\n"""
100 content += dir_content(os.path.join(root, dir), level + 1)
101 if level > 0:
1890e605 102 for file in files:
4dabedd6
P
103 content += space + u"""<li>"""
104 content += u"""<a href="%s">%s</a> <em>(%s)</em>""" \
71b028d0
P
105 % (urllib.quote(root+'/'+file),
106 file.decode('utf-8').replace('_',' '),
4dabedd6
P
107 human_size(my_getsize(os.path.join(root, file))) )
108 content += u"""</li>\n"""
109 content += space + u"""</ul></div>\n"""
110 return content
111
112def handler(request):
113 html = HTML % {'content': dir_content(".")}
114 request.content_type = "text/html; charset=utf-8"
115 request.write(html.encode('utf-8'))
116 return mod_python.apache.OK
117