<s> """<STR_LIT>""" <EOL> import os <EOL> import io <EOL> import codecs <EOL> from . _compat import text_type <EOL> from . import backend , tools <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Base ( object ) : <EOL> _format = '<STR_LIT>' <EOL> _engine = '<STR_LIT>' <EOL> _encoding = '<STR_LIT:utf-8>' <EOL> @ property <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _format <EOL> @ format . setter <EOL> def format ( self , format ) : <EOL> format = format . lower ( ) <EOL> if format not in backend . FORMATS : <EOL> raise ValueError ( '<STR_LIT>' % format ) <EOL> self . _format = format <EOL> @ property <EOL> def engine ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _engine <EOL> @ engine . setter <EOL> def engine ( self , engine ) : <EOL> engine = engine . lower ( ) <EOL> if engine not in backend . ENGINES : <EOL> raise ValueError ( '<STR_LIT>' % engine ) <EOL> self . _engine = engine <EOL> @ property <EOL> def encoding ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _encoding <EOL> @ encoding . setter <EOL> def encoding ( self , encoding ) : <EOL> if encoding is not None : <EOL> codecs . lookup ( encoding ) <EOL> self . _encoding = encoding <EOL> class File ( Base ) : <EOL> directory = '<STR_LIT>' <EOL> _default_extension = '<STR_LIT>' <EOL> def __init__ ( self , filename = None , directory = None , format = None , engine = None , encoding = None ) : <EOL> if filename is None : <EOL> name = getattr ( self , '<STR_LIT:name>' , None ) or self . __class__ . __name__ <EOL> filename = '<STR_LIT>' % ( name , self . _default_extension ) <EOL> self . filename = filename <EOL> if directory is not None : <EOL> self . directory = directory <EOL> if format is not None : <EOL> self . format = format <EOL> if engine is not None : <EOL> self . engine = engine <EOL> if encoding is not None : <EOL> self . encoding = encoding <EOL> def _repr_svg_ ( self ) : <EOL> return self . pipe ( format = '<STR_LIT>' ) . decode ( self . _encoding ) <EOL> def pipe ( self , format = None ) : <EOL> """<STR_LIT>""" <EOL> if format is None : <EOL> format = self . _format <EOL> data = text_type ( self . source ) . encode ( self . _encoding ) <EOL> outs = backend . pipe ( self . _engine , format , data ) <EOL> return outs <EOL> @ property <EOL> def filepath ( self ) : <EOL> return os . path . join ( self . directory , self . filename ) <EOL> def save ( self , filename = None , directory = None ) : <EOL> """<STR_LIT>""" <EOL> if filename is not None : <EOL> self . filename = filename <EOL> if directory is not None : <EOL> self . directory = directory <EOL> filepath = self . filepath <EOL> tools . mkdirs ( filepath ) <EOL> data = text_type ( self . source ) <EOL> with io . open ( filepath , '<STR_LIT:w>' , encoding = self . encoding ) as fd : <EOL> fd . write ( data ) <EOL> return filepath <EOL> def render ( self , filename = None , directory = None , view = False , cleanup = False ) : <EOL> """<STR_LIT>""" <EOL> filepath = self . save ( filename , directory ) <EOL> rendered = backend . render ( self . _engine , self . _format , filepath ) <EOL> if cleanup : <EOL> os . remove ( filepath ) <EOL> if view : <EOL> self . _view ( rendered , self . _format ) <EOL> return rendered <EOL> def view ( self ) : <EOL> """<STR_LIT>""" <EOL> rendered = self . render ( ) <EOL> self . _view ( rendered , self . _format ) <EOL> return rendered <EOL> def _view ( self , filepath , format ) : <EOL> """<STR_LIT>""" <EOL> methods = [ <EOL> '<STR_LIT>' % ( format , backend . PLATFORM ) , <EOL> '<STR_LIT>' % backend . PLATFORM , <EOL> ] <EOL> for name in methods : <EOL> method = getattr ( self , name , None ) <EOL> if method is not None : <EOL> method ( filepath ) <EOL> break <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . __class__ , format , backend . PLATFORM ) ) <EOL> _view_darwin = staticmethod ( backend . view_darwin ) <EOL> _view_linux = staticmethod ( backend . view_linux ) <EOL> _view_windows = staticmethod ( backend . view_windows ) <EOL> class Source ( File ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename = None , directory = None , format = None , engine = None , encoding = None ) : <EOL> super ( Source , self ) . __init__ ( filename , directory , format , engine , encoding ) <EOL> self . source = source </s>
