Skip to content

Commit

Permalink
Common: Renamed functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeanEYE committed Jan 9, 2019
1 parent 6bb30ca commit 6df6a6b
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 51 deletions.
16 changes: 8 additions & 8 deletions sunflower/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from gi.repository import Gtk, Gio
from collections import namedtuple
from urllib.request import pathname2url
from sunflower.common import is_x_app, disp_fn, encode_fn
from sunflower.common import is_gui_app, decode_file_name, encode_file_name
from sunflower.parameters import Parameters
from sunflower.plugin_base.provider import Mode
from sunflower.plugin_base.terminal import TerminalType
Expand Down Expand Up @@ -118,7 +118,7 @@ def get_mime_type(self, path=None, data=None):
# detect content type based on file name
# due to a bug in the GI bindings of GIO, we can't pass non-UTF-8
# file names in here. In this case, that probably doesn't matter.
path = disp_fn(path)
path = decode_file_name(path)
result = Gio.content_type_guess(filename=path)[0]

elif data is not None:
Expand Down Expand Up @@ -220,12 +220,12 @@ def open_file(self, selection, application_info=None, exec_command=None):
if application is not None:
if application.supports_uris():
selection = [
'file://{0}'.format(pathname2url(encode_fn(path)))
if not path.startswith('file://') else encode_fn(path)
'file://{0}'.format(pathname2url(encode_file_name(path)))
if not path.startswith('file://') else encode_file_name(path)
for path in selection]
application.launch_uris(selection)
else:
application.launch([Gio.File.new_for_path(encode_fn(path)) for path in selection])
application.launch([Gio.File.new_for_path(encode_file_name(path)) for path in selection])

elif exec_command is not None:
# use specified command
Expand All @@ -238,7 +238,7 @@ def open_file(self, selection, application_info=None, exec_command=None):
split_command = shlex.split(exec_string, posix=False)
test_command = split_command[0] if len(split_command) > 1 else exec_string

if is_x_app(test_command):
if is_gui_app(test_command):
subprocess.Popen(split_command, cwd=os.path.dirname(selection[0]))

else:
Expand All @@ -264,7 +264,7 @@ def edit_file(self, selection):
test_command = split_command[0] if len(split_command) > 1 else exec_string

if (section.get('terminal_command') and section.get('type') == 1) \
or not is_x_app(test_command):
or not is_gui_app(test_command):
active_object = self._application.get_active_object()

options = Parameters()
Expand Down Expand Up @@ -296,7 +296,7 @@ def execute_file(self, path, provider=None):

if Gio.content_type_can_be_executable(mime_type) and should_execute:
# file type is executable
if is_x_app(path):
if is_gui_app(path):
subprocess.Popen((path,), cwd=os.path.dirname(path))

else:
Expand Down
37 changes: 15 additions & 22 deletions sunflower/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def get_user_directory(directory):

return result

def is_x_app(command):
def is_gui_app(command):
"""Checks if command uses graphical user interfaces."""
# TODO: Add check for Wayland
try:
env = os.environ.copy()
env.update({'LD_TRACE_LOADED_OBJECTS': '1'})
Expand Down Expand Up @@ -189,29 +190,21 @@ def load_translation():
'ngettext': translation.ngettext
})

def disp_fn(fn):
"""
Replaces surrogate codepoints in a filename with a replacement character
to display non-UTF-8 filenames.
"""
def decode_file_name(file_name):
"""Replace surrogate codepoints in a filename with a replacement character
to display non-UTF-8 filenames."""
if sys.version_info[0] == 2:
return fn
return file_name

if isinstance(fn, bytes):
return fn.decode('utf-8', 'replace')
else:
return disp_fn(encode_fn(fn))
if isinstance(file_name, bytes):
return file_name.decode('utf-8', 'replace')

def display_basename(fn):
return disp_fn(os.path.basename(fn))
return decode_file_name(encode_file_name(file_name))

