summaryrefslogtreecommitdiff
path: root/PyMca5/PyMcaCore/HtmlIndex.py
blob: 3a75c6fe2f8aadb2484aaf875881156376503535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2014 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import os
import sys
import time
from . import PyMcaLogo

class HtmlIndex(object):
    def __init__(self, htmldir):
        if htmldir is None:htmldir = "/tmp/HTML"
        self.htmldir = htmldir

    def isHtml(self, x):
        if len(x) < 5: return 0
        if x[-5:] == ".html":return 1

    def isHtmlDir(self, x):
        if len(x) < 7: return 0
        if x[-7:] == "HTMLDIR":return 1

    def getHeader(self,addlink=None):
        link= [ ['http://www.esrf.fr', 'ESRF home'],
                ['http://www.esrf.fr/computing/bliss/', 'BLISS home'],
          ]
        if addlink is not None:
            for item in addlink:
                link.append(item)
        text =""
        text+= "<HTML>"
        text+= "<HEAD>"
        text+= "<TITLE>PyMCA : Advanced Fit Results</TITLE>"
        text+= "</HEAD>"
        text+= "<BODY TEXT=#000000 BGCOLOR=#FFFFFF ALINK=#ff6600 LINK=#0000cc VLINK=#0000cc marginwidth=10 marginheight=10  topmargin=10 leftmargin=10>"
        text+= "<CENTER>"
        text+= "<TABLE WIDTH=100%% border=0 Height=70>"
        text+= "  <TR>"
        text+= "    <TD><Font Size=5 Color=#0000cc>"
        text+= "        <b>PyMCA : Advanced Fit Results</b></Font>"
        text+= "    </td>"
        text+= "    <td rowspan=2 ALIGN=RIGHT VALIGN=bottom>"
        text+= "        <a HREF=""http://www.esrf.fr/"">"
        logofile = self.htmldir + "/" + "PyMcaLogo.png"
        if not os.path.exists(logofile):
            try:
                import qt
                pixmap = qt.QPixmap(PyMcaLogo.PyMcaLogo)
                pixmap.save(logofile,"PNG")
            except:
                pass
        text+= "        <img SRC=%s ALT=""ESRF home"" WIDTH=55 HEIGHT=68 BORDER=0></a>" % "PyMcaLogo.png"
        text+= "    </td>"
        text+= "  </tr>"
        text+= "  <tr>"
        text+= "     <td width=100%%  VALIGN=bottom>"
        text+= "        <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%%>"
        text+= "          <TR>"
        text+= "            <TD WIDTH=100%% BGCOLOR=#ee22aa HEIGHT=17  ALIGN=LEFT VALIGN=middle>"
        text+= "            <FONT color=#000000>&nbsp;"
        for name in link:
            text+= "|&nbsp;&nbsp;<A STYLE=""color: #FFFFFF"" HREF=""%s"">%s</a>&nbsp;&nbsp;"%(tuple(name))
        text+= "            </FONT>"
        text+= "            </TD>"
        text+= "          </TR>"
        text+= "        </TABLE>"
        text+= "     </td>"
        text+= "  </tr>"
        text+= "  <tr>"
        text+= "     <td colspan=2 height=5><spacer type=block height=10 width=0>"
        text+= "     </td>"
        text+= "  </tr>"
        text+= "</table>"
        text+= "</center>"
        return text

    def getFooter(self):
        now = time.time()
        text =""
        text+= "<center>"
        text+= "<table width=100%% border=0 cellspacing=0 cellpadding=0>"
        text+= "    <tr><td colspan=2 height=10><spacer type=block height=10 width=0></td></tr>"
        text+= "    <tr><td colspan=2 bgcolor=#cc0066 height=5><spacer type=block height=5 width=0></td></tr>"
        text+= "    <tr><td colspan=2 height=5><spacer type=block height=5 width=0></td></tr>"
        text+= "    <TR>"
        text+= "        <TD><FONT size=1 >created:  %s</font></TD>" % time.ctime(now)
        text+= "        <TD ALIGN=RIGHT><FONT size=1 >last modified: %s by" % time.ctime(now)
        #text+= "        <A STYLE=""color: #0000cc"" HREF=""mailto:papillon@esrf.fr"">papillon@esrf.fr</A></FONT></TD>"
        if sys.platform == 'win32':
            try:
                user = os.environ['USERNAME']
                text+= "        <A STYLE=""color: #0000cc"">%s</A></FONT></TD>" % user
            except:
                text +="</FONT></TD>"
        else:
            try:
                user = os.getlogin()
                text+= "        <A STYLE=""color: #0000cc"">%s</A></FONT></TD>" % user
            except:
                text +="</FONT></TD>"
        text+= "    </TR>"
        text+= "</TABLE>"
        text+= "</center>"
        text+= "</BODY>"
        text+= "</HTML>"
        return text

    def getBody(self, htmldir=None):
        if htmldir is None:htmldir = self.htmldir
        dirlist  = filter(self.isHtmlDir, os.listdir(htmldir))
        filelist = []
        for directory in dirlist:
            fulldir = os.path.join(self.htmldir,directory)
            filelist += filter(self.isHtml, os.listdir(fulldir))


        #I have a list of directories and of indexes
        for directory in dirlist:
            fulldir = os.path.join(htmldir,directory)
            index   = os.path.join(fulldir,"index.html")
            if os.path.exists(index):
                try:
                    os.remove(index)
                except:
                    print("cannot delete file %s" % index)
                    continue

    def _getHtmlFileList(self, directory):
        return filter(self.isHtml, os.listdir(directory))

    def _getHtmlDirList(self, directory):
        return filter(self.isHtmlDir, os.listdir(directory))

    def buildIndex(self, directory = None):
        if directory is None: directory = self.htmldir
        index = os.path.join(directory, "index.html")
        if os.path.exists(index):
            try:
                os.remove(index)
            except:
                print("cannot delete file %s" % index)
                return
        filelist = self._getHtmlFileList(directory)
        text = ""
        text += self.getHeader()
        for file in filelist:
            text +="<a href=""%s"">%s</a><BR>" % (file, file.split(".html")[0])
        text += self.getFooter()
        file=open(index,'wb')
        file.write(text)
        file.close()

    def buildRecursiveIndex(self, directory = None):
        if directory is None: directory = self.htmldir
        index = os.path.join(directory, "index.html")
        if os.path.exists(index):
            try:
                os.remove(index)
            except:
                print("cannot delete file %s" % index)
                return
        directorylist = self._getHtmlDirList(directory)
        text = ""
        text += self.getHeader()
        for file in directorylist:
            fulldir = os.path.join(directory,file)
            self.buildIndex(fulldir)
            fileroot = file.split('_HTMLDIR')[0]
            link     = "./"+file+"/index.html"
            text +="<a href=""%s"">%s</a><BR>" % (link, fileroot)
        text += self.getFooter()
        file=open(index,'wb')
        file.write(text)
        file.close()


if __name__ == "__main__":
    if len(sys.argv) > 1:
        a = HtmlIndex(sys.argv[1])
    else:
        print("Trying /tmp/HTML as input directory")
        a = HtmlIndex('/tmp/HTML')
    a.buildRecursiveIndex()