Skip to content

Commit f0f4ba1

Browse files
committed
updated style/syntax per black and flake8. added black config
1 parent eef1e5b commit f0f4ba1

5 files changed

Lines changed: 114 additions & 45 deletions

File tree

tools/add_cc0_links.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,33 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import os.path, re, sys
1817
from pathlib import Path
18+
import os.path
19+
import re
20+
import sys
1921

2022

2123
class AddCC0Links(object):
2224
def usage(self):
2325
print("add-cc0-links.py LANGUAGE_CODE LANGUAGE_NAME")
2426
print(" e.g. add-cc0-links.py nl Nederlands")
2527
print(
26-
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename."
28+
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N,"
29+
" the same used in filename."
2730
)
2831
print(" LANGUAGE_NAME must be in the relevant language")
29-
print(" if it contains whitespace, enclose in quotes.")
32+
print(
33+
" if it contains whitespace, enclose in quotes."
34+
)
3035

3136
def get_args(self):
3237
# Make sure there are enough args
3338
# Make sure arg 2 is a language code
3439
# Make sure arg 3 is not a language code
3540
self.args_ok = (
36-
(len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) and (len(sys.argv[2]) >= 2)
41+
(len(sys.argv) == 3)
42+
and (len(sys.argv[1]) >= 2)
43+
and (len(sys.argv[2]) >= 2)
3744
)
3845
if self.args_ok:
3946
self.language_code = sys.argv[1]
@@ -56,7 +63,7 @@ def get_path(self):
5663
self.path = path.parent / "docroot" / "legalcode"
5764
if not self.path:
5865
print("Please run from within the checked-out project.")
59-
return self.path != False
66+
return self.path is not False
6067

6168
def get_files(self):
6269
"""Get all the CC0 files *except* those we are linking to"""
@@ -83,13 +90,14 @@ def file_license_and_language(self, filepath):
8390
def links_in_page(self, content):
8491
"""Find the translated license links at the bottom of the page"""
8592
return re.findall(
86-
r'//creativecommons\.org/publicdomain/zero/1\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>',
93+
r"//creativecommons\.org/publicdomain/zero/1\.0/"
94+
'legalcode([.][^"]{2,})?">([^>]+)</a>',
8795
content,
8896
)
8997

9098
def is_rtl(self, content):
9199
"""Determine whether the page is in a right-to-left script"""
92-
return re.search(r' dir="rtl"', content) != None
100+
return re.search(r' dir="rtl"', content) is not None
93101

