17
17
import re , sys
18
18
from pathlib import Path
19
19
20
- class AddCC0Links (object ):
21
20
21
+ class AddCC0Links (object ):
22
22
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." )
28
30
29
31
def get_args (self ):
30
32
# Make sure there are enough args
31
33
# Make sure arg 2 is a language code
32
34
# 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
+ )
35
38
if self .args_ok :
36
39
self .language_code = sys .argv [1 ]
37
40
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"
39
42
else :
40
43
self .usage ()
41
44
return self .args_ok
@@ -45,20 +48,23 @@ def get_path(self):
45
48
self .path = False
46
49
path = Path .cwd ()
47
50
pathdir = path .name
48
- if pathdir == ' legalcode' :
51
+ if pathdir == " legalcode" :
49
52
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"
54
57
if not self .path :
55
- print (' Please run from within the checked-out project.' )
58
+ print (" Please run from within the checked-out project." )
56
59
return self .path != False
57
60
58
61
def get_files (self ):
59
62
"""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 f .match (self .exclude_pattern )
67
+ ]
62
68
63
69
def process_files (self ):
64
70
"""Add links to all the license files"""
@@ -67,15 +73,18 @@ def process_files(self):
67
73
68
74
def file_license_and_language (self , filepath ):
69
75
"""Get the license number and language code from the file path"""
70
- elements = filepath .stem .split ('_' )
76
+ elements = filepath .stem .split ("_" )
71
77
# Un-translated deeds don't have a language code, so set to English
72
78
if len (elements ) != 3 :
73
- elements += ['en' ]
79
+ elements += ["en" ]
74
80
return elements [0 ], elements [2 ]
75
81
76
82
def links_in_page (self , content ):
77
83
"""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 )
84
+ return re .findall (
85
+ r'//creativecommons\.org/publicdomain/zero/1\.0/legalcode(\...)?">([^>]+)</a>' ,
86
+ content ,
87
+ )
79
88
80
89
def is_rtl (self , content ):
81
90
"""Determine whether the page is in a right-to-left script"""
@@ -98,22 +107,35 @@ def insert_link(self, content, lic, links, index):
98
107
"""Insert the link to the correct version of the license
99
108
in the correct position in the list of links at the bottom of the
100
109
page"""
101
- link = '<a href="//creativecommons.org/publicdomain/zero/1.0/legalcode.' + self .language_code + '">' + self .language_name + '</a>'
110
+ link = (
111
+ '<a href="//creativecommons.org/publicdomain/zero/1.0/legalcode.'
112
+ + self .language_code
113
+ + '">'
114
+ + self .language_name
115
+ + "</a>"
116
+ )
102
117
if index == - 1 :
103
118
target = '<a href="//creativecommons.org/publicdomain/zero/1.0/'
104
- replace = link + ', ' + target
119
+ replace = link + ", " + target
105
120
else :
106
121
lang = links [index ][1 ]
107
- target = '>' + lang + ' </a>'
108
- replace = target + ', ' + link
122
+ target = ">" + lang + " </a>"
123
+ replace = target + ", " + link
109
124
return content .replace (target , replace , 1 )
110
125
111
126
def file_contains_link_already (self , links ):
112
127
"""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
128
+ return (
129
+ next (
130
+ (
131
+ code
132
+ for code , name in links
133
+ if name == self .language_name or code == self .language_code
134
+ ),
135
+ False ,
136
+ )
137
+ != False
138
+ )
117
139
118
140
def process_file (self , filepath ):
119
141
"""Get the file's details and insert a link to the translated version
@@ -130,18 +152,19 @@ def process_file(self, filepath):
130
152
print (index )
131
153
print (links [index ])
132
154
updated_content = self .insert_link (content , lic , links , index )
133
- with filepath .open ('w' ) as outfile :
155
+ with filepath .open ("w" ) as outfile :
134
156
outfile .write (updated_content )
135
- print (' Added link to file: ' + filepath .name )
157
+ print (" Added link to file: " + filepath .name )
136
158
else :
137
- print (' File already contains link: ' + filepath .name )
159
+ print (" File already contains link: " + filepath .name )
138
160
139
161
def main (self ):
140
162
"""Get the command line arguments, find the files, and process them"""
141
163
if self .get_args () and self .get_path ():
142
164
self .get_files ()
143
165
self .process_files ()
144
166
145
- if __name__ == '__main__' :
167
+
168
+ if __name__ == "__main__" :
146
169
link_adder = AddCC0Links ()
147
170
link_adder .main ()
0 commit comments