1818 along with this program. If not, see http://www.gnu.org/licenses/.
1919'''
2020
21- import sys , json , os , sys , re , fnmatch
2221import argparse
22+ import fnmatch
23+ import json
24+ import os
25+ import sys
26+ import re
27+
2328import lxml .etree as e
2429import lxml .html as html
2530
@@ -94,6 +99,32 @@ def get_item_type(el):
9499 return ITEM_TYPE_ENUM
95100 return None # not recognized
96101
102+ class DDGDebug :
103+
104+ def __init__ (self , enabled = False , ident_match = None ):
105+ self .enabled = enabled
106+ self .ident_match = ident_match
107+ self .stat_line_nums = []
108+
109+ # track the statistics of number of lines used by the entries
110+ def submit_line_num (self , line_num ):
111+ while len (self .stat_line_nums ) <= line_num :
112+ self .stat_line_nums .append (0 )
113+ self .stat_line_nums [line_num ] += 1
114+
115+ def should_skip_ident (self , ident ):
116+ if self .ident_match is None :
117+ return False
118+
119+ if isinstance (ident , list ):
120+ for i in ident :
121+ if self .ident_match in i :
122+ return False
123+ else :
124+ if self .ident_match in ident :
125+ return False
126+ return True
127+
97128class Index2DuckDuckGoList (IndexTransform ):
98129
99130 def __init__ (self , ident_map ):
@@ -156,7 +187,7 @@ def get_version(decls):
156187 return None
157188 return rv
158189
159- def build_abstract (decls , desc , debug , debug_num_lines ):
190+ def build_abstract (decls , desc , debug = DDGDebug () ):
160191 line_limit = MAX_CODE_LINES
161192 num_lines = 0
162193
@@ -203,9 +234,9 @@ def build_abstract(decls, desc, debug, debug_num_lines):
203234 if limited :
204235 num_lines += 1
205236
206- debug_num_lines [ num_lines ] += 1
237+ debug . submit_line_num ( num_lines )
207238
208- if debug and num_lines >= 10 :
239+ if debug . enabled and num_lines >= 10 :
209240 print ("# error : large number of lines: " )
210241 print ("# BEGIN ======" )
211242 print (all_code + desc )
@@ -341,23 +372,23 @@ def output_redirects(out, redirects):
341372 out .write (line )
342373
343374def process_identifier (out , redirects , root , link , item_ident , item_type ,
344- debug = False , debug_ident = False , debug_num_lines = [] ):
375+ debug = DDGDebug () ):
345376 # get the name by extracting the unqualified identifier
346377 name = get_name (item_ident )
378+ debug_verbose = True if debug .enabled and debug .ident_match is not None else False
347379
348380 try :
349- debug_verbose = True if debug and debug_ident != None else False
350381 if item_type == ITEM_TYPE_CLASS :
351382 decls = get_declarations (root , name )
352383 desc = get_short_description (root , get_version (decls ), debug = debug_verbose )
353- abstract = build_abstract (decls , desc , debug , debug_num_lines )
384+ abstract = build_abstract (decls , desc , debug = debug )
354385
355386 elif item_type in [ ITEM_TYPE_FUNCTION ,
356387 ITEM_TYPE_CONSTRUCTOR ,
357388 ITEM_TYPE_DESTRUCTOR ]:
358389 decls = get_declarations (root , name )
359390 desc = get_short_description (root , get_version (decls ), debug = debug_verbose )
360- abstract = build_abstract (decls , desc , debug , debug_num_lines )
391+ abstract = build_abstract (decls , desc , debug = debug )
361392
362393 elif item_type in [ ITEM_TYPE_FUNCTION_INLINEMEM ,
363394 ITEM_TYPE_CONSTRUCTOR_INLINEMEM ,
@@ -405,7 +436,7 @@ def process_identifier(out, redirects, root, link, item_ident, item_type,
405436 build_redirects (redirects , item_ident , item_type )
406437
407438 except DdgException as err :
408- if debug :
439+ if debug . enabled :
409440 line = '# error (' + str (err ) + "): " + link + ": " + item_ident + "\n "
410441 out .write (line )
411442
@@ -426,11 +457,7 @@ def main():
426457 # prints everything to stdout. If the third argument is provided, the program
427458 # processes only the identifiers that match the provided string
428459
429- debug = args .debug
430- debug_ident = args .debug_ident
431-
432- # track the statistics of number of lines used by the entries
433- debug_num_lines = [0 for i in range (40 )]
460+ debug = DDGDebug (args .debug , args .debug_ident )
434461
435462 index_file = args .index
436463 output_file = args .output
@@ -466,7 +493,7 @@ def main():
466493
467494 redirects = []
468495
469- if debug :
496+ if debug . enabled :
470497 out = sys .stdout
471498 else :
472499 out = open (output_file , 'w' )
@@ -477,14 +504,8 @@ def main():
477504 link = page ['link' ]
478505 fn = page ['fn' ]
479506
480- if debug_ident :
481- ignore = True
482- for ident in idents :
483- if ident ['ident' ].find (debug_ident ) != - 1 :
484- ignore = False
485- break
486- if ignore :
487- continue
507+ if debug .should_skip_ident ([ i ['ident' ] for i in idents ]):
508+ continue
488509
489510 #print(str(i) + '/' + str(len(proc_ins)) + ': ' + link)
490511 #i+=1
@@ -497,14 +518,14 @@ def main():
497518 item_type = ident ['type' ]
498519
499520 process_identifier (out , redirects , root , link , item_ident , item_type ,
500- debug = debug , debug_ident = debug_ident , debug_num_lines = debug_num_lines )
521+ debug = debug )
501522
502523 output_redirects (out , redirects )
503524
504- if debug :
525+ if debug . enabled :
505526 print ('=============================' )
506527 print ('Numbers of lines used:' )
507- for i ,l in enumerate (debug_num_lines ):
528+ for i ,l in enumerate (debug . stat_line_nums ):
508529 print (str (i ) + ': ' + str (l ) + ' result(s)' )
509530
510531if __name__ == "__main__" :
0 commit comments