10
10
import importlib
11
11
import proselint
12
12
from blessings import Terminal
13
+ from docutils .core import publish_doctree
13
14
14
15
CONTEXT_SETTINGS = dict (help_option_names = ['-h' , '--help' ])
15
16
17
+ # path of docs directory
18
+ dir_path = os .path .dirname (os .path .realpath (__file__ ))
19
+
16
20
# variables to hold error and warning count
17
21
err_cnt = 0
18
22
warn_cnt = 0
25
29
os .environ ["TERMINFO" ]= "/etc/terminfo"
26
30
t = Terminal ()
27
31
32
+ def parse_code ():
33
+ """Parse python code-blocks."""
34
+ global dir_path
35
+ test_file = dir_path + '/style-guide/style-checks.py'
36
+ extra_file = dir_path + '/style-guide/extra.py'
37
+
38
+ def is_style_code_block (node ):
39
+ """Check for style-checks python code-blocks."""
40
+ return (node .tagname == 'literal_block'
41
+ and 'code' in node .attributes ['classes' ]
42
+ and 'python' in node .attributes ['classes' ]
43
+ and 'style-checks' in node .attributes ['classes' ])
44
+
45
+ def is_extra_code_block (node ):
46
+ """Check for extra-checks python code-blocks."""
47
+ return (node .tagname == 'literal_block'
48
+ and 'code' in node .attributes ['classes' ]
49
+ and 'python' in node .attributes ['classes' ]
50
+ and 'extra-checks' in node .attributes ['classes' ])
51
+
52
+ style_guide = open (dir_path + "/src/docs-style-guide.rst" , "r" )
53
+
54
+ # publish doctree, report only severe errors
55
+ doctree = publish_doctree (style_guide .read (),
56
+ settings_overrides = {'report_level' : 4 })
57
+
58
+ # write source code into style-check file
59
+ code_blocks = doctree .traverse (condition = is_style_code_block )
60
+ source_code = [block .astext () for block in code_blocks ]
61
+
62
+ f = open (test_file ,"w+" )
63
+ f .write ("# -*- coding: utf-8 -*-\n \n " )
64
+ f .write ('"""Style Guide testing."""\n \n ' )
65
+
66
+ # modules to import from proselint
67
+ modules = "memoize, existence_check, preferred_forms_check"
68
+ f .write ("from proselint.tools import %s\n " % modules )
69
+
70
+ for line in source_code :
71
+ if not line .endswith ('\n ' ):
72
+ line = line + "\n "
73
+ if "@memoize" in line :
74
+ line = "\n " + line
75
+ f .write (line )
76
+
77
+ # write source code into extra file
78
+ code_blocks = doctree .traverse (condition = is_extra_code_block )
79
+ source_code = [block .astext () for block in code_blocks ]
80
+
81
+ f = open (extra_file ,"w+" )
82
+ f .write ("# -*- coding: utf-8 -*-\n \n " )
83
+ f .write ('"""Style Guide testing."""\n \n ' )
84
+ f .write ("import re\n " )
85
+ f .write ("from proselint.tools import line_and_column\n " )
86
+
87
+ for line in source_code :
88
+ if not line .endswith ('\n ' ):
89
+ line = line + "\n "
90
+ if "def" in line :
91
+ line = "\n " + line
92
+ f .write (line )
93
+
94
+
28
95
def add_checks ():
29
96
"""Add checks to proselint."""
30
- file_path = os . path . realpath ( __file__ )
31
- src = file_path [ 0 : file_path . rfind ( '/' )] + '/style-guide'
97
+ global dir_path
98
+ src = dir_path + '/style-guide'
32
99
dest = os .path .dirname (proselint .__file__ )
33
100
dest_prc = dest + '/.proselintrc'
34
101
dest = dest + '/checks/style-guide'
@@ -70,7 +137,8 @@ def add_checks():
70
137
def remove_lines (text ):
71
138
"""Remove ignored lines and directive blocks from text."""
72
139
directive_list = [".. image::" , ".. figure::" , ".. video::" , ".. code::" ,
73
- ".. code-block::" , ".. csv-table::" , ".. toctree::" ,]
140
+ ".. code-block::" , ".. csv-table::" , ".. toctree::" ,
141
+ ".. py:" , ".. math::" ,]
74
142
75
143
index = 0
76
144
length = len (text )
@@ -187,6 +255,7 @@ def exclude_checks():
187
255
"""Removes the checks which are to be excluded."""
188
256
list_exclude = [
189
257
"typography.symbols" , "weasel_words.very" ,
258
+ "misc.but" , "consistency.spelling" ,
190
259
]
191
260
dest = os .path .dirname (proselint .__file__ )
192
261
dest_prc = dest + '/.proselintrc'
@@ -206,10 +275,10 @@ def exclude_checks():
206
275
207
276
def get_paths (paths ):
208
277
"""Return a list of files to run the checks on."""
209
- file_path = os . path . realpath ( __file__ )
278
+ global dir_path
210
279
211
280
# find path for all .rst files
212
- search_path = file_path [ 0 : file_path . rfind ( '/' )] + "/src"
281
+ search_path = dir_path + "/src"
213
282
214
283
# Make a list of paths to check for
215
284
path_list = []
@@ -235,13 +304,13 @@ def get_paths(paths):
235
304
236
305
def get_changed_files ():
237
306
"""Return currently modified rst files."""
238
- file_path = os . path . realpath ( __file__ )
239
- repo_path = file_path [ 0 : file_path . rfind ( '/' )]
307
+ global dir_path
308
+ repo_path = dir_path
240
309
repo = git .Repo (repo_path )
241
- changedFiles = [item .a_path for item in repo .index .diff (None )]
242
- changedFiles += repo .untracked_files
243
- changedFiles = tuple (changedFiles )
244
- return changedFiles
310
+ changed_files = [item .a_path for item in repo .index .diff (None )]
311
+ changed_files += repo .untracked_files
312
+ changed_files = tuple (changed_files )
313
+ return changed_files
245
314
246
315
247
316
def run_checks (paths , disp , fix ):
@@ -266,7 +335,7 @@ def run_checks(paths, disp, fix):
266
335
text = remove_lines (text )
267
336
268
337
# Import extra check module from style-guide
269
- extra = importlib .import_module ('style-guide.extra ' , None )
338
+ extra = importlib .import_module ('style-guide.extrarun ' , None )
270
339
271
340
# run checks for quotes, curly quotes, section labels
272
341
errors = extra .check (temp_file (text ))
@@ -335,7 +404,7 @@ def disp_checks(errors, filename, shortname):
335
404
# e[0]=check
336
405
# Set warning or error severity
337
406
# Don't set errors for style-guide as they might be examples
338
- if e [0 ] in list_errors and " style-guide.rst" not in shortname :
407
+ if e [0 ] in list_errors and shortname != "docs- style-guide.rst" :
339
408
severity = "error"
340
409
else :
341
410
severity = "warning"
@@ -365,8 +434,20 @@ def disp_cnt():
365
434
global warn_cnt
366
435
global t
367
436
368
- print (t .yellow ("Found %d warnings" % warn_cnt ))
369
- print (t .red ("Found %d errors" ) % err_cnt )
437
+ warn = "%d warnings." % warn_cnt
438
+ err = "%d errors." % err_cnt
439
+
440
+ if warn_cnt == 0 :
441
+ warn = "no warnings!"
442
+ elif warn_cnt == 1 :
443
+ warn = "1 warning."
444
+ if err_cnt == 0 :
445
+ err = "no errors!"
446
+ elif err_cnt == 1 :
447
+ err = "1 error."
448
+
449
+ print (t .yellow ("Testing found %s" % warn ))
450
+ print (t .red ("Testing found %s" % err ))
370
451
if err_cnt :
371
452
raise Exception ("Style-guide testing failed! Fix the errors" )
372
453
@@ -426,17 +507,30 @@ def gen_list(paths = None):
426
507
427
508
return err_list
428
509
510
+ def remove_file ():
511
+ """Remove generated check files."""
512
+ global dir_path
513
+ test_file = dir_path + '/style-guide/style-checks.py'
514
+ extra_file = dir_path + '/style-guide/extra.py'
515
+ os .remove (test_file )
516
+ os .remove (extra_file )
517
+
429
518
430
519
@click .command (context_settings = CONTEXT_SETTINGS )
431
520
@click .option ('--diff' , '-d' , is_flag = True ,
432
521
help = "Run check on the modified files" )
433
522
@click .option ('--fix' , '-f' , is_flag = True ,
434
- help = "Removes the fixable errors" )
523
+ help = "Remove the fixable errors" )
435
524
@click .option ('--out_path' ,'-o' , type = click .Path ())
525
+ @click .option ('--store' ,'-s' ,is_flag = True ,
526
+ help = "Store the generated test scripts" )
436
527
@click .argument ('in_path' , nargs = - 1 , type = click .Path ())
437
528
def style_test (in_path = None , out_path = None , diff = None ,
438
- fix = None , output = None ):
439
- """A CLI for style guide testing"""
529
+ fix = None , output = None , store = None ):
530
+ """A CLI for style guide testing."""
531
+ # generate source code for checks
532
+ parse_code ()
533
+
440
534
# add custom style-guide checks to proselint
441
535
add_checks ()
442
536
@@ -456,7 +550,10 @@ def style_test(in_path = None, out_path = None, diff = None,
456
550
# generate output
457
551
if out_path :
458
552
gen_out (out_path )
459
-
553
+
554
+ # remove generated test scripts
555
+ if not store :
556
+ remove_file ()
460
557
461
558
if __name__ == '__main__' :
462
559
style_test ()
0 commit comments