Premiers exemples...
[progfou.git] / mod_python / 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 mod_python
15 import urllib
16
17 HTML = u"""
18 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
19 <html>
20 <head>
21 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
22 <title>CRE</title>
23 <style type="text/css">
24 body { background: url('Bamboo2-jc.png') #80FF80; }
25 a { color: black; text-decoration: none; }
26 a:hover { color: black; text-decoration: underline; }
27 a:visited { color: black; text-decoration: underline; }
28 li { padding: 3pt; list-style: none; font-weight: bold; font-family: sans; }
29 li em { color: black; font-size: 70%%; }
30 .hidden { display: none; }
31 .visible { display: inline; }
32 </style>
33 <script type="text/javascript">
34 function sw(id) {
35 elt = document.getElementById(id)
36 if (elt.className == 'hidden') {
37 elt.className = 'visible';
38 } else {
39 elt.className = 'hidden';
40 }
41 }
42 </script>
43 </head>
44 <body>
45 %(content)s
46 </body>
47 </html>
48 """
49
50 id_number = 0
51
52 def human_size(size):
53 if size < 1024: return "%s octets" % size
54 size /= 1024
55 if size < 1024: return "%s Kio" % size
56 size /= 1024
57 if size < 1024: return "%s Mio" % size
58 size /= 1024
59 if size < 1024: return "%s Gio" % size
60 size /= 1024
61 return "%s Tio" % size
62
63 def my_listdir(dir):
64 path = os.path.join(os.environ["PWD"], dir)
65 names = os.listdir(path)
66 dirs = [x for x in names if os.path.isdir(os.path.join(path, x))]
67 files = [x for x in names if os.path.isfile(os.path.join(path, x))]
68 return dirs, files
69
70 def my_getsize(file):
71 path = os.path.join(os.environ["PWD"], file)
72 return os.path.getsize(path)
73
74 def dir_content(root, level=0):
75 global id_number
76 space = u" " * level
77 dirs, files = my_listdir(root)
78 content = space + u"""<div id="l%s" class="%s"><ul>\n""" \
79 % (id_number, level == 0 and "visible" or "hidden")
80 id_number += 1
81 for dir in sorted(dirs):
82 content += space + u"""<li>"""
83 content += u"""<a href="javascript:void(0)" onclick="sw('l%s')">%s</a>""" \
84 % (id_number, dir.decode('utf-8'))
85 content += u"""</li>\n"""
86 content += dir_content(os.path.join(root, dir), level + 1)
87 if level > 0:
88 for file in sorted(files):
89 content += space + u"""<li>"""
90 content += u"""<a href="%s">%s</a> <em>(%s)</em>""" \
91 % (urllib.quote(root+'/'+file), file.decode('utf-8'),
92 human_size(my_getsize(os.path.join(root, file))) )
93 content += u"""</li>\n"""
94 content += space + u"""</ul></div>\n"""
95 return content
96
97 def handler(request):
98 html = HTML % {'content': dir_content(".")}
99 request.content_type = "text/html; charset=utf-8"
100 request.write(html.encode('utf-8'))
101 return mod_python.apache.OK
102