<s> """<STR_LIT>""" <EOL> __docformat__ = '<STR_LIT>' <EOL> import sys <EOL> from docutils import nodes , utils <EOL> from docutils . parsers . rst import Directive <EOL> from docutils . parsers . rst import directives , states <EOL> from docutils . nodes import fully_normalize_name , whitespace_normalize_name <EOL> from docutils . parsers . rst . roles import set_classes <EOL> try : <EOL> import Image as PIL <EOL> except ImportError : <EOL> PIL = None <EOL> class Image ( Directive ) : <EOL> align_h_values = ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT:right>' ) <EOL> align_v_values = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> align_values = align_v_values + align_h_values <EOL> def align ( argument ) : <EOL> return directives . choice ( argument , Image . align_values ) <EOL> required_arguments = <NUM_LIT:1> <EOL> optional_arguments = <NUM_LIT:0> <EOL> final_argument_whitespace = True <EOL> option_spec = { '<STR_LIT>' : directives . unchanged , <EOL> '<STR_LIT>' : directives . length_or_unitless , <EOL> '<STR_LIT:width>' : directives . length_or_percentage_or_unitless , <EOL> '<STR_LIT>' : directives . percentage , <EOL> '<STR_LIT>' : align , <EOL> '<STR_LIT:name>' : directives . unchanged , <EOL> '<STR_LIT:target>' : directives . unchanged_required , <EOL> '<STR_LIT:class>' : directives . class_option } <EOL> def run ( self ) : <EOL> if '<STR_LIT>' in self . options : <EOL> if isinstance ( self . state , states . SubstitutionDef ) : <EOL> if self . options [ '<STR_LIT>' ] not in self . align_v_values : <EOL> raise self . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( self . name , self . options [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' . join ( self . align_v_values ) ) ) <EOL> elif self . options [ '<STR_LIT>' ] not in self . align_h_values : <EOL> raise self . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( self . name , self . options [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' . join ( self . align_h_values ) ) ) <EOL> messages = [ ] <EOL> reference = directives . uri ( self . arguments [ <NUM_LIT:0> ] ) <EOL> self . options [ '<STR_LIT>' ] = reference <EOL> reference_node = None <EOL> if '<STR_LIT:target>' in self . options : <EOL> block = states . escape2null ( <EOL> self . options [ '<STR_LIT:target>' ] ) . splitlines ( ) <EOL> block = [ line for line in block ] <EOL> target_type , data = self . state . parse_target ( <EOL> block , self . block_text , self . lineno ) <EOL> if target_type == '<STR_LIT>' : <EOL> reference_node = nodes . reference ( refuri = data ) <EOL> elif target_type == '<STR_LIT>' : <EOL> reference_node = nodes . reference ( <EOL> refname = fully_normalize_name ( data ) , <EOL> name = whitespace_normalize_name ( data ) ) <EOL> reference_node . indirect_reference_name = data <EOL> self . state . document . note_refname ( reference_node ) <EOL> else : <EOL> messages . append ( data ) <EOL> del self . options [ '<STR_LIT:target>' ] <EOL> set_classes ( self . options ) <EOL> image_node = nodes . image ( self . block_text , ** self . options ) <EOL> self . add_name ( image_node ) <EOL> if reference_node : <EOL> reference_node += image_node <EOL> return messages + [ reference_node ] <EOL> else : <EOL> return messages + [ image_node ] <EOL> class Figure ( Image ) : <EOL> def align ( argument ) : <EOL> return directives . choice ( argument , Figure . align_h_values ) <EOL> def figwidth_value ( argument ) : <EOL> if argument . lower ( ) == '<STR_LIT:image>' : <EOL> return '<STR_LIT:image>' <EOL> else : <EOL> return directives . length_or_percentage_or_unitless ( argument , '<STR_LIT>' ) <EOL> option_spec = Image . option_spec . copy ( ) <EOL> option_spec [ '<STR_LIT>' ] = figwidth_value <EOL> option_spec [ '<STR_LIT>' ] = directives . class_option <EOL> option_spec [ '<STR_LIT>' ] = align <EOL> has_content = True <EOL> def run ( self ) : <EOL> figwidth = self . options . pop ( '<STR_LIT>' , None ) <EOL> figclasses = self . options . pop ( '<STR_LIT>' , None ) <EOL> align = self . options . pop ( '<STR_LIT>' , None ) <EOL> ( image_node , ) = Image . run ( self ) <EOL> if isinstance ( image_node , nodes . system_message ) : <EOL> return [ image_node ] <EOL> figure_node = nodes . figure ( '<STR_LIT>' , image_node ) <EOL> if figwidth == '<STR_LIT:image>' : <EOL> if PIL and self . state . document . settings . file_insertion_enabled : <EOL> try : <EOL> i = PIL . open ( str ( image_node [ '<STR_LIT>' ] ) ) <EOL> except ( IOError , UnicodeError ) : <EOL> pass <EOL> else : <EOL> self . state . document . settings . record_dependencies . add ( <EOL> image_node [ '<STR_LIT>' ] ) <EOL> figure_node [ '<STR_LIT:width>' ] = i . size [ <NUM_LIT:0> ] <EOL> elif figwidth is not None : <EOL> figure_node [ '<STR_LIT:width>' ] = figwidth <EOL> if figclasses : <EOL> figure_node [ '<STR_LIT>' ] += figclasses <EOL> if align : <EOL> figure_node [ '<STR_LIT>' ] = align <EOL> if self . content : <EOL> node = nodes . Element ( ) <EOL> self . state . nested_parse ( self . content , self . content_offset , node ) <EOL> first_node = node [ <NUM_LIT:0> ] <EOL> if isinstance ( first_node , nodes . paragraph ) : <EOL> caption = nodes . caption ( first_node . rawsource , '<STR_LIT>' , <EOL> * first_node . children ) <EOL> figure_node += caption <EOL> elif not ( isinstance ( first_node , nodes . comment ) <EOL> and len ( first_node ) == <NUM_LIT:0> ) : <EOL> error = self . state_machine . reporter . error ( <EOL> '<STR_LIT>' , <EOL> nodes . literal_block ( self . block_text , self . block_text ) , <EOL> line = self . lineno ) <EOL> return [ figure_node , error ] <EOL> if len ( node ) > <NUM_LIT:1> : <EOL> figure_node += nodes . legend ( '<STR_LIT>' , * node [ <NUM_LIT:1> : ] ) <EOL> return [ figure_node ] </s>
