4444from io import StringIO
4545from contextlib import redirect_stdout
4646
47+
4748def printn (line ):
4849 """Shortcut to print without new line"""
4950 print (line , end = "" )
5051
52+
5153class CodeBlock :
5254 """The code block
5355
@@ -73,70 +75,70 @@ class CodeBlock:
7375 stdout: The standard output of current code piece
7476 It will get overwritten by next code piece
7577 """
78+
7679 def __init__ (self , indent , backticks , lang , index , envs = None ):
7780 self .indent = indent
7881 self .backticks = backticks
7982 self .lang = lang
80- self .codes = ''
83+ self .codes = ""
8184 self .envs = envs or {}
82- self .produced = ''
85+ self .produced = ""
8386 self .alive = True
8487 self .index = index
8588 self .piece = 0
86- self .stdout = ''
89+ self .stdout = ""
8790
8891 def compile_exec (self , code ):
8992 """Compile and execute the code"""
9093 sourcefile = path .join (
9194 tempfile ._get_default_tempdir (),
9295 f"codeblock_{ self .index } _{ self .piece } -"
93- f"{ next (tempfile ._get_candidate_names ())} "
96+ f"{ next (tempfile ._get_candidate_names ())} " ,
9497 )
95- with open (sourcefile , 'w' ) as fsrc :
98+ with open (sourcefile , "w" ) as fsrc :
9699 fsrc .write (code )
97- code = compile (code , sourcefile , mode = ' exec' )
100+ code = compile (code , sourcefile , mode = " exec" )
98101 sio = StringIO ()
99102 with redirect_stdout (sio ):
100103 exec (code , self .envs )
101104 self .stdout = sio .getvalue ()
102105 self .piece += 1
103106
104-
105107 def feed (self , line ):
106108 """Feed a single line to the code block, with line break"""
107- if self .lang not in (' python' , ' python3' ):
109+ if self .lang not in (" python" , " python3" ):
108110 self .produced += line
109111
110112 else :
111- if not line .strip (): # empty line
113+ if not line .strip (): # empty line
112114 self .codes += "\n "
113115 self .produced += line
114116 else :
115- line = line [len (self .indent ):]
117+ line = line [len (self .indent ) :]
116118 if CodeBlock .should_compile (line ):
117119 if self .codes :
118120 self .compile_exec (self .codes )
119- self .codes = ''
121+ self .codes = ""
120122 self .produced += self .indent + self .compile_line (line )
121123 else :
122124 self .codes += line
123125 self .produced += self .indent + line
124126
125127 def _compile_expr (self , line ):
126128 """Compile {_expr}"""
127- varname = ' _expr'
128- source = f' { varname } = { line } '
129+ varname = " _expr"
130+ source = f" { varname } = { line } "
129131 self .compile_exec (source )
130- code , comment = line .split ('#' , 1 )
132+ code , comment = line .split ("#" , 1 )
131133 comment = comment .format (** self .envs )
132- return '#' .join ((code , comment ))
134+ return "#" .join ((code , comment ))
133135
134136 def _compile_out (self , line ):
135137 """Compile {_out}"""
136138 self .compile_exec (line )
137- code , comment = line .split ('#' , 1 )
139+ code , comment = line .split ("#" , 1 )
138140 comment = comment .format (** self .envs , _out = self .stdout )
139- return '#' .join ((code , comment ))
141+ return "#" .join ((code , comment ))
140142
141143 def _compile_exc (self , line , msg = False ):
142144 """Compile {_exc} and {_exc_msg}"""
@@ -153,36 +155,36 @@ def _compile_exc(self, line, msg=False):
153155 source += " + ': ' + str(exc)"
154156 source += "\n "
155157 self .compile_exec (source )
156- code , comment = line .split ('#' , 1 )
158+ code , comment = line .split ("#" , 1 )
157159 comment = comment .format (** self .envs )
158- return '#' .join ((code , comment ))
160+ return "#" .join ((code , comment ))
159161
160162 def _compile_hidden (self , line ):
161163 """Compile {_hidden}"""
162164 self .compile_exec (line )
163- return ''
165+ return ""
164166
165167 def _compile_var (self , line ):
166168 """Compile variables"""
167169 self .compile_exec (line )
168- code , comment = line .split ('#' , 1 )
170+ code , comment = line .split ("#" , 1 )
169171 comment = comment .format (** self .envs )
170- return '#' .join ((code , comment ))
172+ return "#" .join ((code , comment ))
171173
172174 def compile_line (self , line ):
173175 """Compile a single line"""
174- code , comment = line .split ('#' , 1 )
176+ code , comment = line .split ("#" , 1 )
175177 if not code :
176- return '#' + comment .format (** self .envs , _out = self .stdout )
177- if ' {_hidden}' in comment :
178+ return "#" + comment .format (** self .envs , _out = self .stdout )
179+ if " {_hidden}" in comment :
178180 return self ._compile_hidden (line )
179- if re .search (r' \{_expr(?:!\w+)?\}' , comment ):
181+ if re .search (r" \{_expr(?:!\w+)?\}" , comment ):
180182 return self ._compile_expr (line )
181- if re .search (r' \{_out(?:!\w+)?\}' , comment ):
183+ if re .search (r" \{_out(?:!\w+)?\}" , comment ):
182184 return self ._compile_out (line )
183- if ' {_exc}' in comment :
185+ if " {_exc}" in comment :
184186 return self ._compile_exc (line )
185- if ' {_exc_msg}' in comment :
187+ if " {_exc_msg}" in comment :
186188 return self ._compile_exc (line , msg = True )
187189
188190 return self ._compile_var (line )
@@ -207,7 +209,9 @@ def try_open(cls, line, envs, index):
207209 return None
208210
209211 indent , backticks , lang = (
210- matches .group (1 ), matches .group (2 ), matches .group (3 ),
212+ matches .group (1 ),
213+ matches .group (2 ),
214+ matches .group (3 ),
211215 )
212216 return cls (indent , backticks , lang , index , envs )
213217
@@ -218,19 +222,19 @@ def close(self, line):
218222 self .alive = False
219223 if self .codes :
220224 self .compile_exec (self .codes )
221- self .codes = ''
225+ self .codes = ""
222226 return True
223227
224228 return False
225229
226230 @staticmethod
227231 def should_compile (line ):
228232 """Whether we should compile a line or treat it as a plain line"""
229- if '#' not in line :
233+ if "#" not in line :
230234 return False
231235
232- comment = line .split ('#' , 1 )[1 ]
233- return re .search (r' \{[^}]+\}' , comment )
236+ comment = line .split ("#" , 1 )[1 ]
237+ return re .search (r" \{[^}]+\}" , comment )
234238
235239
236240def compile_readme (rawfile ):
@@ -254,6 +258,7 @@ def compile_readme(rawfile):
254258 if maybe_codeblock :
255259 codeblock = maybe_codeblock
256260
261+
257262if __name__ == "__main__" :
258263 if len (sys .argv ) < 2 :
259264 print ("Usage: make-readme.py README.raw.md > README.md" )
0 commit comments