5
5
@author: jhodges
6
6
"""
7
7
8
- import glob , os
8
+ import os , subprocess , platform , argparse , glob , sys
9
+
10
+ def sortWhitelist (firemodels ):
11
+ whitelist = os .path .join (firemodels , 'fds' ,'Manuals' ,'Bibliography' )+ os .sep + 'whitelist.txt'
12
+ whitelist = os .path .abspath (whitelist )
13
+
14
+ with open (whitelist ,'r' ) as f :
15
+ txt = f .readlines ()
16
+
17
+ words = list (set (txt [1 :]))
18
+ words .sort ()
19
+
20
+ outtxt = txt [0 ] + '\n ' + '\n ' .join (words ) + '\n '
21
+ while '\n \n ' in outtxt :
22
+ outtxt = outtxt .replace ('\n \n ' ,'\n ' )
23
+
24
+ with open (whitelist .replace ('.txt' ,'_back.txt' ), 'w' ) as f :
25
+ f .writelines (txt )
26
+
27
+ with open (whitelist , 'w' ) as f :
28
+ f .write (outtxt )
29
+
30
+ def checkSpelling (file , firemodels ):
31
+ whitelist = os .path .join (firemodels , 'fds' ,'Manuals' ,'Bibliography' )+ os .sep + 'whitelist.txt'
32
+ whitelist = os .path .abspath (whitelist )
33
+ if 'Windows' in platform .platform ():
34
+ whitelist = whitelist .replace ('\\ ' ,'/' )
35
+ whitelist = whitelist [0 ].upper () + whitelist [1 :]
36
+ cmd = ['aspell' ,'--lang=en' ,'--mode=tex' ,'--add-extra-dicts=%s' % (whitelist ), 'list' , '<' , file ]
37
+
38
+ p = subprocess .run (cmd , capture_output = True , shell = True )
39
+ txt = p .stdout .decode ('utf-8' )
40
+ txt = txt .replace ('\r \n ' ,'\n ' )
41
+ return txt
9
42
10
43
def checkCaption (caption ):
11
44
@@ -44,7 +77,7 @@ def checkCaption(caption):
44
77
return outtxt
45
78
46
79
def check_disallowed_commands (txt , file ):
47
- disallowed_commands = ['\\ bf{' ,'\\ tt{' ]
80
+ disallowed_commands = [] #[ '\\bf{','\\tt{']
48
81
outtxt = ''
49
82
for cmd in disallowed_commands :
50
83
split = txt .split (cmd )
@@ -54,7 +87,43 @@ def check_disallowed_commands(txt, file):
54
87
outtxt = outtxt + "ERROR, %s %s located at line %d\n " % (file , cmd , line_count )
55
88
return outtxt
56
89
57
- texfiles = glob .glob ('..' + os .sep + '*' + os .sep + '*.tex' )
90
+ args = sys .argv
91
+ parser = argparse .ArgumentParser (prog = 'check_manuals' ,
92
+ description = 'checks latex manuals for alignment with FDS developer guidelines' )
93
+ parser .add_argument ('call' )
94
+ parser .add_argument ('--file' , help = 'filename to analyze' , default = '' )
95
+ parser .add_argument ('--datafile' , help = 'filename containing list of files' , default = '' )
96
+ parser .add_argument ('--globfile' , help = 'glob search for files' , default = '' )
97
+ parser .add_argument ('--outdir' , help = 'directory to store output' , default = '' )
98
+ parser .add_argument ('--outname' , help = 'output filename' , default = 'check_output.err' )
99
+ parser .add_argument ('--suppressconsole' , help = 'boolean flag specifying whether findings are printed to console' , action = 'store_true' )
100
+
101
+ cmdargs = parser .parse_args (args )
102
+ suppressconsole = cmdargs .suppressconsole
103
+ if cmdargs .file != '' :
104
+ texfiles = [cmdargs .file ]
105
+ elif cmdargs .datafile != '' :
106
+ with open (cmdargs .datafile , 'r' ) as f :
107
+ texfiles = f .readlines ()
108
+ texfiles = [x .replace ('\n ' ,'' ) for x in texfiles ]
109
+ elif cmdargs .globfile != '' :
110
+ texfiles = glob .glob (cmdargs .globfile )
111
+ else :
112
+ if not suppressconsole :
113
+ print ("Warning, one of --file, --datafile, or --globfile is expected. Checking tex files in current directory" )
114
+ texfiles = glob .glob ('*.tex' )
115
+
116
+ if not suppressconsole :
117
+ print ("Files to check:" )
118
+ for file in texfiles :
119
+ print (file )
120
+ outdir = cmdargs .outdir
121
+ outname = cmdargs .outname
122
+
123
+ firemodels = os .path .join (os .path .dirname (__file__ ),'..' ,'..' ,'..' )
124
+
125
+ # This can be called if the white list is being edited and you want sort it
126
+ # sortWhitelist(firemodels)
58
127
59
128
outtxt = '\n '
60
129
for i in range (0 , len (texfiles )):
@@ -83,10 +152,23 @@ def check_disallowed_commands(txt, file):
83
152
84
153
# Check disallowed commands
85
154
outtxt = outtxt + check_disallowed_commands (txt , file )
155
+
156
+ # Check spelling
157
+ txt = checkSpelling (file , firemodels )
158
+
159
+ if len (txt ) > 0 :
160
+ txt_list = list (set (txt .split ('\n ' )))
161
+ txt_list .sort ()
162
+ #while '\n\n' in txt:
163
+ # txt = txt.replace('\n\n','\n')
164
+ #outtxt = outtxt + '\n\nMisspelt Words in %s:\n'%(file) + '\n'.join(txt_list) + '\n\n'
165
+ for j in range (0 , len (txt_list )):
166
+ outtxt = outtxt + '\n \n Misspelt Words in %s: %s\n ' % (file ,txt_list [j ])
86
167
87
- if len (outtxt ) > 1 :
168
+ if len (outtxt ) > 1 and not suppressconsole :
88
169
print ("Warnings identified in the manual check:" )
89
170
print (outtxt )
90
171
91
- with open ('check_output.txt' , 'w' ) as f :
172
+ if outdir != '' : outname = outdir + os .sep + outname
173
+ with open (outname , 'w' ) as f :
92
174
f .write (outtxt )
0 commit comments