list2form : gestion des erreurs et des petits fichiers (merci cgi.py…)
[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):
b6eb652d 54 if size <= 1: return "%s octet" % size
4dabedd6
P
55 if size < 1024: return "%s octets" % size
56 size /= 1024
57 if size < 1024: return "%s Kio" % size
58 size /= 1024
59 if size < 1024: return "%s Mio" % size
60 size /= 1024
61 if size < 1024: return "%s Gio" % size
62 size /= 1024
63 return "%s Tio" % size
64
1890e605 65def my_cmp(name1, name2):
b6eb652d
P
66 m1 = re.match('[0-9]{1,8}', name1)
67 m2 = re.match('[0-9]{1,8}', name2)
1890e605
P
68 if not m1 or not m2:
69 return cmp(name1, name2)
70 n1 = int(m1.group())
71 n2 = int(m2.group())
72 if n1 == n2:
73 return cmp(name1[m1.end():], name2[m2.end():])
74 return n1 - n2
75
4dabedd6
P
76def my_listdir(dir):
77 path = os.path.join(os.environ["PWD"], dir)
78 names = os.listdir(path)
79 dirs = [x for x in names if os.path.isdir(os.path.join(path, x))]
1890e605 80 dirs.sort(cmp=my_cmp)
4dabedd6 81 files = [x for x in names if os.path.isfile(os.path.join(path, x))]
1890e605 82 files.sort(cmp=my_cmp)
4dabedd6
P
83 return dirs, files
84
85def my_getsize(file):
86 path = os.path.join(os.environ["PWD"], file)
87 return os.path.getsize(path)
88
89def dir_content(root, level=0):
90 global id_number
91 space = u" " * level
92 dirs, files = my_listdir(root)
93 content = space + u"""<div id="l%s" class="%s"><ul>\n""" \
94 % (id_number, level == 0 and "visible" or "hidden")
95 id_number += 1
1890e605 96 for dir in dirs:
4dabedd6
P
97 content += space + u"""<li>"""
98 content += u"""<a href="javascript:void(0)" onclick="sw('l%s')">%s</a>""" \
71b028d0 99 % (id_number, dir.decode('utf-8').replace('_',' '))
4dabedd6
P
100 content += u"""</li>\n"""
101 content += dir_content(os.path.join(root, dir), level + 1)
102 if level > 0:
1890e605 103 for file in files:
4dabedd6
P
104 content += space + u"""<li>"""
105 content += u"""<a href="%s">%s</a> <em>(%s)</em>""" \
71b028d0
P
106 % (urllib.quote(root+'/'+file),
107 file.decode('utf-8').replace('_',' '),
4dabedd6
P
108 human_size(my_getsize(os.path.join(root, file))) )
109 content += u"""</li>\n"""
110 content += space + u"""</ul></div>\n"""
111 return content
112
113def handler(request):
114 html = HTML % {'content': dir_content(".")}
115 request.content_type = "text/html; charset=utf-8"
116 request.write(html.encode('utf-8'))
117 return mod_python.apache.OK
118