<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import errno <EOL> from collections import namedtuple <EOL> VERBIAGE_NAMES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> Verbiage = namedtuple ( '<STR_LIT>' , VERBIAGE_NAMES ) <EOL> verbiageNames = Verbiage . _make ( VERBIAGE_NAMES ) <EOL> Consoles = { } <EOL> class Console ( object ) : <EOL> """<STR_LIT>""" <EOL> Wordage = Verbiage ( mute = <NUM_LIT:0> , terse = <NUM_LIT:1> , concise = <NUM_LIT:2> , verbose = <NUM_LIT:3> , profuse = <NUM_LIT:4> ) <EOL> _Name = '<STR_LIT>' <EOL> _Path = '<STR_LIT>' <EOL> _Verbosity = Wordage . concise <EOL> _Flushy = False <EOL> def __init__ ( self , name = "<STR_LIT>" , verbosity = None , flushy = None , path = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . _name = name or self . _Name <EOL> self . _verbosity = int ( verbosity ) if verbosity is not None else self . _Verbosity <EOL> self . _flushy = flushy if flushy is not None else self . _Flushy <EOL> self . _path = path or self . _Path <EOL> self . _file = None <EOL> self . reopen ( ) <EOL> def reinit ( self , verbosity = None , flushy = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> if verbosity is not None : <EOL> self . _verbosity = int ( verbosity ) <EOL> if flushy is not None : <EOL> self . _flushy = flushy <EOL> if path is not None : <EOL> self . _path = path <EOL> self . reopen ( ) <EOL> def reopen ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> if self . _path : <EOL> try : <EOL> self . _path = os . path . abspath ( os . path . expanduser ( self . _path ) ) <EOL> dirpath = os . path . dirname ( self . _path ) <EOL> if not os . path . exists ( dirpath ) : <EOL> try : <EOL> os . makedirs ( dirpath ) <EOL> except OSError as ex : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( dirpath , ex ) ) <EOL> sys . stderr . write ( msg ) <EOL> return False <EOL> self . _file = Console . ocfn ( self . _path , '<STR_LIT>' ) <EOL> self . _file . seek ( <NUM_LIT:0> , os . SEEK_END ) <EOL> except IOError as ex : <EOL> msg = "<STR_LIT>" . format ( self . _path , ex ) <EOL> sys . stderr . write ( msg ) <EOL> return False <EOL> else : <EOL> self . _file = sys . stdout <EOL> return True <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _file and not self . _file . closed : <EOL> if self . _file . name not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> try : <EOL> self . _file . close ( ) <EOL> except ( AttributeError ) : <EOL> pass <EOL> self . _file = None <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _file . flush ( ) <EOL> try : <EOL> os . fsync ( self . _file . fileno ( ) ) <EOL> except ( AttributeError , ValueError , OSError ) as ex : <EOL> pass <EOL> except ( AttributeError , ValueError ) as ex : <EOL> pass <EOL> def write ( self , msg , verbosity = None ) : <EOL> """<STR_LIT>""" <EOL> if verbosity is None or verbosity <= self . _verbosity : <EOL> self . _file . write ( msg ) <EOL> if self . _flushy : <EOL> self . flush ( ) <EOL> def terse ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . write ( msg , verbosity = self . Wordage . terse ) <EOL> def concise ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . write ( msg , verbosity = self . Wordage . concise ) <EOL> def verbose ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . write ( msg , verbosity = self . Wordage . verbose ) <EOL> def profuse ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . write ( msg , verbosity = self . Wordage . profuse ) <EOL> @ staticmethod <EOL> def ocfn ( filename , openMode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> newfd = os . open ( filename , os . O_EXCL | os . O_CREAT | os . O_RDWR , <NUM_LIT> ) <EOL> newfile = os . fdopen ( newfd , openMode ) <EOL> except OSError as ex : <EOL> if ex . errno == errno . EEXIST : <EOL> newfile = open ( filename , openMode ) <EOL> else : <EOL> raise <EOL> return newfile <EOL> def getConsole ( name = '<STR_LIT>' , ** kw ) : <EOL> """<STR_LIT>""" <EOL> console = Consoles . get ( name ) <EOL> if not console : <EOL> console = Console ( name = name , ** kw ) <EOL> Consoles [ console . _name ] = console <EOL> return console <EOL> console . reinit ( ** kw ) <EOL> return console <EOL> getConsole ( '<STR_LIT>' ) <EOL> def Test ( ) : <EOL> """<STR_LIT>""" <EOL> console = Console ( verbosity = Console . Wordage . concise ) <EOL> console . flush ( ) <EOL> console . close ( ) <EOL> console . reopen ( ) <EOL> console . write ( "<STR_LIT>" ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . concise ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . verbose ) <EOL> console . terse ( "<STR_LIT>" ) <EOL> console . concise ( '<STR_LIT>' ) <EOL> console . verbose ( '<STR_LIT>' ) <EOL> console . profuse ( '<STR_LIT>' ) <EOL> console . close ( ) <EOL> console = Console ( verbosity = Console . Wordage . verbose , path = "<STR_LIT>" ) <EOL> console . flush ( ) <EOL> console . close ( ) <EOL> console . reopen ( ) <EOL> console . write ( "<STR_LIT>" ) <EOL> console . close ( ) <EOL> console = getConsole ( verbosity = Console . Wordage . concise ) <EOL> console . flush ( ) <EOL> console . close ( ) <EOL> console . reopen ( ) <EOL> console . write ( "<STR_LIT>" ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . concise ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . verbose ) <EOL> console . terse ( "<STR_LIT>" ) <EOL> console . concise ( '<STR_LIT>' ) <EOL> console . verbose ( '<STR_LIT>' ) <EOL> console . profuse ( '<STR_LIT>' ) <EOL> console . terse ( "<STR_LIT>" . format ( Consoles ) ) <EOL> console . close ( ) <EOL> console = getConsole ( '<STR_LIT>' , verbosity = Console . Wordage . concise ) <EOL> console . flush ( ) <EOL> console . close ( ) <EOL> console . reopen ( ) <EOL> console . write ( "<STR_LIT>" ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . concise ) <EOL> console . write ( "<STR_LIT>" , verbosity = console . Wordage . verbose ) <EOL> console . terse ( "<STR_LIT>" ) <EOL> console . concise ( '<STR_LIT>' ) <EOL> console . verbose ( '<STR_LIT>' ) <EOL> console . profuse ( '<STR_LIT>' ) <EOL> console . terse ( "<STR_LIT>" . format ( Consoles ) ) <EOL> console . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> Test ( ) </s>