94102
def insert_at_index(self, links, rtl):
95103
"""Find the alphabetic position in the list of translated license links
@@ -135,7 +143,7 @@ def file_contains_link_already(self, links):
135143
),
136144
False,
137145
)
138-
!= False
146+
is not False
139147
)
140148

141149
def process_file(self, filepath):

tools/add_cc4_links.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,44 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import os.path, re, sys
1817
from pathlib import Path
18+
import os.path
19+
import re
20+
import sys
1921

2022

2123
class AddCC4Links(object):
2224
"""Adds a link to a license, specified by language code and name, to all
2325
existing CC 4.0 license legalcodes where they do not already contain a
2426
link to it.
27+
2528
Make sure to run it in a checked out version of the creativecommons.org
2629
repository, either in the tools directory, docroot,
2730
or docroot/legalcode.
31+
2832
Note that this code modifies files inline. Commit any other changes
2933
before running it."""
3034

3135
def usage(self):
3236
print("add-cc4-links.py LANGUAGE_CODE LANGUAGE_NAME")
3337
print(" e.g. add-cc4-links.py nl Nederlands")
3438
print(
35-
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename."
39+
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N,"
40+
" the same used in filename."
3641
)
3742
print(" LANGUAGE_NAME must be in the relevant language")
38-
print(" if it contains whitespace, enclose in quotes.")
43+
print(
44+
" if it contains whitespace, enclose in quotes."
45+
)
3946

4047
def get_args(self):
4148
# Make sure there are enough args
4249
# Make sure arg 2 is a language code
4350
# Make sure arg 3 is not a language code
4451
self.args_ok = (
45-
(len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) and (len(sys.argv[2]) >= 2)
52+
(len(sys.argv) == 3)
53+
and (len(sys.argv[1]) >= 2)
54+
and (len(sys.argv[2]) >= 2)
4655
)
4756
if self.args_ok:
4857
self.language_code = sys.argv[1]
@@ -65,7 +74,7 @@ def get_path(self):
6574
self.path = path.parent / "docroot" / "legalcode"
6675
if not self.path:
6776
print("Please run from within the checked-out project.")
68-
return self.path != False
77+
return self.path is not False
6978

7079
def get_files(self):
7180
"""Get all the 4.0 files *except* those we are linking to"""
@@ -92,14 +101,15 @@ def file_license_and_language(self, filepath):
92101
def links_in_page(self, content):
93102
"""Find the translated license links at the bottom of the page"""
94103
return re.findall(
95-
r'//creativecommons\.org/licenses/[^/]+/4\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>',
104+
r"//creativecommons\.org/licenses/[^/]+/4\.0/"
105+
'legalcode([.][^"]{2,})?">([^>]+)</a>',
96106
content,
97107
)
98108

99109
def is_rtl(self, content):
100110
"""Determine whether the page is in a right-to-left script"""
101-
return (re.search(r' dir="rtl"', content) != None) or (
102-
re.search(r'class="rtl"', content) != None
111+
return (re.search(r' dir="rtl"', content) is not None) or (
112+
re.search(r'class="rtl"', content) is not None
103113
)
104114

105115
def insert_at_index_rtl(self, links):
@@ -160,7 +170,7 @@ def file_contains_link_already(self, links):
160170
),
161171
False,
162172
)
163-
!= False
173+
is not False
164174
)
165175

166176
def process_file(self, filepath):

tools/prep_cc4_code.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import re, sys, re, getopt
1817
from pathlib import Path
18+
import getopt
19+
import re
20+
import sys
1921

2022

2123
class UpdateLicenseCode(object):
22-
"""One time script modifying 4.0 legal code files for updated look. This does not change
23-
legal code language. It adds a header and footer placeholders, and updates the HTML head.
24-
This allows the update_cc4_code.py script to function."""
24+
"""One time script modifying 4.0 legal code files for updated look. This
25+
does not change legal code language. It adds a header and footer
26+
placeholders, and updates the HTML head.
27+
28+
This allows the update_cc4_code.py script to function."""
2529

2630
placeholders = {
2731
"head": (
@@ -43,7 +47,10 @@ class UpdateLicenseCode(object):
4347
}
4448

4549
image_map = {
46-
"by": {"file": "attribution_icon_white.svg", "alt_text": "Attribution"},
50+
"by": {
51+
"file": "attribution_icon_white.svg",
52+
"alt_text": "Attribution",
53+
},
4754
"sa": {"file": "sa_white.svg", "alt_text": "Share Alike"},
4855
"nd": {"file": "nd_white.svg", "alt_text": "No Derivatives"},
4956
"nc": {"file": "nc_white.svg", "alt_text": "Non-Commerical"},
@@ -62,8 +69,8 @@ def log(self, message, type="standard"):
6269
print(message)
6370

6471
def get_args(self):
65-
"""Get arguments/options and set corresponding flags. On validation error
66-
print usage help"""
72+
"""Get arguments/options and set corresponding flags. On validation
73+
error print usage help"""
6774
try:
6875
opts, args = getopt.getopt(sys.argv[1:], "va")
6976
except getopt.GetoptError:
@@ -95,7 +102,7 @@ def get_path(self):
95102
print("Please run from within the checked-out project.")
96103
if self.path:
97104
self.includes_path = self.path / "includes"
98-
return self.path != False
105+
return self.path is not False
99106

100107
def process_files(self, filelist):
101108
"""File processing loop"""
@@ -129,8 +136,9 @@ def process_file(self, filepath):
129136

130137
def handle_placeholders(self, content):
131138
self.log(" Adding placeholders", "verbose")
132-
# The language selector has to come after the header. Because dictionaries don't
133-
# maint order the easiest way to maintain order is sorting the interation keys.
139+
# The language selector has to come after the header. Because
140+
# dictionaries don't maintain order the easiest way to maintain order
141+
# is sorting the interation keys.
134142
for placeholder_pair in sorted(UpdateLicenseCode.placeholders):
135143
if self.has_placeholders(content, placeholder_pair):
136144
self.log(
@@ -166,18 +174,27 @@ def remove_deed3_css(self, content):
166174
"""Remove refererences to deed3 css stylesheets from HEAD"""
167175
self.log(" Removing deed3 css references from head", "verbose")
168176
content = re.sub(r"\n.*?<link.*?deed3\.css.*?>.*?\n", "\n", content)
169-
content = re.sub(r"\n.*?<link.*?deed3\-print\.css.*?>.*?\n", "\n", content)
170-
content = re.sub(r"\n.*?<link.*?deed3\-ie\.css.*?>.*?\n", "\n", content)
177+
content = re.sub(
178+
r"\n.*?<link.*?deed3\-print\.css.*?>.*?\n", "\n", content
179+
)
180+
content = re.sub(
181+
r"\n.*?<link.*?deed3\-ie\.css.*?>.*?\n", "\n", content
182+
)
171183
return content
172184

173185
def handle_rtl_css(self, content):
174186
"""The Right-to-Left stylesheet needs to come after the HEAD includes
175187
and be renamed"""
176188
self.log(" Handling right to left css", "verbose")
177189
if content.find("deed3-rtl.css") != -1:
178-
content = re.sub(r"\n.*?<link.*?deed3\-rtl\.css.*?>.*?\n", "\n", content)
190+
content = re.sub(
191+
r"\n.*?<link.*?deed3\-rtl\.css.*?>.*?\n", "\n", content
192+
)
179193
bottom_placholder = UpdateLicenseCode.placeholders["head"][1]
180-
new_rtl_css = '<link rel="stylesheet" type="text/css" href="/includes/legalcode-rtl.css" media="all">'
194+
new_rtl_css = (
195+
'<link rel="stylesheet" type="text/css"'
196+
' href="/includes/legalcode-rtl.css" media="all">'
197+
)
181198
content = content.replace(
182199
bottom_placholder, bottom_placholder + "\n" + new_rtl_css
183200
)
@@ -211,9 +228,15 @@ def add_language_class(self, content, language_code):
211228
if body_tag.find(language_class) == -1:
212229
# If language class not on body, add it
213230
if body_tag.find("class") > 0:
214-
existing_classes = re.search('class="(.*?)"', body_tag).group(1)
231+
existing_classes = re.search('class="(.*?)"', body_tag).group(
232+
1
233+
)
215234
new_body_tag = (
216-
'<body class="' + existing_classes + " " + language_class + '">'
235+
'<body class="'
236+
+ existing_classes
237+
+ " "
238+
+ language_class
239+
+ '">'
217240
)
218241
else:
219242
new_body_tag = '<body class="' + language_class + '">'
@@ -236,25 +259,36 @@ def add_type_logos(self, content, lic_type):
236259
filename = UpdateLicenseCode.image_map[lic_attr]["file"]
237260
alt_text = UpdateLicenseCode.image_map[lic_attr]["alt_text"]
238261
image_tag = (
239-
'<img src="/images/deed/svg/' + filename + '" alt="' + alt_text + '"/>'
262+
'<img src="/images/deed/svg/'
263+
+ filename
264+
+ '" alt="'
265+
+ alt_text
266+
+ '"/>'
240267
)
241268
lic_images += (
242-
'<span class="cc-icon-' + lic_attr + '">' + image_tag + "</span>"
269+
'<span class="cc-icon-'
270+
+ lic_attr
271+
+ '">'
272+
+ image_tag
273+
+ "</span>"
243274
)
244275
cc_logo_section = re.search(
245276
'<div id="cc-logo">.*?</div>', content, re.DOTALL
246277
).group()
247278
new_cc_logo_section = (
248279
'<div id="cc-logo">'
249-
+ '<span class="cc-icon-logo"><img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>'
280+
+ '<span class="cc-icon-logo">'
281+
+ '<img src="/images/deed/svg/cc_white.svg" alt="CC"/></span>'
250282
+ lic_images
251283
+ "</div>"
252284
)
253285
content = content.replace(cc_logo_section, new_cc_logo_section)
254286
return content
255287

256288
def handling_consideration_blockquotes(self, content):
257-
content = content.replace("<blockquote>", '<p class="usage-considerations">')
289+
content = content.replace(
290+
"<blockquote>", '<p class="usage-considerations">'
291+
)
258292
content = content.replace("</blockquote>", "</p>")
259293
return content
260294

tools/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
line-length = 79

0 commit comments

Comments
 (0)