<s> from __future__ import print_function , unicode_literals <EOL> DEBUG = <NUM_LIT:0> <EOL> import sys <EOL> import re <EOL> from . timemachine import * <EOL> from . book import Book , Name <EOL> from . biffh import error_text_from_code , XLRDError , XL_CELL_BLANK , XL_CELL_TEXT , XL_CELL_BOOLEAN , XL_CELL_ERROR <EOL> from . formatting import is_date_format_string , Format , XF <EOL> from . sheet import Sheet <EOL> DLF = sys . stdout <EOL> ET = None <EOL> ET_has_iterparse = False <EOL> Element_has_iter = False <EOL> def ensure_elementtree_imported ( verbosity , logfile ) : <EOL> global ET , ET_has_iterparse , Element_has_iter <EOL> if ET is not None : <EOL> return <EOL> if "<STR_LIT>" in sys . version : <EOL> import xml . etree . ElementTree as ET <EOL> else : <EOL> try : import xml . etree . cElementTree as ET <EOL> except ImportError : <EOL> try : import cElementTree as ET <EOL> except ImportError : <EOL> try : import lxml . etree as ET <EOL> except ImportError : <EOL> try : import xml . etree . ElementTree as ET <EOL> except ImportError : <EOL> try : import elementtree . ElementTree as ET <EOL> except ImportError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if hasattr ( ET , '<STR_LIT>' ) : <EOL> _dummy_stream = BYTES_IO ( b'<STR_LIT>' ) <EOL> try : <EOL> ET . iterparse ( _dummy_stream ) <EOL> ET_has_iterparse = True <EOL> except NotImplementedError : <EOL> pass <EOL> Element_has_iter = hasattr ( ET . ElementTree , '<STR_LIT>' ) <EOL> if verbosity : <EOL> etree_version = repr ( [ <EOL> ( item , getattr ( ET , item ) ) <EOL> for item in ET . __dict__ . keys ( ) <EOL> if item . lower ( ) . replace ( '<STR_LIT:_>' , '<STR_LIT>' ) == '<STR_LIT:version>' <EOL> ] ) <EOL> print ( ET . __file__ , ET . __name__ , etree_version , ET_has_iterparse , file = logfile ) <EOL> def split_tag ( tag ) : <EOL> pos = tag . rfind ( '<STR_LIT:}>' ) + <NUM_LIT:1> <EOL> if pos >= <NUM_LIT:2> : <EOL> return tag [ : pos ] , tag [ pos : ] <EOL> return '<STR_LIT>' , tag <EOL> def augment_keys ( adict , uri ) : <EOL> for x in list ( adict . keys ( ) ) : <EOL> adict [ uri + x ] = adict [ x ] <EOL> _UPPERCASE_1_REL_INDEX = { } <EOL> for _x in xrange ( <NUM_LIT> ) : <EOL> _UPPERCASE_1_REL_INDEX [ "<STR_LIT>" [ _x ] ] = _x + <NUM_LIT:1> <EOL> for _x in "<STR_LIT>" : <EOL> _UPPERCASE_1_REL_INDEX [ _x ] = <NUM_LIT:0> <EOL> del _x <EOL> def cell_name_to_rowx_colx ( cell_name , letter_value = _UPPERCASE_1_REL_INDEX ) : <EOL> colx = <NUM_LIT:0> <EOL> charx = - <NUM_LIT:1> <EOL> try : <EOL> for c in cell_name : <EOL> charx += <NUM_LIT:1> <EOL> lv = letter_value [ c ] <EOL> if lv : <EOL> colx = colx * <NUM_LIT> + lv <EOL> else : <EOL> colx = colx - <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= colx < X12_MAX_COLS <EOL> break <EOL> except KeyError : <EOL> raise Exception ( '<STR_LIT>' % ( c , cell_name ) ) <EOL> rowx = int ( cell_name [ charx : ] ) - <NUM_LIT:1> <EOL> return rowx , colx <EOL> error_code_from_text = { } <EOL> for _code , _text in error_text_from_code . items ( ) : <EOL> error_code_from_text [ _text ] = _code <EOL> U_SSML12 = "<STR_LIT>" <EOL> U_ODREL = "<STR_LIT>" <EOL> U_PKGREL = "<STR_LIT>" <EOL> U_CP = "<STR_LIT>" <EOL> U_DC = "<STR_LIT>" <EOL> U_DCTERMS = "<STR_LIT>" <EOL> XML_SPACE_ATTR = "<STR_LIT>" <EOL> XML_WHITESPACE = "<STR_LIT>" <EOL> X12_MAX_ROWS = <NUM_LIT:2> ** <NUM_LIT:20> <EOL> X12_MAX_COLS = <NUM_LIT:2> ** <NUM_LIT> <EOL> V_TAG = U_SSML12 + '<STR_LIT:v>' <EOL> F_TAG = U_SSML12 + '<STR_LIT:f>' <EOL> IS_TAG = U_SSML12 + '<STR_LIT>' <EOL> def unescape ( s , <EOL> subber = re . compile ( r'<STR_LIT>' , re . UNICODE ) . sub , <EOL> repl = lambda mobj : unichr ( int ( mobj . group ( <NUM_LIT:0> ) [ <NUM_LIT:2> : <NUM_LIT:6> ] , <NUM_LIT:16> ) ) , <EOL> ) : <EOL> if "<STR_LIT:_>" in s : <EOL> return subber ( repl , s ) <EOL> return s <EOL> def cooked_text ( self , elem ) : <EOL> t = elem . text <EOL> if t is None : <EOL> return '<STR_LIT>' <EOL> if elem . get ( XML_SPACE_ATTR ) != '<STR_LIT>' : <EOL> t = t . strip ( XML_WHITESPACE ) <EOL> return ensure_unicode ( unescape ( t ) ) <EOL> def get_text_from_si_or_is ( self , elem , r_tag = U_SSML12 + '<STR_LIT:r>' , t_tag = U_SSML12 + '<STR_LIT:t>' ) : <EOL> "<STR_LIT>" <EOL> accum = [ ] <EOL> for child in elem : <EOL> tag = child . tag <EOL> if tag == t_tag : <EOL> t = cooked_text ( self , child ) <EOL> if t : <EOL> accum . append ( t ) <EOL> elif tag == r_tag : <EOL> for tnode in child : <EOL> if tnode . tag == t_tag : <EOL> t = cooked_text ( self , tnode ) <EOL> if t : <EOL> accum . append ( t ) <EOL> return '<STR_LIT>' . join ( accum ) <EOL> def map_attributes ( amap , elem , obj ) : <EOL> for xml_attr , obj_attr , cnv_func_or_const in amap : <EOL> if not xml_attr : <EOL> setattr ( obj , obj_attr , cnv_func_or_const ) <EOL> continue <EOL> if not obj_attr : continue <EOL> raw_value = elem . get ( xml_attr ) <EOL> cooked_value = cnv_func_or_const ( raw_value ) <EOL> setattr ( obj , obj_attr , cooked_value ) <EOL> def cnv_ST_Xstring ( s ) : <EOL> if s is None : return "<STR_LIT>" <EOL> return ensure_unicode ( s ) <EOL> def cnv_xsd_unsignedInt ( s ) : <EOL> if not s : <EOL> return None <EOL> value = int ( s ) <EOL> assert value >= <NUM_LIT:0> <EOL> return value <EOL> def cnv_xsd_boolean ( s ) : <EOL> if not s : <EOL> return <NUM_LIT:0> <EOL> if s in ( "<STR_LIT:1>" , "<STR_LIT:true>" , "<STR_LIT>" ) : <EOL> return <NUM_LIT:1> <EOL> if s in ( "<STR_LIT:0>" , "<STR_LIT:false>" , "<STR_LIT>" ) : <EOL> return <NUM_LIT:0> <EOL> raise ValueError ( "<STR_LIT>" % s ) <EOL> _defined_name_attribute_map = ( <EOL> ( "<STR_LIT:name>" , "<STR_LIT:name>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT:description>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_unsignedInt , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_unsignedInt , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_ST_Xstring , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , cnv_xsd_boolean , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:0> , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:result>" , None , ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , None , ) , <EOL> ) <EOL> def make_name_access_maps ( bk ) : <EOL> name_and_scope_map = { } <EOL> name_map = { } <EOL> num_names = len ( bk . name_obj_list ) <EOL> for namex in xrange ( num_names ) : <EOL> nobj = bk . name_obj_list [ namex ] <EOL> name_lcase = nobj . name . lower ( ) <EOL> key = ( name_lcase , nobj . scope ) <EOL> if key in name_and_scope_map : <EOL> msg = '<STR_LIT>' % ( key , ) <EOL> if <NUM_LIT:0> : <EOL> raise XLRDError ( msg ) <EOL> else : <EOL> if bk . verbosity : <EOL> print ( msg , file = bk . logfile ) <EOL> name_and_scope_map [ key ] = nobj <EOL> sort_data = ( nobj . scope , namex , nobj ) <EOL> if name_lcase in name_map : <EOL> name_map [ name_lcase ] . append ( sort_data ) <EOL> else : <EOL> name_map [ name_lcase ] = [ sort_data ] <EOL> for key in name_map . keys ( ) : <EOL> alist = name_map [ key ] <EOL> alist . sort ( ) <EOL> name_map [ key ] = [ x [ <NUM_LIT:2> ] for x in alist ] <EOL> bk . name_and_scope_map = name_and_scope_map <EOL> bk . name_map = name_map <EOL> class X12General ( object ) : <EOL> def process_stream ( self , stream , heading = None ) : <EOL> if self . verbosity >= <NUM_LIT:2> and heading is not None : <EOL> fprintf ( self . logfile , "<STR_LIT>" , heading ) <EOL> self . tree = ET . parse ( stream ) <EOL> getmethod = self . tag2meth . get <EOL> for elem in self . tree . iter ( ) if Element_has_iter else self . tree . getiterator ( ) : <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> self . dump_elem ( elem ) <EOL> meth = getmethod ( elem . tag ) <EOL> if meth : <EOL> meth ( self , elem ) <EOL> self . finish_off ( ) <EOL> def finish_off ( self ) : <EOL> pass <EOL> def dump_elem ( self , elem ) : <EOL> fprintf ( self . logfile , <EOL> "<STR_LIT>" , <EOL> split_tag ( elem . tag ) [ <NUM_LIT:1> ] , len ( elem ) , elem . attrib , elem . text , elem . tail ) <EOL> def dumpout ( self , fmt , * vargs ) : <EOL> text = ( <NUM_LIT:12> * '<STR_LIT:U+0020>' + fmt + '<STR_LIT:\n>' ) % vargs <EOL> self . logfile . write ( text ) <EOL> class X12Book ( X12General ) : <EOL> def __init__ ( self , bk , logfile = DLF , verbosity = False ) : <EOL> self . bk = bk <EOL> self . logfile = logfile <EOL> self . verbosity = verbosity <EOL> self . bk . nsheets = <NUM_LIT:0> <EOL> self . bk . props = { } <EOL> self . relid2path = { } <EOL> self . relid2reltype = { } <EOL> self . sheet_targets = [ ] <EOL> self . sheetIds = [ ] <EOL> core_props_menu = { <EOL> U_CP + "<STR_LIT>" : ( "<STR_LIT>" , cnv_ST_Xstring ) , <EOL> U_DC + "<STR_LIT>" : ( "<STR_LIT>" , cnv_ST_Xstring ) , <EOL> U_DCTERMS + "<STR_LIT>" : ( "<STR_LIT>" , cnv_ST_Xstring ) , <EOL> U_DCTERMS + "<STR_LIT>" : ( "<STR_LIT>" , cnv_ST_Xstring ) , <EOL> } <EOL> def process_coreprops ( self , stream ) : <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> fprintf ( self . logfile , "<STR_LIT>" ) <EOL> self . tree = ET . parse ( stream ) <EOL> getmenu = self . core_props_menu . get <EOL> props = { } <EOL> for elem in self . tree . iter ( ) if Element_has_iter else self . tree . getiterator ( ) : <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> self . dump_elem ( elem ) <EOL> menu = getmenu ( elem . tag ) <EOL> if menu : <EOL> attr , func = menu <EOL> value = func ( elem . text ) <EOL> props [ attr ] = value <EOL> self . bk . user_name = props . get ( '<STR_LIT>' ) or props . get ( '<STR_LIT>' ) <EOL> self . bk . props = props <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> fprintf ( self . logfile , "<STR_LIT>" , props ) <EOL> self . finish_off ( ) <EOL> @ staticmethod <EOL> def convert_filename ( name ) : <EOL> return name . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) . lower ( ) <EOL> def process_rels ( self , stream ) : <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> fprintf ( self . logfile , "<STR_LIT>" ) <EOL> tree = ET . parse ( stream ) <EOL> r_tag = U_PKGREL + '<STR_LIT>' <EOL> for elem in tree . findall ( r_tag ) : <EOL> rid = elem . get ( '<STR_LIT>' ) <EOL> target = X12Book . convert_filename ( elem . get ( '<STR_LIT>' ) ) <EOL> reltype = elem . get ( '<STR_LIT>' ) . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( '<STR_LIT>' , rid , reltype , target ) <EOL> self . relid2reltype [ rid ] = reltype <EOL> if target . startswith ( '<STR_LIT:/>' ) : <EOL> self . relid2path [ rid ] = target [ <NUM_LIT:1> : ] <EOL> else : <EOL> self . relid2path [ rid ] = '<STR_LIT>' + target <EOL> def do_defined_name ( self , elem ) : <EOL> if <NUM_LIT:0> and self . verbosity >= <NUM_LIT:3> : <EOL> self . dump_elem ( elem ) <EOL> nobj = Name ( ) <EOL> bk = self . bk <EOL> nobj . bk = bk <EOL> nobj . name_index = len ( bk . name_obj_list ) <EOL> bk . name_obj_list . append ( nobj ) <EOL> nobj . name = elem . get ( '<STR_LIT:name>' ) <EOL> nobj . raw_formula = None <EOL> nobj . formula_text = cooked_text ( self , elem ) <EOL> map_attributes ( _defined_name_attribute_map , elem , nobj ) <EOL> if nobj . scope is None : <EOL> nobj . scope = - <NUM_LIT:1> <EOL> if nobj . name . startswith ( "<STR_LIT>" ) : <EOL> nobj . builtin = <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> nobj . dump ( header = '<STR_LIT>' ) <EOL> def do_defined_names ( self , elem ) : <EOL> for child in elem : <EOL> self . do_defined_name ( child ) <EOL> make_name_access_maps ( self . bk ) <EOL> def do_sheet ( self , elem ) : <EOL> bk = self . bk <EOL> sheetx = bk . nsheets <EOL> rid = elem . get ( U_ODREL + '<STR_LIT:id>' ) <EOL> sheetId = int ( elem . get ( '<STR_LIT>' ) ) <EOL> name = unescape ( ensure_unicode ( elem . get ( '<STR_LIT:name>' ) ) ) <EOL> reltype = self . relid2reltype [ rid ] <EOL> target = self . relid2path [ rid ] <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( <EOL> '<STR_LIT>' , <EOL> sheetx , sheetId , rid , reltype , name ) <EOL> if reltype != '<STR_LIT>' : <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( '<STR_LIT>' , reltype , name ) <EOL> return <EOL> state = elem . get ( '<STR_LIT:state>' ) <EOL> visibility_map = { <EOL> None : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } <EOL> bk . _sheet_visibility . append ( visibility_map [ state ] ) <EOL> sheet = Sheet ( bk , position = None , name = name , number = sheetx ) <EOL> sheet . utter_max_rows = X12_MAX_ROWS <EOL> sheet . utter_max_cols = X12_MAX_COLS <EOL> bk . _sheet_list . append ( sheet ) <EOL> bk . _sheet_names . append ( name ) <EOL> bk . nsheets += <NUM_LIT:1> <EOL> self . sheet_targets . append ( target ) <EOL> self . sheetIds . append ( sheetId ) <EOL> def do_workbookpr ( self , elem ) : <EOL> datemode = cnv_xsd_boolean ( elem . get ( '<STR_LIT>' ) ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( '<STR_LIT>' , datemode ) <EOL> self . bk . datemode = datemode <EOL> tag2meth = { <EOL> '<STR_LIT>' : do_defined_names , <EOL> '<STR_LIT>' : do_workbookpr , <EOL> '<STR_LIT>' : do_sheet , <EOL> } <EOL> augment_keys ( tag2meth , U_SSML12 ) <EOL> class X12SST ( X12General ) : <EOL> def __init__ ( self , bk , logfile = DLF , verbosity = <NUM_LIT:0> ) : <EOL> self . bk = bk <EOL> self . logfile = logfile <EOL> self . verbosity = verbosity <EOL> if ET_has_iterparse : <EOL> self . process_stream = self . process_stream_iterparse <EOL> else : <EOL> self . process_stream = self . process_stream_findall <EOL> def process_stream_iterparse ( self , stream , heading = None ) : <EOL> if self . verbosity >= <NUM_LIT:2> and heading is not None : <EOL> fprintf ( self . logfile , "<STR_LIT>" , heading ) <EOL> si_tag = U_SSML12 + '<STR_LIT>' <EOL> elemno = - <NUM_LIT:1> <EOL> sst = self . bk . _sharedstrings <EOL> for event , elem in ET . iterparse ( stream ) : <EOL> if elem . tag != si_tag : continue <EOL> elemno = elemno + <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> fprintf ( self . logfile , "<STR_LIT>" , elemno ) <EOL> self . dump_elem ( elem ) <EOL> result = get_text_from_si_or_is ( self , elem ) <EOL> sst . append ( result ) <EOL> elem . clear ( ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( '<STR_LIT>' , len ( sst ) ) <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> for x , s in enumerate ( sst ) : <EOL> fprintf ( self . logfile , "<STR_LIT>" , x , s ) <EOL> def process_stream_findall ( self , stream , heading = None ) : <EOL> if self . verbosity >= <NUM_LIT:2> and heading is not None : <EOL> fprintf ( self . logfile , "<STR_LIT>" , heading ) <EOL> self . tree = ET . parse ( stream ) <EOL> si_tag = U_SSML12 + '<STR_LIT>' <EOL> elemno = - <NUM_LIT:1> <EOL> sst = self . bk . _sharedstrings <EOL> for elem in self . tree . findall ( si_tag ) : <EOL> elemno = elemno + <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> fprintf ( self . logfile , "<STR_LIT>" , elemno ) <EOL> self . dump_elem ( elem ) <EOL> result = get_text_from_si_or_is ( self , elem ) <EOL> sst . append ( result ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . dumpout ( '<STR_LIT>' , len ( sst ) ) <EOL> class X12Styles ( X12General ) : <EOL> def __init__ ( self , bk , logfile = DLF , verbosity = <NUM_LIT:0> ) : <EOL> self . bk = bk <EOL> self . logfile = logfile <EOL> self . verbosity = verbosity <EOL> self . xf_counts = [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> self . xf_type = None <EOL> self . fmt_is_date = { } <EOL> for x in list ( range ( <NUM_LIT> , <NUM_LIT> ) ) + list ( range ( <NUM_LIT> , <NUM_LIT> ) ) : <EOL> self . fmt_is_date [ x ] = <NUM_LIT:1> <EOL> self . bk . _xf_index_to_xl_type_map [ <NUM_LIT:0> ] = <NUM_LIT:2> <EOL> def do_cellstylexfs ( self , elem ) : <EOL> self . xf_type = <NUM_LIT:0> <EOL> def do_cellxfs ( self , elem ) : <EOL> self . xf_type = <NUM_LIT:1> <EOL> def do_numfmt ( self , elem ) : <EOL> formatCode = ensure_unicode ( elem . get ( '<STR_LIT>' ) ) <EOL> numFmtId = int ( elem . get ( '<STR_LIT>' ) ) <EOL> is_date = is_date_format_string ( self . bk , formatCode ) <EOL> self . fmt_is_date [ numFmtId ] = is_date <EOL> fmt_obj = Format ( numFmtId , is_date + <NUM_LIT:2> , formatCode ) <EOL> self . bk . format_map [ numFmtId ] = fmt_obj <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> self . dumpout ( '<STR_LIT>' , numFmtId , formatCode , is_date ) <EOL> def do_xf ( self , elem ) : <EOL> if self . xf_type != <NUM_LIT:1> : <EOL> return <EOL> xfx = self . xf_counts [ self . xf_type ] <EOL> self . xf_counts [ self . xf_type ] = xfx + <NUM_LIT:1> <EOL> xf = XF ( ) <EOL> self . bk . xf_list . append ( xf ) <EOL> self . bk . xfcount += <NUM_LIT:1> <EOL> numFmtId = int ( elem . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> xf . format_key = numFmtId <EOL> is_date = self . fmt_is_date . get ( numFmtId , <NUM_LIT:0> ) <EOL> self . bk . _xf_index_to_xl_type_map [ xfx ] = is_date + <NUM_LIT:2> <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> self . dumpout ( <EOL> '<STR_LIT>' , <EOL> xfx , numFmtId , <EOL> ) <EOL> self . dumpout ( repr ( self . bk . _xf_index_to_xl_type_map ) ) <EOL> tag2meth = { <EOL> '<STR_LIT>' : do_cellstylexfs , <EOL> '<STR_LIT>' : do_cellxfs , <EOL> '<STR_LIT>' : do_numfmt , <EOL> '<STR_LIT>' : do_xf , <EOL> } <EOL> augment_keys ( tag2meth , U_SSML12 ) <EOL> class X12Sheet ( X12General ) : <EOL> def __init__ ( self , sheet , logfile = DLF , verbosity = <NUM_LIT:0> ) : <EOL> self . sheet = sheet <EOL> self . logfile = logfile <EOL> self . verbosity = verbosity <EOL> self . rowx = - <NUM_LIT:1> <EOL> self . bk = sheet . book <EOL> self . sst = self . bk . _sharedstrings <EOL> self . merged_cells = sheet . merged_cells <EOL> self . warned_no_cell_name = <NUM_LIT:0> <EOL> self . warned_no_row_num = <NUM_LIT:0> <EOL> if ET_has_iterparse : <EOL> self . process_stream = self . own_process_stream <EOL> def own_process_stream ( self , stream , heading = None ) : <EOL> if self . verbosity >= <NUM_LIT:2> and heading is not None : <EOL> fprintf ( self . logfile , "<STR_LIT>" , heading ) <EOL> getmethod = self . tag2meth . get <EOL> row_tag = U_SSML12 + "<STR_LIT>" <EOL> self_do_row = self . do_row <EOL> for event , elem in ET . iterparse ( stream ) : <EOL> if elem . tag == row_tag : <EOL> self_do_row ( elem ) <EOL> elem . clear ( ) <EOL> elif elem . tag == U_SSML12 + "<STR_LIT>" : <EOL> self . do_dimension ( elem ) <EOL> elif elem . tag == U_SSML12 + "<STR_LIT>" : <EOL> self . do_merge_cell ( elem ) <EOL> self . finish_off ( ) <EOL> def process_comments_stream ( self , stream ) : <EOL> root = ET . parse ( stream ) . getroot ( ) <EOL> author_list = root [ <NUM_LIT:0> ] <EOL> assert author_list . tag == U_SSML12 + '<STR_LIT>' <EOL> authors = [ elem . text for elem in author_list ] <EOL> comment_list = root [ <NUM_LIT:1> ] <EOL> assert comment_list . tag == U_SSML12 + '<STR_LIT>' <EOL> cell_note_map = self . sheet . cell_note_map <EOL> from . sheet import Note <EOL> text_tag = U_SSML12 + '<STR_LIT:text>' <EOL> r_tag = U_SSML12 + '<STR_LIT:r>' <EOL> t_tag = U_SSML12 + '<STR_LIT:t>' <EOL> for elem in comment_list . findall ( U_SSML12 + '<STR_LIT>' ) : <EOL> ts = elem . findall ( '<STR_LIT>' + text_tag + '<STR_LIT:/>' + t_tag ) <EOL> ts += elem . findall ( '<STR_LIT>' + text_tag + '<STR_LIT:/>' + r_tag + '<STR_LIT:/>' + t_tag ) <EOL> ref = elem . get ( '<STR_LIT>' ) <EOL> note = Note ( ) <EOL> note . author = authors [ int ( elem . get ( '<STR_LIT>' ) ) ] <EOL> note . rowx , note . colx = coords = cell_name_to_rowx_colx ( ref ) <EOL> note . text = '<STR_LIT>' <EOL> for t in ts : <EOL> note . text += cooked_text ( self , t ) <EOL> cell_note_map [ coords ] = note <EOL> def do_dimension ( self , elem ) : <EOL> ref = elem . get ( '<STR_LIT>' ) <EOL> if ref : <EOL> last_cell_ref = ref . split ( '<STR_LIT::>' ) [ - <NUM_LIT:1> ] <EOL> rowx , colx = cell_name_to_rowx_colx ( last_cell_ref ) <EOL> self . sheet . _dimnrows = rowx + <NUM_LIT:1> <EOL> self . sheet . _dimncols = colx + <NUM_LIT:1> <EOL> def do_merge_cell ( self , elem ) : <EOL> ref = elem . get ( '<STR_LIT>' ) <EOL> if ref : <EOL> first_cell_ref , last_cell_ref = ref . split ( '<STR_LIT::>' ) <EOL> first_rowx , first_colx = cell_name_to_rowx_colx ( first_cell_ref ) <EOL> last_rowx , last_colx = cell_name_to_rowx_colx ( last_cell_ref ) <EOL> self . merged_cells . append ( ( first_rowx , last_rowx + <NUM_LIT:1> , <EOL> first_colx , last_colx + <NUM_LIT:1> ) ) <EOL> def do_row ( self , row_elem ) : <EOL> def bad_child_tag ( child_tag ) : <EOL> raise Exception ( '<STR_LIT>' % ( cell_type , child_tag , rowx , colx ) ) <EOL> row_number = row_elem . get ( '<STR_LIT:r>' ) <EOL> if row_number is None : <EOL> self . rowx += <NUM_LIT:1> <EOL> explicit_row_number = <NUM_LIT:0> <EOL> if self . verbosity and not self . warned_no_row_num : <EOL> self . dumpout ( "<STR_LIT>" , self . rowx ) <EOL> self . warned_no_row_num = <NUM_LIT:1> <EOL> else : <EOL> self . rowx = int ( row_number ) - <NUM_LIT:1> <EOL> explicit_row_number = <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= self . rowx < X12_MAX_ROWS <EOL> rowx = self . rowx <EOL> colx = - <NUM_LIT:1> <EOL> if self . verbosity >= <NUM_LIT:3> : <EOL> self . dumpout ( "<STR_LIT>" , <EOL> row_number , self . rowx , explicit_row_number ) <EOL> letter_value = _UPPERCASE_1_REL_INDEX <EOL> for cell_elem in row_elem : <EOL> cell_name = cell_elem . get ( '<STR_LIT:r>' ) <EOL> if cell_name is None : <EOL> colx += <NUM_LIT:1> <EOL> if self . verbosity and not self . warned_no_cell_name : <EOL> self . dumpout ( "<STR_LIT>" , rowx , colx ) <EOL> self . warned_no_cell_name = <NUM_LIT:1> <EOL> else : <EOL> colx = <NUM_LIT:0> <EOL> charx = - <NUM_LIT:1> <EOL> try : <EOL> for c in cell_name : <EOL> charx += <NUM_LIT:1> <EOL> if c == '<STR_LIT:$>' : <EOL> continue <EOL> lv = letter_value [ c ] <EOL> if lv : <EOL> colx = colx * <NUM_LIT> + lv <EOL> else : <EOL> colx = colx - <NUM_LIT:1> <EOL> assert <NUM_LIT:0> <= colx < X12_MAX_COLS <EOL> break <EOL> except KeyError : <EOL> raise Exception ( '<STR_LIT>' % ( c , cell_name ) ) <EOL> if explicit_row_number and cell_name [ charx : ] != row_number : <EOL> raise Exception ( '<STR_LIT>' % ( cell_name , row_number ) ) <EOL> xf_index = int ( cell_elem . get ( '<STR_LIT:s>' , '<STR_LIT:0>' ) ) <EOL> cell_type = cell_elem . get ( '<STR_LIT:t>' , '<STR_LIT:n>' ) <EOL> tvalue = None <EOL> formula = None <EOL> if cell_type == '<STR_LIT:n>' : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == V_TAG : <EOL> tvalue = child . text <EOL> elif child_tag == F_TAG : <EOL> formula = cooked_text ( self , child ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % child_tag ) <EOL> if not tvalue : <EOL> if self . bk . formatting_info : <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_BLANK , '<STR_LIT>' , xf_index ) <EOL> else : <EOL> self . sheet . put_cell ( rowx , colx , None , float ( tvalue ) , xf_index ) <EOL> elif cell_type == "<STR_LIT:s>" : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == V_TAG : <EOL> tvalue = child . text <EOL> elif child_tag == F_TAG : <EOL> formula = child . text <EOL> else : <EOL> bad_child_tag ( child_tag ) <EOL> if not tvalue : <EOL> if self . bk . formatting_info : <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_BLANK , '<STR_LIT>' , xf_index ) <EOL> else : <EOL> value = self . sst [ int ( tvalue ) ] <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_TEXT , value , xf_index ) <EOL> elif cell_type == "<STR_LIT:str>" : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == V_TAG : <EOL> tvalue = cooked_text ( self , child ) <EOL> elif child_tag == F_TAG : <EOL> formula = cooked_text ( self , child ) <EOL> else : <EOL> bad_child_tag ( child_tag ) <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_TEXT , tvalue , xf_index ) <EOL> elif cell_type == "<STR_LIT:b>" : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == V_TAG : <EOL> tvalue = child . text <EOL> elif child_tag == F_TAG : <EOL> formula = cooked_text ( self , child ) <EOL> else : <EOL> bad_child_tag ( child_tag ) <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_BOOLEAN , int ( tvalue ) , xf_index ) <EOL> elif cell_type == "<STR_LIT:e>" : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == V_TAG : <EOL> tvalue = child . text <EOL> elif child_tag == F_TAG : <EOL> formula = cooked_text ( self , child ) <EOL> else : <EOL> bad_child_tag ( child_tag ) <EOL> value = error_code_from_text [ tvalue ] <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_ERROR , value , xf_index ) <EOL> elif cell_type == "<STR_LIT>" : <EOL> for child in cell_elem : <EOL> child_tag = child . tag <EOL> if child_tag == IS_TAG : <EOL> tvalue = get_text_from_si_or_is ( self , child ) <EOL> elif child_tag == V_TAG : <EOL> tvalue = child . text <EOL> elif child_tag == F_TAG : <EOL> formula = child . text <EOL> else : <EOL> bad_child_tag ( child_tag ) <EOL> if not tvalue : <EOL> if self . bk . formatting_info : <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_BLANK , '<STR_LIT>' , xf_index ) <EOL> else : <EOL> self . sheet . put_cell ( rowx , colx , XL_CELL_TEXT , tvalue , xf_index ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( cell_type , rowx , colx ) ) <EOL> tag2meth = { <EOL> '<STR_LIT>' : do_row , <EOL> } <EOL> augment_keys ( tag2meth , U_SSML12 ) <EOL> def open_workbook_2007_xml ( <EOL> zf , <EOL> component_names , <EOL> logfile = sys . stdout , <EOL> verbosity = <NUM_LIT:0> , <EOL> use_mmap = <NUM_LIT:0> , <EOL> formatting_info = <NUM_LIT:0> , <EOL> on_demand = <NUM_LIT:0> , <EOL> ragged_rows = <NUM_LIT:0> , <EOL> ) : <EOL> ensure_elementtree_imported ( verbosity , logfile ) <EOL> bk = Book ( ) <EOL> bk . logfile = logfile <EOL> bk . verbosity = verbosity <EOL> bk . formatting_info = formatting_info <EOL> if formatting_info : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> bk . use_mmap = False <EOL> bk . on_demand = on_demand <EOL> if on_demand : <EOL> if verbosity : <EOL> print ( "<STR_LIT>" , file = bk . logfile ) <EOL> bk . on_demand = False <EOL> bk . ragged_rows = ragged_rows <EOL> x12book = X12Book ( bk , logfile , verbosity ) <EOL> zflo = zf . open ( component_names [ '<STR_LIT>' ] ) <EOL> x12book . process_rels ( zflo ) <EOL> del zflo <EOL> zflo = zf . open ( component_names [ '<STR_LIT>' ] ) <EOL> x12book . process_stream ( zflo , '<STR_LIT>' ) <EOL> del zflo <EOL> props_name = '<STR_LIT>' <EOL> if props_name in component_names : <EOL> zflo = zf . open ( component_names [ props_name ] ) <EOL> x12book . process_coreprops ( zflo ) <EOL> x12sty = X12Styles ( bk , logfile , verbosity ) <EOL> if '<STR_LIT>' in component_names : <EOL> zflo = zf . open ( component_names [ '<STR_LIT>' ] ) <EOL> x12sty . process_stream ( zflo , '<STR_LIT>' ) <EOL> del zflo <EOL> else : <EOL> pass <EOL> sst_fname = '<STR_LIT>' <EOL> x12sst = X12SST ( bk , logfile , verbosity ) <EOL> if sst_fname in component_names : <EOL> zflo = zf . open ( component_names [ sst_fname ] ) <EOL> x12sst . process_stream ( zflo , '<STR_LIT>' ) <EOL> del zflo <EOL> for sheetx in range ( bk . nsheets ) : <EOL> fname = x12book . sheet_targets [ sheetx ] <EOL> zflo = zf . open ( component_names [ fname ] ) <EOL> sheet = bk . _sheet_list [ sheetx ] <EOL> x12sheet = X12Sheet ( sheet , logfile , verbosity ) <EOL> heading = "<STR_LIT>" % ( sheet . name , sheetx , fname ) <EOL> x12sheet . process_stream ( zflo , heading ) <EOL> del zflo <EOL> comments_fname = '<STR_LIT>' % ( sheetx + <NUM_LIT:1> ) <EOL> if comments_fname in component_names : <EOL> comments_stream = zf . open ( component_names [ comments_fname ] ) <EOL> x12sheet . process_comments_stream ( comments_stream ) <EOL> del comments_stream <EOL> sheet . tidy_dimensions ( ) <EOL> return bk </s>
