#/*##########################################################################
#
# 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
[docs]class HtmlIndex(object):
def __init__(self, htmldir):
if htmldir is None:htmldir = "/tmp/HTML"
self.htmldir = htmldir
[docs] def isHtml(self, x):
if len(x) < 5: return 0
if x[-5:] == ".html":return 1
[docs] def isHtmlDir(self, x):
if len(x) < 7: return 0
if x[-7:] == "HTMLDIR":return 1
[docs] 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))
[docs] 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()
[docs] 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()