1212""" 
1313import  logging 
1414import  posixpath 
15+ import  typing  as  t 
1516from  hashlib  import  sha1  as  sha 
1617from  os  import  path 
1718
2829
2930logger  =  logging .getLogger (__name__ )
3031
31- DEFAULT_FORMATS  =  dict ( html = " svg"latex = " pdf"text = None ) 
32+ DEFAULT_FORMATS  =  { " html" :  " svg"" latex" :  " pdf"" text" :  None } 
3233
3334
3435def  merge_dict (dst , src ):
35-     for  ( k , v )  in  src .items ():
36+     for  k , v  in  src .items ():
3637        if  k  not  in dst :
3738            dst [k ] =  v 
3839    return  dst 
@@ -58,22 +59,22 @@ class AafigDirective(images.Image):
5859
5960    has_content  =  True 
6061    required_arguments  =  0 
61-     own_option_spec   =   dict ( 
62-         line_width = float ,
63-         background = str ,
64-         foreground = str ,
65-         fill = str ,
66-         aspect = nonnegative_int ,
67-         textual = flag ,
68-         proportional = flag ,
69-     ) 
62+     own_option_spec :  t . ClassVar   =  { 
63+         " line_width" :  float ,
64+         " background" :  str ,
65+         " foreground" :  str ,
66+         " fill" :  str ,
67+         " aspect" :  nonnegative_int ,
68+         " textual" :  flag ,
69+         " proportional" :  flag ,
70+     } 
7071    option_spec  =  images .Image .option_spec .copy ()
7172    option_spec .update (own_option_spec )
7273
7374    def  run (self ):
74-         aafig_options  =  dict () 
75-         own_options_keys  =  [self .own_option_spec .keys ()]  +  [ "scale" ]
76-         for  ( k , v )  in  self .options .items ():
75+         aafig_options  =  {} 
76+         own_options_keys  =  [self .own_option_spec .keys (),  "scale" ]
77+         for  k , v  in  self .options .items ():
7778            if  k  in  own_options_keys :
7879                # convert flags to booleans 
7980                if  v  is  None :
@@ -88,7 +89,7 @@ def run(self):
8889        if  isinstance (image_node , nodes .system_message ):
8990            return  [image_node ]
9091        text  =  "\n " .join (self .content )
91-         image_node .aafig  =  dict ( options = aafig_options , text = text ) 
92+         image_node .aafig  =  { " options" :  aafig_options , " text" :  text } 
9293        return  [image_node ]
9394
9495
@@ -138,13 +139,18 @@ def render_aafig_images(app, doctree):
138139                img ["height" ] =  height 
139140
140141
142+ class  AafigureNotInstalled (AafigError ):
143+     def  __init__ (self , * args : object , ** kwargs : object ) ->  None :
144+         return  super ().__init__ ("aafigure module not installed" , * args , ** kwargs )
145+ 
146+ 
141147def  render_aafigure (app , text , options ):
142148    """ 
143149    Render an ASCII art figure into the requested format output file. 
144150    """ 
145151
146152    if  aafigure  is  None :
147-         raise  AafigError ( "aafigure module not installed" )
153+         raise  AafigureNotInstalled ( )
148154
149155    fname  =  get_basename (text , options )
150156    fname  =  "{}.{}" .format (get_basename (text , options ), options ["format" ])
@@ -173,10 +179,10 @@ def render_aafigure(app, text, options):
173179                f  =  None 
174180                try :
175181                    try :
176-                         f   =   open (metadata_fname )
177-                         extra  =  f .read ()
178-                     except  Exception :
179-                         raise  AafigError ()
182+                         with   open (metadata_fname )  as   f : 
183+                              extra  =  f .read ()
184+                     except  Exception   as   e :
185+                         raise  AafigError ()  from   e 
180186                finally :
181187                    if  f  is  not None :
182188                        f .close ()
@@ -190,14 +196,13 @@ def render_aafigure(app, text, options):
190196        (visitor , output ) =  aafigure .render (text , outfn , options )
191197        output .close ()
192198    except  aafigure .UnsupportedFormatError  as  e :
193-         raise  AafigError (str (e ))
199+         raise  AafigError (str (e ))  from   e 
194200
195201    extra  =  None 
196202    if  options ["format" ].lower () ==  "svg" :
197203        extra  =  visitor .get_size_attrs ()
198-         f  =  open (metadata_fname , "w" )
199-         f .write (extra )
200-         f .close ()
204+         with  open (metadata_fname , "w" ) as  f :
205+             f .write (extra )
201206
202207    return  relfn , outfn , id , extra 
203208
@@ -206,7 +211,7 @@ def setup(app):
206211    app .add_directive ("aafig" , AafigDirective )
207212    app .connect ("doctree-read" , render_aafig_images )
208213    app .add_config_value ("aafig_format" , DEFAULT_FORMATS , "html" )
209-     app .add_config_value ("aafig_default_options" , dict () , "html" )
214+     app .add_config_value ("aafig_default_options" , {} , "html" )
210215
211216
212217# vim: set expandtab shiftwidth=4 softtabstop=4 : 
0 commit comments