1
1
from snippet .config import Config
2
+ from snippet import exceptions
2
3
3
4
4
5
def extract_snippets (config : Config , lines , path ):
@@ -19,32 +20,32 @@ def extract_snippets(config: Config, lines, path):
19
20
examples [current_key ] = current_block
20
21
current_strip = len (line ) - len (line .lstrip ())
21
22
if capture :
22
- raise Exception ( 'Start/end example mismatch - already capturing at %s' % ( current_key ,) )
23
+ raise exceptions . StartEndMismatch ( f'Already capturing at { current_key } ' )
23
24
capture = True
24
25
continue
25
26
26
27
if config .end_flag in line :
27
28
# stop capturing, and discard empty blocks
28
29
if not capture :
29
- raise Exception ( 'Start/end example mismatch - not yet capturing at %s' % ( current_key ,) )
30
+ raise exceptions . StartEndMismatch ( f'Not yet capturing at { current_key } ' )
30
31
capture = False
31
32
if not current_block :
32
33
examples .pop (current_key )
33
34
34
35
if capture :
35
36
# whilst capturing, append code lines to the current block
36
37
if config .fail_on_dedent and any (line [:current_strip ].split (' ' )):
37
- raise Exception ( 'Unexpected dedent whilst capturing %s' % ( current_key ,) )
38
+ raise exceptions . ValidationFailure ( f 'Unexpected dedent whilst capturing { current_key } ' )
38
39
clean_line = line [current_strip :].rstrip ()
39
40
for r_before , r_after in config .replacements .items ():
40
41
clean_line = clean_line .replace (r_before , r_after )
41
42
for trigger in config .fail_on_contains :
42
43
if trigger in clean_line :
43
- raise Exception ( 'Unexpected phrase %r at %s' % (trigger , current_key ) )
44
+ raise exceptions . ValidationFailure ( f 'Unexpected phrase { repr (trigger ) } at { current_key } ' )
44
45
# add this line of code to the example block
45
46
current_block .append (clean_line )
46
47
47
48
if capture :
48
- raise Exception ( 'EOF reached whilst still capturing %s' % ( current_key ,) )
49
+ raise exceptions . StartEndMismatch ( f 'EOF reached whilst still capturing { current_key } ' )
49
50
50
51
return examples
0 commit comments