~juliank/ftpsync/main

« back to all changes in this revision

Viewing changes to ftpsynclib/html_index.py

  • Committer: Julian Andres Klode
  • Date: 2007-06-20 16:37:24 UTC
  • Revision ID: jak@jak-linux.org-20070620163724-fhqyx84z6q2wtg8y
* Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Julian Andres Klode <jak@jak-linux.org>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
import os
 
18
from ftpsynclib import fsum,size
 
19
 
 
20
def html_index(dir,remote="/", base=True, ignore=[]):
 
21
        '''Create an index of a folder and sub-folders'''
 
22
        dirs = []
 
23
        files = []
 
24
        real = {}
 
25
 
 
26
        if dir[-1] != "/": 
 
27
                dir=dir + "/"
 
28
        if remote[-1] != "/":
 
29
                remote=remote + "/"
 
30
        std = os.getcwd()
 
31
        os.chdir(dir)
 
32
        o=open("index.html", "w")
 
33
        o.write("""<?xml version="1.0"?>\n""")
 
34
        o.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n""")
 
35
        o.write("""<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
36
        <head>
 
37
                <!--ftpsync-->
 
38
                <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
 
39
                <title>Index of """ + remote + """</title>
 
40
                <style type="text/css">
 
41
                        table, .a,.logo { margin: auto; width: 75%}
 
42
                        .logo, .logo a, .logo img { border: none; text-decoration: none; text-align: center }
 
43
                        td, th { padding:  0 30px}
 
44
                        h1{ text-align: center }
 
45
                        a { color: blue; text-decoration: none }
 
46
                        #xvisited { color: #ccc}
 
47
                        table,tr,td,th { border: 1px solid #ddd; border-collapse: collapse }
 
48
                        td.size { text-align: right; border-right: 0px}
 
49
                </style>
 
50
        </head>
 
51
        <body>""")
 
52
        if os.path.exists(".top.html"):
 
53
                o.write("""
 
54
                <div class="a">
 
55
                %s
 
56
                </div>""" % ( file(".top.html").read()))
 
57
        else:
 
58
                o.write("""<h1>Index of """ + remote + """</h1>""")
 
59
        o.write("""
 
60
                <table>
 
61
                        <tr><th>Filename</th><th>SHA1sum</th><th>Size</th></tr>\n""")
 
62
 
 
63
 
 
64
        for i in os.listdir("."):
 
65
                if i == "index.html" or i[-1] == "~" or i[0] == "." or i in ignore  : continue
 
66
                if os.path.isdir(i):
 
67
                        html_index(i, remote + i + "/", False)
 
68
                        dirs += [i]
 
69
                else:
 
70
                        files += [i.replace(".tar.gz", "")]
 
71
                        real[i.replace(".tar.gz", "")] = i
 
72
        dirs.sort()
 
73
        files.sort()
 
74
 
 
75
        if base == False: o.write('\t\t\t<tr><td><a style="color: #888" href="../">Parent Folder (../)</a></td><td colspan="3"></td></tr>\n')
 
76
        for i in dirs:
 
77
                o.write('\t\t\t<tr><td><a href="%s/">%s/</a></td><td colspan="3"></td></tr>\n' % (i, i))
 
78
        for x in files:
 
79
                i=real[x]
 
80
                o.write('\t\t\t<tr><td><a href="%s">%s</a></td><td class="size">%s</td><td class="size">%s</td></tr>\n' % (i, i, fsum(i), size(i)) )
 
81
        o.write("""
 
82
                </table>""")
 
83
        if os.path.exists(".foot.html"):
 
84
                o.write("""
 
85
                <div class="a">
 
86
                %s
 
87
                </div>""" % ( file(".foot.html").read()))
 
88
        o.write("""
 
89
                <p style="text-align: center; font-size: 75%; color: #ccc"><a style="color: #ccc; text-decoration: underline" href="http://jak-linux.org/projects/jl-ftpsync/">jl-ftpsync</a>/0.1 - &copy; 2007 <a style="color: #ccc; text-decoration: underline" href="mailto:Julian Andres Klode &lt;jak@jak-linux.org&gt;">Julian Andres Klode</a></p>
 
90
                <div class="logo">
 
91
                        <a href="http://validator.w3.org/check?uri=referer">
 
92
                                <img src="/valid-xhtml10.png" alt="Valid XHTML 1.0 Strict" height="31" width="88" />
 
93
                        </a>
 
94
                </div>
 
95
        </body>\n</html>""")
 
96
        o.close()
 
97
        os.chdir(std)