<s> """<STR_LIT>""" <EOL> import re <EOL> from . import cell <EOL> from . cell import coordinate_from_string , column_index_from_string , get_column_letter <EOL> from . shared . exc import SheetTitleException , InsufficientCoordinatesException , CellCoordinatesException , NamedRangeException <EOL> from . shared . password_hasher import hash_password <EOL> from . style import Style , DEFAULTS as DEFAULTS_STYLE <EOL> from . drawing import Drawing <EOL> _DEFAULTS_STYLE_HASH = hash ( DEFAULTS_STYLE ) <EOL> def flatten ( results ) : <EOL> rows = [ ] <EOL> for row in results : <EOL> cells = [ ] <EOL> for cell in row : <EOL> cells . append ( cell . value ) <EOL> rows . append ( tuple ( cells ) ) <EOL> return tuple ( rows ) <EOL> class Relationship ( object ) : <EOL> """<STR_LIT>""" <EOL> TYPES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , rel_type ) : <EOL> if rel_type not in self . TYPES : <EOL> raise ValueError ( "<STR_LIT>" % rel_type ) <EOL> self . type = self . TYPES [ rel_type ] <EOL> self . target = "<STR_LIT>" <EOL> self . target_mode = "<STR_LIT>" <EOL> self . id = "<STR_LIT>" <EOL> class PageSetup ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class HeaderFooter ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class SheetView ( object ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class RowDimension ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> def __init__ ( self , index = <NUM_LIT:0> ) : <EOL> self . row_index = index <EOL> self . height = - <NUM_LIT:1> <EOL> self . visible = True <EOL> self . outline_level = <NUM_LIT:0> <EOL> self . collapsed = False <EOL> self . style_index = None <EOL> class ColumnDimension ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , <EOL> '<STR_LIT:width>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> def __init__ ( self , index = '<STR_LIT:A>' ) : <EOL> self . column_index = index <EOL> self . width = - <NUM_LIT:1> <EOL> self . auto_size = False <EOL> self . visible = True <EOL> self . outline_level = <NUM_LIT:0> <EOL> self . collapsed = False <EOL> self . style_index = <NUM_LIT:0> <EOL> class PageMargins ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . left = self . right = <NUM_LIT> <EOL> self . top = self . bottom = <NUM_LIT> <EOL> self . header = self . footer = <NUM_LIT> <EOL> class SheetProtection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . sheet = False <EOL> self . objects = False <EOL> self . scenarios = False <EOL> self . format_cells = False <EOL> self . format_columns = False <EOL> self . format_rows = False <EOL> self . insert_columns = False <EOL> self . insert_rows = False <EOL> self . insert_hyperlinks = False <EOL> self . delete_columns = False <EOL> self . delete_rows = False <EOL> self . select_locked_cells = False <EOL> self . sort = False <EOL> self . auto_filter = False <EOL> self . pivot_tables = False <EOL> self . select_unlocked_cells = False <EOL> self . _password = '<STR_LIT>' <EOL> def set_password ( self , value = '<STR_LIT>' , already_hashed = False ) : <EOL> """<STR_LIT>""" <EOL> if not already_hashed : <EOL> value = hash_password ( value ) <EOL> self . _password = value <EOL> def _set_raw_password ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . set_password ( value , already_hashed = False ) <EOL> def _get_raw_password ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _password <EOL> password = property ( _get_raw_password , _set_raw_password , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class Worksheet ( object ) : <EOL> """<STR_LIT>""" <EOL> BREAK_NONE = <NUM_LIT:0> <EOL> BREAK_ROW = <NUM_LIT:1> <EOL> BREAK_COLUMN = <NUM_LIT:2> <EOL> SHEETSTATE_VISIBLE = '<STR_LIT>' <EOL> SHEETSTATE_HIDDEN = '<STR_LIT>' <EOL> SHEETSTATE_VERYHIDDEN = '<STR_LIT>' <EOL> def __init__ ( self , parent_workbook , title = '<STR_LIT>' ) : <EOL> self . _parent = parent_workbook <EOL> self . _title = '<STR_LIT>' <EOL> if not title : <EOL> self . title = '<STR_LIT>' % ( <NUM_LIT:1> + len ( self . _parent . worksheets ) ) <EOL> else : <EOL> self . title = title <EOL> self . row_dimensions = { } <EOL> self . column_dimensions = { } <EOL> self . _cells = { } <EOL> self . _styles = { } <EOL> self . _charts = [ ] <EOL> self . relationships = [ ] <EOL> self . selected_cell = '<STR_LIT>' <EOL> self . active_cell = '<STR_LIT>' <EOL> self . sheet_state = self . SHEETSTATE_VISIBLE <EOL> self . page_setup = PageSetup ( ) <EOL> self . page_margins = PageMargins ( ) <EOL> self . header_footer = HeaderFooter ( ) <EOL> self . sheet_view = SheetView ( ) <EOL> self . protection = SheetProtection ( ) <EOL> self . show_gridlines = True <EOL> self . print_gridlines = False <EOL> self . show_summary_below = True <EOL> self . show_summary_right = True <EOL> self . default_row_dimension = RowDimension ( ) <EOL> self . default_column_dimension = ColumnDimension ( ) <EOL> self . _auto_filter = None <EOL> self . _freeze_panes = None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . title <EOL> def garbage_collect ( self ) : <EOL> """<STR_LIT>""" <EOL> delete_list = [ coordinate for coordinate , cell in self . _cells . items ( ) if ( cell . value in ( '<STR_LIT>' , None ) and hash ( cell . style ) == _DEFAULTS_STYLE_HASH ) ] <EOL> for coordinate in delete_list : <EOL> del self . _cells [ coordinate ] <EOL> def get_cell_collection ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _cells . values ( ) <EOL> def _set_title ( self , value ) : <EOL> """<STR_LIT>""" <EOL> bad_title_char_re = re . compile ( r'<STR_LIT>' ) <EOL> if bad_title_char_re . search ( value ) : <EOL> msg = '<STR_LIT>' <EOL> raise SheetTitleException ( msg ) <EOL> if self . _parent . get_sheet_by_name ( value ) : <EOL> i = <NUM_LIT:1> <EOL> while self . _parent . get_sheet_by_name ( '<STR_LIT>' % ( value , i ) ) : <EOL> i += <NUM_LIT:1> <EOL> value = '<STR_LIT>' % ( value , i ) <EOL> if len ( value ) > <NUM_LIT> : <EOL> msg = '<STR_LIT>' <EOL> raise SheetTitleException ( msg ) <EOL> self . _title = value <EOL> def _get_title ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _title <EOL> title = property ( _get_title , _set_title , doc = <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _set_auto_filter ( self , range ) : <EOL> if not range : <EOL> range = None <EOL> elif isinstance ( range , str ) : <EOL> range = range . upper ( ) <EOL> else : <EOL> range = range [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . address + '<STR_LIT::>' + range [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] . address <EOL> self . _auto_filter = range <EOL> def _get_auto_filter ( self ) : <EOL> return self . _auto_filter <EOL> auto_filter = property ( _get_auto_filter , _set_auto_filter , doc = <EOL> '<STR_LIT>' ) <EOL> def _set_freeze_panes ( self , topLeftCell ) : <EOL> if not topLeftCell : <EOL> topLeftCell = None <EOL> elif isinstance ( topLeftCell , str ) : <EOL> topLeftCell = topLeftCell . upper ( ) <EOL> else : <EOL> topLeftCell = topLeftCell . address <EOL> if topLeftCell == '<STR_LIT>' : <EOL> topLeftCell = None <EOL> self . _freeze_panes = topLeftCell <EOL> def _get_freeze_panes ( self ) : <EOL> return self . _freeze_panes <EOL> freeze_panes = property ( _get_freeze_panes , _set_freeze_panes , doc = <EOL> "<STR_LIT>" ) <EOL> def cell ( self , coordinate = None , row = None , column = None ) : <EOL> """<STR_LIT>""" <EOL> if not coordinate : <EOL> if ( row is None or column is None ) : <EOL> msg = "<STR_LIT>" "<STR_LIT>" <EOL> raise InsufficientCoordinatesException ( msg ) <EOL> else : <EOL> coordinate = '<STR_LIT>' % ( get_column_letter ( column + <NUM_LIT:1> ) , row + <NUM_LIT:1> ) <EOL> else : <EOL> coordinate = coordinate . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> return self . _get_cell ( coordinate ) <EOL> def _get_cell ( self , coordinate ) : <EOL> if not coordinate in self . _cells : <EOL> column , row = coordinate_from_string ( coordinate ) <EOL> new_cell = cell . Cell ( self , column , row ) <EOL> self . _cells [ coordinate ] = new_cell <EOL> if column not in self . column_dimensions : <EOL> self . column_dimensions [ column ] = ColumnDimension ( column ) <EOL> if row not in self . row_dimensions : <EOL> self . row_dimensions [ row ] = RowDimension ( row ) <EOL> return self . _cells [ coordinate ] <EOL> def get_highest_row ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . row_dimensions : <EOL> return max ( self . row_dimensions . keys ( ) ) <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def get_highest_column ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . column_dimensions : <EOL> return max ( [ column_index_from_string ( column_index ) <EOL> for column_index in self . column_dimensions ] ) <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def calculate_dimension ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( get_column_letter ( self . get_highest_column ( ) ) , <EOL> self . get_highest_row ( ) ) <EOL> def range ( self , range_string , row = <NUM_LIT:0> , column = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT::>' in range_string : <EOL> result = [ ] <EOL> min_range , max_range = range_string . split ( '<STR_LIT::>' ) <EOL> min_col , min_row = coordinate_from_string ( min_range ) <EOL> max_col , max_row = coordinate_from_string ( max_range ) <EOL> if column : <EOL> min_col = get_column_letter ( <EOL> column_index_from_string ( min_col ) + column ) <EOL> max_col = get_column_letter ( <EOL> column_index_from_string ( max_col ) + column ) <EOL> min_col = column_index_from_string ( min_col ) <EOL> max_col = column_index_from_string ( max_col ) <EOL> cache_cols = { } <EOL> for col in xrange ( min_col , max_col + <NUM_LIT:1> ) : <EOL> cache_cols [ col ] = get_column_letter ( col ) <EOL> rows = xrange ( min_row + row , max_row + row + <NUM_LIT:1> ) <EOL> cols = xrange ( min_col , max_col + <NUM_LIT:1> ) <EOL> for row in rows : <EOL> new_row = [ ] <EOL> for col in cols : <EOL> new_row . append ( self . cell ( '<STR_LIT>' % ( cache_cols [ col ] , row ) ) ) <EOL> result . append ( tuple ( new_row ) ) <EOL> return tuple ( result ) <EOL> else : <EOL> try : <EOL> return self . cell ( coordinate = range_string , row = row , <EOL> column = column ) <EOL> except CellCoordinatesException : <EOL> pass <EOL> named_range = self . _parent . get_named_range ( range_string ) <EOL> if named_range is None : <EOL> msg = '<STR_LIT>' % range_string <EOL> raise NamedRangeException ( msg ) <EOL> result = [ ] <EOL> for destination in named_range . destinations : <EOL> worksheet , cells_range = destination <EOL> if worksheet is not self : <EOL> msg = '<STR_LIT>' % ( cells_range , self . title ) <EOL> raise NamedRangeException ( msg ) <EOL> content = self . range ( cells_range ) <EOL> if isinstance ( content , tuple ) : <EOL> for cells in content : <EOL> result . extend ( cells ) <EOL> else : <EOL> result . append ( content ) <EOL> if len ( result ) == <NUM_LIT:1> : <EOL> return result [ <NUM_LIT:0> ] <EOL> else : <EOL> return tuple ( result ) <EOL> def get_style ( self , coordinate ) : <EOL> """<STR_LIT>""" <EOL> if not coordinate in self . _styles : <EOL> self . _styles [ coordinate ] = Style ( ) <EOL> return self . _styles [ coordinate ] <EOL> def create_relationship ( self , rel_type ) : <EOL> """<STR_LIT>""" <EOL> rel = Relationship ( rel_type ) <EOL> self . relationships . append ( rel ) <EOL> rel_id = self . relationships . index ( rel ) <EOL> rel . id = '<STR_LIT>' + str ( rel_id + <NUM_LIT:1> ) <EOL> return self . relationships [ rel_id ] <EOL> def add_chart ( self , chart ) : <EOL> """<STR_LIT>""" <EOL> chart . _sheet = self <EOL> self . _charts . append ( chart ) <EOL> def append ( self , list_or_dict ) : <EOL> """<STR_LIT>""" <EOL> row_idx = len ( self . row_dimensions ) <EOL> if isinstance ( list_or_dict , ( list , tuple ) ) : <EOL> for col_idx , content in enumerate ( list_or_dict ) : <EOL> self . cell ( row = row_idx , column = col_idx ) . value = content <EOL> elif isinstance ( list_or_dict , dict ) : <EOL> for col_idx , content in list_or_dict . items ( ) : <EOL> if isinstance ( col_idx , basestring ) : <EOL> col_idx = column_index_from_string ( col_idx ) - <NUM_LIT:1> <EOL> self . cell ( row = row_idx , column = col_idx ) . value = content <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> @ property <EOL> def rows ( self ) : <EOL> return self . range ( self . calculate_dimension ( ) ) <EOL> @ property <EOL> def columns ( self ) : <EOL> max_row = self . get_highest_row ( ) <EOL> cols = [ ] <EOL> for col_idx in range ( self . get_highest_column ( ) ) : <EOL> col = get_column_letter ( col_idx + <NUM_LIT:1> ) <EOL> res = self . range ( '<STR_LIT>' % ( col , col , max_row ) ) <EOL> cols . append ( tuple ( [ x [ <NUM_LIT:0> ] for x in res ] ) ) <EOL> return tuple ( cols ) </s>
