from pywb.utils.binsearch import iter_range
from pywb.utils.wbexception import NotFoundException
from pywb.warcserver.index.cdxobject import CDXObject
from pywb.utils.format import res_template
def modified_load_index(self, params):
filename = res_template(self.filename_template, params)
try:
fh = open(filename, 'rb')
except IOError:
raise NotFoundException(filename)
def do_load(fh):
with fh:
gen = iter_range(fh, params['key'], params['end_key'])
for line in gen:
yield CDXObject(line)
# (... some modification on this method)
return do_load(fh)
# Change the "load_index" method on the instance of FileIndexSource
my_file_index_source.load_index = modified_load_index
load_index
是FileIndexSource
类的方法。您可以在实例级别修改方法,而无需更改库的源代码。例如:因此,每次在
my_file_index_source
上调用方法load_index
,都将运行修改后的方法。你知道吗另一种选择是创建一个新类,该类从
FileIndexSource
继承并覆盖load_index
方法。你知道吗相关问题 更多 >
编程相关推荐