Beispiel #1
0
 def add_startup(self) -> None:
     """Creates the auto-start entry if the user wants one."""
     entry = DesktopEntry()
     entry.addGroup(DesktopEntry.defaultGroup)
     entry.set("Type", "Application")
     entry.set("Name", State.current.my_name)
     entry.set("Exec", "{0} \"{1}\"".format(State.current.python, State.current.my_path))
     entry.write(self.__auto_start_entry)
    def _create_desktop_file_from_command(self, command):
        basename = os.path.basename(command)
        path = os.path.expanduser('~/.local/share/applications/%s.desktop' % basename)

        desktop = DesktopEntry()
        desktop.addGroup('Desktop Entry')
        desktop.set('Type', 'Application')
        desktop.set('Version', '1.0')
        desktop.set('Terminal', 'false')
        desktop.set('Exec', command)
        desktop.set('Name', basename)
        desktop.set('X-Ubuntu-Tweak', 'true')
        desktop.write(path)

        return '%s.desktop' % basename
    def _create_desktop_file_from_command(self, command):
        basename = os.path.basename(command)
        path = os.path.expanduser('~/.local/share/applications/%s.desktop' %
                                  basename)

        desktop = DesktopEntry()
        desktop.addGroup('Desktop Entry')
        desktop.set('Type', 'Application')
        desktop.set('Version', '1.0')
        desktop.set('Terminal', 'false')
        desktop.set('Exec', command)
        desktop.set('Name', basename)
        desktop.set('X-Ubuntu-Tweak', 'true')
        desktop.write(path)

        return '%s.desktop' % basename
Beispiel #4
0
 def test_write_file(self):
     de = DesktopEntry()
     de.parse(self.test_file)
     de.removeKey("Name")
     de.addGroup("Hallo")
     de.set("key", "value", "Hallo")
     
     new_file = os.path.join(self.tmpdir, "test.desktop")
     de.write(new_file, trusted=True)
     
     with io.open(new_file, encoding='utf-8') as f:
         contents = f.read()
     
     assert "[Hallo]" in contents, contents
     assert re.search("key\s*=\s*value", contents), contents
     
     # This is missing the Name key, and has an unknown Hallo group, so it
     # shouldn't validate.
     new_entry = DesktopEntry(new_file)
     self.assertRaises(ValidationError, new_entry.validate)
Beispiel #5
0
    def test_write_file(self):
        de = DesktopEntry()
        de.parse(self.test_file)
        de.removeKey("Name")
        de.addGroup("Hallo")
        de.set("key", "value", "Hallo")

        new_file = os.path.join(self.tmpdir, "test.desktop")
        de.write(new_file, trusted=True)

        with io.open(new_file, encoding='utf-8') as f:
            contents = f.read()

        assert "[Hallo]" in contents, contents
        assert re.search("key\s*=\s*value", contents), contents

        # This is missing the Name key, and has an unknown Hallo group, so it
        # shouldn't validate.
        new_entry = DesktopEntry(new_file)
        self.assertRaises(ValidationError, new_entry.validate)
