<s> from __future__ import ( unicode_literals , division , absolute_import , print_function ) <EOL> import os <EOL> import re <EOL> from io import StringIO <EOL> from bzrlib import ( workingtree , status , library_state , trace , ui ) <EOL> from powerline . lib . vcs import get_branch_name , get_file_status <EOL> from powerline . lib . path import join <EOL> from powerline . lib . encoding import get_preferred_file_contents_encoding <EOL> class CoerceIO ( StringIO ) : <EOL> def write ( self , arg ) : <EOL> if isinstance ( arg , bytes ) : <EOL> arg = arg . decode ( get_preferred_file_contents_encoding ( ) , '<STR_LIT:replace>' ) <EOL> return super ( CoerceIO , self ) . write ( arg ) <EOL> nick_pat = re . compile ( br'<STR_LIT>' ) <EOL> def branch_name_from_config_file ( directory , config_file ) : <EOL> ans = None <EOL> try : <EOL> with open ( config_file , '<STR_LIT:rb>' ) as f : <EOL> for line in f : <EOL> m = nick_pat . match ( line ) <EOL> if m is not None : <EOL> ans = m . group ( <NUM_LIT:1> ) . strip ( ) . decode ( get_preferred_file_contents_encoding ( ) , '<STR_LIT:replace>' ) <EOL> break <EOL> except Exception : <EOL> pass <EOL> return ans or os . path . basename ( directory ) <EOL> state = None <EOL> class Repository ( object ) : <EOL> def __init__ ( self , directory , create_watcher ) : <EOL> self . directory = os . path . abspath ( directory ) <EOL> self . create_watcher = create_watcher <EOL> def status ( self , path = None ) : <EOL> '''<STR_LIT>''' <EOL> if path is not None : <EOL> return get_file_status ( <EOL> directory = self . directory , <EOL> dirstate_file = join ( self . directory , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> file_path = path , <EOL> ignore_file_name = '<STR_LIT>' , <EOL> get_func = self . do_status , <EOL> create_watcher = self . create_watcher , <EOL> ) <EOL> return self . do_status ( self . directory , path ) <EOL> def do_status ( self , directory , path ) : <EOL> try : <EOL> return self . _status ( self . directory , path ) <EOL> except Exception : <EOL> pass <EOL> def _status ( self , directory , path ) : <EOL> global state <EOL> if state is None : <EOL> state = library_state . BzrLibraryState ( ui = ui . SilentUIFactory , trace = trace . DefaultConfig ( ) ) <EOL> buf = CoerceIO ( ) <EOL> w = workingtree . WorkingTree . open ( directory ) <EOL> status . show_tree_status ( w , specific_files = [ path ] if path else None , to_file = buf , short = True ) <EOL> raw = buf . getvalue ( ) <EOL> if not raw . strip ( ) : <EOL> return <EOL> if path : <EOL> ans = raw [ : <NUM_LIT:2> ] <EOL> if ans == '<STR_LIT>' : <EOL> ans = None <EOL> return ans <EOL> dirtied = untracked = '<STR_LIT:U+0020>' <EOL> for line in raw . splitlines ( ) : <EOL> if len ( line ) > <NUM_LIT:1> and line [ <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> dirtied = '<STR_LIT:D>' <EOL> elif line and line [ <NUM_LIT:0> ] == '<STR_LIT:?>' : <EOL> untracked = '<STR_LIT>' <EOL> ans = dirtied + untracked <EOL> return ans if ans . strip ( ) else None <EOL> def branch ( self ) : <EOL> config_file = join ( self . directory , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return get_branch_name ( <EOL> directory = self . directory , <EOL> config_file = config_file , <EOL> get_func = branch_name_from_config_file , <EOL> create_watcher = self . create_watcher , <EOL> ) </s>
