Package docs :: Package latex :: Module find_replicate_titles
[hide private]
[frames] | no frames]

Source Code for Module docs.latex.find_replicate_titles

  1  #! /usr/bin/env python 
  2   
  3  ############################################################################### 
  4  #                                                                             # 
  5  # Copyright (C) 2015 Edward d'Auvergne                                        # 
  6  #                                                                             # 
  7  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  8  #                                                                             # 
  9  # This program is free software: you can redistribute it and/or modify        # 
 10  # it under the terms of the GNU General Public License as published by        # 
 11  # the Free Software Foundation, either version 3 of the License, or           # 
 12  # (at your option) any later version.                                         # 
 13  #                                                                             # 
 14  # This program is distributed in the hope that it will be useful,             # 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 17  # GNU General Public License for more details.                                # 
 18  #                                                                             # 
 19  # You should have received a copy of the GNU General Public License           # 
 20  # along with relax; if not, write to the Free Software                        # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Python module imports. 
 25  from os import getcwd, path, walk 
 26  from re import search 
 27  import sys 
 28   
 29   
30 -class Replicated_titles:
31 """Class used to find replicated titles in the LaTeX sources.""" 32
33 - def __init__(self):
34 """Set up the required data structures.""" 35 36 # The data structure for holding the unique title names. 37 self.titles = [] 38 39 # The replicate count structure. 40 self.replicate = {}
41 42
43 - def find(self):
44 """Find the replicates.""" 45 46 # Reset the data structures if necessary. 47 if len(self.titles): 48 self.titles = [] 49 self.replicate = {} 50 51 # Walk through the directories. 52 for root, dirs, files in walk(getcwd()): 53 # Loop over the files in the current directory. 54 for file_name in files: 55 # Skip non-LaTeX files. 56 if not search("tex$", file_name): 57 continue 58 59 # Skip files not in 'docs/latex'. 60 if not search(path.join('docs', 'latex'), root): 61 continue 62 63 # The full path. 64 file_path = path.join(root, file_name) 65 66 # Read the contents of the file. 67 file = open(file_path) 68 lines = file.readlines() 69 file.close() 70 71 # Loop over the file contents. 72 for line in lines: 73 # Skip everything that is not a chapter or section. 74 if not (search("\\\\chapter", line) or search("\\\\section", line) or search("\\\\subsection", line)): 75 continue 76 77 # Strip off the newline character. 78 line = line.replace('\n', '') 79 80 # Strip off any label. 81 if search(' \\\\label', line): 82 line = line[:line.index(' \label')] 83 84 # Extract the short title string, if it exists. 85 if '[' in line: 86 title = line[line.index('[')+1:line.index(']')] 87 88 # Extract the full title string by finding the first '{' and chop off the final '}'. 89 else: 90 title = line[line.index('{')+1:-1] 91 92 # Is the title new? 93 if not title in self.titles: 94 self.titles.append(title) 95 96 # Replicate! 97 else: 98 # No replicates yet, so 2 identical titles exist. 99 if not title in self.replicate: 100 self.replicate[title] = 2 101 102 # At least two identical titles exist, so increment the counter. 103 else: 104 self.replicate[title] += 1 105 106 # Final printout. 107 if len(self.replicate): 108 # The replicate titles. 109 print("%-80s %-10s" % ("Title", "Count")) 110 for title in self.replicate: 111 print("%-80s %10i" % (title, self.replicate[title])) 112 113 # Return a status that replicates have been found. 114 return True 115 116 # No replicates. 117 return False
118 119 120 121 if __name__ == "__main__": 122 repli = Replicated_titles() 123 if repli.find(): 124 sys.exit(1) 125 else: 126 sys.exit(0) 127