CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PackageSplitter.py
Go to the documentation of this file.
1 from BeautifulSoup import *
2 import copy, os, sys
3 
4 ## PackageSplitter class is used for splitting package web page (pages.html) to increase usability
6  ## Constructor method.
7  # @param path is the reference manual directory path and it is used as destination and source.
8  # @param prefix is used for file naming as prefix.
9  def __init__(self, path, prefix = 'package_'):
10  self.path = path
11  self.prefix = prefix
12  self.packageSource = self.ReadFile('pages.html')
13  self.WriteFile('pages_backup.html', self.packageSource)
14  self.packages = {}
15  self.data = None
16 
17  def ReadFile(self, fileName, pathFlag = True):
18  """This method reads file directly or from path."""
19  if pathFlag:
20  print "Read:", self.path + fileName
21  f = open(self.path + fileName)
22  else:
23  f = open(fileName)
24  print "Read:", fileName
25  data = f.read()
26  f.close()
27  return data
28 
29  def WriteFile(self, fileName, data):
30  """This method writes data"""
31  print "Write:", self.path + fileName
32  f = open(self.path + fileName, "w")
33  f.write(data)
34  f.close()
35 
36  def __GetPackageName(self, str_):
37  if str_.find('/') != -1:
38  return str_[0:str_.find('/')]
39  else:
40  return str_
41 
42  def __GetFileName(self, str_):
43  return self.prefix + str_.replace(' ', '_') + '.html'
44 
45  def GenerateTab(self, current = None, linkPrefix = ""):
46  html = '<div class="tabs3">\n<ul class="tablist">\n'
47 
48  keys = self.data.keys()
49  keys.sort()
50 
51  for i in keys:
52  if i == current:
53  html += '<li class="current"><a href="%s"><span>%s</span></a></li>\n' % (linkPrefix + self.__GetFileName(i), i)
54  else:
55  html += '<li><a href="%s"><span>%s</span></a></li>\n' % (linkPrefix + self.__GetFileName(i), i)
56 
57  html += '</ul>\n</div>'
58 
59  return html
60 
61  def CreateSubPage(self, packageName):
62  if not self.data:
63  self.PrepareData()
64  tab = self.GenerateTab(current = packageName)
65  counter = 0
66  htmlList = '<table class="directory">\n<tbody>\n'
67  keysI = self.data[packageName].keys()
68  keysI.sort()
69  for i in keysI:
70  if counter % 2 == 0:
71  htmlList += '<tr id="row_%d_" class="even">\n' % counter
72  else:
73  htmlList += '<tr id="row_%d_">\n' % counter
74  htmlList += '<td class="entry">\n<img src="ftv2node.png" alt="o" width="16" height="22">\n'
75  htmlList += '<a class="el" href="%s" target="_self">%s</a>\n' % (self.data[packageName][i], i)
76  htmlList += '</td>\n<td class="desc">\n</td>\n</tr>\n'
77 
78  counter += 1
79  htmlList += '</tbody>\n</table>\n'
80 
81  temp = copy.deepcopy(self.packageSource)
82  soup = BeautifulSoup(temp)
83  list_ = soup.find('div', { "class" : "directory" })
84  list_.replaceWith(htmlList)
85 
86  tab_ = soup.find('ul', { "class" : "tablist" })
87  tab_.replaceWith(tab_.prettify() + tab)
88 
89  data = str(soup.prettify())
90 
91  self.WriteFile(self.__GetFileName(packageName), data.replace('&lt;','<').replace('&gt;', '>'))
92 
93  def CreatePackagePage(self, outputFile):
94  if not self.data:
95  self.PrepareData()
96 
97  html = self.GenerateTab()
98 
99  temp = copy.deepcopy(self.packageSource)
100  soup = BeautifulSoup(temp)
101  tab = soup.find('ul', { "class" : "tablist" })
102  tab.replaceWith(tab.prettify() + html)
103 
104  data = str(soup.prettify())
105 
106  self.WriteFile(outputFile, data.replace('&lt;','<').replace('&gt;', '>'))
107 
108  for i in self.data:
109  self.CreateSubPage(i)
110 
111  for i in self.data:
112  for j in self.data[i]:
113  self.AddTab2Page(self.data[i][j])
114 
115  def AddTab2Page(self, packageLink):
116  tabHTML = self.GenerateTab(linkPrefix = "../../")
117 
118  orgSource = self.ReadFile(packageLink)
119 
120  soup = BeautifulSoup(orgSource)
121  tab = soup.find('ul', { "class" : "tablist" })
122  tab.replaceWith(tab.prettify() + tabHTML)
123  data = str(soup.prettify())
124  self.WriteFile(packageLink, data.replace('&lt;','<').replace('&gt;', '>'))
125 
126  def PrepareData(self):
127  soup = BeautifulSoup(self.packageSource)
128  contents = soup.find("div", { "class" : "contents" })
129  self.tr = contents.findAll("tr", {})
130 
131  for i in self.tr:
132  self.packages[i.text] = i.a["href"]
133 
134  self.data = {}
135 
136  for i in self.packages:
137  if not "Package" in i:
138  continue
139  if not self.data.has_key(self.__GetPackageName(i).replace(u'Package ', u'')):
140  self.data[self.__GetPackageName(i).replace(u'Package ', u'')] = {}
141 
142  self.data[self.__GetPackageName(i).replace(u'Package ', u'')][i] = self.packages[i]
143 
144 
145 if len(sys.argv) == 4:
146  PATH = sys.argv[1]
147  OUTF = sys.argv[2]
148  PREF = sys.argv[3]
149 
150  l = PackageSplitter(PATH, PREF)
151 
152  l.CreatePackagePage(OUTF)
153 else:
154  print "parameter error. It must be like this:\nrun.py CMSSW/doc/html/ INPUT_FILE PREFIX\nExample: PackageSplitter.py OUTPUT_PATH/ pages.html packageDocumentation_"
PackageSplitter class is used for splitting package web page (pages.html) to increase usability...
def __init__
Constructor method.