<s> from __future__ import absolute_import <EOL> """<STR_LIT>""" <EOL> import re <EOL> import warnings <EOL> from openpyxl . utils import absolute_coordinate <EOL> from openpyxl . compat import unicode <EOL> from openpyxl . utils . exceptions import NamedRangeException <EOL> from openpyxl . xml . functions import fromstring , safe_iterator <EOL> from openpyxl . xml . constants import SHEET_MAIN_NS <EOL> NAMED_RANGE_RE = re . compile ( """<STR_LIT>""" , re . VERBOSE ) <EOL> SPLIT_NAMED_RANGE_RE = re . compile ( r"<STR_LIT>" ) <EOL> EXTERNAL_RE = re . compile ( r"<STR_LIT>" ) <EOL> FORMULA_REGEX = re . compile ( r"<STR_LIT>" ) <EOL> DISCARDED_RANGES = re . compile ( "<STR_LIT>" ) <EOL> class NamedValue ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT:name>' , '<STR_LIT:value>' , '<STR_LIT>' ) <EOL> def __init__ ( self , name , value ) : <EOL> self . name = name <EOL> self . value = value <EOL> self . scope = None <EOL> @ property <EOL> def localSheetId ( self ) : <EOL> return self . scope <EOL> def __repr__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . __class__ . __name__ , self . value ) <EOL> def __iter__ ( self ) : <EOL> for attr in ( '<STR_LIT:name>' , '<STR_LIT>' ) : <EOL> value = getattr ( self , attr , None ) <EOL> if value is not None : <EOL> yield attr , value <EOL> NamedRangeContainingValue = NamedValue <EOL> class NamedRange ( NamedValue ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> str_format = unicode ( '<STR_LIT>' ) <EOL> repr_format = unicode ( '<STR_LIT>' ) <EOL> def __init__ ( self , name , destinations , scope = None ) : <EOL> self . name = name <EOL> self . destinations = destinations <EOL> self . scope = scope <EOL> @ property <EOL> def value ( self ) : <EOL> dest_cells = [ ] <EOL> for ws , xlrange in self . destinations : <EOL> dest_cells . append ( "<STR_LIT>" % ( ws . title . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) , <EOL> absolute_coordinate ( xlrange ) ) ) <EOL> return "<STR_LIT:U+002C>" . join ( dest_cells ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+002C>' . join ( [ self . str_format % ( sheet , name ) for sheet , name in self . destinations ] ) <EOL> def __repr__ ( self ) : <EOL> return self . repr_format % ( self . __class__ . __name__ , str ( self ) ) <EOL> def split_named_range ( range_string ) : <EOL> """<STR_LIT>""" <EOL> for range_string in SPLIT_NAMED_RANGE_RE . split ( range_string ) [ <NUM_LIT:1> : : <NUM_LIT:2> ] : <EOL> match = NAMED_RANGE_RE . match ( range_string ) <EOL> if match is None : <EOL> raise NamedRangeException ( '<STR_LIT>' % range_string ) <EOL> else : <EOL> match = match . groupdict ( ) <EOL> sheet_name = match [ '<STR_LIT>' ] or match [ '<STR_LIT>' ] <EOL> xlrange = match [ '<STR_LIT>' ] <EOL> sheet_name = sheet_name . replace ( "<STR_LIT>" , "<STR_LIT:'>" ) <EOL> yield sheet_name , xlrange <EOL> def refers_to_range ( range_string ) : <EOL> if FORMULA_REGEX . match ( range_string ) : <EOL> return <EOL> if range_string : <EOL> return NAMED_RANGE_RE . match ( range_string ) is not None <EOL> def external_range ( range_string ) : <EOL> m = EXTERNAL_RE . match ( range_string ) <EOL> if m is not None : <EOL> return m . group ( '<STR_LIT>' ) is not None <EOL> def read_named_ranges ( xml_source , workbook ) : <EOL> """<STR_LIT>""" <EOL> sheetnames = set ( sheet . title for sheet in workbook . worksheets ) <EOL> root = fromstring ( xml_source ) <EOL> for name_node in safe_iterator ( root , '<STR_LIT>' % SHEET_MAIN_NS ) : <EOL> range_name = name_node . get ( '<STR_LIT:name>' ) <EOL> if DISCARDED_RANGES . match ( range_name ) : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> continue <EOL> node_text = name_node . text <EOL> if external_range ( node_text ) : <EOL> named_range = NamedValue ( range_name , node_text ) <EOL> elif refers_to_range ( node_text ) : <EOL> destinations = split_named_range ( node_text ) <EOL> destinations = [ ( workbook [ sheet ] , cells ) for sheet , cells in destinations <EOL> if sheet in sheetnames ] <EOL> if not destinations : <EOL> continue <EOL> named_range = NamedRange ( range_name , destinations ) <EOL> else : <EOL> named_range = NamedValue ( range_name , node_text ) <EOL> named_range . scope = name_node . get ( "<STR_LIT>" ) <EOL> yield named_range </s>
