@breakout.html


001| from sys import argv
002| from os import path
003| from files import linefile
004| 
005| BasePath = r'C:\CJS\prj\Python\blog\hcc\source'
006| ArchPath = path.join(BasePath, 'archive')
007| 
008| FileName = path.join(ArchPath, argv[1] if 1 < len(argv) else 'source.py')
009| FoutName = lambda n:path.join(BasePath, 'example%03d.py' % n)
010| 
011| # Input source file...
012| fi = linefile(FileName)
013| fi.load()
014| 
015| # Filename pattern, code flag, and file index...
016| flag = 0
017| fnbr = 1
018| fout = linefile(FoutName(fnbr))
019| 
020| print(f'Scanning: {FileName}')
021| # Scan for code snippets and save to example{fx:2d}.py...
022| for line in fi:
023|     # If in a snippet...
024|     if flag:
025|         # If end code, end of snippet...
026|         if line.startswith('#:end:code::'):
027|             flag = 0
028|             # Save file...
029|             fout.save()
030|             print(f'wrote: {fout.fullname}')
031|             break
032| 
033|         # If divider code, end of snippet...
034|         if line.startswith('#:code::'):
035|             # Save file...
036|             fout.save()
037|             print(f'wrote: {fout.fullname}')
038|             fnbr += 1
039|             fout = linefile(FoutName(fnbr))
040|             continue
041| 
042|         # Source code line, so save to buffer...
043|         fout.data.append(line)
044|         continue
045| 
046|     # Not in a snippet...
047|     # A divider code begins a snippet...
048|     if line.startswith('#:code::'):
049|         flag = 1
050| 
051| print('Done!')
052| print()
053|