1717import os .path , re , sys
1818from pathlib import Path
1919
20+
2021class 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,48 +27,53 @@ 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 os .path .islink (f ) and
70- 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+ ]
7177 self .files .sort ()
7278
7379 def process_files (self ):
@@ -77,20 +83,24 @@ def process_files(self):
7783
7884 def file_license_and_language (self , filepath ):
7985 """Get the license number and language code from the file path"""
80- elements = filepath .stem .split ('_' )
86+ elements = filepath .stem .split ("_" )
8187 # Un-translated deeds don't have a language code, so set to English
8288 if len (elements ) != 3 :
83- elements += ['en' ]
89+ elements += ["en" ]
8490 return elements [0 ], elements [2 ]
8591
8692 def links_in_page (self , content ):
8793 """Find the translated license links at the bottom of the page"""
88- return re .findall (r'//creativecommons\.org/licenses/[^/]+/4\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>' , content )
94+ return re .findall (
95+ r'//creativecommons\.org/licenses/[^/]+/4\.0/legalcode(\.[^"]{2,})?">([^>]+)</a>' ,
96+ content ,
97+ )
8998
9099 def is_rtl (self , content ):
91100 """Determine whether the page is in a right-to-left script"""
92- return (re .search (r' dir="rtl"' , content ) != None ) or \
93- (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+ )
94104
95105 def insert_at_index_rtl (self , links ):
96106 index = - 1
@@ -109,38 +119,50 @@ def insert_at_index_ltr(self, links):
109119 index += 1
110120 return index
111121
112-
113122 def insert_at_index (self , links , rtl ):
114123 """Find the alphabetic position in the list of translated license links
115124 to insert the link at"""
116125 if rtl :
117126 return self .insert_at_index_rtl (links )
118127 else :
119128 return self .insert_at_index_ltr (links )
120-
129+
121130 def insert_link (self , content , lic , links , index ):
122131 """Insert the link to the correct version of the license
123132 in the correct position in the list of links at the bottom of the
124133 page"""
125- link = '<a href="//creativecommons.org/licenses/' + lic \
126- + '/4.0/legalcode.' + self .language_code \
127- + '">' + 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+ )
128143 if index == - 1 :
129144 target = '<a href="//creativecommons.org/licenses/' + lic
130- replace = link + ', ' + target
145+ replace = link + ", " + target
131146 else :
132147 lang = links [index ][1 ]
133- target = '>' + lang + ' </a>'
134- replace = target + ', ' + link
148+ target = ">" + lang + " </a>"
149+ replace = target + ", " + link
135150 return content .replace (target , replace , 1 )
136-
151+
137152 def file_contains_link_already (self , links ):
138153 """Did we already add a link to this page?"""
139- return next ((code for code , name in links
140- if name == self .language_name
141- or code == self .language_code ),
142- False ) != False
143-
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+
144166 def process_file (self , filepath ):
145167 """Get the file's details and insert a link to the translated version
146168 into it"""
@@ -151,25 +173,26 @@ def process_file(self, filepath):
151173 if not self .file_contains_link_already (links ):
152174 rtl = self .is_rtl (content )
153175 index = self .insert_at_index (links , rtl )
154- #print(links)
155- #print(index)
156- #print(links[index])
176+ # print(links)
177+ # print(index)
178+ # print(links[index])
157179 updated_content = self .insert_link (content , lic , links , index )
158- with filepath .open ('w' ) as outfile :
180+ with filepath .open ("w" ) as outfile :
159181 outfile .write (updated_content )
160- direction = ' ltr'
182+ direction = " ltr"
161183 if rtl :
162- direction = ' rtl'
163- print (' Added link to ' + direction + ' file: ' + filepath .name )
164- #else:
184+ direction = " rtl"
185+ print (" Added link to " + direction + " file: " + filepath .name )
186+ # else:
165187 # print('File already contains link: ' + filepath.name)
166-
188+
167189 def main (self ):
168190 """Get the command line arguments, find the files, and process them"""
169191 if self .get_args () and self .get_path ():
170192 self .get_files ()
171193 self .process_files ()
172194
173- if __name__ == '__main__' :
195+
196+ if __name__ == "__main__" :
174197 link_adder = AddCC4Links ()
175198 link_adder .main ()
0 commit comments