~chicharreros/magicicada-client/1.0

« back to all changes in this revision

Viewing changes to ubuntuone/platform/os_helper/linux.py

  • Committer: Magicicada Bot
  • Author(s): Facundo Batista
  • Date: 2018-03-22 18:27:26 UTC
  • mfrom: (1440.1.2 new-trash)
  • Revision ID: magicicada_bot-20180322182726-s9zxdqdnrqnuyav9
[r=nataliabidart] Start using a new way to send to trash, going away from Gnome.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
to support the linux platform.
32
32
"""
33
33
 
34
 
import errno
35
34
import logging
36
35
import os
37
36
import shutil
38
37
 
39
38
try:
40
 
    from gi.repository import Gio, GLib
 
39
    from gi.repository import GObject
41
40
    has_gi = True
42
41
except ImportError:
43
 
    import gio
 
42
    import gobject
44
43
    has_gi = False
 
44
from send2trash import send2trash
45
45
 
46
46
from ubuntuone.platform.os_helper import unix
47
47
 
50
50
logger = logging.getLogger('ubuntuone.SyncDaemon')
51
51
 
52
52
 
 
53
def _remove_path(path):
 
54
    """Remove the path, no matter if file or dir structure."""
 
55
    if os.path.isdir(path):
 
56
        shutil.rmtree(path)
 
57
    else:
 
58
        os.remove(path)
 
59
 
 
60
 
53
61
def move_to_trash(path):
54
62
    """Move the file or dir to trash.
55
63
 
56
64
    If had any error, or the system can't do it, just remove it.
57
65
    """
58
 
    if has_gi:
59
 
        not_supported = Gio.IOErrorEnum.NOT_SUPPORTED
60
 
        try:
61
 
            return_code = Gio.File.new_for_path(path).trash(None)
62
 
        except GLib.GError:
63
 
            exc = OSError()
64
 
            exc.errno = errno.ENOENT
65
 
            raise exc
66
 
    else:
67
 
        not_supported = gio.ERROR_NOT_SUPPORTED
68
 
        try:
69
 
            return_code = gio.File(path).trash()
70
 
        except gio.Error:
71
 
            exc = OSError()
72
 
            exc.errno = errno.ENOENT
73
 
            raise exc
74
 
 
75
 
    if not return_code or return_code == not_supported:
76
 
        logger.warning("Problems moving to trash! (%s) Removing anyway: %r",
77
 
                       return_code, path)
78
 
        if os.path.isdir(path):
79
 
            shutil.rmtree(path)
80
 
        else:
81
 
            os.remove(path)
 
66
    try:
 
67
        send2trash(path)
 
68
    except OSError as exc:
 
69
        logger.warning(
 
70
            "Problems moving to trash! (%s) Removing anyway: %r", exc, path)
 
71
        _remove_path(path)
82
72
 
83
73
 
84
74
def set_application_name(app_name):
85
75
    """Set the name of the application."""
86
76
    if has_gi:
87
 
        from gi.repository import GObject
88
77
        GObject.set_application_name(app_name)
89
78
    else:
90
 
        import gobject
91
79
        gobject.set_application_name(app_name)
92
80
 
93
81