def get_file_type(text): if not pathexists(text): # FIXME: Real test for URL here return shortcuts.SHORTCUT_TYPE_URL if isdir(text): return shortcuts.SHORTCUT_TYPE_FOLDER if not isfile(text) and not islink(text): return shortcuts.SHORTCUT_TYPE_DOCUMENT filename = text # Sample file contents with open(filename, "r") as fd: sample = fd.read(128) # Guess if it's executable can_execute = False content_type = None try: content_type = gio.content_type_guess( filename, sample, want_uncertain=False ) # IGNORE:E1101 @UndefinedVariable Keep PyLint and PyDev happy can_execute = gio.content_type_can_be_executable( content_type ) # IGNORE:E1101 @UndefinedVariable Keep PyLint and PyDev happy except Exception as e: logging.error("Error guessing file type: %s", e) if not can_execute: if os.access(filename, os.X_OK): return shortcuts.SHORTCUT_TYPE_EXECUTABLE if can_execute and os.access(filename, os.X_OK): return shortcuts.SHORTCUT_TYPE_EXECUTABLE return shortcuts.SHORTCUT_TYPE_DOCUMENT
def __launch_bookmark(self): treeselection = self.bookmarks_treeview.get_selection() (model, list_iter) = treeselection.get_selected() bookmark = model.get_value(list_iter, 0) url = bookmark.get_url() # Determine the current window parent parent = self.__get_current_window_parent() parse_result = urlparse.urlparse(url) # It seems to be a file-based bookmark if parse_result.scheme == 'file': # Test if it is an executable filepath = urllib.url2pathname(parse_result.path) mimetype = self.__get_mimetype(filepath) is_executable = gio.content_type_can_be_executable(mimetype) and \ os.access(filepath, os.X_OK) # If it is an executable attempt to open it if is_executable: subprocess.Popen([filepath]) # Just attempt to open the file with the default handler described # in the system else: subprocess.Popen(['xdg-open', filepath]) else: connection = bookmark.get_connection() if connection == "": connection = None msdcm = tgcm.ui.MSD.MSDConnectionsManager() if connection == None: if self.connection_manager.ppp_manager.nmConnectionState( ) == NetworkManager.State.CONNECTED: if url.startswith("http:"): if bookmark.userdata == 1: self.security_manager.launch_url(url, parent=parent) else: webbrowser.open(url) else: webbrowser.open("http://%s" % url) else: msdcm.do_connect_with_smart_connector( bookmark_info=bookmark) else: conn_settings = self.connection_settings_manager.get_connection_info_dict( name=connection) if msdcm.connect_to_connection( connection_settings=conn_settings, bookmark_info=bookmark) != 0: msdcm.error_on_connection()
def __launch_bookmark(self) : treeselection = self.bookmarks_treeview.get_selection() (model, list_iter) = treeselection.get_selected() bookmark = model.get_value(list_iter, 0) url = bookmark.get_url() # Determine the current window parent parent = self.__get_current_window_parent() parse_result = urlparse.urlparse(url) # It seems to be a file-based bookmark if parse_result.scheme == 'file': # Test if it is an executable filepath = urllib.url2pathname(parse_result.path) mimetype = self.__get_mimetype(filepath) is_executable = gio.content_type_can_be_executable(mimetype) and \ os.access(filepath, os.X_OK) # If it is an executable attempt to open it if is_executable: subprocess.Popen([filepath]) # Just attempt to open the file with the default handler described # in the system else: subprocess.Popen(['xdg-open', filepath]) else: connection = bookmark.get_connection() if connection == "": connection = None msdcm = tgcm.ui.MSD.MSDConnectionsManager() if connection == None : if self.connection_manager.ppp_manager.nmConnectionState() == NetworkManager.State.CONNECTED: if url.startswith ("http:"): if bookmark.userdata == 1: self.security_manager.launch_url(url, parent=parent) else: webbrowser.open(url) else: webbrowser.open("http://%s" % url) else: msdcm.do_connect_with_smart_connector(bookmark_info=bookmark) else: conn_settings=self.connection_settings_manager.get_connection_info_dict(name=connection) if msdcm.connect_to_connection(connection_settings=conn_settings, bookmark_info=bookmark) != 0: msdcm.error_on_connection()
def execute_file(self, path, provider=None): """Execute specified item properly.""" mime_type = self.get_mime_type(path) terminal_type = self._application.options.section('terminal').get('type') should_execute = False if provider is not None and provider.is_local: # only allow local files which have execute # bit set to be executed locally should_execute = os.access(path, os.X_OK) # if we still don't know content type, try to guess if self.is_mime_type_unknown(mime_type): data = self.get_sample_data(path, provider) mime_type = self.get_mime_type(data=data) if gio.content_type_can_be_executable(mime_type) and should_execute: # file type is executable if is_x_app(path): subprocess.Popen( (path, '&'), cwd=os.path.dirname(path) ) else: # command is console based, create terminal tab and fork it if terminal_type != TerminalType.EXTERNAL: active_object = self._application.get_active_object() options = Parameters() options.set('close_with_child', False) options.set('shell_command', path) options.set('path', os.path.dirname(path)) tab = self._application.create_terminal_tab(active_object._notebook, options) else: # file type is not executable, try to open with default associated application default_application = self.get_default_application_for_type(mime_type) if default_application is not None: self.open_file((path,), default_application) else: # no default application selected, show application selection dialog dialog = ApplicationSelectDialog(self._application, path) result = dialog.get_response() if result[0] == gtk.RESPONSE_OK: self.open_file(selection=(path,), exec_command=result[2])
def execute_file(self, path, provider=None): """Execute specified item properly.""" mime_type = self.get_mime_type(path) terminal_type = self._application.options.section('terminal').get('type') should_execute = False if provider is not None and provider.is_local: # only allow local files which have execute # bit set to be executed locally should_execute = os.access(path, os.X_OK) # if we still don't know content type, try to guess if self.is_mime_type_unknown(mime_type): data = self.get_sample_data(path, provider) mime_type = self.get_mime_type(data=data) if gio.content_type_can_be_executable(mime_type) and should_execute: # file type is executable if is_x_app(path): subprocess.Popen( (path, '&'), cwd=os.path.dirname(path) ) else: # command is console based, create terminal tab and fork it active_object = self._application.get_active_object() options = Parameters() options.set('close_with_child', False) options.set('shell_command', path) options.set('path', os.path.dirname(path)) self._application.create_terminal_tab(active_object._notebook, options) else: # file type is not executable, try to open with default associated application default_application = self.get_default_application_for_type(mime_type) if default_application is not None: self.open_file((path,), default_application) else: # no default application selected, show application selection dialog dialog = ApplicationSelectDialog(self._application, path) result = dialog.get_response() if result[0] == gtk.RESPONSE_OK: self.open_file(selection=(path,), exec_command=result[2])
def is_good_executable(fileleaf): if not fileleaf._is_executable(): return False ctype, uncertain = gio.content_type_guess(fileleaf.object, None, True) return uncertain or gio.content_type_can_be_executable(ctype)