<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from docx . opc . constants import RELATIONSHIP_TYPE as RT <EOL> from docx . opc . rel import Relationships <EOL> from docx . opc . constants import NAMESPACE as NS <EOL> from docx . opc . oxml import parse_xml <EOL> class BaseBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def element ( self ) : <EOL> """<STR_LIT>""" <EOL> return parse_xml ( self . xml ) <EOL> def with_indent ( self , indent ) : <EOL> """<STR_LIT>""" <EOL> self . _indent = indent <EOL> return self <EOL> class RelationshipsBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> partname_tmpls = { <EOL> RT . SLIDE_MASTER : '<STR_LIT>' , <EOL> RT . SLIDE : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self ) : <EOL> self . relationships = [ ] <EOL> self . next_rel_num = <NUM_LIT:1> <EOL> self . next_partnums = { } <EOL> def _next_partnum ( self , reltype ) : <EOL> if reltype not in self . next_partnums : <EOL> self . next_partnums [ reltype ] = <NUM_LIT:1> <EOL> partnum = self . next_partnums [ reltype ] <EOL> self . next_partnums [ reltype ] = partnum + <NUM_LIT:1> <EOL> return partnum <EOL> @ property <EOL> def next_rId ( self ) : <EOL> rId = '<STR_LIT>' % self . next_rel_num <EOL> self . next_rel_num += <NUM_LIT:1> <EOL> return rId <EOL> def _next_tuple_partname ( self , reltype ) : <EOL> partname_tmpl = self . partname_tmpls [ reltype ] <EOL> partnum = self . _next_partnum ( reltype ) <EOL> return partname_tmpl % partnum <EOL> def build ( self ) : <EOL> rels = Relationships ( ) <EOL> for rel in self . relationships : <EOL> rels . add_rel ( rel ) <EOL> return rels <EOL> class CT_DefaultBuilder ( BaseBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _content_type = '<STR_LIT>' <EOL> self . _extension = '<STR_LIT>' <EOL> self . _indent = <NUM_LIT:0> <EOL> self . _namespace = '<STR_LIT>' % NS . OPC_CONTENT_TYPES <EOL> def with_content_type ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> self . _content_type = content_type <EOL> return self <EOL> def with_extension ( self , extension ) : <EOL> """<STR_LIT>""" <EOL> self . _extension = extension <EOL> return self <EOL> def without_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _namespace = '<STR_LIT>' <EOL> return self <EOL> @ property <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> tmpl = '<STR_LIT>' <EOL> indent = '<STR_LIT:U+0020>' * self . _indent <EOL> return tmpl % ( indent , self . _namespace , self . _extension , <EOL> self . _content_type ) <EOL> class CT_OverrideBuilder ( BaseBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _content_type = '<STR_LIT>' <EOL> self . _indent = <NUM_LIT:0> <EOL> self . _namespace = '<STR_LIT>' % NS . OPC_CONTENT_TYPES <EOL> self . _partname = '<STR_LIT>' <EOL> def with_content_type ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> self . _content_type = content_type <EOL> return self <EOL> def with_partname ( self , partname ) : <EOL> """<STR_LIT>""" <EOL> self . _partname = partname <EOL> return self <EOL> def without_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _namespace = '<STR_LIT>' <EOL> return self <EOL> @ property <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> tmpl = '<STR_LIT>' <EOL> indent = '<STR_LIT:U+0020>' * self . _indent <EOL> return tmpl % ( indent , self . _namespace , self . _partname , <EOL> self . _content_type ) <EOL> class CT_RelationshipBuilder ( BaseBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _rId = '<STR_LIT>' <EOL> self . _reltype = '<STR_LIT>' <EOL> self . _target = '<STR_LIT>' <EOL> self . _target_mode = None <EOL> self . _indent = <NUM_LIT:0> <EOL> self . _namespace = '<STR_LIT>' % NS . OPC_RELATIONSHIPS <EOL> def with_rId ( self , rId ) : <EOL> """<STR_LIT>""" <EOL> self . _rId = rId <EOL> return self <EOL> def with_reltype ( self , reltype ) : <EOL> """<STR_LIT>""" <EOL> self . _reltype = reltype <EOL> return self <EOL> def with_target ( self , target ) : <EOL> """<STR_LIT>""" <EOL> self . _target = target <EOL> return self <EOL> def with_target_mode ( self , target_mode ) : <EOL> """<STR_LIT>""" <EOL> self . _target_mode = None if target_mode == '<STR_LIT>' else target_mode <EOL> return self <EOL> def without_namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _namespace = '<STR_LIT>' <EOL> return self <EOL> @ property <EOL> def target_mode ( self ) : <EOL> if self . _target_mode is None : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' % self . _target_mode <EOL> @ property <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> tmpl = '<STR_LIT>' <EOL> indent = '<STR_LIT:U+0020>' * self . _indent <EOL> return tmpl % ( indent , self . _namespace , self . _rId , self . _reltype , <EOL> self . _target , self . target_mode ) <EOL> class CT_RelationshipsBuilder ( BaseBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _rels = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> @ property <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> xml = '<STR_LIT>' % NS . OPC_RELATIONSHIPS <EOL> for rId , reltype , target , target_mode in self . _rels : <EOL> xml += ( a_Relationship ( ) . with_rId ( rId ) <EOL> . with_reltype ( reltype ) <EOL> . with_target ( target ) <EOL> . with_target_mode ( target_mode ) <EOL> . with_indent ( <NUM_LIT:2> ) <EOL> . without_namespace ( ) <EOL> . xml ) <EOL> xml += '<STR_LIT>' <EOL> return xml <EOL> class CT_TypesBuilder ( BaseBuilder ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _defaults = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> self . _empty = False <EOL> self . _overrides = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def empty ( self ) : <EOL> self . _empty = True <EOL> return self <EOL> @ property <EOL> def xml ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _empty : <EOL> return '<STR_LIT>' % NS . OPC_CONTENT_TYPES <EOL> xml = '<STR_LIT>' % NS . OPC_CONTENT_TYPES <EOL> for extension , content_type in self . _defaults : <EOL> xml += ( a_Default ( ) . with_extension ( extension ) <EOL> . with_content_type ( content_type ) <EOL> . with_indent ( <NUM_LIT:2> ) <EOL> . without_namespace ( ) <EOL> . xml ) <EOL> for partname , content_type in self . _overrides : <EOL> xml += ( an_Override ( ) . with_partname ( partname ) <EOL> . with_content_type ( content_type ) <EOL> . with_indent ( <NUM_LIT:2> ) <EOL> . without_namespace ( ) <EOL> . xml ) <EOL> xml += '<STR_LIT>' <EOL> return xml <EOL> def a_Default ( ) : <EOL> return CT_DefaultBuilder ( ) <EOL> def a_Relationship ( ) : <EOL> return CT_RelationshipBuilder ( ) <EOL> def a_Relationships ( ) : <EOL> return CT_RelationshipsBuilder ( ) <EOL> def a_Types ( ) : <EOL> return CT_TypesBuilder ( ) <EOL> def an_Override ( ) : <EOL> return CT_OverrideBuilder ( ) </s>
