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
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,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