17
17
import re , sys , re , getopt
18
18
from pathlib import Path
19
19
20
+
20
21
class UpdateLicenseCode (object ):
21
22
"""One time script modifying 4.0 legal code files for updated look. This does not change
22
23
legal code language. It adds a header and footer placeholders, and updates the HTML head.
23
24
This allows the update_cc4_code.py script to function."""
24
25
25
- placeholders = {\
26
- 'head' : ('<!-- Head Start - DO NOT DELETE -->' , '<!-- Head End - DO NOT DELETE -->' ), \
27
- 'header' : ('<!-- Site Header Start - DO NOT DELETE -->' , '<!-- Site Header End - DO NOT DELETE -->' ), \
28
- 'footer' : ('<!-- Site Footer Start - DO NOT DELETE -->' , '<!-- Site Footer End - DO NOT DELETE -->' ), \
29
- 'language-selector' : ('<!-- Language Selector Start - DO NOT DELETE -->' , '<!-- Language Selector End - DO NOT DELETE -->' ) \
26
+ placeholders = {
27
+ "head" : (
28
+ "<!-- Head Start - DO NOT DELETE -->" ,
29
+ "<!-- Head End - DO NOT DELETE -->" ,
30
+ ),
31
+ "header" : (
32
+ "<!-- Site Header Start - DO NOT DELETE -->" ,
33
+ "<!-- Site Header End - DO NOT DELETE -->" ,
34
+ ),
35
+ "footer" : (
36
+ "<!-- Site Footer Start - DO NOT DELETE -->" ,
37
+ "<!-- Site Footer End - DO NOT DELETE -->" ,
38
+ ),
39
+ "language-selector" : (
40
+ "<!-- Language Selector Start - DO NOT DELETE -->" ,
41
+ "<!-- Language Selector End - DO NOT DELETE -->" ,
42
+ ),
30
43
}
31
44
32
45
image_map = {
33
- 'by' : {' file' : ' attribution_icon_white.svg' , ' alt_text' : ' Attribution' },
34
- 'sa' : {' file' : ' sa_white.svg' , ' alt_text' : ' Share Alike' },
35
- 'nd' : {' file' : ' nd_white.svg' , ' alt_text' : ' No Derivatives' },
36
- 'nc' : {' file' : ' nc_white.svg' , ' alt_text' : ' Non-Commerical' }
46
+ "by" : {" file" : " attribution_icon_white.svg" , " alt_text" : " Attribution" },
47
+ "sa" : {" file" : " sa_white.svg" , " alt_text" : " Share Alike" },
48
+ "nd" : {" file" : " nd_white.svg" , " alt_text" : " No Derivatives" },
49
+ "nc" : {" file" : " nc_white.svg" , " alt_text" : " Non-Commerical" },
37
50
}
38
51
39
52
def usage (self ):
40
- print ('' )
41
- print (' prep_cc4_code.py [-v]' )
42
- print (' -v: Verbose output' )
43
- print ('' )
44
- print (' e.g. prep_cc4_code.py' )
45
- print (' prep_cc4_code.py -v' )
46
-
47
- def log (self , message , type = ' standard' ):
48
- if (type == ' standard' ) or (type == ' verbose' and self .verbose ):
53
+ print ("" )
54
+ print (" prep_cc4_code.py [-v]" )
55
+ print (" -v: Verbose output" )
56
+ print ("" )
57
+ print (" e.g. prep_cc4_code.py" )
58
+ print (" prep_cc4_code.py -v" )
59
+
60
+ def log (self , message , type = " standard" ):
61
+ if (type == " standard" ) or (type == " verbose" and self .verbose ):
49
62
print (message )
50
63
51
64
def get_args (self ):
@@ -60,9 +73,9 @@ def get_args(self):
60
73
self .verbose = False
61
74
self .add_placeholders = False
62
75
for option in opts :
63
- if '-v' in option :
76
+ if "-v" in option :
64
77
self .verbose = True
65
- elif '-a' in option :
78
+ elif "-a" in option :
66
79
self .add_placeholders = True
67
80
68
81
return True
@@ -72,16 +85,16 @@ def get_path(self):
72
85
self .path = False
73
86
path = Path .cwd ()
74
87
pathdir = path .name
75
- if pathdir == ' legalcode' :
88
+ if pathdir == " legalcode" :
76
89
self .path = path
77
- if pathdir == ' docroot' :
78
- self .path = path / ' legalcode'
79
- if pathdir == ' tools' :
80
- self .path = path .parent / ' docroot' / ' legalcode'
90
+ if pathdir == " docroot" :
91
+ self .path = path / " legalcode"
92
+ if pathdir == " tools" :
93
+ self .path = path .parent / " docroot" / " legalcode"
81
94
if not self .path :
82
- print (' Please run from within the checked-out project.' )
95
+ print (" Please run from within the checked-out project." )
83
96
if self .path :
84
- self .includes_path = self .path / ' includes'
97
+ self .includes_path = self .path / " includes"
85
98
return self .path != False
86
99
87
100
def process_files (self , filelist ):
@@ -95,45 +108,48 @@ def process_file(self, filepath):
95
108
- Remove references to deed3 css files
96
109
- Remove inline styles
97
110
- Remove Creative Commons text header"""
98
- self .log (' Processing: ' + filepath .name , ' verbose' )
99
- with filepath .open (encoding = ' utf-8' ) as infile :
111
+ self .log (" Processing: " + filepath .name , " verbose" )
112
+ with filepath .open (encoding = " utf-8" ) as infile :
100
113
content = infile .read ()
101
114
license_attrs = self .get_license_attrs (filepath .name )
102
-
115
+
103
116
content = self .handle_placeholders (content )
104
117
content = self .remove_deed3_css (content )
105
118
content = self .handle_rtl_css (content )
106
119
content = self .remove_old_text_header (content )
107
120
content = self .remove_inline_styles (content )
108
121
content = self .remove_unported_image (content )
109
- content = self .add_language_class (content , license_attrs [' language' ])
110
- content = self .add_type_logos (content , license_attrs [' type' ])
122
+ content = self .add_language_class (content , license_attrs [" language" ])
123
+ content = self .add_type_logos (content , license_attrs [" type" ])
111
124
content = self .handling_consideration_blockquotes (content )
112
125
113
- self .log (' Updating content: ' + filepath .name , ' verbose' )
114
- with filepath .open ('w' , encoding = ' utf-8' ) as outfile :
126
+ self .log (" Updating content: " + filepath .name , " verbose" )
127
+ with filepath .open ("w" , encoding = " utf-8" ) as outfile :
115
128
outfile .write (content )
116
129
117
130
def handle_placeholders (self , content ):
118
- self .log (' Adding placeholders' , ' verbose' )
131
+ self .log (" Adding placeholders" , " verbose" )
119
132
# The language selector has to come after the header. Because dictionaries don't
120
133
# maint order the easiest way to maintain order is sorting the interation keys.
121
134
for placeholder_pair in sorted (UpdateLicenseCode .placeholders ):
122
135
if self .has_placeholders (content , placeholder_pair ):
123
- self .log (' Found placeholder: ' + placeholder_pair + ', skipping' , 'verbose' )
136
+ self .log (
137
+ " Found placeholder: " + placeholder_pair + ", skipping" ,
138
+ "verbose" ,
139
+ )
124
140
else :
125
141
start , end = UpdateLicenseCode .placeholders [placeholder_pair ]
126
- if placeholder_pair == ' head' :
127
- target = ' </head>'
142
+ if placeholder_pair == " head" :
143
+ target = " </head>"
128
144
replacement = start + "\n " + end + "\n " + target
129
- elif placeholder_pair == ' header' :
130
- target = re .search (' <body.*?>' , content ).group ()
145
+ elif placeholder_pair == " header" :
146
+ target = re .search (" <body.*?>" , content ).group ()
131
147
replacement = target + "\n " + start + "\n " + end
132
- elif placeholder_pair == ' footer' :
133
- target = ' </body>'
148
+ elif placeholder_pair == " footer" :
149
+ target = " </body>"
134
150
replacement = start + "\n " + end + "\n " + target
135
- elif placeholder_pair == ' language-selector' :
136
- target = ' <!-- Site Header End - DO NOT DELETE -->'
151
+ elif placeholder_pair == " language-selector" :
152
+ target = " <!-- Site Header End - DO NOT DELETE -->"
137
153
replacement = target + "\n " + start + "\n " + end
138
154
content = content .replace (target , replacement , 1 )
139
155
return content
@@ -148,94 +164,107 @@ def has_placeholders(self, content, pair_name):
148
164
149
165
def remove_deed3_css (self , content ):
150
166
"""Remove refererences to deed3 css stylesheets from HEAD"""
151
- self .log (' Removing deed3 css references from head' , ' verbose' )
167
+ self .log (" Removing deed3 css references from head" , " verbose" )
152
168
content = re .sub (r"\n.*?<link.*?deed3\.css.*?>.*?\n" , "\n " , content )
153
169
content = re .sub (r"\n.*?<link.*?deed3\-print\.css.*?>.*?\n" , "\n " , content )
154
170
content = re .sub (r"\n.*?<link.*?deed3\-ie\.css.*?>.*?\n" , "\n " , content )
155
171
return content
156
-
172
+
157
173
def handle_rtl_css (self , content ):
158
174
"""The Right-to-Left stylesheet needs to come after the HEAD includes
159
175
and be renamed"""
160
- self .log (' Handling right to left css' , ' verbose' )
161
- if content .find (' deed3-rtl.css' ) != - 1 :
176
+ self .log (" Handling right to left css" , " verbose" )
177
+ if content .find (" deed3-rtl.css" ) != - 1 :
162
178
content = re .sub (r"\n.*?<link.*?deed3\-rtl\.css.*?>.*?\n" , "\n " , content )
163
- bottom_placholder = UpdateLicenseCode .placeholders [' head' ][1 ]
179
+ bottom_placholder = UpdateLicenseCode .placeholders [" head" ][1 ]
164
180
new_rtl_css = '<link rel="stylesheet" type="text/css" href="/includes/legalcode-rtl.css" media="all">'
165
- content = content .replace (bottom_placholder , bottom_placholder + '\n ' + new_rtl_css )
181
+ content = content .replace (
182
+ bottom_placholder , bottom_placholder + "\n " + new_rtl_css
183
+ )
166
184
return content
167
185
168
186
def remove_old_text_header (self , content ):
169
187
"""Remove the paragraph string with id=header"""
170
- self .log (' Removing paragraph with id="header"' , ' verbose' )
188
+ self .log (' Removing paragraph with id="header"' , " verbose" )
171
189
content = re .sub (r'<p.*?id="header".*?</p>' , "" , content , 0 , re .DOTALL )
172
190
return content
173
191
174
192
def remove_inline_styles (self , content ):
175
193
"""Remove inline styles"""
176
- self .log (' Remove inline styles' , ' verbose' )
177
- content = re .sub (r' <style.*?</style>' , "" , content , 0 , re .DOTALL )
194
+ self .log (" Remove inline styles" , " verbose" )
195
+ content = re .sub (r" <style.*?</style>" , "" , content , 0 , re .DOTALL )
178
196
return content
179
197
180
198
def remove_unported_image (self , content ):
181
199
"""Remove inline styles"""
182
- self .log (' Remove unported image' , ' verbose' )
183
- content = re .sub (r' <img.*?src=.*?unported\.png.*?>' , "" , content )
200
+ self .log (" Remove unported image" , " verbose" )
201
+ content = re .sub (r" <img.*?src=.*?unported\.png.*?>" , "" , content )
184
202
return content
185
203
186
204
def add_language_class (self , content , language_code ):
187
205
"""Add language class to body tag"""
188
- self .log (' Add language class to body tag' , ' verbose' )
206
+ self .log (" Add language class to body tag" , " verbose" )
189
207
if not language_code :
190
- language_code = 'en'
191
- language_class = ' lang-' + language_code
192
- body_tag = re .search (' <body.*?>' , content , re .IGNORECASE ).group ()
208
+ language_code = "en"
209
+ language_class = " lang-" + language_code
210
+ body_tag = re .search (" <body.*?>" , content , re .IGNORECASE ).group ()
193
211
if body_tag .find (language_class ) == - 1 :
194
212
# If language class not on body, add it
195
- if body_tag .find (' class' ) > 0 :
213
+ if body_tag .find (" class" ) > 0 :
196
214
existing_classes = re .search ('class="(.*?)"' , body_tag ).group (1 )
197
- new_body_tag = '<body class="' + existing_classes + ' ' + language_class + '">'
215
+ new_body_tag = (
216
+ '<body class="' + existing_classes + " " + language_class + '">'
217
+ )
198
218
else :
199
219
new_body_tag = '<body class="' + language_class + '">'
200
220
content = content .replace (body_tag , new_body_tag )
201
221
return content
202
222
203
223
def get_license_attrs (self , filename ):
204
- parts = filename .replace (' .html' , '' ).split ('_' )
224
+ parts = filename .replace (" .html" , "" ).split ("_" )
205
225
lic_type = parts [0 ]
206
- version = parts [1 ]
207
- language = ''
226
+ version = parts [1 ]
227
+ language = ""
208
228
if len (parts ) == 3 :
209
229
language = parts [2 ]
210
- return {' language' : language , ' version' : version , ' type' : lic_type }
230
+ return {" language" : language , " version" : version , " type" : lic_type }
211
231
212
232
def add_type_logos (self , content , lic_type ):
213
- lic_type_attrs = lic_type .split ('-' )
214
- lic_images = ''
233
+ lic_type_attrs = lic_type .split ("-" )
234
+ lic_images = ""
215
235
for lic_attr in lic_type_attrs :
216
- filename = UpdateLicenseCode .image_map [lic_attr ]['file' ]
217
- alt_text = UpdateLicenseCode .image_map [lic_attr ]['alt_text' ]
218
- image_tag = '<img src="/images/deed/svg/' + filename + '" alt="' + alt_text + '"/>'
219
- lic_images += '<span class="cc-icon-' + lic_attr + '">' + image_tag + '</span>'
220
- cc_logo_section = re .search ('<div id="cc-logo">.*?</div>' , content , re .DOTALL ).group ()
221
- new_cc_logo_section = '<div id="cc-logo">' \
222
- + '<span class="cc-icon-logo"><img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>' \
223
- + lic_images \
224
- + '</div>'
236
+ filename = UpdateLicenseCode .image_map [lic_attr ]["file" ]
237
+ alt_text = UpdateLicenseCode .image_map [lic_attr ]["alt_text" ]
238
+ image_tag = (
239
+ '<img src="/images/deed/svg/' + filename + '" alt="' + alt_text + '"/>'
240
+ )
241
+ lic_images += (
242
+ '<span class="cc-icon-' + lic_attr + '">' + image_tag + "</span>"
243
+ )
244
+ cc_logo_section = re .search (
245
+ '<div id="cc-logo">.*?</div>' , content , re .DOTALL
246
+ ).group ()
247
+ new_cc_logo_section = (
248
+ '<div id="cc-logo">'
249
+ + '<span class="cc-icon-logo"><img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>'
250
+ + lic_images
251
+ + "</div>"
252
+ )
225
253
content = content .replace (cc_logo_section , new_cc_logo_section )
226
254
return content
227
255
228
256
def handling_consideration_blockquotes (self , content ):
229
- content = content .replace (' <blockquote>' , '<p class="usage-considerations">' )
230
- content = content .replace (' </blockquote>' , ' </p>' )
257
+ content = content .replace (" <blockquote>" , '<p class="usage-considerations">' )
258
+ content = content .replace (" </blockquote>" , " </p>" )
231
259
return content
232
260
233
261
def main (self ):
234
262
"""Get the command line arguments, find the files, and process them"""
235
263
if self .get_args () and self .get_path ():
236
- file_list = [f for f in self .path .glob (' *4.0*.html' )]
264
+ file_list = [f for f in self .path .glob (" *4.0*.html" )]
237
265
self .process_files (file_list )
238
266
239
- if __name__ == '__main__' :
267
+
268
+ if __name__ == "__main__" :
240
269
updater = UpdateLicenseCode ()
241
270
updater .main ()
0 commit comments