OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
update-links.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 
3 import fileinput, string, sys
4 import os
5 import subprocess
6 
7 # the tree of pages; each entry consists of a list
8 # with two elements: the depth (1 based) and the name
9 lay_tree = []
10 
11 def readLayout(lay_p):
12  ''' Reads the layout.ini file into a global data structure - lay_tree
13 
14  lay_p indicates the path to the layout file.
15  Returns False for error, True for success
16  '''
17  global lay_tree
18  try:
19  f = open( lay_p, "r" )
20  layout = f.read()
21  f.close()
22  except Exception, e:
23  print ( e )
24  return False
25  # the level of the last item that we've seen
26  crt_lev = 1
27  # iterate in each line
28  for lay_line in layout.split("\n"):
29  # text only
30  lay_lcont = lay_line.strip()
31  if ( len( lay_lcont ) == 0 ):
32  continue
33  # comments that start with #
34  if ( lay_lcont.startswith( "#" ) ):
35  continue
36  # we're building the tree based on indentation
37  it_level = 1
38  for char_itr in lay_line:
39  if ( char_itr != " " ):
40  if ( char_itr != "\t" ):
41  break
42  it_level += 1
43  # make a sane value
44  if ( it_level >= crt_lev + 1 ):
45  # adding a child to last item
46  it_level = crt_lev + 1
47  # elif ( it_level == crt_lev ): # adding a sibling
48  # else: # adding a sibling to some parent
49  lay_tree.append([it_level,lay_lcont])
50 
51  # save our level for next iteration
52  crt_lev = it_level
53 
54  return True
55 
56 def pageLink(page_name, link_text):
57  ''' get the text to be used as a link to the page
58  '''
59  if ( page_name == "main" ):
60  ret_txt = "\\ref index"
61  if ( len(link_text) != 0 ):
62  ret_txt += " \"" + link_text + "\""
63  else:
64  ret_txt += " \"Index\""
65  else:
66  ret_txt = "\\ref " + page_name
67  if ( len(link_text) != 0 ):
68  ret_txt += " \"" + link_text + "\""
69  return ret_txt
70 
71 def updateAFile(dox_file_p,fileindex):
72  ''' updats dynamic data between the markers
73 
74  The function reads the file and modified the content between the
75  markers.
76  '''
77  print( " Processing " + dox_file_p + "..." )
78  # read old content
79  try:
80  f = open( dox_file_p, "r" )
81  dox_cont = f.read()
82  f.close()
83  except Exception, e:
84  print ( e )
85  return False
86 
87  # we accumulate new content here
88  new_cont = "\n"
89  # first, the list of kids
90  my_level = lay_tree[fileindex][0]
91  i = fileindex + 1
92  itr_max = len(lay_tree)
93  while (i < itr_max):
94  crt_level = lay_tree[i][0]
95  # only kids and grandkids
96  if (crt_level <= my_level):
97  break
98  if (crt_level == my_level+1):
99  new_cont += "- \\subpage " + lay_tree[i][1] + "\n"
100  else:
101  new_cont += "\t"*(crt_level-my_level-1)
102  new_cont += "- " + pageLink(lay_tree[i][1],"") + "\n"
103  i = i + 1
104 
105  # now the next/previous navigation
106  new_cont += "\n<TABLE width=\"100%\" border=\"0\"><TR>\n<TD>"
107  if ( fileindex != 0 ):
108  new_cont += pageLink(lay_tree[fileindex-1][1], "Previous")
109  new_cont += "</TD>\n<TD width=\"100%\"></TD>\n<TD>"
110  if ( fileindex != itr_max-1 ):
111  new_cont += pageLink(lay_tree[fileindex+1][1], "Next")
112  new_cont += "</TD>\n</TR></TABLE>\n"
113 
114  # locate markers
115  while ( True ):
116  # ignore the files that have no markers
117  idx_st = dox_cont.find( "\\if MARKER_TREE_START" )
118  if ( idx_st == -1 ): break
119  idx_st = dox_cont.find( "\\endif", idx_st)
120  if ( idx_st == -1 ): break
121  idx_st += len( "\\endif" )
122  idx_end = dox_cont.find( "\\if MARKER_TREE_END", idx_st )
123  if ( idx_end == -1 ): break
124  # we reach this point only if all the markers were found
125  # do we really need an update of this file?
126  if ( dox_cont[idx_st:idx_end] == new_cont ):
127  print( " ... skipped" )
128  return True
129  # now change the content
130  dox_cont = dox_cont[:idx_st] + new_cont + dox_cont[idx_end:]
131  # and save new content
132  try:
133  f = open( dox_file_p, "w" )
134  f.write( dox_cont )
135  f.close()
136  except Exception, e:
137  print ( e )
138  return False
139  break;
140 
141  print( " ... done" )
142  return True
143 
144 def main():
145  ''' Main function that is called when the module is loaded by itself
146 
147  The function assumes that the documentation is in the same directory.
148  '''
149  global lay_tree
150  # read the layout file
151  lay_p = "layout.ini"
152  if (readLayout(lay_p)==False):
153  return -1
154  # print( lay_tree )
155  lay_tree.insert(0,[0,"main"])
156  idx = 0
157  for itr_lay in lay_tree:
158  updateAFile(itr_lay[1]+".dox", idx)
159  idx = idx + 1
160 
161  return 0
162 
163 if __name__ == "__main__":
164  sys.exit( main() )