<s> """<STR_LIT>""" <EOL> try : <EOL> from future . builtins import open <EOL> from future . builtins import str <EOL> except : <EOL> pass <EOL> import locale <EOL> import logging <EOL> import mimetypes <EOL> import os <EOL> from pyqode . core . api . manager import Manager <EOL> from pyqode . core . api . utils import TextHelper <EOL> from pyqode . qt import QtCore , QtWidgets <EOL> from pyqode . core . cache import Cache <EOL> mimetypes . add_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> mimetypes . add_type ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _logger ( ) : <EOL> return logging . getLogger ( __name__ ) <EOL> class FileManager ( Manager ) : <EOL> """<STR_LIT>""" <EOL> class EOL : <EOL> """<STR_LIT>""" <EOL> System = <NUM_LIT:0> <EOL> Linux = <NUM_LIT:1> <EOL> Mac = <NUM_LIT:2> <EOL> Windows = <NUM_LIT:3> <EOL> _map = { <EOL> System : os . linesep , <EOL> Linux : '<STR_LIT:\n>' , <EOL> Mac : '<STR_LIT:\r>' , <EOL> Windows : '<STR_LIT:\r\n>' <EOL> } <EOL> @ classmethod <EOL> def string ( cls , value ) : <EOL> return cls . _map [ value ] <EOL> @ property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _path : <EOL> return os . path . normpath ( self . _path ) <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . split ( self . path ) [ <NUM_LIT:1> ] <EOL> @ property <EOL> def extension ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . splitext ( self . path ) [ <NUM_LIT:1> ] <EOL> @ property <EOL> def dirname ( self ) : <EOL> """<STR_LIT>""" <EOL> return os . path . dirname ( self . _path ) <EOL> @ property <EOL> def encoding ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _encoding <EOL> @ property <EOL> def icon ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_icon ( ) <EOL> @ property <EOL> def autodetect_eol ( self ) : <EOL> return self . _autodetect_eol <EOL> @ autodetect_eol . setter <EOL> def autodetect_eol ( self , value ) : <EOL> self . _autodetect_eol = value <EOL> if not self . _autodetect_eol : <EOL> self . _eol = self . EOL . string ( self . _preferred_eol ) <EOL> @ property <EOL> def preferred_eol ( self ) : <EOL> return self . _preferred_eol <EOL> @ preferred_eol . setter <EOL> def preferred_eol ( self , eol ) : <EOL> self . _preferred_eol = eol <EOL> if not self . _autodetect_eol : <EOL> self . _eol = self . EOL . string ( self . _preferred_eol ) <EOL> @ property <EOL> def file_size_limit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _limit <EOL> @ file_size_limit . setter <EOL> def file_size_limit ( self , value ) : <EOL> self . _limit = value <EOL> def _get_icon ( self ) : <EOL> return QtWidgets . QFileIconProvider ( ) . icon ( QtCore . QFileInfo ( self . path ) ) <EOL> def __init__ ( self , editor , replace_tabs_by_spaces = True ) : <EOL> """<STR_LIT>""" <EOL> super ( FileManager , self ) . __init__ ( editor ) <EOL> self . _limit = <NUM_LIT> <EOL> self . _path = '<STR_LIT>' <EOL> self . mimetype = '<STR_LIT>' <EOL> self . _encoding = locale . getpreferredencoding ( ) <EOL> self . replace_tabs_by_spaces = replace_tabs_by_spaces <EOL> self . opening = False <EOL> self . saving = True <EOL> self . safe_save = True <EOL> self . clean_trailing_whitespaces = True <EOL> self . restore_cursor = True <EOL> self . _preferred_eol = self . EOL . System <EOL> self . _eol = self . EOL . string ( self . _preferred_eol ) <EOL> self . _autodetect_eol = True <EOL> @ staticmethod <EOL> def get_mimetype ( path ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . split ( path ) [ <NUM_LIT:1> ] <EOL> mimetype = mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] <EOL> if mimetype is None : <EOL> mimetype = '<STR_LIT>' <EOL> _logger ( ) . debug ( '<STR_LIT>' , mimetype ) <EOL> return mimetype <EOL> def open ( self , path , encoding = None , use_cached_encoding = True ) : <EOL> """<STR_LIT>""" <EOL> ret_val = False <EOL> if encoding is None : <EOL> encoding = locale . getpreferredencoding ( ) <EOL> self . opening = True <EOL> settings = Cache ( ) <EOL> self . _path = path <EOL> if use_cached_encoding : <EOL> try : <EOL> cached_encoding = settings . get_file_encoding ( <EOL> path , preferred_encoding = encoding ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> encoding = cached_encoding <EOL> enable_modes = os . path . getsize ( path ) < self . _limit <EOL> for m in self . editor . modes : <EOL> if m . enabled : <EOL> m . enabled = enable_modes <EOL> if not enable_modes : <EOL> self . editor . modes . clear ( ) <EOL> try : <EOL> with open ( path , '<STR_LIT>' , encoding = encoding ) as file : <EOL> content = file . read ( ) <EOL> if self . autodetect_eol : <EOL> self . _eol = file . newlines <EOL> if isinstance ( self . _eol , tuple ) : <EOL> self . _eol = self . _eol [ <NUM_LIT:0> ] <EOL> if self . _eol is None : <EOL> self . _eol = self . EOL . string ( self . preferred_eol ) <EOL> else : <EOL> self . _eol = self . EOL . string ( self . preferred_eol ) <EOL> except ( UnicodeDecodeError , UnicodeError ) as e : <EOL> try : <EOL> from pyqode . core . panels import EncodingPanel <EOL> panel = self . editor . panels . get ( EncodingPanel ) <EOL> except KeyError : <EOL> raise e <EOL> else : <EOL> panel . on_open_failed ( path , encoding ) <EOL> else : <EOL> settings . set_file_encoding ( path , encoding ) <EOL> self . _encoding = encoding <EOL> if self . replace_tabs_by_spaces : <EOL> content = content . replace ( "<STR_LIT:\t>" , "<STR_LIT:U+0020>" * self . editor . tab_length ) <EOL> self . editor . setPlainText ( <EOL> content , self . get_mimetype ( path ) , self . encoding ) <EOL> self . editor . setDocumentTitle ( self . editor . file . name ) <EOL> ret_val = True <EOL> _logger ( ) . debug ( '<STR_LIT>' , path ) <EOL> self . opening = False <EOL> if self . restore_cursor : <EOL> self . _restore_cached_pos ( ) <EOL> self . _check_for_readonly ( ) <EOL> return ret_val <EOL> def _check_for_readonly ( self ) : <EOL> self . read_only = not os . access ( self . path , os . W_OK ) <EOL> self . editor . setReadOnly ( self . read_only ) <EOL> def _restore_cached_pos ( self ) : <EOL> pos = Cache ( ) . get_cursor_position ( self . path ) <EOL> max_pos = self . editor . document ( ) . characterCount ( ) <EOL> if pos > max_pos : <EOL> pos = max_pos - <NUM_LIT:1> <EOL> tc = self . editor . textCursor ( ) <EOL> tc . setPosition ( pos ) <EOL> self . editor . setTextCursor ( tc ) <EOL> QtCore . QTimer . singleShot ( <NUM_LIT:1> , self . editor . centerCursor ) <EOL> def reload ( self , encoding ) : <EOL> """<STR_LIT>""" <EOL> assert os . path . exists ( self . path ) <EOL> self . open ( self . path , encoding = encoding , <EOL> use_cached_encoding = False ) <EOL> @ staticmethod <EOL> def _rm ( tmp_path ) : <EOL> if os . path . exists ( tmp_path ) : <EOL> os . remove ( tmp_path ) <EOL> def _reset_selection ( self , sel_end , sel_start ) : <EOL> text_cursor = self . editor . textCursor ( ) <EOL> text_cursor . setPosition ( sel_start ) <EOL> text_cursor . setPosition ( sel_end , text_cursor . KeepAnchor ) <EOL> self . editor . setTextCursor ( text_cursor ) <EOL> def _get_selection ( self ) : <EOL> sel_start = self . editor . textCursor ( ) . selectionStart ( ) <EOL> sel_end = self . editor . textCursor ( ) . selectionEnd ( ) <EOL> return sel_end , sel_start <EOL> def _get_text ( self , encoding ) : <EOL> lines = self . editor . toPlainText ( ) . splitlines ( ) <EOL> if self . clean_trailing_whitespaces : <EOL> lines = [ l . rstrip ( ) for l in lines ] <EOL> try : <EOL> last_line = lines [ - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> pass <EOL> else : <EOL> while last_line == '<STR_LIT>' : <EOL> try : <EOL> lines . pop ( ) <EOL> last_line = lines [ - <NUM_LIT:1> ] <EOL> except IndexError : <EOL> last_line = None <EOL> text = self . _eol . join ( lines ) + self . _eol <EOL> return text . encode ( encoding ) <EOL> def save ( self , path = None , encoding = None , fallback_encoding = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . editor . dirty and ( encoding is None and encoding == self . encoding ) and ( path is None and path == self . path ) : <EOL> return <EOL> if fallback_encoding is None : <EOL> fallback_encoding = locale . getpreferredencoding ( ) <EOL> _logger ( ) . log ( <EOL> <NUM_LIT:5> , "<STR_LIT>" , path , encoding ) <EOL> if path is None : <EOL> if self . path : <EOL> path = self . path <EOL> else : <EOL> _logger ( ) . debug ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return False <EOL> if encoding is None : <EOL> encoding = self . _encoding <EOL> self . saving = True <EOL> self . editor . text_saving . emit ( str ( path ) ) <EOL> try : <EOL> st_mode = os . stat ( path ) . st_mode <EOL> except ( ImportError , TypeError , AttributeError , OSError ) : <EOL> st_mode = None <EOL> if self . safe_save : <EOL> tmp_path = path + '<STR_LIT>' <EOL> else : <EOL> tmp_path = path <EOL> try : <EOL> with open ( tmp_path , '<STR_LIT:wb>' ) as file : <EOL> file . write ( self . _get_text ( encoding ) ) <EOL> except UnicodeEncodeError : <EOL> with open ( tmp_path , '<STR_LIT:wb>' ) as file : <EOL> file . write ( self . _get_text ( fallback_encoding ) ) <EOL> except ( IOError , OSError ) as e : <EOL> self . _rm ( tmp_path ) <EOL> self . saving = False <EOL> self . editor . text_saved . emit ( str ( path ) ) <EOL> raise e <EOL> else : <EOL> Cache ( ) . set_file_encoding ( path , encoding ) <EOL> self . _encoding = encoding <EOL> if self . safe_save : <EOL> self . _rm ( path ) <EOL> os . rename ( tmp_path , path ) <EOL> self . _rm ( tmp_path ) <EOL> self . editor . document ( ) . setModified ( False ) <EOL> self . _path = os . path . normpath ( path ) <EOL> self . editor . text_saved . emit ( str ( path ) ) <EOL> self . saving = False <EOL> _logger ( ) . debug ( '<STR_LIT>' , path ) <EOL> self . _check_for_readonly ( ) <EOL> if st_mode : <EOL> try : <EOL> os . chmod ( path , st_mode ) <EOL> except ( ImportError , TypeError , AttributeError ) : <EOL> pass <EOL> def close ( self , clear = True ) : <EOL> """<STR_LIT>""" <EOL> Cache ( ) . set_cursor_position ( <EOL> self . path , self . editor . textCursor ( ) . position ( ) ) <EOL> self . editor . _original_text = '<STR_LIT>' <EOL> if clear : <EOL> self . editor . clear ( ) <EOL> self . _path = '<STR_LIT>' <EOL> self . mimetype = '<STR_LIT>' <EOL> self . _encoding = locale . getpreferredencoding ( ) <EOL> def clone_settings ( self , original ) : <EOL> self . replace_tabs_by_spaces = original . replace_tabs_by_spaces <EOL> self . safe_save = original . replace_tabs_by_spaces <EOL> self . clean_trailing_whitespaces = original . clean_trailing_whitespaces <EOL> self . restore_cursor = original . restore_cursor </s>
