<s> """<STR_LIT>""" <EOL> from . . shared . xmltools import fromstring , QName <EOL> from . . shared . ooxml import NAMESPACES <EOL> from . . workbook import DocumentProperties <EOL> from . . shared . date_time import W3CDTF_to_datetime <EOL> from . . namedrange import NamedRange , split_named_range <EOL> import datetime <EOL> BUGGY_NAMED_RANGES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> DISCARDED_RANGES = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_sheet_ids ( xml_source ) : <EOL> sheet_names = read_sheets_titles ( xml_source ) <EOL> return dict ( ( sheet , '<STR_LIT>' % ( i + <NUM_LIT:1> ) ) for i , sheet in enumerate ( sheet_names ) ) <EOL> def read_properties_core ( xml_source ) : <EOL> """<STR_LIT>""" <EOL> properties = DocumentProperties ( ) <EOL> root = fromstring ( xml_source ) <EOL> creator_node = root . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> if creator_node is not None : <EOL> properties . creator = creator_node . text <EOL> else : <EOL> properties . creator = '<STR_LIT>' <EOL> last_modified_by_node = root . find ( <EOL> QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> if last_modified_by_node is not None : <EOL> properties . last_modified_by = last_modified_by_node . text <EOL> else : <EOL> properties . last_modified_by = '<STR_LIT>' <EOL> created_node = root . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> if created_node is not None : <EOL> properties . created = W3CDTF_to_datetime ( created_node . text ) <EOL> else : <EOL> properties . created = datetime . datetime . now ( ) <EOL> modified_node = root . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> if modified_node is not None : <EOL> properties . modified = W3CDTF_to_datetime ( modified_node . text ) <EOL> else : <EOL> properties . modified = properties . created <EOL> return properties <EOL> def get_number_of_parts ( xml_source ) : <EOL> """<STR_LIT>""" <EOL> parts_size = { } <EOL> parts_names = [ ] <EOL> root = fromstring ( xml_source ) <EOL> heading_pairs = root . find ( QName ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) . text ) <EOL> vector = heading_pairs . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> children = vector . getchildren ( ) <EOL> for child_id in range ( <NUM_LIT:0> , len ( children ) , <NUM_LIT:2> ) : <EOL> part_name = children [ child_id ] . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) . text ) . text <EOL> if not part_name in parts_names : <EOL> parts_names . append ( part_name ) <EOL> part_size = int ( children [ child_id + <NUM_LIT:1> ] . find ( QName ( <EOL> NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) . text ) <EOL> parts_size [ part_name ] = part_size <EOL> return parts_size , parts_names <EOL> def read_sheets_titles ( xml_source ) : <EOL> """<STR_LIT>""" <EOL> root = fromstring ( xml_source ) <EOL> titles_root = root . find ( QName ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) . text ) <EOL> vector = titles_root . find ( QName ( NAMESPACES [ '<STR_LIT>' ] , '<STR_LIT>' ) . text ) <EOL> parts , names = get_number_of_parts ( xml_source ) <EOL> size = parts [ names [ <NUM_LIT:0> ] ] <EOL> children = [ c . text for c in vector . getchildren ( ) ] <EOL> return children [ : size ] <EOL> def read_named_ranges ( xml_source , workbook ) : <EOL> """<STR_LIT>""" <EOL> named_ranges = [ ] <EOL> root = fromstring ( xml_source ) <EOL> names_root = root . find ( QName ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) . text ) <EOL> if names_root is not None : <EOL> for name_node in names_root . getchildren ( ) : <EOL> range_name = name_node . get ( '<STR_LIT:name>' ) <EOL> if name_node . get ( "<STR_LIT>" , '<STR_LIT:0>' ) == '<STR_LIT:1>' : <EOL> continue <EOL> valid = True <EOL> for discarded_range in DISCARDED_RANGES : <EOL> if discarded_range in range_name : <EOL> valid = False <EOL> for bad_range in BUGGY_NAMED_RANGES : <EOL> if bad_range in name_node . text : <EOL> valid = False <EOL> if valid : <EOL> destinations = split_named_range ( name_node . text ) <EOL> new_destinations = [ ] <EOL> for worksheet , cells_range in destinations : <EOL> worksheet = workbook . get_sheet_by_name ( worksheet ) <EOL> if worksheet : <EOL> new_destinations . append ( ( worksheet , cells_range ) ) <EOL> named_range = NamedRange ( range_name , new_destinations ) <EOL> named_ranges . append ( named_range ) <EOL> return named_ranges </s>
