partageweb : gestion des nombres trop grands.
[progfou.git] / partageweb / index.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Index 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
7 Copyright : Agence universitaire de la Francophonie
8 Licence : GNU General Public Licence, version 2
9 Auteur : Jean Christophe André
10 Date de création : 25 mars 2009
11 """
12
13 import os
14 import re
15 import mod_python
16 import urllib
17
18 HTML = 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">
25 body { background: url('Bamboo2.png') #80FF80; }
26 a { color: black; text-decoration: none; }
27 a:hover { color: black; text-decoration: underline; }
28 a:visited { color: black; text-decoration: underline; }
29 li { padding: 3pt; list-style: none; font-weight: bold; font-family: sans; }
30 li em { color: black; font-size: 70%%; }
31 .hidden { display: none; }
32 .visible { display: inline; }
33 </style>
34 <script type="text/javascript">
35 function 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
51 id_number = 0
52
53 def human_size(size):
54 if size <= 1: return "%s octet" % size
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
65 def my_cmp(name1, name2):
66 m1 = re.match('[0-9]{1,8}', name1)
67 m2 = re.match('[0-9]{1,8}', name2)
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
76 def 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))]
80 dirs.sort(cmp=my_cmp)
81 files = [x for x in names if os.path.isfile(os.path.join(path, x))]
82 files.sort(cmp=my_cmp)
83 return dirs, files
84
85 def my_getsize(file):
86 path = os.path.join(os.environ["PWD"], file)
87 return os.path.getsize(path)
88
89 def 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
96 for dir in dirs:
97 content += space + u"""<li>"""
98 content += u"""<a href="javascript:void(0)" onclick="sw('l%s')">%s</a>""" \
99 % (id_number, dir.decode('utf-8').replace('_',' '))
100 content += u"""</li>\n"""
101 content += dir_content(os.path.join(root, dir), level + 1)
102 if level > 0:
103 for file in files:
104 content += space + u"""<li>"""
105 content += u"""<a href="%s">%s</a> <em>(%s)</em>""" \
106 % (urllib.quote(root+'/'+file),
107 file.decode('utf-8').replace('_',' '),
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
113 def 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