16
16
17
17
from pathlib import Path
18
18
import getopt
19
+ import os
19
20
import re
20
21
import sys
21
22
@@ -146,8 +147,8 @@ def log(self, message, type="standard"):
146
147
print (message )
147
148
148
149
def get_args (self ):
149
- """Get arguments/options and set corresponding flags. On validation error
150
- print usage help"""
150
+ """Get arguments/options and set corresponding flags. On validation
151
+ error print usage help"""
151
152
try :
152
153
opts , args = getopt .getopt (sys .argv [1 :], "v" )
153
154
except getopt .GetoptError :
@@ -175,7 +176,7 @@ def get_path(self):
175
176
if not self .path :
176
177
print ("Please run from within the checked-out project." )
177
178
if self .path :
178
- self .includes_path = Path (sys .path [0 ] + " /legalcode-includes" )
179
+ self .includes_path = Path (f" { sys .path [0 ]} /legalcode-includes" )
179
180
return self .path is not False
180
181
181
182
def process_files (self , filelist ):
@@ -186,19 +187,19 @@ def process_files(self, filelist):
186
187
def process_file (self , filepath ):
187
188
"""Verify the required placeholders exist and update file with common
188
189
elements"""
189
- self .log (" \n " + "Processing: " + filepath .name , "verbose" )
190
+ self .log (f" \n Processing: { filepath .name } " , "verbose" )
190
191
with filepath .open (encoding = "utf-8" ) as infile :
191
192
content = infile .read ()
192
193
193
194
if self .has_placeholders (content ):
194
- self .log (" Updating content: " + filepath .name , "verbose" )
195
+ self .log (f " Updating content: { filepath .name } " , "verbose" )
195
196
content = self .add_includes (content )
196
197
content = self .add_language_selector (content , filepath )
197
198
with filepath .open ("w" , encoding = "utf-8" ) as outfile :
198
199
outfile .write (content )
199
200
else :
200
201
self .log (
201
- " No placeholders, skipping: " + filepath .name , "standard"
202
+ f " No placeholders, skipping: { filepath .name } " , "standard"
202
203
)
203
204
204
205
return
@@ -221,9 +222,9 @@ def add_includes(self, content):
221
222
with includefile .open () as infile :
222
223
includetext = infile .read ()
223
224
224
- replacement = start + " \n " + includetext + " \n " + end
225
+ replacement = f" { start } \n { includetext } \n { end } "
225
226
target_string = re .search (
226
- start + " .*?" + end , content , re .DOTALL
227
+ f" { start } .*?{ end } " , content , re .DOTALL
227
228
).group ()
228
229
content = content .replace (target_string , replacement , 1 )
229
230
@@ -237,7 +238,7 @@ def add_language_selector(self, content, filepath):
237
238
if license_data ["type" ] not in self .languages :
238
239
self .languages [license_data ["type" ]] = []
239
240
glob_string = (
240
- license_data [" type" ] + "_" + license_data [" version" ] + " *.html"
241
+ f" { license_data [' type' ] } _ { license_data [' version' ] } *.html"
241
242
)
242
243
language_file_list = [f for f in self .path .glob (glob_string )]
243
244
for filepath in language_file_list :
@@ -250,44 +251,38 @@ def add_language_selector(self, content, filepath):
250
251
current_language = license_data ["language" ]
251
252
sibling_languages = self .languages [license_data ["type" ]]
252
253
253
- selector = '<div id="language-selector-block" class="container">'
254
- selector += ' <div class ="language-selector-inner ">'
255
- selector += self . lang_sel_text [ current_language ]
256
- selector += (
254
+ selector = (
255
+ ' <div id ="language-selector-block" class="container ">'
256
+ ' <div class="language-selector-inner">'
257
+ f" { self . lang_sel_text [ current_language ] } "
257
258
' <img class="language-icon"'
258
259
' src="/images/language_icon_x2.png" alt="Languages" />'
260
+ " <select>"
259
261
)
260
- selector += " <select>"
261
262
for iso_code in sibling_languages :
262
263
# Set the selected option to the current language of the page
263
264
selected = ""
264
265
if iso_code == current_language :
265
266
selected = ' selected="selected" '
266
267
# Determine to option value for the language. English breaks the
267
268
# pattern so handle it differently.
268
- option_value = "legalcode." + iso_code
269
+ option_value = f "legalcode.{ iso_code } "
269
270
if iso_code == "en" :
270
271
option_value = "legalcode"
271
272
# Add the selector vlaue
272
- selector += (
273
- '<option value="'
274
- + option_value
275
- + '"'
276
- + selected
277
- + ">"
278
- + self .iso_to_language [iso_code ]
279
- + "</option>"
273
+ selector = (
274
+ f'{ selector } <option value="{ option_value } "{ selected } >'
275
+ f"{ self .iso_to_language [iso_code ]} "
276
+ "</option>"
280
277
)
281
- selector += " </select>"
282
- selector += " </div>"
283
- selector += "</div>"
278
+ selector = f"{ selector } </select> </div></div>"
284
279
285
280
# Add the language selector block to the content
286
281
start , end = UpdateLicenseCode .placeholders ["language-selector" ]
287
282
target_string = re .search (
288
- start + " .*?" + end , content , re .DOTALL
283
+ f" { start } .*?{ end } " , content , re .DOTALL
289
284
).group ()
290
- replacement = start + " \n " + selector + " \n " + end
285
+ replacement = f" { start } \n { selector } \n { end } "
291
286
content = content .replace (target_string , replacement , 1 )
292
287
293
288
return content
@@ -315,7 +310,11 @@ def has_placeholders(self, content):
315
310
def main (self ):
316
311
"""Get the command line arguments, find the files, and process them"""
317
312
if self .get_args () and self .get_path ():
318
- file_list = [f for f in self .path .glob ("*4.0*.html" )]
313
+ file_list = [
314
+ f
315
+ for f in self .path .glob ("*4.0*.html" )
316
+ if not os .path .islink (f )
317
+ ]
319
318
self .process_files (file_list )
320
319
321
320
0 commit comments