def parse_sphinx_searchindex(searchindex):
 def _select_block(str_in, start_tag, end_tag):
  'Select first block delimited by start_tag and end_tag'
  start_pos = str_in.find(start_tag)
  if (start_pos < 0):
   raise ValueError('start_tag not found')
  depth = 0
  for pos in range(start_pos, len(str_in)):
   if (str_in[pos] == start_tag):
    depth += 1
   elif (str_in[pos] == end_tag):
    depth -= 1
   if (depth == 0):
    break
  sel = str_in[(start_pos + 1):pos]
  return sel
 def _parse_dict_recursive(dict_str):
  'Parse a dictionary from the search index'
  dict_out = dict()
  pos_last = 0
  pos = dict_str.find(':')
  while (pos >= 0):
   key = dict_str[pos_last:pos]
   if (dict_str[(pos + 1)] == '['):
    pos_tmp = dict_str.find(']', (pos + 1))
    if (pos_tmp < 0):
     raise RuntimeError('error when parsing dict')
    value = dict_str[(pos + 2):pos_tmp].split(',')
    for i in range(len(value)):
     try:
      value[i] = int(value[i])
     except ValueError:
      pass
   elif (dict_str[(pos + 1)] == '{'):
    subdict_str = _select_block(dict_str[pos:], '{', '}')
    value = _parse_dict_recursive(subdict_str)
    pos_tmp = (pos + len(subdict_str))
   else:
    raise ValueError('error when parsing dict: unknown elem')
   key = key.strip('"')
   if (len(key) > 0):
    dict_out[key] = value
   pos_last = dict_str.find(',', pos_tmp)
   if (pos_last < 0):
    break
   pos_last += 1
   pos = dict_str.find(':', pos_last)
  return dict_out
 query = 'objects:'
 pos = searchindex.find(query)
 if (pos < 0):
  raise ValueError('"objects:" not found in search index')
 sel = _select_block(searchindex[pos:], '{', '}')
 objects = _parse_dict_recursive(sel)
 query = 'filenames:'
 pos = searchindex.find(query)
 if (pos < 0):
  raise ValueError('"filenames:" not found in search index')
 filenames = searchindex[((pos + len(query)) + 1):]
 filenames = filenames[:filenames.find(']')]
 filenames = [f.strip('"') for f in filenames.split(',')]
 return (filenames, objects)

def parse_sphinx_searchindex ( searchindex ) : def _select_block ( str_in , start_tag , end_tag ) : start_pos = str_in . find ( start_tag ) if ( start_pos < 0 ) : raise ValueError ( 'start_tag not found' ) depth = 0 for pos in range ( start_pos , len ( str_in ) ) : if ( str_in [ pos ] == start_tag ) : depth += 1 elif ( str_in [ pos ] == end_tag ) : depth -= 1 if ( depth == 0 ) : break sel = str_in [ ( start_pos + 1 ) : pos ] return sel def _parse_dict_recursive ( dict_str ) : dict_out = dict ( ) pos_last = 0 pos = dict_str . find ( ':' ) while ( pos >= 0 ) : key = dict_str [ pos_last : pos ] if ( dict_str [ ( pos + 1 ) ] == '[' ) : pos_tmp = dict_str . find ( ']' , ( pos + 1 ) ) if ( pos_tmp < 0 ) : raise RuntimeError ( 'error when parsing dict' ) value = dict_str [ ( pos + 2 ) : pos_tmp ] . split ( ',' ) for i in range ( len ( value ) ) : try : value [ i ] = int ( value [ i ] ) except ValueError : pass elif ( dict_str [ ( pos + 1 ) ] == '{' ) : subdict_str = _select_block ( dict_str [ pos : ] , '{' , '}' ) value = _parse_dict_recursive ( subdict_str ) pos_tmp = ( pos + len ( subdict_str ) ) else : raise ValueError ( 'error when parsing dict: unknown elem' ) key = key . strip ( '"' ) if ( len ( key ) > 0 ) : dict_out [ key ] = value pos_last = dict_str . find ( ',' , pos_tmp ) if ( pos_last < 0 ) : break pos_last += 1 pos = dict_str . find ( ':' , pos_last ) return dict_out query = 'objects:' pos = searchindex . find ( query ) if ( pos < 0 ) : raise ValueError ( '"objects:" not found in search index' ) sel = _select_block ( searchindex [ pos : ] , '{' , '}' ) objects = _parse_dict_recursive ( sel ) query = 'filenames:' pos = searchindex . find ( query ) if ( pos < 0 ) : raise ValueError ( '"filenames:" not found in search index' ) filenames = searchindex [ ( ( pos + len ( query ) ) + 1 ) : ] filenames = filenames [ : filenames . find ( ']' ) ] filenames = [ f . strip ( '"' ) for f in filenames . split ( ',' ) ] return ( filenames , objects )
parse a sphinx search index parameters searchindex : str the sphinx search index returns filenames : list of str the file names parsed from the search index .