def encode_fn(fn):
"""
Encodes a filename to bytes so it can be passed to GI APIs that expect a
file name (and specify 'filename' as their argument type in the GIR
bindings)
"""
def encode_file_name(file_name):
"""Encode filename to bytes so it can be passed to GI APIs that expect a file name
(and specify `filename` as their argument type in the GIR bindings)."""
if sys.version_info[0] == 2:
return fn
else:
return str(fn).encode('utf-8', 'surrogateescape')
return file_name

return str(file_name).encode('utf-8', 'surrogateescape')
4 changes: 2 additions & 2 deletions sunflower/emblems.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sqlite3 as sql

from gi.repository import Gtk
from sunflower.common import get_cache_directory, encode_fn
from sunflower.common import get_cache_directory, encode_file_name


class EmblemManager:
Expand Down Expand Up @@ -260,7 +260,7 @@ def get_emblems_for_path(self, path):
cursor = self._get_cursor()

# get all the items in path
cursor.execute('SELECT id, name FROM items WHERE path=?', (encode_fn(path),))
cursor.execute('SELECT id, name FROM items WHERE path=?', (encode_file_name(path),))
items = cursor.fetchall()

# exit if there are no items in path
Expand Down
9 changes: 5 additions & 4 deletions sunflower/gui/input_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from gi.repository import Gtk
from sunflower.plugin_base.provider import FileType, Support as ProviderSupport
from sunflower.common import get_user_directory, UserDirectory, display_basename
from sunflower.common import get_user_directory, decode_file_name, UserDirectory
from sunflower.widgets.completion_entry import PathCompletionEntry
from sunflower.queue import OperationQueue

Expand Down Expand Up @@ -165,7 +165,7 @@ def _browse_original_path(self, widget, data=None):

# if link name is empty, add original path name
if self._entry.get_text() == '':
self._entry.set_text(display_basename(dialog.get_filename()))
self._entry.set_text(decode_file_name(os.path.basename(dialog.get_filename())))

dialog.destroy()

Expand Down Expand Up @@ -958,7 +958,7 @@ def _update_label(self, widget=None, data=None):
else:
icon = icon_manager.get_icon_for_file(item)

self._affected.append((icon, display_basename(item)))
self._affected.append((icon, decode_file_name(os.path.basename(item))))

def set_title(self, title_text):
"""Set dialog title"""
Expand Down Expand Up @@ -1714,7 +1714,8 @@ def __init__(self, application, path=None):
label_open_with.set_label(_('Select application:'))

else:
label_open_with.set_label(_('Open <i>{0}</i> with:').format(display_basename(path)))
decoded_path = decode_file_name(os.path.basename(path))
label_open_with.set_label(_('Open <i>{0}</i> with:').format(decoded_path))

# create application list
list_container = Gtk.ScrolledWindow()
Expand Down
2 changes: 1 addition & 1 deletion sunflower/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,7 @@ def execute_command(self, widget, data=None):
if not handled:
# try executing command
try:
if common.is_x_app(command[0]):
if common.is_gui_app(command[0]):
# command is X based, just execute it
process = subprocess.Popen(shlex.split(raw_command), cwd=active_object.path)

Expand Down
3 changes: 2 additions & 1 deletion sunflower/gui/properties_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def _rename_item(self, widget=None, data=None):

def _create_monitor(self):
"""Create item monitor"""
self._monitor = Gio.File.new_for_path(common.encode_fn(self._path)).monitor(Gio.FileMonitorFlags.SEND_MOVED)
path = common.encode_file_name(self._path)
self._monitor = Gio.File.new_for_path(path).monitor(Gio.FileMonitorFlags.SEND_MOVED)
self._monitor.connect('changed', self._item_changes)

def _load_associated_applications(self):
Expand Down
2 changes: 1 addition & 1 deletion sunflower/plugin_base/item_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ def _root_directory(self, widget=None, data=None):
def _control_got_focus(self, widget, data=None):
"""List focus in event"""
PluginBase._control_got_focus(self, widget, data)
self._parent.set_location_label(common.disp_fn(self.path))
self._parent.set_location_label(common.decode_file_name(self.path))

