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 # The full path. 60 file_path = path.join(root, file_name) 61 62 # Read the contents of the file. 63 file = open(file_path) 64 lines = file.readlines() 65 file.close() 66 67 # Loop over the file contents. 68 for line in lines: 69 # Skip everything that is not a chapter or section. 70 if not (search("\\\\chapter", line) or search("\\\\section", line) or search("\\\\subsection", line)): 71 continue 72 73 # Strip off the newline character. 74 line = line.replace('\n', '') 75 76 # Strip off any label. 77 if search(' \\\\label', line): 78 line = line[:line.index(' \label')] 79 80 # Extract the short title string, if it exists. 81 if '[' in line: 82 title = line[line.index('[')+1:line.index(']')] 83 84 # Extract the full title string by finding the first '{' and chop off the final '}'. 85 else: 86 title = line[line.index('{')+1:-1] 87 88 # Is the title new? 89 if not title in self.titles: 90 self.titles.append(title) 91 92 # Replicate! 93 else: 94 # No replicates yet, so 2 identical titles exist. 95 if not title in self.replicate: 96 self.replicate[title] = 2 97 98 # At least two identical titles exist, so increment the counter. 99 else: 100 self.replicate[title] += 1 101 102 # Final printout. 103 if len(self.replicate): 104 # The replicate titles. 105 print("%-80s %-10s" % ("Title", "Count")) 106 for title in self.replicate: 107 print("%-80s %10i" % (title, self.replicate[title])) 108 109 # Return a status that replicates have been found. 110 return True 111 112 # No replicates. 113 return False
114 115 116 117 if __name__ == "__main__": 118 repli = Replicated_titles() 119 if repli.find(): 120 sys.exit(1) 121 else: 122 sys.exit(0) 123