1414# You should have received a copy of the GNU Affero General Public License
1515# along with Booktype. If not, see <http://www.gnu.org/licenses/>.
1616
17- from django .core .management .base import BaseCommand , CommandError
18- from optparse import make_option
19- import os .path
17+ import os
2018import urllib2
21-
22- from booki .editor import models
2319from lxml import etree , html
2420
2521from django .test import Client
26-
22+ from django .core .management .base import BaseCommand , CommandError
23+ from booki .editor import models
2724
2825cacheLinks = {}
2926
27+
3028class HeadRequest (urllib2 .Request ):
3129 def get_method (self ):
3230 return "HEAD"
@@ -59,13 +57,14 @@ def checkLink(options, chapter, urlLink):
5957
6058 if not options ['no_cache' ]:
6159 cacheLinks [urlLink ] = returnCode
62-
60+
6361 print ' [%s]' % returnCode
6462 else :
6563 if options ['no_local' ]: return
6664
6765 c = Client ()
68- newUrl = os .path .normpath ('/%s/_v/%s/%s/%s' % (chapter .version .book .url_title , chapter .version .getVersion (), chapter .url_title , urlLink ))
66+ newUrl = os .path .normpath ('/%s/_v/%s/%s/%s' % (
67+ chapter .version .book .url_title , chapter .version .getVersion (), chapter .url_title , urlLink ))
6968
7069 print ' >> ' , newUrl ,
7170 response = c .get (newUrl )
@@ -74,60 +73,61 @@ def checkLink(options, chapter, urlLink):
7473
7574
7675class Command (BaseCommand ):
77- args = '<book name> [, <book name>, ...]'
76+ BOOK_NAMES = '<book name> [, <book name>, ...]'
7877 help = 'Check links in books.'
7978
80- option_list = BaseCommand . option_list + (
81- make_option ( '--no-remote' ,
82- action = 'store_true' ,
83- dest = 'no_remote ' ,
84- default = False ,
85- help = 'Do we check for remote links?' ) ,
86-
87- make_option ( '--no-local' ,
88- action = 'store_true' ,
89- dest = 'no_local ' ,
90- default = False ,
91- help = 'Do we check for local links?' ) ,
92-
93- make_option ( '--no-cache' ,
94- action = 'store_true' ,
95- dest = 'no_cache ' ,
96- default = False ,
97- help = 'Do not cache network links.' ) ,
98-
99- make_option ( '--ignore-url' ,
100- action = 'append' ,
101- dest = 'ignore_url ' ,
102- default = [] ,
103- help = 'What hosts to ignore, e.g. http://www.wikipedia.org/' ) ,
104-
105- )
106-
79+ def add_arguments ( self , parser ):
80+ parser . add_argument ( self . BOOK_NAMES , nargs = '+' , type = str )
81+
82+ parser . add_argument ( '--no-remote ' ,
83+ action = 'store_true' ,
84+ dest = 'no_remote' ,
85+ default = False ,
86+ help = 'Do we check for remote links?' )
87+
88+ parser . add_argument ( '--no-local ' ,
89+ action = 'store_true' ,
90+ dest = 'no_local' ,
91+ default = False ,
92+ help = 'Do we check for local links?' )
93+
94+ parser . add_argument ( '--no-cache ' ,
95+ action = 'store_true' ,
96+ dest = 'no_cache' ,
97+ default = False ,
98+ help = 'Do not cache network links.' )
99+
100+ parser . add_argument ( '--ignore-url ' ,
101+ action = 'append' ,
102+ dest = 'ignore_url' ,
103+ default = [],
104+ help = 'What hosts to ignore, e.g. http://www.wikipedia.org/' )
105+
107106 def handle (self , * args , ** options ):
108107 global cacheLinks
109108
109+ book_names = options .get (self .BOOK_NAMES , [])
110+
110111 # filter only books we want
111- if len ( args ) > 0 :
112- booksList = models .Book .objects .filter (url_title__in = args ).order_by ('url_title' )
112+ if book_names :
113+ booksList = models .Book .objects .filter (url_title__in = book_names ).order_by ('url_title' )
113114 else :
114115 booksList = models .Book .objects .all ().order_by ('url_title' )
115116
116-
117117 for book in booksList :
118118 print '[%s]' % book .url_title
119119
120120 try :
121121 for chapter in models .Chapter .objects .filter (version__book = book ):
122122 print ' [%s]' % chapter .url_title ,
123-
123+
124124 try :
125125 tree = html .document_fromstring (chapter .content )
126126 print ''
127127 except :
128128 print ' [ERROR PARSING HTML]'
129129 continue
130-
130+
131131 for elem in tree .iter ():
132132 src = elem .get ('src' )
133133 if src :
0 commit comments