Skip to content

Commit d484426

Browse files
committed
Merge branch 'master' into dev2019
2 parents edd4bef + 99947bd commit d484426

4 files changed

Lines changed: 413 additions & 282 deletions

File tree

tools/add_cc0_links.py

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,31 @@
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
17+
import os.path, re, sys
1818
from pathlib import Path
1919

20-
class AddCC0Links(object):
2120

21+
class AddCC0Links(object):
2222
def usage(self):
23-
print('add-cc0-links.py LANGUAGE_CODE LANGUAGE_NAME')
24-
print(' e.g. add-cc0-links.py nl Nederlands')
25-
print(' LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename.')
26-
print(' LANGUAGE_NAME must be in the relevant language')
27-
print(' if it contains whitespace, enclose in quotes.')
23+
print("add-cc0-links.py LANGUAGE_CODE LANGUAGE_NAME")
24+
print(" e.g. add-cc0-links.py nl Nederlands")
25+
print(
26+
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename."
27+
)
28+
print(" LANGUAGE_NAME must be in the relevant language")
29+
print(" if it contains whitespace, enclose in quotes.")
2830

2931
def get_args(self):
3032
# Make sure there are enough args
3133
# Make sure arg 2 is a language code
3234
# Make sure arg 3 is not a language code
33-
self.args_ok = (len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) \
34-
and (len(sys.argv[2]) >= 2)
35+
self.args_ok = (
36+
(len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) and (len(sys.argv[2]) >= 2)
37+
)
3538
if self.args_ok:
3639
self.language_code = sys.argv[1]
3740
self.language_name = sys.argv[2]
38-
self.exclude_pattern = 'zero_1.0_' + self.language_code + '.html'
41+
self.exclude_pattern = "zero_1.0_" + self.language_code + ".html"
3942
else:
4043
self.usage()
4144
return self.args_ok
@@ -45,20 +48,24 @@ def get_path(self):
4548
self.path = False
4649
path = Path.cwd()
4750
pathdir = path.name
48-
if pathdir == 'legalcode':
51+
if pathdir == "legalcode":
4952
self.path = path
50-
if pathdir == 'docroot':
51-
self.path = path / 'legalcode'
52-
if pathdir == 'tools':
53-
self.path = path.parent / 'docroot' /'legalcode'
53+
if pathdir == "docroot":
54+
self.path = path / "legalcode"
55+
if pathdir == "tools":
56+
self.path = path.parent / "docroot" / "legalcode"
5457
if not self.path:
55-
print('Please run from within the checked-out project.')
58+
print("Please run from within the checked-out project.")
5659
return self.path != False
5760

5861
def get_files(self):
5962
"""Get all the CC0 files *except* those we are linking to"""
60-
self.files = [f for f in self.path.glob('zero_1.0*.html')
61-
if not f.match(self.exclude_pattern)]
63+
self.files = [
64+
f
65+
for f in self.path.glob("zero_1.0*.html")
66+
if not os.path.islink(f) and not f.match(self.exclude_pattern)
67+
]
68+
self.files.sort()
6269

6370
def process_files(self):
6471
"""Add links to all the license files"""
@@ -67,15 +74,18 @@ def process_files(self):
6774

6875
def file_license_and_language(self, filepath):
6976
"""Get the license number and language code from the file path"""
70-
elements = filepath.stem.split('_')
77+
elements = filepath.stem.split("_")
7178
# Un-translated deeds don't have a language code, so set to English
7279
if len(elements) != 3:
73-
elements += ['en']
80+
elements += ["en"]
7481
return elements[0], elements[2]
7582

7683
def links_in_page(self, content):
7784
"""Find the translated license links at the bottom of the page"""
78-
return re.findall(r'//creativecommons\.org/publicdomain/zero/1\.0/legalcode(\...)?">([^>]+)</a>', content)
85+
return re.findall(
86+
r'//creativecommons\.org/publicdomain/zero/1\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>',
87+
content,
88+
)
7989

8090
def is_rtl(self, content):
8191
"""Determine whether the page is in a right-to-left script"""
@@ -98,22 +108,35 @@ def insert_link(self, content, lic, links, index):
98108
"""Insert the link to the correct version of the license
99109
in the correct position in the list of links at the bottom of the
100110
page"""
101-
link = '<a href="//creativecommons.org/publicdomain/zero/1.0/legalcode.' + self.language_code + '">' + self.language_name + '</a>'
111+
link = (
112+
'<a href="//creativecommons.org/publicdomain/zero/1.0/legalcode.'
113+
+ self.language_code
114+
+ '">'
115+
+ self.language_name
116+
+ "</a>"
117+
)
102118
if index == -1:
103119
target = '<a href="//creativecommons.org/publicdomain/zero/1.0/'
104-
replace = link + ', ' + target
120+
replace = link + ", " + target
105121
else:
106122
lang = links[index][1]
107-
target = '>' + lang + '</a>'
108-
replace = target + ', ' + link
123+
target = ">" + lang + "</a>"
124+
replace = target + ", " + link
109125
return content.replace(target, replace, 1)
110126

