<s> from __future__ import absolute_import <EOL> import os <EOL> import termios <EOL> import tty <EOL> import fcntl <EOL> import struct <EOL> def size ( fd ) : <EOL> """<STR_LIT>""" <EOL> if not os . isatty ( fd . fileno ( ) ) : <EOL> return None <EOL> try : <EOL> dims = struct . unpack ( '<STR_LIT>' , fcntl . ioctl ( fd , termios . TIOCGWINSZ , '<STR_LIT>' ) ) <EOL> except : <EOL> try : <EOL> dims = ( os . environ [ '<STR_LIT>' ] , os . environ [ '<STR_LIT>' ] ) <EOL> except : <EOL> return None <EOL> return dims <EOL> class Terminal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fd , raw = True ) : <EOL> """<STR_LIT>""" <EOL> self . fd = fd <EOL> self . raw = raw <EOL> self . original_attributes = None <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . start ( ) <EOL> return self <EOL> def __exit__ ( self , * _ ) : <EOL> """<STR_LIT>""" <EOL> self . stop ( ) <EOL> def israw ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . raw <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . isatty ( self . fd . fileno ( ) ) and self . israw ( ) : <EOL> self . original_attributes = termios . tcgetattr ( self . fd ) <EOL> tty . setraw ( self . fd ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . original_attributes is not None : <EOL> termios . tcsetattr ( <EOL> self . fd , <EOL> termios . TCSADRAIN , <EOL> self . original_attributes , <EOL> ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> cls = type ( self ) . __name__ , <EOL> fd = self . fd , <EOL> raw = self . raw ) </s>
