<s> from . import Pipeline , Converter , Options , Accessor , accessors <EOL> from . . import xlplatform <EOL> from . . main import Range <EOL> import datetime <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> np = None <EOL> _date_handlers = { <EOL> datetime . datetime : datetime . datetime , <EOL> datetime . date : lambda year , month , day , ** kwargs : datetime . date ( year , month , day ) <EOL> } <EOL> _number_handlers = { <EOL> } <EOL> class ExpandRangeStage ( object ) : <EOL> def __init__ ( self , options ) : <EOL> self . expand = options . get ( '<STR_LIT>' , None ) <EOL> def __call__ ( self , c ) : <EOL> if c . range : <EOL> if self . expand : <EOL> c . range = getattr ( c . range , self . expand ) <EOL> class WriteValueToRangeStage ( object ) : <EOL> def __init__ ( self , raw = False ) : <EOL> self . raw = raw <EOL> def __call__ ( self , ctx ) : <EOL> if ctx . range : <EOL> if self . raw : <EOL> xlplatform . set_value ( ctx . range . xl_range , ctx . value ) <EOL> else : <EOL> if ctx . meta . get ( '<STR_LIT>' , False ) : <EOL> ctx . value = ctx . value [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> row2 = ctx . range . row2 <EOL> col2 = ctx . range . col2 <EOL> else : <EOL> row2 = ctx . range . row1 + len ( ctx . value ) - <NUM_LIT:1> <EOL> col2 = ctx . range . col1 + len ( ctx . value [ <NUM_LIT:0> ] ) - <NUM_LIT:1> <EOL> xlplatform . set_value ( xlplatform . get_range_from_indices ( <EOL> ctx . range . xl_sheet , <EOL> ctx . range . row1 , <EOL> ctx . range . col1 , <EOL> row2 , <EOL> col2 <EOL> ) , ctx . value ) <EOL> class ReadValueFromRangeStage ( object ) : <EOL> def __call__ ( self , c ) : <EOL> if c . range : <EOL> c . value = xlplatform . get_value_from_range ( c . range . xl_range ) <EOL> class CleanDataFromReadStage ( object ) : <EOL> def __init__ ( self , options ) : <EOL> dates_as = options . get ( '<STR_LIT>' , datetime . datetime ) <EOL> self . empty_as = options . get ( '<STR_LIT>' , None ) <EOL> self . dates_handler = _date_handlers . get ( dates_as , dates_as ) <EOL> numbers_as = options . get ( '<STR_LIT>' , None ) <EOL> self . numbers_handler = _number_handlers . get ( numbers_as , numbers_as ) <EOL> def __call__ ( self , c ) : <EOL> c . value = xlplatform . clean_value_data ( c . value , self . dates_handler , self . empty_as , self . numbers_handler ) <EOL> class CleanDataForWriteStage ( object ) : <EOL> def __call__ ( self , c ) : <EOL> c . value = [ <EOL> [ <EOL> None <EOL> if np and isinstance ( x , float ) and np . isnan ( x ) <EOL> else xlplatform . prepare_xl_data_element ( x ) <EOL> for x in y <EOL> ] <EOL> for y in c . value <EOL> ] <EOL> class AdjustDimensionsStage ( object ) : <EOL> def __init__ ( self , options ) : <EOL> self . ndim = options . get ( '<STR_LIT>' , None ) <EOL> def __call__ ( self , c ) : <EOL> if self . ndim is None : <EOL> if len ( c . value ) == <NUM_LIT:1> : <EOL> c . value = c . value [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] if len ( c . value [ <NUM_LIT:0> ] ) == <NUM_LIT:1> else c . value [ <NUM_LIT:0> ] <EOL> elif len ( c . value [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> c . value = [ x [ <NUM_LIT:0> ] for x in c . value ] <EOL> else : <EOL> c . value = c . value <EOL> elif self . ndim == <NUM_LIT:1> : <EOL> if len ( c . value ) == <NUM_LIT:1> : <EOL> c . value = c . value [ <NUM_LIT:0> ] <EOL> elif len ( c . value [ <NUM_LIT:0> ] ) == <NUM_LIT:1> : <EOL> c . value = [ x [ <NUM_LIT:0> ] for x in c . value ] <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> elif self . ndim != <NUM_LIT:2> : <EOL> raise ValueError ( '<STR_LIT>' % self . ndim ) <EOL> class Ensure2DStage ( object ) : <EOL> def __call__ ( self , c ) : <EOL> if isinstance ( c . value , ( list , tuple ) ) : <EOL> if len ( c . value ) > <NUM_LIT:0> : <EOL> if not isinstance ( c . value [ <NUM_LIT:0> ] , ( list , tuple ) ) : <EOL> c . value = [ c . value ] <EOL> else : <EOL> c . meta [ '<STR_LIT>' ] = True <EOL> c . value = [ [ c . value ] ] <EOL> class TransposeStage ( object ) : <EOL> def __call__ ( self , c ) : <EOL> c . value = [ [ e [ i ] for e in c . value ] for i in range ( len ( c . value [ <NUM_LIT:0> ] ) if c . value else <NUM_LIT:0> ) ] <EOL> class BaseAccessor ( Accessor ) : <EOL> @ classmethod <EOL> def reader ( cls , options ) : <EOL> return ( <EOL> Pipeline ( ) <EOL> . append_stage ( ExpandRangeStage ( options ) , only_if = options . get ( '<STR_LIT>' , None ) ) <EOL> ) <EOL> class RangeAccessor ( Accessor ) : <EOL> @ staticmethod <EOL> def copy_range_to_value ( c ) : <EOL> c . value = c . range <EOL> @ classmethod <EOL> def reader ( cls , options ) : <EOL> return ( <EOL> BaseAccessor . reader ( options ) <EOL> . append_stage ( RangeAccessor . copy_range_to_value ) <EOL> ) <EOL> RangeAccessor . register ( Range ) <EOL> class RawValueAccessor ( Accessor ) : <EOL> @ classmethod <EOL> def reader ( cls , options ) : <EOL> return ( <EOL> Accessor . reader ( options ) <EOL> . append_stage ( ReadValueFromRangeStage ( ) ) <EOL> ) <EOL> @ classmethod <EOL> def writer ( cls , options ) : <EOL> return ( <EOL> Accessor . writer ( options ) <EOL> . prepend_stage ( WriteValueToRangeStage ( raw = True ) ) <EOL> ) <EOL> RawValueAccessor . register ( '<STR_LIT>' ) <EOL> class ValueAccessor ( Accessor ) : <EOL> @ staticmethod <EOL> def reader ( options ) : <EOL> return ( <EOL> BaseAccessor . reader ( options ) <EOL> . append_stage ( ReadValueFromRangeStage ( ) ) <EOL> . append_stage ( Ensure2DStage ( ) ) <EOL> . append_stage ( CleanDataFromReadStage ( options ) ) <EOL> . append_stage ( TransposeStage ( ) , only_if = options . get ( '<STR_LIT>' , False ) ) <EOL> . append_stage ( AdjustDimensionsStage ( options ) ) <EOL> ) <EOL> @ staticmethod <EOL> def writer ( options ) : <EOL> return ( <EOL> Pipeline ( ) <EOL> . prepend_stage ( WriteValueToRangeStage ( ) ) <EOL> . prepend_stage ( CleanDataForWriteStage ( ) ) <EOL> . prepend_stage ( TransposeStage ( ) , only_if = options . get ( '<STR_LIT>' , False ) ) <EOL> . prepend_stage ( Ensure2DStage ( ) ) <EOL> ) <EOL> @ classmethod <EOL> def router ( cls , value , rng , options ) : <EOL> return accessors . get ( type ( value ) , cls ) <EOL> ValueAccessor . register ( None ) <EOL> class DictConverter ( Converter ) : <EOL> writes_types = dict <EOL> @ classmethod <EOL> def base_reader ( cls , options ) : <EOL> return ( <EOL> super ( DictConverter , cls ) . base_reader ( <EOL> Options ( options ) <EOL> . override ( ndim = <NUM_LIT:2> ) <EOL> ) <EOL> ) <EOL> @ classmethod <EOL> def read_value ( cls , value , options ) : <EOL> assert not value or len ( value [ <NUM_LIT:0> ] ) == <NUM_LIT:2> <EOL> return dict ( value ) <EOL> @ classmethod <EOL> def write_value ( cls , value , options ) : <EOL> return list ( value . items ( ) ) <EOL> DictConverter . register ( dict ) </s>
