<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import time <EOL> import math <EOL> from . _compat import _default_text_stdout , range_type , PY2 , isatty , open_stream , strip_ansi , term_len , get_best_encoding , WIN , int_types <EOL> from . utils import echo <EOL> from . exceptions import ClickException <EOL> if os . name == '<STR_LIT>' : <EOL> BEFORE_BAR = '<STR_LIT:\r>' <EOL> AFTER_BAR = '<STR_LIT:\n>' <EOL> else : <EOL> BEFORE_BAR = '<STR_LIT>' <EOL> AFTER_BAR = '<STR_LIT>' <EOL> def _length_hint ( obj ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return len ( obj ) <EOL> except TypeError : <EOL> try : <EOL> get_hint = type ( obj ) . __length_hint__ <EOL> except AttributeError : <EOL> return None <EOL> try : <EOL> hint = get_hint ( obj ) <EOL> except TypeError : <EOL> return None <EOL> if hint is NotImplemented or not isinstance ( hint , int_types ) or hint < <NUM_LIT:0> : <EOL> return None <EOL> return hint <EOL> class ProgressBar ( object ) : <EOL> def __init__ ( self , iterable , length = None , fill_char = '<STR_LIT:#>' , empty_char = '<STR_LIT:U+0020>' , <EOL> bar_template = '<STR_LIT>' , info_sep = '<STR_LIT:U+0020>' , show_eta = True , <EOL> show_percent = None , show_pos = False , item_show_func = None , <EOL> label = None , file = None , color = None , width = <NUM_LIT:30> ) : <EOL> self . fill_char = fill_char <EOL> self . empty_char = empty_char <EOL> self . bar_template = bar_template <EOL> self . info_sep = info_sep <EOL> self . show_eta = show_eta <EOL> self . show_percent = show_percent <EOL> self . show_pos = show_pos <EOL> self . item_show_func = item_show_func <EOL> self . label = label or '<STR_LIT>' <EOL> if file is None : <EOL> file = _default_text_stdout ( ) <EOL> self . file = file <EOL> self . color = color <EOL> self . width = width <EOL> self . autowidth = width == <NUM_LIT:0> <EOL> if length is None : <EOL> length = _length_hint ( iterable ) <EOL> if iterable is None : <EOL> if length is None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> iterable = range_type ( length ) <EOL> self . iter = iter ( iterable ) <EOL> self . length = length <EOL> self . length_known = length is not None <EOL> self . pos = <NUM_LIT:0> <EOL> self . avg = [ ] <EOL> self . start = self . last_eta = time . time ( ) <EOL> self . eta_known = False <EOL> self . finished = False <EOL> self . max_width = None <EOL> self . entered = False <EOL> self . current_item = None <EOL> self . is_hidden = not isatty ( self . file ) <EOL> self . _last_line = None <EOL> def __enter__ ( self ) : <EOL> self . entered = True <EOL> self . render_progress ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , tb ) : <EOL> self . render_finish ( ) <EOL> def __iter__ ( self ) : <EOL> if not self . entered : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . render_progress ( ) <EOL> return self <EOL> def render_finish ( self ) : <EOL> if self . is_hidden : <EOL> return <EOL> self . file . write ( AFTER_BAR ) <EOL> self . file . flush ( ) <EOL> @ property <EOL> def pct ( self ) : <EOL> if self . finished : <EOL> return <NUM_LIT:1.0> <EOL> return min ( self . pos / ( float ( self . length ) or <NUM_LIT:1> ) , <NUM_LIT:1.0> ) <EOL> @ property <EOL> def time_per_iteration ( self ) : <EOL> if not self . avg : <EOL> return <NUM_LIT:0.0> <EOL> return sum ( self . avg ) / float ( len ( self . avg ) ) <EOL> @ property <EOL> def eta ( self ) : <EOL> if self . length_known and not self . finished : <EOL> return self . time_per_iteration * ( self . length - self . pos ) <EOL> return <NUM_LIT:0.0> <EOL> def format_eta ( self ) : <EOL> if self . eta_known : <EOL> t = self . eta + <NUM_LIT:1> <EOL> seconds = t % <NUM_LIT> <EOL> t /= <NUM_LIT> <EOL> minutes = t % <NUM_LIT> <EOL> t /= <NUM_LIT> <EOL> hours = t % <NUM_LIT> <EOL> t /= <NUM_LIT> <EOL> if t > <NUM_LIT:0> : <EOL> days = t <EOL> return '<STR_LIT>' % ( days , hours , minutes , seconds ) <EOL> else : <EOL> return '<STR_LIT>' % ( hours , minutes , seconds ) <EOL> return '<STR_LIT>' <EOL> def format_pos ( self ) : <EOL> pos = str ( self . pos ) <EOL> if self . length_known : <EOL> pos += '<STR_LIT>' % self . length <EOL> return pos <EOL> def format_pct ( self ) : <EOL> return ( '<STR_LIT>' % int ( self . pct * <NUM_LIT:100> ) ) [ <NUM_LIT:1> : ] <EOL> def format_progress_line ( self ) : <EOL> show_percent = self . show_percent <EOL> info_bits = [ ] <EOL> if self . length_known : <EOL> bar_length = int ( self . pct * self . width ) <EOL> bar = self . fill_char * bar_length <EOL> bar += self . empty_char * ( self . width - bar_length ) <EOL> if show_percent is None : <EOL> show_percent = not self . show_pos <EOL> else : <EOL> if self . finished : <EOL> bar = self . fill_char * self . width <EOL> else : <EOL> bar = list ( self . empty_char * ( self . width or <NUM_LIT:1> ) ) <EOL> if self . time_per_iteration != <NUM_LIT:0> : <EOL> bar [ int ( ( math . cos ( self . pos * self . time_per_iteration ) <EOL> / <NUM_LIT> + <NUM_LIT:0.5> ) * self . width ) ] = self . fill_char <EOL> bar = '<STR_LIT>' . join ( bar ) <EOL> if self . show_pos : <EOL> info_bits . append ( self . format_pos ( ) ) <EOL> if show_percent : <EOL> info_bits . append ( self . format_pct ( ) ) <EOL> if self . show_eta and self . eta_known and not self . finished : <EOL> info_bits . append ( self . format_eta ( ) ) <EOL> if self . item_show_func is not None : <EOL> item_info = self . item_show_func ( self . current_item ) <EOL> if item_info is not None : <EOL> info_bits . append ( item_info ) <EOL> return ( self . bar_template % { <EOL> '<STR_LIT:label>' : self . label , <EOL> '<STR_LIT:bar>' : bar , <EOL> '<STR_LIT:info>' : self . info_sep . join ( info_bits ) <EOL> } ) . rstrip ( ) <EOL> def render_progress ( self ) : <EOL> from . termui import get_terminal_size <EOL> nl = False <EOL> if self . is_hidden : <EOL> buf = [ self . label ] <EOL> nl = True <EOL> else : <EOL> buf = [ ] <EOL> if self . autowidth : <EOL> old_width = self . width <EOL> self . width = <NUM_LIT:0> <EOL> clutter_length = term_len ( self . format_progress_line ( ) ) <EOL> new_width = max ( <NUM_LIT:0> , get_terminal_size ( ) [ <NUM_LIT:0> ] - clutter_length ) <EOL> if new_width < old_width : <EOL> buf . append ( BEFORE_BAR ) <EOL> buf . append ( '<STR_LIT:U+0020>' * self . max_width ) <EOL> self . max_width = new_width <EOL> self . width = new_width <EOL> clear_width = self . width <EOL> if self . max_width is not None : <EOL> clear_width = self . max_width <EOL> buf . append ( BEFORE_BAR ) <EOL> line = self . format_progress_line ( ) <EOL> line_len = term_len ( line ) <EOL> if self . max_width is None or self . max_width < line_len : <EOL> self . max_width = line_len <EOL> buf . append ( line ) <EOL> buf . append ( '<STR_LIT:U+0020>' * ( clear_width - line_len ) ) <EOL> line = '<STR_LIT>' . join ( buf ) <EOL> if line != self . _last_line : <EOL> self . _last_line = line <EOL> echo ( line , file = self . file , color = self . color , nl = nl ) <EOL> self . file . flush ( ) <EOL> def make_step ( self , n_steps ) : <EOL> self . pos += n_steps <EOL> if self . length_known and self . pos >= self . length : <EOL> self . finished = True <EOL> if ( time . time ( ) - self . last_eta ) < <NUM_LIT:1.0> : <EOL> return <EOL> self . last_eta = time . time ( ) <EOL> self . avg = self . avg [ - <NUM_LIT:6> : ] + [ - ( self . start - time . time ( ) ) / ( self . pos ) ] <EOL> self . eta_known = self . length_known <EOL> def update ( self , n_steps ) : <EOL> self . make_step ( n_steps ) <EOL> self . render_progress ( ) <EOL> def finish ( self ) : <EOL> self . eta_known = <NUM_LIT:0> <EOL> self . current_item = None <EOL> self . finished = True <EOL> def next ( self ) : <EOL> if self . is_hidden : <EOL> return next ( self . iter ) <EOL> try : <EOL> rv = next ( self . iter ) <EOL> self . current_item = rv <EOL> except StopIteration : <EOL> self . finish ( ) <EOL> self . render_progress ( ) <EOL> raise StopIteration ( ) <EOL> else : <EOL> self . update ( <NUM_LIT:1> ) <EOL> return rv <EOL> if not PY2 : <EOL> __next__ = next <EOL> del next <EOL> def pager ( text , color = None ) : <EOL> """<STR_LIT>""" <EOL> stdout = _default_text_stdout ( ) <EOL> if not isatty ( sys . stdin ) or not isatty ( stdout ) : <EOL> return _nullpager ( stdout , text , color ) <EOL> pager_cmd = ( os . environ . get ( '<STR_LIT>' , None ) or '<STR_LIT>' ) . strip ( ) <EOL> if pager_cmd : <EOL> if WIN : <EOL> return _tempfilepager ( text , pager_cmd , color ) <EOL> return _pipepager ( text , pager_cmd , color ) <EOL> if os . environ . get ( '<STR_LIT>' ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return _nullpager ( stdout , text , color ) <EOL> if WIN or sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return _tempfilepager ( text , '<STR_LIT>' , color ) <EOL> if hasattr ( os , '<STR_LIT>' ) and os . system ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> return _pipepager ( text , '<STR_LIT>' , color ) <EOL> import tempfile <EOL> fd , filename = tempfile . mkstemp ( ) <EOL> os . close ( fd ) <EOL> try : <EOL> if hasattr ( os , '<STR_LIT>' ) and os . system ( '<STR_LIT>' % filename ) == <NUM_LIT:0> : <EOL> return _pipepager ( text , '<STR_LIT>' , color ) <EOL> return _nullpager ( stdout , text , color ) <EOL> finally : <EOL> os . unlink ( filename ) <EOL> def _pipepager ( text , cmd , color ) : <EOL> """<STR_LIT>""" <EOL> import subprocess <EOL> env = dict ( os . environ ) <EOL> cmd_detail = cmd . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] . split ( ) <EOL> if color is None and cmd_detail [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> less_flags = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) + '<STR_LIT:U+0020>' . join ( cmd_detail [ <NUM_LIT:1> : ] ) <EOL> if not less_flags : <EOL> env [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> color = True <EOL> elif '<STR_LIT:r>' in less_flags or '<STR_LIT:R>' in less_flags : <EOL> color = True <EOL> if not color : <EOL> text = strip_ansi ( text ) <EOL> c = subprocess . Popen ( cmd , shell = True , stdin = subprocess . PIPE , <EOL> env = env ) <EOL> encoding = get_best_encoding ( c . stdin ) <EOL> try : <EOL> c . stdin . write ( text . encode ( encoding , '<STR_LIT:replace>' ) ) <EOL> c . stdin . close ( ) <EOL> except ( IOError , KeyboardInterrupt ) : <EOL> pass <EOL> while True : <EOL> try : <EOL> c . wait ( ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> else : <EOL> break <EOL> def _tempfilepager ( text , cmd , color ) : <EOL> """<STR_LIT>""" <EOL> import tempfile <EOL> filename = tempfile . mktemp ( ) <EOL> if not color : <EOL> text = strip_ansi ( text ) <EOL> encoding = get_best_encoding ( sys . stdout ) <EOL> with open_stream ( filename , '<STR_LIT:wb>' ) [ <NUM_LIT:0> ] as f : <EOL> f . write ( text . encode ( encoding ) ) <EOL> try : <EOL> os . system ( cmd + '<STR_LIT>' + filename + '<STR_LIT:">' ) <EOL> finally : <EOL> os . unlink ( filename ) <EOL> def _nullpager ( stream , text , color ) : <EOL> """<STR_LIT>""" <EOL> if not color : <EOL> text = strip_ansi ( text ) <EOL> stream . write ( text ) <EOL> class Editor ( object ) : <EOL> def __init__ ( self , editor = None , env = None , require_save = True , <EOL> extension = '<STR_LIT>' ) : <EOL> self . editor = editor <EOL> self . env = env <EOL> self . require_save = require_save <EOL> self . extension = extension <EOL> def get_editor ( self ) : <EOL> if self . editor is not None : <EOL> return self . editor <EOL> for key in '<STR_LIT>' , '<STR_LIT>' : <EOL> rv = os . environ . get ( key ) <EOL> if rv : <EOL> return rv <EOL> if WIN : <EOL> return '<STR_LIT>' <EOL> for editor in '<STR_LIT>' , '<STR_LIT>' : <EOL> if os . system ( '<STR_LIT>' % editor ) == <NUM_LIT:0> : <EOL> return editor <EOL> return '<STR_LIT>' <EOL> def edit_file ( self , filename ) : <EOL> import subprocess <EOL> editor = self . get_editor ( ) <EOL> if self . env : <EOL> environ = os . environ . copy ( ) <EOL> environ . update ( self . env ) <EOL> else : <EOL> environ = None <EOL> try : <EOL> c = subprocess . Popen ( '<STR_LIT>' % ( editor , filename ) , <EOL> env = environ , shell = True ) <EOL> exit_code = c . wait ( ) <EOL> if exit_code != <NUM_LIT:0> : <EOL> raise ClickException ( '<STR_LIT>' % editor ) <EOL> except OSError as e : <EOL> raise ClickException ( '<STR_LIT>' % ( editor , e ) ) <EOL> def edit ( self , text ) : <EOL> import tempfile <EOL> text = text or '<STR_LIT>' <EOL> if text and not text . endswith ( '<STR_LIT:\n>' ) : <EOL> text += '<STR_LIT:\n>' <EOL> fd , name = tempfile . mkstemp ( prefix = '<STR_LIT>' , suffix = self . extension ) <EOL> try : <EOL> if WIN : <EOL> encoding = '<STR_LIT>' <EOL> text = text . replace ( '<STR_LIT:\n>' , '<STR_LIT:\r\n>' ) <EOL> else : <EOL> encoding = '<STR_LIT:utf-8>' <EOL> text = text . encode ( encoding ) <EOL> f = os . fdopen ( fd , '<STR_LIT:wb>' ) <EOL> f . write ( text ) <EOL> f . close ( ) <EOL> timestamp = os . path . getmtime ( name ) <EOL> self . edit_file ( name ) <EOL> if self . require_save and os . path . getmtime ( name ) == timestamp : <EOL> return None <EOL> f = open ( name , '<STR_LIT:rb>' ) <EOL> try : <EOL> rv = f . read ( ) <EOL> finally : <EOL> f . close ( ) <EOL> return rv . decode ( '<STR_LIT>' ) . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) <EOL> finally : <EOL> os . unlink ( name ) <EOL> def open_url ( url , wait = False , locate = False ) : <EOL> import subprocess <EOL> def _unquote_file ( url ) : <EOL> try : <EOL> import urllib <EOL> except ImportError : <EOL> import urllib <EOL> if url . startswith ( '<STR_LIT>' ) : <EOL> url = urllib . unquote ( url [ <NUM_LIT:7> : ] ) <EOL> return url <EOL> if sys . platform == '<STR_LIT>' : <EOL> args = [ '<STR_LIT>' ] <EOL> if wait : <EOL> args . append ( '<STR_LIT>' ) <EOL> if locate : <EOL> args . append ( '<STR_LIT>' ) <EOL> args . append ( _unquote_file ( url ) ) <EOL> null = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> try : <EOL> return subprocess . Popen ( args , stderr = null ) . wait ( ) <EOL> finally : <EOL> null . close ( ) <EOL> elif WIN : <EOL> if locate : <EOL> url = _unquote_file ( url ) <EOL> args = '<STR_LIT>' % _unquote_file ( <EOL> url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) <EOL> else : <EOL> args = '<STR_LIT>' % ( <EOL> wait and '<STR_LIT>' or '<STR_LIT>' , url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) <EOL> return os . system ( args ) <EOL> try : <EOL> if locate : <EOL> url = os . path . dirname ( _unquote_file ( url ) ) or '<STR_LIT:.>' <EOL> else : <EOL> url = _unquote_file ( url ) <EOL> c = subprocess . Popen ( [ '<STR_LIT>' , url ] ) <EOL> if wait : <EOL> return c . wait ( ) <EOL> return <NUM_LIT:0> <EOL> except OSError : <EOL> if url . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) and not locate and not wait : <EOL> import webbrowser <EOL> webbrowser . open ( url ) <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def _translate_ch_to_exc ( ch ) : <EOL> if ch == '<STR_LIT>' : <EOL> raise KeyboardInterrupt ( ) <EOL> if ch == '<STR_LIT>' : <EOL> raise EOFError ( ) <EOL> if WIN : <EOL> import msvcrt <EOL> def getchar ( echo ) : <EOL> rv = msvcrt . getch ( ) <EOL> if echo : <EOL> msvcrt . putchar ( rv ) <EOL> _translate_ch_to_exc ( rv ) <EOL> if PY2 : <EOL> enc = getattr ( sys . stdin , '<STR_LIT>' , None ) <EOL> if enc is not None : <EOL> rv = rv . decode ( enc , '<STR_LIT:replace>' ) <EOL> else : <EOL> rv = rv . decode ( '<STR_LIT>' , '<STR_LIT:replace>' ) <EOL> return rv <EOL> else : <EOL> import tty <EOL> import termios <EOL> def getchar ( echo ) : <EOL> if not isatty ( sys . stdin ) : <EOL> f = open ( '<STR_LIT>' ) <EOL> fd = f . fileno ( ) <EOL> else : <EOL> fd = sys . stdin . fileno ( ) <EOL> f = None <EOL> try : <EOL> old_settings = termios . tcgetattr ( fd ) <EOL> try : <EOL> tty . setraw ( fd ) <EOL> ch = os . read ( fd , <NUM_LIT:32> ) <EOL> if echo and isatty ( sys . stdout ) : <EOL> sys . stdout . write ( ch ) <EOL> finally : <EOL> termios . tcsetattr ( fd , termios . TCSADRAIN , old_settings ) <EOL> sys . stdout . flush ( ) <EOL> if f is not None : <EOL> f . close ( ) <EOL> except termios . error : <EOL> pass <EOL> _translate_ch_to_exc ( ch ) <EOL> return ch . decode ( get_best_encoding ( sys . stdin ) , '<STR_LIT:replace>' ) </s>