def _locations_button_clicked(self, widget, data=None):
"""Handle clicking on locations button."""
Expand Down
13 changes: 7 additions & 6 deletions sunflower/plugins/file_list/file_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,8 +991,9 @@ def _rename_file(self, widget=None, data=None):
if selection is None:
return
is_dir = self.get_provider().is_dir(selection)

# get base name from selection
selection = common.display_basename(selection)
selection = common.decode_file_name(os.path.basename(selection))

dialog = RenameDialog(self._parent, selection, is_dir)
result = dialog.get_response()
Expand Down Expand Up @@ -1697,8 +1698,8 @@ def _add_item(self, filename, parent=None, parent_path=None):

data = (
os.path.join(parent_path, filename) if parent_path else filename,
common.disp_fn(file_info[0]),
common.disp_fn(file_info[1][1:]),
common.decode_file_name(file_info[0]),
common.decode_file_name(file_info[1][1:]),
file_size,
formated_file_size,
file_mode,
Expand Down Expand Up @@ -2309,11 +2310,11 @@ def change_path(self, path=None, selected=None):
if path_name == "":
path_name = self.path

self._change_tab_text(common.disp_fn(path_name))
self._change_tab_text(common.decode_file_name(path_name))
self._change_title_text(self.path)

if self._parent.get_active_object() is self:
self._parent.set_location_label(common.disp_fn(self.path))
self._parent.set_location_label(common.decode_file_name(self.path))

# change list icon
self._title_bar.set_icon_from_name(provider.get_protocol_icon())
Expand Down Expand Up @@ -2350,7 +2351,7 @@ def change_path(self, path=None, selected=None):
_(
"Error changing working directory to:"
"\n{1}\n\n{0}\n\nWould you like to retry?"
).format(error, common.disp_fn(path))
).format(error, common.decode_file_name(path))
)
dialog.set_default_response(Gtk.ResponseType.YES)
result = dialog.run()
Expand Down
4 changes: 2 additions & 2 deletions sunflower/tools/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess

from gi.repository import Gtk, Gdk, Pango, GObject, GdkPixbuf
from sunflower.common import executable_exists, disp_fn
from sunflower.common import executable_exists, decode_file_name
from sunflower.widgets.status_bar import StatusBar
from sunflower.plugin_base.provider import Mode as FileMode

Expand All @@ -32,7 +32,7 @@ def __init__(self, path, provider, parent):
self._mime_type = associations_manager.get_mime_type(data=data)

# configure window
display_filename = disp_fn(os.path.basename(self.path))
display_filename = decode_file_name(os.path.basename(self.path))
self._window.set_title(_('{0} - Viewer').format(display_filename))
self._window.set_size_request(800, 600)
self._window.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
Expand Down
4 changes: 2 additions & 2 deletions sunflower/widgets/breadcrumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

from gi.repository import Gtk, GObject
from sunflower.common import disp_fn
from sunflower.common import decode_file_name

class Breadcrumbs(Gtk.HBox):
"""Linked list of buttons navigating to different paths."""
Expand Down Expand Up @@ -77,7 +77,7 @@ def refresh(self, path):
control = Gtk.RadioButton.new()

control.set_focus_on_click(False)
control.set_label(disp_fn(element))
control.set_label(decode_file_name(element))
control.set_mode(False)
control.connect('clicked', self.__fragment_click)
control.path = current_path
Expand Down
4 changes: 2 additions & 2 deletions sunflower/widgets/title_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from gi.repository import Gtk, Pango, Gdk
from sunflower.widgets.breadcrumbs import Breadcrumbs
from sunflower.widgets.context_menu import ContextMenu
from sunflower.common import disp_fn
from sunflower.common import decode_file_name


class Mode:
Expand Down Expand Up @@ -124,7 +124,7 @@ def set_title(self, path):
if self._breadcrumbs is not None:
self._breadcrumbs.refresh(path)
else:
self._title_label.set_markup(disp_fn(path).replace('&', '&amp;'))
self._title_label.set_markup(decode_file_name(path).replace('&', '&amp;'))

def set_subtitle(self, text):
"""Set subtitle text"""
Expand Down

0 comments on commit 6df6a6b

Please sign in to comment.