<s> """<STR_LIT>""" <EOL> import os <EOL> from pyexcel_io import get_data , save_data , RWManager <EOL> from pyexcel_io . utils import AVAILABLE_READERS , AVAILABLE_WRITERS <EOL> from . . constants import DEFAULT_SHEET_NAME <EOL> from . import params <EOL> from . base import FileSource , one_sheet_tuple <EOL> class IOSource ( FileSource ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def can_i_handle ( cls , action , file_type ) : <EOL> if action == params . READ_ACTION : <EOL> status = file_type in RWManager . reader_factories or file_type in AVAILABLE_READERS <EOL> elif action == params . WRITE_ACTION : <EOL> status = file_type in RWManager . writer_factories or file_type in AVAILABLE_WRITERS <EOL> else : <EOL> status = False <EOL> return status <EOL> class SheetSource ( IOSource ) : <EOL> """<STR_LIT>""" <EOL> fields = [ params . FILE_NAME ] <EOL> targets = ( params . SHEET , ) <EOL> actions = ( params . READ_ACTION , params . WRITE_ACTION ) <EOL> def __init__ ( self , file_name = None , ** keywords ) : <EOL> self . file_name = file_name <EOL> self . keywords = keywords <EOL> def get_data ( self ) : <EOL> """<STR_LIT>""" <EOL> sheets = get_data ( self . file_name , ** self . keywords ) <EOL> return one_sheet_tuple ( sheets . items ( ) ) <EOL> def write_data ( self , sheet ) : <EOL> sheet_name = DEFAULT_SHEET_NAME <EOL> if sheet . name : <EOL> sheet_name = sheet . name <EOL> data = { sheet_name : sheet . to_array ( ) } <EOL> if isinstance ( self . file_name , tuple ) : <EOL> save_data ( self . file_name [ <NUM_LIT:1> ] , <EOL> data , <EOL> file_type = self . file_name [ <NUM_LIT:0> ] , <EOL> ** self . keywords ) <EOL> else : <EOL> save_data ( self . file_name , <EOL> data , <EOL> ** self . keywords ) <EOL> class BookSource ( SheetSource ) : <EOL> """<STR_LIT>""" <EOL> targets = ( params . BOOK , ) <EOL> def get_data ( self ) : <EOL> sheets = get_data ( self . file_name , ** self . keywords ) <EOL> path , filename_alone = os . path . split ( self . file_name ) <EOL> return sheets , filename_alone , path <EOL> def write_data ( self , book ) : <EOL> book_dict = book . to_dict ( ) <EOL> if isinstance ( self . file_name , tuple ) : <EOL> save_data ( self . file_name [ <NUM_LIT:1> ] , <EOL> book_dict , <EOL> file_type = self . file_name [ <NUM_LIT:0> ] , <EOL> ** self . keywords ) <EOL> else : <EOL> save_data ( self . file_name , <EOL> book_dict , <EOL> ** self . keywords ) <EOL> sources = ( SheetSource , BookSource ) </s>