Beispiel #6
0
class shortcut:
    def __init__(self,
                 dest,
                 path,
                 arguments,
                 name=None,
                 ttype=TargetType.FILESYSTEM):
        '''
        :param dest: Path to resulting file on file system
        :param path: Path where the link should point to
        :param arguments: Arguemnts to eecutable file
        :param name: Name of the application
        :param type: Link type - FILESYSTEM or URL
        '''
        self.dest = dest
        self.path = path
        self.arguments = arguments
        self.name = name
        self.changed = ''
        self.icon = None
        self.is_in_user_context = self.set_usercontext()
        self.type = ttype

    def __str__(self):
        result = self.to_json()
        return result

    def set_changed(self, change_date):
        '''
        Set object change date
        '''
        self.changed = change_date

    def set_clsid(self, clsid):
        self.clsid = clsid

    def set_guid(self, uid):
        self.guid = uid

    def set_icon(self, icon_name):
        self.icon = icon_name

    def set_type(self, ttype):
        '''
        Set type of the hyperlink - FILESYSTEM or URL

        :ttype: - object of class TargetType
        '''
        self.type = ttype

    def set_usercontext(self, usercontext=False):
        '''
        Perform action in user context or not
        '''
        ctx = False

        if usercontext in [1, '1', True]:
            ctx = True

        self.is_in_user_context = ctx

    def is_usercontext(self):
        return self.is_in_user_context

    def to_json(self):
        '''
        Return shortcut's JSON for further serialization.
        '''
        content = dict()
        content['dest'] = self.dest
        content['path'] = self.path
        content['name'] = self.name
        content['arguments'] = self.arguments
        content['clsid'] = self.clsid
        content['guid'] = self.guid
        content['changed'] = self.changed
        content['is_in_user_context'] = self.is_in_user_context
        content['type'] = ttype2str(self.type)
        if self.icon:
            content['icon'] = self.icon
        result = self.desktop()
        result.content.update(content)

        return json.dumps(result.content)

    def desktop(self):
        '''
        Returns desktop file object which may be written to disk.
        '''
        self.desktop_file = DesktopEntry()
        self.desktop_file.addGroup('Desktop Entry')

        if self.type == TargetType.URL:
            self.desktop_file.set('Type', 'Link')
        else:
            self.desktop_file.set('Type', 'Application')

        self.desktop_file.set('Version', '1.0')
        self.desktop_file.set('Name', self.name)

        if self.type == TargetType.URL:
            self.desktop_file.set('URL', self.path)
        else:
            self.desktop_file.set('Terminal', 'false')
            self.desktop_file.set('Exec',
                                  '{} {}'.format(self.path, self.arguments))

        if self.icon:
            self.desktop_file.set('Icon', self.icon)

        return self.desktop_file

    def write_desktop(self, dest):
        '''
        Write .desktop file to disk using path 'dest'. Please note that
        .desktop files must have executable bit set in order to work in
        GUI.
        '''
        self.desktop().write(dest)
        sc = Path(dest)
        sc.chmod(sc.stat().st_mode | stat.S_IEXEC)
Beispiel #7
0
class shortcut:
    def __init__(self, dest, path, arguments, name=None):
        self.dest = dest
        self.path = path
        self.arguments = arguments
        self.name = name
        self.changed = ''
        self.is_in_user_context = self.set_usercontext()

    def __str__(self):
        result = self.to_json()
        return result

    def set_changed(self, change_date):
        '''
        Set object change date
        '''
        self.changed = change_date

    def set_clsid(self, clsid):
        self.clsid = clsid

    def set_guid(self, uid):
        self.guid = uid

    def set_usercontext(self, usercontext=False):
        '''
        Perform action in user context or not
        '''
        ctx = False

        if usercontext in [1, '1', True]:
            ctx = True

        self.is_in_user_context = ctx

    def is_usercontext(self):
        return self.is_in_user_context

    def to_json(self):
        '''
        Return shortcut's JSON for further serialization.
        '''
        content = dict()
        content['dest'] = self.dest
        content['path'] = self.path
        content['name'] = self.name
        content['arguments'] = self.arguments
        content['clsid'] = self.clsid
        content['guid'] = self.guid
        content['changed'] = self.changed
        content['is_in_user_context'] = self.is_in_user_context

        result = self.desktop()
        result.content.update(content)

        return json.dumps(result.content)

    def desktop(self):
        '''
        Returns desktop file object which may be written to disk.
        '''
        self.desktop_file = DesktopEntry()
        self.desktop_file.addGroup('Desktop Entry')
        self.desktop_file.set('Type', 'Application')
        self.desktop_file.set('Version', '1.0')
        self.desktop_file.set('Terminal', 'false')
        self.desktop_file.set('Exec', '{} {}'.format(self.path,
                                                     self.arguments))
        self.desktop_file.set('Name', self.name)

        return self.desktop_file

    def write_desktop(self, dest):
        '''
        Write .desktop file to disk using path 'dest'
        '''
        self.desktop().write(dest)