<s> import sublime <EOL> import os <EOL> import contextlib <EOL> import client <EOL> class NoRepositoryFoundError ( Exception ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> @ contextlib . contextmanager <EOL> def pushd ( to ) : <EOL> old_cwd = os . getcwdu ( ) <EOL> os . chdir ( to ) <EOL> yield <EOL> os . chdir ( old_cwd ) <EOL> def get_hg_exe_name ( ) : <EOL> view = sublime . active_window ( ) . active_view ( ) <EOL> if view : <EOL> return view . settings ( ) . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> def get_preferred_terminal ( ) : <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> return settings . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> def find_hg_root ( path ) : <EOL> abs_path = os . path . join ( path , '<STR_LIT>' ) <EOL> if os . path . exists ( abs_path ) and os . path . isdir ( abs_path ) : <EOL> return path <EOL> elif os . path . dirname ( path ) == path : <EOL> return None <EOL> else : <EOL> return find_hg_root ( os . path . dirname ( path ) ) <EOL> def is_flag_set ( flags , which_one ) : <EOL> return flags & which_one == which_one <EOL> class HgServers ( object ) : <EOL> def __getitem__ ( self , key ) : <EOL> return self . _select_server ( key ) <EOL> def _select_server ( self , current_path = None ) : <EOL> """<STR_LIT>""" <EOL> v = sublime . active_window ( ) . active_view ( ) <EOL> repo_root = find_hg_root ( current_path or v . file_name ( ) ) <EOL> if not repo_root : <EOL> raise NoRepositoryFound ( ) <EOL> if not repo_root in self . __dict__ : <EOL> server = self . _start_server ( repo_root ) <EOL> self . __dict__ [ repo_root ] = server <EOL> return self . __dict__ [ repo_root ] <EOL> def _start_server ( self , repo_root ) : <EOL> """<STR_LIT>""" <EOL> hg_bin = get_hg_exe_name ( ) <EOL> server = client . CmdServerClient ( hg_bin = hg_bin , repo_root = repo_root ) <EOL> return server <EOL> def shut_down ( self , repo_root ) : <EOL> self [ repo_root ] . shut_down ( ) <EOL> del self . __dict__ [ repo_root ] </s>
