Skip to content

Commit bc06081

Browse files
committed
updated syntax using Black
1 parent 7613776 commit bc06081

File tree

1 file changed

+109
-80
lines changed

1 file changed

+109
-80
lines changed

tools/prep_cc4_code.py

+109-80
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,48 @@
1717
import re, sys, re, getopt
1818
from pathlib import Path
1919

20+
2021
class UpdateLicenseCode(object):
2122
"""One time script modifying 4.0 legal code files for updated look. This does not change
2223
legal code language. It adds a header and footer placeholders, and updates the HTML head.
2324
This allows the update_cc4_code.py script to function."""
2425

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+
),
3043
}
3144

3245
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"},
3750
}
3851

3952
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):
4962
print(message)
5063

5164
def get_args(self):
@@ -60,9 +73,9 @@ def get_args(self):
6073
self.verbose = False
6174
self.add_placeholders = False
6275
for option in opts:
63-
if '-v' in option:
76+
if "-v" in option:
6477
self.verbose = True
65-
elif '-a' in option:
78+
elif "-a" in option:
6679
self.add_placeholders = True
6780

6881
return True
@@ -72,16 +85,16 @@ def get_path(self):
7285
self.path = False
7386
path = Path.cwd()
7487
pathdir = path.name
75-
if pathdir == 'legalcode':
88+
if pathdir == "legalcode":
7689
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"
8194
if not self.path:
82-
print('Please run from within the checked-out project.')
95+
print("Please run from within the checked-out project.")
8396
if self.path:
84-
self.includes_path = self.path / 'includes'
97+
self.includes_path = self.path / "includes"
8598
return self.path != False
8699

87100
def process_files(self, filelist):
@@ -95,45 +108,48 @@ def process_file(self, filepath):
95108
- Remove references to deed3 css files
96109
- Remove inline styles
97110
- 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:
100113
content = infile.read()
101114
license_attrs = self.get_license_attrs(filepath.name)
102-
115+
103116
content = self.handle_placeholders(content)
104117
content = self.remove_deed3_css(content)
105118
content = self.handle_rtl_css(content)
106119
content = self.remove_old_text_header(content)
107120
content = self.remove_inline_styles(content)
108121
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"])
111124
content = self.handling_consideration_blockquotes(content)
112125

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:
115128
outfile.write(content)
116129

117130
def handle_placeholders(self, content):
118-
self.log(' Adding placeholders', 'verbose')
131+
self.log(" Adding placeholders", "verbose")
119132
# The language selector has to come after the header. Because dictionaries don't
120133
# maint order the easiest way to maintain order is sorting the interation keys.
121134
for placeholder_pair in sorted(UpdateLicenseCode.placeholders):
122135
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+
)
124140
else:
125141
start, end = UpdateLicenseCode.placeholders[placeholder_pair]
126-
if placeholder_pair == 'head':
127-
target = '</head>'
142+
if placeholder_pair == "head":
143+
target = "</head>"
128144
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()
131147
replacement = target + "\n" + start + "\n" + end
132-
elif placeholder_pair == 'footer':
133-
target = '</body>'
148+
elif placeholder_pair == "footer":
149+
target = "</body>"
134150
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 -->"
137153
replacement = target + "\n" + start + "\n" + end
138154
content = content.replace(target, replacement, 1)
139155
return content
@@ -148,94 +164,107 @@ def has_placeholders(self, content, pair_name):
148164

149165
def remove_deed3_css(self, content):
150166
"""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")
152168
content = re.sub(r"\n.*?<link.*?deed3\.css.*?>.*?\n", "\n", content)
153169
content = re.sub(r"\n.*?<link.*?deed3\-print\.css.*?>.*?\n", "\n", content)
154170
content = re.sub(r"\n.*?<link.*?deed3\-ie\.css.*?>.*?\n", "\n", content)
155171
return content
156-
172+
157173
def handle_rtl_css(self, content):
158174
"""The Right-to-Left stylesheet needs to come after the HEAD includes
159175
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:
162178
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]
164180
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+
)
166184
return content
167185

168186
def remove_old_text_header(self, content):
169187
"""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")
171189
content = re.sub(r'<p.*?id="header".*?</p>', "", content, 0, re.DOTALL)
172190
return content
173191

174192
def remove_inline_styles(self, content):
175193
"""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)
178196
return content
179197

180198
def remove_unported_image(self, content):
181199
"""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)
184202
return content
185203

186204
def add_language_class(self, content, language_code):
187205
"""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")
189207
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()
193211
if body_tag.find(language_class) == -1:
194212
# If language class not on body, add it
195-
if body_tag.find('class') > 0:
213+
if body_tag.find("class") > 0:
196214
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+
)
198218
else:
199219
new_body_tag = '<body class="' + language_class + '">'
200220
content = content.replace(body_tag, new_body_tag)
201221
return content
202222

203223
def get_license_attrs(self, filename):
204-
parts = filename.replace('.html', '').split('_')
224+
parts = filename.replace(".html", "").split("_")
205225
lic_type = parts[0]
206-
version = parts[1]
207-
language = ''
226+
version = parts[1]
227+
language = ""
208228
if len(parts) == 3:
209229
language = parts[2]
210-
return {'language': language, 'version': version, 'type': lic_type}
230+
return {"language": language, "version": version, "type": lic_type}
211231

212232
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 = ""
215235
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+
)
225253
content = content.replace(cc_logo_section, new_cc_logo_section)
226254
return content
227255

228256
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>")
231259
return content
232260

233261
def main(self):
234262
"""Get the command line arguments, find the files, and process them"""
235263
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")]
237265
self.process_files(file_list)
238266

239-
if __name__ == '__main__':
267+
268+
if __name__ == "__main__":
240269
updater = UpdateLicenseCode()
241270
updater.main()

0 commit comments

Comments
 (0)