111127
def file_contains_link_already(self, links):
112128
"""Did we already add a link to this page?"""
113-
return next((code for code, name in links
114-
if name == self.language_name
115-
or code == self.language_code),
116-
False) != False
129+
return (
130+
next(
131+
(
132+
code
133+
for code, name in links
134+
if name == self.language_name or code == self.language_code
135+
),
136+
False,
137+
)
138+
!= False
139+
)
117140

118141
def process_file(self, filepath):
119142
"""Get the file's details and insert a link to the translated version
@@ -130,18 +153,19 @@ def process_file(self, filepath):
130153
print(index)
131154
print(links[index])
132155
updated_content = self.insert_link(content, lic, links, index)
133-
with filepath.open('w') as outfile:
156+
with filepath.open("w") as outfile:
134157
outfile.write(updated_content)
135-
print('Added link to file: ' + filepath.name)
136-
else:
137-
print('File already contains link: ' + filepath.name)
158+
print("Added link to file: " + filepath.name)
159+
# else:
160+
# print("File already contains link: " + filepath.name)
138161

139162
def main(self):
140163
"""Get the command line arguments, find the files, and process them"""
141164
if self.get_args() and self.get_path():
142165
self.get_files()
143166
self.process_files()
144167

145-
if __name__ == '__main__':
168+
169+
if __name__ == "__main__":
146170
link_adder = AddCC0Links()
147171
link_adder.main()

tools/add_cc4_links.py

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
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
17+
import os.path, re, sys
1818
from pathlib import Path
1919

20+
2021
class AddCC4Links(object):
2122
"""Adds a link to a license, specified by language code and name, to all
2223
existing CC 4.0 license legalcodes where they do not already contain a
@@ -26,47 +27,54 @@ class AddCC4Links(object):
2627
or docroot/legalcode.
2728
Note that this code modifies files inline. Commit any other changes
2829
before running it."""
29-
30+
3031
def usage(self):
31-
print('add-cc4-links.py LANGUAGE_CODE LANGUAGE_NAME')
32-
print(' e.g. add-cc4-links.py nl Nederlands')
33-
print(' LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename.')
34-
print(' LANGUAGE_NAME must be in the relevant language')
35-
print(' if it contains whitespace, enclose in quotes.')
36-
32+
print("add-cc4-links.py LANGUAGE_CODE LANGUAGE_NAME")
33+
print(" e.g. add-cc4-links.py nl Nederlands")
34+
print(
35+
" LANGUAGE_CODE must be 2 letters or 2-hyphen-N, the same used in filename."
36+
)
37+
print(" LANGUAGE_NAME must be in the relevant language")
38+
print(" if it contains whitespace, enclose in quotes.")
39+
3740
def get_args(self):
3841
# Make sure there are enough args
3942
# Make sure arg 2 is a language code
4043
# Make sure arg 3 is not a language code
41-
self.args_ok = (len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) \
42-
and (len(sys.argv[2]) >= 2)
44+
self.args_ok = (
45+
(len(sys.argv) == 3) and (len(sys.argv[1]) >= 2) and (len(sys.argv[2]) >= 2)
46+
)
4347
if self.args_ok:
4448
self.language_code = sys.argv[1]
4549
self.language_name = sys.argv[2]
46-
self.exclude_pattern = '*_4.0_' + self.language_code + '.html'
50+
self.exclude_pattern = "*_4.0_" + self.language_code + ".html"
4751
else:
4852
self.usage()
4953
return self.args_ok
50-
54+
5155
def get_path(self):
5256
"""Where are the licenses?"""
5357
self.path = False
5458
path = Path.cwd()
5559
pathdir = path.name
56-
if pathdir == 'legalcode':
60+
if pathdir == "legalcode":
5761
self.path = path
58-
if pathdir == 'docroot':
59-
self.path = path / 'legalcode'
60-
if pathdir == 'tools':
61-
self.path = path.parent / 'docroot' /'legalcode'
62+
if pathdir == "docroot":
63+
self.path = path / "legalcode"
64+
if pathdir == "tools":
65+
self.path = path.parent / "docroot" / "legalcode"
6266
if not self.path:
63-
print('Please run from within the checked-out project.')
67+
print("Please run from within the checked-out project.")
6468
return self.path != False
6569

6670
def get_files(self):
6771
"""Get all the 4.0 files *except* those we are linking to"""
68-
self.files = [f for f in self.path.glob('*_4.0*.html')
69-
if not f.match(self.exclude_pattern)]
72+
self.files = [
73+
f
74+
for f in self.path.glob("*_4.0*.html")
75+
if (not os.path.islink(f) and not f.match(self.exclude_pattern))
76+
]
77+
self.files.sort()
7078

7179
def process_files(self):
7280
"""Add links to all the license files"""
@@ -75,20 +83,24 @@ def process_files(self):
7583

7684
def file_license_and_language(self, filepath):
7785
"""Get the license number and language code from the file path"""
78-
elements = filepath.stem.split('_')
86+
elements = filepath.stem.split("_")
7987
# Un-translated deeds don't have a language code, so set to English
8088
if len(elements) != 3:
81-
elements += ['en']
89+
elements += ["en"]
8290
return elements[0], elements[2]
8391

8492
def links_in_page(self, content):
8593
"""Find the translated license links at the bottom of the page"""
86-
return re.findall(r'//creativecommons\.org/licenses/[^/]+/4\.0/legalcode(\...)?">([^>]+)</a>', content)
94+
return re.findall(
95+
r'//creativecommons\.org/licenses/[^/]+/4\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>',
96+
content,
97+
)
8798

8899
def is_rtl(self, content):
89100
"""Determine whether the page is in a right-to-left script"""
90-
return (re.search(r' dir="rtl"', content) != None) or \
91-
(re.search(r'class="rtl"', content) != None)
101+
return (re.search(r' dir="rtl"', content) != None) or (
102+
re.search(r'class="rtl"', content) != None
103+
)
92104

93105
def insert_at_index_rtl(self, links):
94106
index = -1
@@ -107,38 +119,50 @@ def insert_at_index_ltr(self, links):
107119
index += 1
108120
return index
109121

110-
111122
def insert_at_index(self, links, rtl):
112123
"""Find the alphabetic position in the list of translated license links
113124
to insert the link at"""
114125
if rtl:
115126
return self.insert_at_index_rtl(links)
116127
else:
117128
return self.insert_at_index_ltr(links)
118-
129+
119130
def insert_link(self, content, lic, links, index):
120131
"""Insert the link to the correct version of the license
121132
in the correct position in the list of links at the bottom of the
122133
page"""
123-
link = '<a href="//creativecommons.org/licenses/' + lic \
124-
+ '/4.0/legalcode.' + self.language_code \
125-
+ '">' + self.language_name + '</a>'
134+
link = (
135+
'<a href="//creativecommons.org/licenses/'
136+
+ lic
137+
+ "/4.0/legalcode."
138+
+ self.language_code
139+
+ '">'
140+
+ self.language_name
141+
+ "</a>"
142+
)
126143
if index == -1:
127144
target = '<a href="//creativecommons.org/licenses/' + lic
128-
replace = link + ', ' + target
145+
replace = link + ", " + target
129146
else:
130147
lang = links[index][1]
131-
target = '>' + lang + '</a>'
132-
replace = target + ', ' + link
148+
target = ">" + lang + "</a>"
149+
replace = target + ", " + link
133150
return content.replace(target, replace, 1)
134-
151+
135152
def file_contains_link_already(self, links):
136153
"""Did we already add a link to this page?"""
137-
return next((code for code, name in links
138-
if name == self.language_name
139-
or code == self.language_code),
140-
False) != False
141-
154+
return (
155+
next(
156+
(
157+
code
158+
for code, name in links
159+
if name == self.language_name or code == self.language_code
160+
),
161+
False,
162+
)
163+
!= False
164+
)
165+
142166
def process_file(self, filepath):
143167
"""Get the file's details and insert a link to the translated version
144168
into it"""
@@ -149,25 +173,26 @@ def process_file(self, filepath):
149173
if not self.file_contains_link_already(links):
150174
rtl = self.is_rtl(content)
151175
index = self.insert_at_index(links, rtl)
152-
#print(links)
153-
#print(index)
154-
#print(links[index])
176+
# print(links)
177+
# print(index)
178+
# print(links[index])
155179
updated_content = self.insert_link(content, lic, links, index)
156-
with filepath.open('w') as outfile:
180+
with filepath.open("w") as outfile:
157181
outfile.write(updated_content)
158-
direction = 'ltr'
182+
direction = "ltr"
159183
if rtl:
160-
direction = 'rtl'
161-
print('Added link to ' + direction + ' file: ' + filepath.name)
162-
#else:
184+
direction = "rtl"
185+
print("Added link to " + direction + " file: " + filepath.name)
186+
# else:
163187
# print('File already contains link: ' + filepath.name)
164-
188+
165189
def main(self):
166190
"""Get the command line arguments, find the files, and process them"""
167191
if self.get_args() and self.get_path():
168192
self.get_files()
169193
self.process_files()
170194

171-
if __name__ == '__main__':
195+
196+
if __name__ == "__main__":
172197
link_adder = AddCC4Links()
173198
link_adder.main()

0 commit comments

Comments
 (0)