<s> import _base <EOL> class HeadingMatcher ( _base . HeadingMatcher ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , useScopeAttr = True , useHeadersAttr = True ) : <EOL> self . useScopeAttr = useScopeAttr <EOL> self . useHeadersAttr = useHeadersAttr <EOL> def matchAll ( self , table ) : <EOL> rv = { } <EOL> self . table = table <EOL> if self . useScopeAttr : <EOL> scope_map = self . getScopeMap ( ) <EOL> for slot in table : <EOL> for cell in slot : <EOL> if cell in rv : <EOL> continue <EOL> if self . useHeadersAttr : <EOL> rv [ cell ] = self . headersAttrHeaders ( cell ) <EOL> if rv [ cell ] is not None : <EOL> continue <EOL> if self . useScopeAttr : <EOL> rv [ cell ] = self . scopeAttrHeaders ( cell , scope_map ) <EOL> if rv [ cell ] is not None : <EOL> continue <EOL> rv [ cell ] = self . implicitHeaders ( cell ) <EOL> if cell not in rv : <EOL> rv [ cell ] = None <EOL> return rv <EOL> def implicitHeaders ( self , cell ) : <EOL> """<STR_LIT>""" <EOL> row_headers = [ ] <EOL> col_headers = [ ] <EOL> cells_examined = [ ] <EOL> def checkAxis ( axis , axis_headers , start_x , start_y ) : <EOL> last_cell = None <EOL> for current_cell in self . table . iterAxis ( ( start_x , start_y ) , <EOL> axis = axis , dir = - <NUM_LIT:1> ) : <EOL> if ( self . isHeading ( current_cell ) and <EOL> current_cell not in axis_headers and <EOL> ( not self . useScopeAttr or <EOL> not "<STR_LIT>" in current_cell . element . attrib ) ) : <EOL> axis_headers . append ( current_cell ) <EOL> if ( self . useHeadersAttr and <EOL> "<STR_LIT>" in current_cell . element . attrib ) : <EOL> axis_headers += self . headersAttrHeaders ( current_cell ) <EOL> break <EOL> else : <EOL> if last_cell in axis_headers : <EOL> break <EOL> last_cell = current_cell <EOL> for x_cell in range ( cell . anchor [ <NUM_LIT:0> ] , cell . anchor [ <NUM_LIT:0> ] + cell . colspan ) : <EOL> checkAxis ( "<STR_LIT>" , col_headers , x_cell , cell . anchor [ <NUM_LIT:1> ] - <NUM_LIT:1> ) <EOL> for y_cell in range ( cell . anchor [ <NUM_LIT:1> ] , cell . anchor [ <NUM_LIT:1> ] + cell . rowspan ) : <EOL> checkAxis ( "<STR_LIT>" , row_headers , cell . anchor [ <NUM_LIT:0> ] - <NUM_LIT:1> , y_cell ) <EOL> headers = row_headers [ : : - <NUM_LIT:1> ] + col_headers [ : : - <NUM_LIT:1> ] <EOL> return headers <EOL> def scopeAttrHeaders ( self , cell , scopeMap = None ) : <EOL> if scopeMap is None : <EOL> scopeMap = self . getScopeMap ( self . table ) <EOL> headers = [ ] <EOL> for header , cells in scopeMap . iteritems ( ) : <EOL> if cell in cells : <EOL> headers . append ( header ) <EOL> if not headers : <EOL> headers = None <EOL> return headers <EOL> def isHeading ( self , cell ) : <EOL> """<STR_LIT>""" <EOL> return ( cell . isHeading or "<STR_LIT>" in cell . element . attrib <EOL> or "<STR_LIT>" in cell . element . attrib ) <EOL> def getScopeMap ( self ) : <EOL> """<STR_LIT>""" <EOL> rv = { } <EOL> for heading_cell in self . table . headings : <EOL> heading_element = heading_cell . element <EOL> if not "<STR_LIT>" in heading_element . attrib : <EOL> continue <EOL> scope = heading_element . attrib [ "<STR_LIT>" ] <EOL> x , y = heading_cell . anchor <EOL> if scope == "<STR_LIT>" : <EOL> for s in range ( heading_cell . rowspan ) : <EOL> rv [ heading_cell ] = [ item for item in <EOL> self . table . iterAxis ( ( x + heading_cell . colspan , y + s ) , axis = "<STR_LIT>" ) ] <EOL> elif scope == "<STR_LIT>" : <EOL> for s in range ( heading_cell . colspan ) : <EOL> rv [ heading_cell ] = [ item for item in <EOL> self . table . iterAxis ( ( x + s , y + heading_cell . rowspan ) , axis = "<STR_LIT>" ) ] <EOL> elif scope == "<STR_LIT>" : <EOL> cells = [ ] <EOL> for rowgroup in self . table . rowgroups : <EOL> if y >= rowgroup . anchor [ <NUM_LIT:1> ] and y <= rowgroup . anchor [ <NUM_LIT:1> ] + rowgroup . span : <EOL> cells += [ item for item in rowgroup if item != heading_cell and <EOL> item . anchor [ <NUM_LIT:0> ] >= heading_cell . anchor [ <NUM_LIT:0> ] and <EOL> item . anchor [ <NUM_LIT:1> ] >= heading_cell . anchor [ <NUM_LIT:1> ] ] <EOL> rv [ heading_cell ] = cells <EOL> elif scope == "<STR_LIT>" : <EOL> cells = [ ] <EOL> for colgroup in self . table . colgroups : <EOL> if x >= colgroup . anchor [ <NUM_LIT:0> ] and x <= colgroup . anchor [ <NUM_LIT:0> ] + colgroup . span : <EOL> cells += [ item for item in colgroup if item != heading_cell and <EOL> item . anchor [ <NUM_LIT:0> ] >= heading_cell . anchor [ <NUM_LIT:0> ] and <EOL> item . anchor [ <NUM_LIT:1> ] >= heading_cell . anchor [ <NUM_LIT:1> ] ] <EOL> rv [ heading_cell ] = cells <EOL> return rv </s>
