1
+ import functools
1
2
import re
2
3
import os
3
4
13
14
pep_url = lambda num : 'dev/peps/pep-{}/' .format (num )
14
15
15
16
16
- def check_paths ():
17
- """ Checks to ensure our PEP_REPO_PATH is setup correctly """
18
- if not hasattr (settings , 'PEP_REPO_PATH' ):
19
- raise ImproperlyConfigured ("No PEP_REPO_PATH in settings" )
20
-
21
- if not os .path .exists (settings .PEP_REPO_PATH ):
22
- raise ImproperlyConfigured ("PEP_REPO_PATH in settings does not exist" )
17
+ def check_paths (func ):
18
+ """Ensure that our PEP_REPO_PATH is setup correctly."""
19
+ @functools .wraps (func )
20
+ def wrapped (* args , ** kwargs ):
21
+ if not hasattr (settings , 'PEP_REPO_PATH' ):
22
+ raise ImproperlyConfigured ('No PEP_REPO_PATH in settings' )
23
+ if not os .path .exists (settings .PEP_REPO_PATH ):
24
+ raise ImproperlyConfigured ('Path set as PEP_REPO_PATH does not exist' )
25
+ return func (* args , ** kwargs )
26
+ return wrapped
23
27
24
28
29
+ @check_paths
25
30
def convert_pep0 ():
26
31
"""
27
32
Take existing generated pep-0000.html and convert to something suitable
28
33
for a Python.org Page returns the core body HTML necessary only
29
34
"""
30
- check_paths ()
31
-
32
35
pep0_path = os .path .join (settings .PEP_REPO_PATH , 'pep-0000.html' )
33
36
pep0_content = open (pep0_path ).read ()
34
37
@@ -60,7 +63,7 @@ def convert_pep0():
60
63
if 'Version:' in t .text and 'N/A' in t .next_sibling .text :
61
64
t .parent .extract ()
62
65
63
- return '' .join ([header . prettify ( ), pep_content . prettify ( )])
66
+ return '' .join ([str ( header ), str ( pep_content )])
64
67
65
68
66
69
def get_pep0_page (commit = True ):
@@ -109,11 +112,11 @@ def fix_headers(soup, data):
109
112
return soup , data
110
113
111
114
115
+ @check_paths
112
116
def convert_pep_page (pep_number , content ):
113
117
"""
114
118
Handle different formats that pep2html.py outputs
115
119
"""
116
- check_paths ()
117
120
data = {
118
121
'title' : None ,
119
122
}
@@ -132,11 +135,11 @@ def convert_pep_page(pep_number, content):
132
135
133
136
header = soup .body .find ('div' , class_ = "header" )
134
137
header , data = fix_headers (header , data )
135
- data ['header' ] = header . prettify ( )
138
+ data ['header' ] = str ( header )
136
139
137
140
main_content = soup .body .find ('div' , class_ = "content" )
138
141
139
- data ['main_content' ] = main_content . prettify ( )
142
+ data ['main_content' ] = str ( main_content )
140
143
data ['content' ] = '' .join ([
141
144
data ['header' ],
142
145
data ['main_content' ]
@@ -155,7 +158,7 @@ def convert_pep_page(pep_number, content):
155
158
data ['title' ],
156
159
)
157
160
158
- data ['content' ] = soup . prettify ( )
161
+ data ['content' ] = str ( soup )
159
162
160
163
# Fix PEP links
161
164
pep_content = BeautifulSoup (data ['content' ])
@@ -172,7 +175,7 @@ def convert_pep_page(pep_number, content):
172
175
173
176
b .attrs ['href' ] = '/dev/peps/pep-{}/' .format (m .group (1 ))
174
177
175
- data ['content' ] = pep_content . prettify ( )
178
+ data ['content' ] = str ( pep_content )
176
179
177
180
source_link = "https://github.com/python/peps/blob/master/pep-{0}.txt" .format (pep_number )
178
181
data ['content' ] += """Source: <a href="{0}">{0}</a>""" .format (source_link )
@@ -187,6 +190,7 @@ def get_pep_page(pep_number, commit=True):
187
190
pep_path = os .path .join (settings .PEP_REPO_PATH , 'pep-{}.html' .format (pep_number ))
188
191
if not os .path .exists (pep_path ):
189
192
print ("PEP Path '{}' does not exist, skipping" .format (pep_path ))
193
+ return
190
194
191
195
pep_content = convert_pep_page (pep_number , open (pep_path ).read ())
192
196
@@ -208,6 +212,7 @@ def add_pep_image(pep_number, path):
208
212
image_path = os .path .join (settings .PEP_REPO_PATH , path )
209
213
if not os .path .exists (image_path ):
210
214
print ("Image Path '{}' does not exist, skipping" .format (image_path ))
215
+ return
211
216
212
217
try :
213
218
page = Page .objects .get (path = pep_url (pep_number ))
@@ -250,12 +255,13 @@ def add_pep_image(pep_number, path):
250
255
if img_tag ['src' ] == path :
251
256
img_tag ['src' ] = os .path .join (settings .MEDIA_URL , page .path , path )
252
257
253
- page .content .raw = soup . prettify ( )
258
+ page .content .raw = str ( soup )
254
259
page .save ()
255
260
256
261
return image
257
262
258
263
264
+ @check_paths
259
265
def get_peps_rss ():
260
266
rss_feed = os .path .join (settings .PEP_REPO_PATH , 'peps.rss' )
261
267
if not os .path .exists (rss_feed ):
0 commit comments