<s> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> import importlib <EOL> from contextlib import contextmanager <EOL> from neupy . core . config import Configurable <EOL> from neupy . core . properties import BaseProperty <EOL> from neupy . helpers import progressbar <EOL> from . import terminal <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> def terminal_echo ( enabled , file_descriptor = sys . stdin ) : <EOL> """<STR_LIT>""" <EOL> is_windows = ( os . name == '<STR_LIT>' ) <EOL> if not is_windows : <EOL> termios = importlib . import_module ( '<STR_LIT>' ) <EOL> attributes = termios . tcgetattr ( file_descriptor ) <EOL> lflag = attributes [ <NUM_LIT:3> ] <EOL> if enabled : <EOL> lflag |= termios . ECHO <EOL> else : <EOL> lflag &= ~ termios . ECHO <EOL> attributes [ <NUM_LIT:3> ] = lflag <EOL> termios . tcsetattr ( file_descriptor , termios . TCSANOW , attributes ) <EOL> class TerminalLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> colors = { <EOL> '<STR_LIT>' : terminal . gray , <EOL> '<STR_LIT>' : terminal . green , <EOL> '<STR_LIT>' : terminal . red , <EOL> '<STR_LIT>' : terminal . white , <EOL> } <EOL> def __init__ ( self ) : <EOL> self . enable = True <EOL> self . template = "<STR_LIT>" <EOL> self . stdout = sys . stdout <EOL> def write ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if self . enable : <EOL> self . stdout . write ( str ( text ) + '<STR_LIT:\n>' ) <EOL> def newline ( self ) : <EOL> """<STR_LIT>""" <EOL> self . write ( '<STR_LIT:\r>' ) <EOL> def message ( self , tag , text , color = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if color not in self . colors : <EOL> available_colors = '<STR_LIT:U+002CU+0020>' . join ( self . colors . keys ( ) ) <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( color , available_colors ) ) <EOL> colorizer = self . colors [ color ] <EOL> formated_tag = colorizer ( tag . upper ( ) ) <EOL> message = self . template . format ( tag = formated_tag , text = text ) <EOL> self . write ( message ) <EOL> def title ( self , text ) : <EOL> """<STR_LIT>""" <EOL> bold_text = terminal . bold ( text ) <EOL> message = "<STR_LIT>" . format ( text = terminal . underline ( bold_text ) ) <EOL> self . write ( message ) <EOL> def error ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . message ( '<STR_LIT>' , text , color = '<STR_LIT>' ) <EOL> def warning ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . message ( '<STR_LIT>' , text , color = '<STR_LIT>' ) <EOL> def progressbar ( self , iterator , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . enable : <EOL> return progressbar ( iterator , * args , ** kwargs ) <EOL> return iterator <EOL> @ contextmanager <EOL> def disable_user_input ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . enable : <EOL> try : <EOL> terminal_echo ( enabled = False ) <EOL> yield <EOL> finally : <EOL> terminal_echo ( enabled = True ) <EOL> else : <EOL> yield <EOL> class VerboseProperty ( BaseProperty ) : <EOL> """<STR_LIT>""" <EOL> expected_type = bool <EOL> def __set__ ( self , instance , value ) : <EOL> instance . logs . enable = value <EOL> return super ( VerboseProperty , self ) . __set__ ( instance , value ) <EOL> class Verbose ( Configurable ) : <EOL> """<STR_LIT>""" <EOL> verbose = VerboseProperty ( default = False ) <EOL> def __init__ ( self , ** options ) : <EOL> self . logs = TerminalLogger ( ) <EOL> self . logs . enable = self . verbose <EOL> super ( Verbose , self ) . __init__ ( ** options ) </s>
