1717import re , sys
1818from 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,23 @@ 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 f .match (self .exclude_pattern )
67+ ]
6268
6369 def process_files (self ):
6470 """Add links to all the license files"""
@@ -67,15 +73,18 @@ def process_files(self):
6773
6874 def file_license_and_language (self , filepath ):
6975 """Get the license number and language code from the file path"""
70- elements = filepath .stem .split ('_' )
76+ elements = filepath .stem .split ("_" )
7177 # Un-translated deeds don't have a language code, so set to English
7278 if len (elements ) != 3 :
73- elements += ['en' ]
79+ elements += ["en" ]
7480 return elements [0 ], elements [2 ]
7581
7682 def links_in_page (self , content ):
7783 """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+ )
7988
8089 def is_rtl (self , content ):
8190 """Determine whether the page is in a right-to-left script"""
@@ -98,22 +107,35 @@ def insert_link(self, content, lic, links, index):
98107 """Insert the link to the correct version of the license
99108 in the correct position in the list of links at the bottom of the
100109 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+ )
102117 if index == - 1 :
103118 target = '<a href="//creativecommons.org/publicdomain/zero/1.0/'
104- replace = link + ', ' + target
119+ replace = link + ", " + target
105120 else :
106121 lang = links [index ][1 ]
107- target = '>' + lang + ' </a>'
108- replace = target + ', ' + link
122+ target = ">" + lang + " </a>"
123+ replace = target + ", " + link
109124 return content .replace (target , replace , 1 )
110125
111126 def file_contains_link_already (self , links ):
112127 """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+ )
117139
118140 def process_file (self , filepath ):
119141 """Get the file's details and insert a link to the translated version
@@ -130,18 +152,19 @@ def process_file(self, filepath):
130152 print (index )
131153 print (links [index ])
132154 updated_content = self .insert_link (content , lic , links , index )
133- with filepath .open ('w' ) as outfile :
155+ with filepath .open ("w" ) as outfile :
134156 outfile .write (updated_content )
135- print (' Added link to file: ' + filepath .name )
157+ print (" Added link to file: " + filepath .name )
136158 else :
137- print (' File already contains link: ' + filepath .name )
159+ print (" File already contains link: " + filepath .name )
138160
139161 def main (self ):
140162 """Get the command line arguments, find the files, and process them"""
141163 if self .get_args () and self .get_path ():
142164 self .get_files ()
143165 self .process_files ()
144166
145- if __name__ == '__main__' :
167+
168+ if __name__ == "__main__" :
146169 link_adder = AddCC0Links ()
147170 link_adder .main ()
0 commit comments